Commit b2e4863c by Jamie Madill Committed by Commit Bot

Replace std::map with switch in load functions.

BUG=angleproject:1455 Change-Id: Ica74ea5503efc0315bc4d98aa322da523a30b24c Reviewed-on: https://chromium-review.googlesource.com/367696Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent 2f9b5e6f
......@@ -271,7 +271,7 @@ gl::Error Image11::loadData(const gl::Box &area,
const d3d11::Format &d3dFormatInfo =
d3d11::Format::Get(mInternalFormat, mRenderer->getRenderer11DeviceCaps());
LoadImageFunction loadFunction = d3dFormatInfo.loadFunctions.at(type).loadFunction;
LoadImageFunction loadFunction = d3dFormatInfo.loadFunctions(type).loadFunction;
D3D11_MAPPED_SUBRESOURCE mappedImage;
gl::Error error = map(D3D11_MAP_WRITE, &mappedImage);
......@@ -310,7 +310,7 @@ gl::Error Image11::loadCompressedData(const gl::Box &area, const void *input)
const d3d11::Format &d3dFormatInfo =
d3d11::Format::Get(mInternalFormat, mRenderer->getRenderer11DeviceCaps());
LoadImageFunction loadFunction = d3dFormatInfo.loadFunctions.at(GL_UNSIGNED_BYTE).loadFunction;
LoadImageFunction loadFunction = d3dFormatInfo.loadFunctions(GL_UNSIGNED_BYTE).loadFunction;
D3D11_MAPPED_SUBRESOURCE mappedImage;
gl::Error error = map(D3D11_MAP_WRITE, &mappedImage);
......@@ -403,7 +403,7 @@ gl::Error Image11::copyFromFramebuffer(const gl::Offset &destOffset,
const auto &destD3D11Format =
d3d11::Format::Get(mInternalFormat, mRenderer->getRenderer11DeviceCaps());
auto loadFunction = destD3D11Format.loadFunctions.at(destFormatInfo.type);
auto loadFunction = destD3D11Format.loadFunctions(destFormatInfo.type);
if (loadFunction.requiresConversion)
{
size_t bufferSize = destFormatInfo.pixelBytes * sourceArea.width * sourceArea.height;
......
......@@ -664,7 +664,7 @@ gl::Error TextureStorage11::setData(const gl::ImageIndex &index,
MemoryBuffer *conversionBuffer = nullptr;
const uint8_t *data = nullptr;
d3d11::LoadImageFunctionInfo loadFunctionInfo = d3d11Format.loadFunctions.at(type);
LoadImageFunctionInfo loadFunctionInfo = d3d11Format.loadFunctions(type);
if (loadFunctionInfo.requiresConversion)
{
ANGLE_TRY(mRenderer->getScratchMemoryBuffer(neededSize, &conversionBuffer));
......
......@@ -76,19 +76,17 @@ void UnreachableLoadFunction(size_t width,
UNREACHABLE();
}}
}} // namespace
{load_functions_data}}} // namespace
// TODO we can replace these maps with more generated code
const std::map<GLenum, LoadImageFunctionInfo> &GetLoadFunctionsMap(GLenum {internal_format},
DXGI_FORMAT {dxgi_format})
LoadFunctionMap GetLoadFunctionsMap(GLenum {internal_format}, DXGI_FORMAT {dxgi_format})
{{
// clang-format off
switch ({internal_format})
{{
{data}
{switch_data}
default:
{{
static std::map<GLenum, LoadImageFunctionInfo> emptyLoadFunctionsMap;
static LoadFunctionMap emptyLoadFunctionsMap;
return emptyLoadFunctionsMap;
}}
}}
......@@ -105,32 +103,38 @@ internal_format_param = 'internalFormat'
dxgi_format_param = 'dxgiFormat'
dxgi_format_unknown = "DXGI_FORMAT_UNKNOWN"
def get_function_maps_string(typestr, function):
requiresConversion = str('LoadToNative<' not in function).lower()
return ' { ' + typestr + ', LoadImageFunctionInfo(' + function + ', ' + requiresConversion + ') },\n'
def get_unknown_format_string(s, dxgi_to_type_map, dxgi_unknown_string):
if dxgi_unknown_string not in dxgi_to_type_map:
return ''
table_data = ''
for gl_type, load_function in sorted(dxgi_to_type_map[dxgi_unknown_string].iteritems()):
table_data += s + get_function_maps_string(gl_type, load_function)
return table_data
def get_load_function_map_snippet(s, insert_map_string):
load_function_map_snippet = ''
load_function_map_snippet += s + 'static const std::map<GLenum, LoadImageFunctionInfo> loadFunctionsMap = {\n'
load_function_map_snippet += insert_map_string
load_function_map_snippet += s + '};\n\n'
load_function_map_snippet += s + 'return loadFunctionsMap;\n'
return load_function_map_snippet
def parse_json_into_switch_string(json_data):
def load_functions_name(internal_format, dxgi_format):
short_name = dxgi_format if dxgi_format == "default" else dxgi_format[len("DXGI_FORMAT_"):]
return internal_format[3:] + "_to_" + short_name
def unknown_func_name(internal_format):
return load_functions_name(internal_format, "default")
def get_load_func(func_name, type_functions):
snippet = "LoadImageFunctionInfo " + func_name + "(GLenum type)\n"
snippet += "{\n"
snippet += " switch (type)\n"
snippet += " {\n"
for gl_type, load_function in sorted(type_functions.iteritems()):
snippet += " case " + gl_type + ":\n"
requiresConversion = str('LoadToNative<' not in load_function).lower()
snippet += " return LoadImageFunctionInfo(" + load_function + ", " + requiresConversion + ");\n"
snippet += " default:\n"
snippet += " UNREACHABLE();\n"
snippet += " return LoadImageFunctionInfo(UnreachableLoadFunction, true);\n"
snippet += " }\n"
snippet += "}\n"
snippet += "\n"
return snippet
def get_unknown_load_func(dxgi_to_type_map, internal_format):
assert dxgi_format_unknown in dxgi_to_type_map
return get_load_func(unknown_func_name(internal_format), dxgi_to_type_map[dxgi_format_unknown])
def parse_json(json_data):
table_data = ''
load_functions_data = ''
for internal_format, dxgi_to_type_map in sorted(json_data.iteritems()):
s = ' '
......@@ -151,35 +155,25 @@ def parse_json_into_switch_string(json_data):
if dxgi_format == dxgi_format_unknown:
continue
func_name = load_functions_name(internal_format, dxgi_format)
# Main case statements
table_data += s + 'case ' + dxgi_format + ':\n'
table_data += s + '{\n'
s += ' '
insert_map_string = ''
table_data += s + ' return ' + func_name + ';\n'
if dxgi_format_unknown in dxgi_to_type_map:
for gl_type, load_function in dxgi_to_type_map[dxgi_format_unknown].iteritems():
if gl_type not in type_functions:
type_functions[gl_type] = load_function
for gl_type, load_function in sorted(type_functions.iteritems()):
insert_map_string += s + get_function_maps_string(gl_type, load_function)
table_data += get_load_function_map_snippet(s, insert_map_string)
s = s[4:]
table_data += s + '}\n'
load_functions_data += get_load_func(func_name, type_functions)
if do_switch:
table_data += s + 'default:\n'
if dxgi_format_unknown in dxgi_to_type_map:
table_data += s + '{\n'
s += ' '
dxgi_unknown_str = get_unknown_format_string(
s, dxgi_to_type_map, dxgi_format_unknown);
table_data += get_load_function_map_snippet(s, dxgi_unknown_str)
s = s[4:]
table_data += s + '}\n'
table_data += s + ' return ' + unknown_func_name(internal_format) + ';\n'
load_functions_data += get_unknown_load_func(dxgi_to_type_map, internal_format)
else:
table_data += s + ' break;\n'
......@@ -189,14 +183,15 @@ def parse_json_into_switch_string(json_data):
s = s[4:]
table_data += s + '}\n'
return table_data
return table_data, load_functions_data
json_data = angle_format.load_json('load_functions_data.json')
table_data = parse_json_into_switch_string(json_data)
switch_data, load_functions_data = parse_json(json_data)
output = template.format(internal_format = internal_format_param,
dxgi_format = dxgi_format_param,
data=table_data)
switch_data = switch_data,
load_functions_data = load_functions_data)
with open('load_functions_table_autogen.cpp', 'wt') as out_file:
out_file.write(output)
......
......@@ -21,8 +21,7 @@ namespace rx
namespace d3d11
{
const std::map<GLenum, LoadImageFunctionInfo> &GetLoadFunctionsMap(GLenum internalFormat,
DXGI_FORMAT dxgiFormat);
LoadFunctionMap GetLoadFunctionsMap(GLenum internalFormat, DXGI_FORMAT dxgiFormat);
} // namespace d3d11
......
......@@ -49,7 +49,6 @@ Format::Format(GLenum internalFormat,
dataInitializerFunction(internalFormatInitializer),
loadFunctions(GetLoadFunctionsMap(internalFormat, texFormat))
{
ASSERT(!loadFunctions.empty() || formatID == angle::Format::ID::NONE);
}
} // namespace d3d11
......
......@@ -26,18 +26,6 @@ struct Renderer11DeviceCaps;
namespace d3d11
{
struct LoadImageFunctionInfo
{
LoadImageFunctionInfo() : loadFunction(nullptr), requiresConversion(false) {}
LoadImageFunctionInfo(LoadImageFunction loadFunction, bool requiresConversion)
: loadFunction(loadFunction), requiresConversion(requiresConversion)
{
}
LoadImageFunction loadFunction;
bool requiresConversion;
};
// For sized GL internal formats, there are several possible corresponding D3D11 formats depending
// on device capabilities.
// This structure allows querying for the DXGI texture formats to use for textures, SRVs, RTVs and
......@@ -71,7 +59,6 @@ struct Format final : angle::NonCopyable
const Format &swizzle;
InitializeTextureDataFunction dataInitializerFunction;
typedef std::map<GLenum, LoadImageFunctionInfo> LoadFunctionMap;
LoadFunctionMap loadFunctions;
};
......
......@@ -24,10 +24,6 @@ struct FormatType;
namespace rx
{
typedef void (*LoadImageFunction)(size_t width, size_t height, size_t depth,
const uint8_t *input, size_t inputRowPitch, size_t inputDepthPitch,
uint8_t *output, size_t outputRowPitch, size_t outputDepthPitch);
typedef void (*InitializeTextureDataFunction)(size_t width, size_t height, size_t depth,
uint8_t *output, size_t outputRowPitch, size_t outputDepthPitch);
......
......@@ -75,6 +75,30 @@ ColorWriteFunction GetColorWriteFunction(const gl::FormatType &formatType);
ColorCopyFunction GetFastCopyFunction(const FastCopyFunctionMap &fastCopyFunctions,
const gl::FormatType &formatType);
using LoadImageFunction = void (*)(size_t width,
size_t height,
size_t depth,
const uint8_t *input,
size_t inputRowPitch,
size_t inputDepthPitch,
uint8_t *output,
size_t outputRowPitch,
size_t outputDepthPitch);
struct LoadImageFunctionInfo
{
LoadImageFunctionInfo() : loadFunction(nullptr), requiresConversion(false) {}
LoadImageFunctionInfo(LoadImageFunction loadFunction, bool requiresConversion)
: loadFunction(loadFunction), requiresConversion(requiresConversion)
{
}
LoadImageFunction loadFunction;
bool requiresConversion;
};
using LoadFunctionMap = LoadImageFunctionInfo (*)(GLenum);
} // namespace rx
#endif // LIBANGLE_RENDERER_RENDERER_UTILS_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment