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