• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python2
2#
3# Copyright 2018 The ANGLE Project Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# gen_blit11helper.py:
8#   Generates the code for retrieving the various blit shaders for D3D11
9#   NOTE: don't run this script directly. Run scripts/run_code_generation.py.
10
11import sys, os, pprint
12from datetime import date
13
14template_blitshader_source = """// GENERATED FILE - DO NOT EDIT.
15// Generated by {script_name}.
16//
17// Copyright {year} The ANGLE Project Authors. All rights reserved.
18// Use of this source code is governed by a BSD-style license that can be
19// found in the LICENSE file.
20//
21// Blit11Helper_autogen.inc:
22//   Defines and retrieves blitshaders for the D3D11 backend.
23
24namespace
25{{
26// Include inline shaders in the anonymous namespace to make sure no symbols are exported
27{shader_includes}
28}} // namespace
29
30enum Blit11::BlitShaderOperation : unsigned int
31{{
32{blitshaderop_enums}
33}};
34
35enum Blit11::BlitShaderType : unsigned int
36{{
37{blitshadertype_enums}
38}};
39
40Blit11::BlitShaderType Blit11::getBlitShaderType(BlitShaderOperation operation, ShaderDimension dimension)
41{{
42    switch(operation)
43    {{
44{get_blitshaders_case_list}
45        default:
46            UNREACHABLE();
47            return BLITSHADER_INVALID;
48    }}
49}}
50
51angle::Result Blit11::mapBlitShader(const gl::Context *context,
52                                    BlitShaderType blitShaderType)
53{{
54    switch(blitShaderType)
55    {{
56{add_blitshader_case_list}
57        default:
58            ANGLE_HR_UNREACHABLE(GetImplAs<Context11>(context));
59    }}
60
61    return angle::Result::Continue;
62}}
63
64"""
65
66template_blitshaders_gni = """# GENERATED FILE - DO NOT EDIT.
67# Generated by {script_name}.
68#
69# Copyright {year} The ANGLE Project Authors. All rights reserved.
70# Use of this source code is governed by a BSD-style license that can be
71# found in the LICENSE file.
72#
73# d3d11_blit_shaders_autogen.gni:
74#   List of generated blit shaders for inclusion in ANGLE's build process.
75
76libangle_d3d11_blit_shaders = [
77{shader_filename_list}
78]"""
79
80template_compiled_blitshader_include = """#include "libANGLE/renderer/d3d/d3d11/shaders/compiled/{filename}\""""
81
82template_get_blitshader_case = """        case {operation}:
83            switch (dimension)
84            {{
85{get_blitshader_dimension_cases}
86                default:
87                    UNREACHABLE();
88                    return BLITSHADER_INVALID;
89            }}"""
90
91template_get_blitshader_case_dimension = """                case SHADER_{dimension}:
92                    return BLITSHADER_{blitshader};"""
93
94template_map_blitshader_case = """        case {blitshader_name}:
95            ANGLE_TRY(addBlitShaderToMap(context, blitShaderType, SHADER_{dimension_upper},
96                                         ShaderData(g_PS_{compiled_shader_name}),
97                                         "Blit11 {dimension} {shader_comment} pixel shader"));
98            break;"""
99
100supported_dimensions = ["2D", "3D", "2DArray"]
101
102# field 1: BlitShaderType enum
103# field 2: Name of compiled shader
104# field 3: Filename of compiled shader
105blitshader_data = [
106    ("RGBAF", "PassthroughRGBA*", "passthroughrgba*11ps.h"), ("BGRAF", "PassthroughRGBA*"),
107    ("RGBF", "PassthroughRGB*", "passthroughrgb*11ps.h"),
108    ("RGF", "PassthroughRG*", "passthroughrg*11ps.h"),
109    ("RF", "PassthroughR*", "passthroughr*11ps.h"),
110    ("ALPHA", "PassthroughA*", "passthrougha*11ps.h"),
111    ("LUMA", "PassthroughLum*", "passthroughlum*11ps.h"),
112    ("LUMAALPHA", "PassthroughLumAlpha*", "passthroughlumalpha*11ps.h"),
113    ("RGBAUI", "PassthroughRGBA*UI", "passthroughrgba*ui11ps.h"),
114    ("RGBAI", "PassthroughRGBA*I", "passthroughrgba*i11ps.h"),
115    ("RGBUI", "PassthroughRGB*UI", "passthroughrgb*ui11ps.h"),
116    ("RGBI", "PassthroughRGB*I", "passthroughrgb*i11ps.h"),
117    ("RGUI", "PassthroughRG*UI", "passthroughrg*ui11ps.h"),
118    ("RGI", "PassthroughRG*I", "passthroughrg*i11ps.h"),
119    ("RUI", "PassthroughR*UI", "passthroughr*ui11ps.h"),
120    ("RI", "PassthroughR*I", "passthroughr*i11ps.h"),
121    ("RGBAF_PREMULTIPLY", "FtoF_PM_RGBA_*", "multiplyalpha_ftof_pm_rgba_*_ps.h"),
122    ("RGBAF_UNMULTIPLY", "FtoF_UM_RGBA_*", "multiplyalpha_ftof_um_rgba_*_ps.h"),
123    ("RGBF_PREMULTIPLY", "FtoF_PM_RGB_*", "multiplyalpha_ftof_pm_rgb_*_ps.h"),
124    ("RGBF_UNMULTIPLY", "FtoF_UM_RGB_*", "multiplyalpha_ftof_um_rgb_*_ps.h"),
125    ("RGBAF_TOUI", "FtoU_PT_RGBA_*", "multiplyalpha_ftou_pt_rgba_*_ps.h"),
126    ("RGBAF_TOUI_PREMULTIPLY", "FtoU_PM_RGBA_*", "multiplyalpha_ftou_pm_rgba_*_ps.h"),
127    ("RGBAF_TOUI_UNMULTIPLY", "FtoU_UM_RGBA_*", "multiplyalpha_ftou_um_rgba_*_ps.h"),
128    ("RGBF_TOUI", "FtoU_PT_RGB_*", "multiplyalpha_ftou_pt_rgb_*_ps.h"),
129    ("RGBF_TOUI_PREMULTIPLY", "FtoU_PM_RGB_*", "multiplyalpha_ftou_pm_rgb_*_ps.h"),
130    ("RGBF_TOUI_UNMULTIPLY", "FtoU_UM_RGB_*", "multiplyalpha_ftou_um_rgb_*_ps.h"),
131    ("RGBAF_TOI", "FtoI_PT_RGBA_*", "multiplyalpha_ftoi_pt_rgba_*_ps.h"),
132    ("RGBAF_TOI_PREMULTIPLY", "FtoI_PM_RGBA_*", "multiplyalpha_ftoi_pm_rgba_*_ps.h"),
133    ("RGBAF_TOI_UNMULTIPLY", "FtoI_UM_RGBA_*", "multiplyalpha_ftoi_um_rgba_*_ps.h"),
134    ("RGBF_TOI", "FtoI_PT_RGB_*", "multiplyalpha_ftoi_pt_rgb_*_ps.h"),
135    ("RGBF_TOI_PREMULTIPLY", "FtoI_PM_RGB_*", "multiplyalpha_ftoi_pm_rgb_*_ps.h"),
136    ("RGBF_TOI_UNMULTIPLY", "FtoI_UM_RGB_*", "multiplyalpha_ftoi_um_rgb_*_ps.h"),
137    ("LUMAF_PREMULTIPLY", "FtoF_PM_LUMA_*", "multiplyalpha_ftof_pm_luma_*_ps.h"),
138    ("LUMAF_UNMULTIPLY", "FtoF_UM_LUMA_*", "multiplyalpha_ftof_um_luma_*_ps.h"),
139    ("LUMAALPHAF_PREMULTIPLY", "FtoF_PM_LUMAALPHA_*", "multiplyalpha_ftof_pm_lumaalpha_*_ps.h"),
140    ("LUMAALPHAF_UNMULTIPLY", "FtoF_UM_LUMAALPHA_*", "multiplyalpha_ftof_um_lumaalpha_*_ps.h"),
141    ("RGBAF_4444", "PassthroughRGBA*_4444", "passthroughrgba*_4444_11ps.h"),
142    ("RGBAF_4444_PREMULTIPLY", "FtoF_PM_RGBA_4444_*", "multiplyalpha_ftof_pm_rgba_4444_*_ps.h"),
143    ("RGBAF_4444_UNMULTIPLY", "FtoF_UM_RGBA_4444_*", "multiplyalpha_ftof_um_rgba_4444_*_ps.h"),
144    ("RGBF_565", "PassthroughRGB*_565", "passthroughrgb*_565_11ps.h"),
145    ("RGBF_565_PREMULTIPLY", "FtoF_PM_RGB_565_*", "multiplyalpha_ftof_pm_rgb_565_*_ps.h"),
146    ("RGBF_565_UNMULTIPLY", "FtoF_UM_RGB_565_*", "multiplyalpha_ftof_um_rgb_565_*_ps.h"),
147    ("RGBAF_5551", "PassthroughRGBA*_5551", "passthroughrgba*_5551_11ps.h"),
148    ("RGBAF_5551_PREMULTIPLY", "FtoF_PM_RGBA_5551_*", "multiplyalpha_ftof_pm_rgba_5551_*_ps.h"),
149    ("RGBAF_5551_UNMULTIPLY", "FtoF_UM_RGBA_5551_*", "multiplyalpha_ftof_um_rgba_5551_*_ps.h")
150]
151
152
153def format_shader_include(dimension, blitshader):
154    return template_compiled_blitshader_include.format(
155        filename=blitshader[2].replace("*", dimension.lower()))
156
157
158def format_get_blitshader_case(operation):
159    dimension_cases = []
160    for dimension in supported_dimensions:
161        dimension_cases.append(format_get_blitshader_case_dimension(operation, dimension))
162
163    return template_get_blitshader_case.format(
164        get_blitshader_dimension_cases="\n".join([c for c in dimension_cases]),
165        operation=operation)
166
167
168def format_get_blitshader_case_dimension(operation, dimension):
169    # 2D float to int shaders have not been implemented
170    if dimension == "2D" and operation.find("TOI") != -1:
171        blitshader = "INVALID"
172    else:
173        blitshader = dimension.upper() + "_" + operation
174
175    return template_get_blitshader_case_dimension.format(
176        dimension=dimension.upper(), blitshader=blitshader)
177
178
179def format_map_blitshader_case(dimension, blitshader):
180    blitshader_name = "BLITSHADER_" + dimension.upper() + "_" + blitshader[0]
181
182    # 3D and 2DArray use the RGBA shader for passthrough alpha
183    if dimension != "2D" and blitshader[0] == "ALPHA":
184        compiled_shader_name = "PassthroughRGBA" + dimension
185    else:
186        compiled_shader_name = blitshader[1].replace("*", dimension)
187
188    shader_comment = compiled_shader_name.replace("_", " ")
189
190    case = template_map_blitshader_case.format(
191        blitshader_name=blitshader_name,
192        dimension=dimension,
193        dimension_upper=dimension.upper(),
194        compiled_shader_name=compiled_shader_name,
195        shader_comment=shader_comment,
196    )
197
198    return case
199
200
201def format_shader_filename(dimension, blitshader):
202    return "shaders/compiled/" + blitshader[2].replace("*", dimension.lower()) + ","
203
204
205def get_shader_includes():
206    includes = []
207
208    for dimension in supported_dimensions:
209        for blitshader in blitshader_data:
210            # 2D float to int shaders have not been implemented
211            if dimension == "2D" and blitshader[0].find("TOI") != -1:
212                continue
213            # 3D and 2DArray just use the RGBA shader for passthrough alpha
214            if dimension != "2D" and blitshader[0] == "ALPHA":
215                continue
216            if len(blitshader) == 3:
217                includes.append(format_shader_include(dimension, blitshader))
218
219    return includes
220
221
222def get_blitshader_cases():
223    blitshader_cases = []
224    for blitshader in blitshader_data:
225        blitshader_cases.append(format_get_blitshader_case(blitshader[0]))
226
227    return blitshader_cases
228
229
230def get_map_blitshader_cases():
231    blitshader_cases = []
232
233    for dimension in supported_dimensions:
234        for blitshader in blitshader_data:
235            # 2D float to int shaders have not been implemented
236            if dimension == "2D" and blitshader[0].find("TOI") != -1:
237                continue
238            blitshader_cases.append(format_map_blitshader_case(dimension, blitshader))
239
240    return blitshader_cases
241
242
243def get_blitshaderop_enums():
244    blitshaderops = []
245
246    for blitshader in blitshader_data:
247        blitshaderops.append("    " + blitshader[0] + ",")
248
249    blitshaderops.append("    OPERATION_INVALID")
250    return blitshaderops
251
252
253def get_blitshadertype_enums():
254    blitshaders = []
255
256    for dimension in supported_dimensions:
257        for blitshader in blitshader_data:
258            # 2D float to int shaders have not been implemented
259            if dimension == "2D" and blitshader[0].find("TOI") != -1:
260                continue
261            blitshaders.append("    BLITSHADER_" + dimension.upper() + "_" + blitshader[0] + ",")
262
263    blitshaders.append("    BLITSHADER_INVALID")
264    return blitshaders
265
266
267def get_shader_filenames():
268    filenames = []
269
270    for dimension in supported_dimensions:
271        for blitshader in blitshader_data:
272            # 2D float to int shaders have not been implemented
273            if dimension == "2D" and blitshader[0].find("TOI") != -1:
274                continue
275            # 3D and 2DArray just use the RGBA shader for passthrough alpha
276            if dimension != "2D" and blitshader[0] == "ALPHA":
277                continue
278            if len(blitshader) == 3:
279                filenames.append(
280                    ("    \"src/libANGLE/renderer/d3d/d3d11/shaders/compiled/{0}\",").format(
281                        blitshader[2].replace("*", dimension.lower())))
282
283    return filenames
284
285
286def write_inc_file(get_blitshaders_case_list, add_blitshader_case_list, shader_includes,
287                   blitshaderop_enums, blitshadertype_enums):
288    content = template_blitshader_source.format(
289        script_name=os.path.basename(sys.argv[0]),
290        year=date.today().year,
291        blitshaderop_enums=blitshaderop_enums,
292        blitshadertype_enums=blitshadertype_enums,
293        get_blitshaders_case_list=get_blitshaders_case_list,
294        add_blitshader_case_list=add_blitshader_case_list,
295        shader_includes=shader_includes)
296
297    path = os.path.join("Blit11Helper_autogen.inc")
298
299    with open(path, "w") as out:
300        out.write(content)
301        out.close()
302
303
304def write_gni_file(shader_filename_list):
305    content = template_blitshaders_gni.format(
306        script_name=os.path.basename(sys.argv[0]),
307        year=date.today().year,
308        shader_filename_list=shader_filename_list)
309
310    path = os.path.join("d3d11_blit_shaders_autogen.gni")
311
312    with open(path, "w") as out:
313        out.write(content)
314        out.close()
315
316
317def main():
318
319    # auto_script parameters.
320    if len(sys.argv) > 1:
321        inputs = []
322        outputs = ['Blit11Helper_autogen.inc', 'd3d11_blit_shaders_autogen.gni']
323
324        if sys.argv[1] == 'inputs':
325            print ','.join(inputs)
326        elif sys.argv[1] == 'outputs':
327            print ','.join(outputs)
328        else:
329            print('Invalid script parameters')
330            return 1
331        return 0
332
333    map_blitshader_cases = []
334    shader_includes = []
335    blitshadertype_cases = []
336    blitshadertype_enums = []
337    blitshaderop_enums = []
338    shader_filenames = []
339
340    map_blitshader_cases = get_map_blitshader_cases()
341    shader_includes = get_shader_includes()
342    blitshadertype_cases = get_blitshader_cases()
343    blitshaderop_enums = get_blitshaderop_enums()
344    blitshadertype_enums = get_blitshadertype_enums()
345    shader_filenames = get_shader_filenames()
346
347    write_inc_file("\n".join([d for d in blitshadertype_cases]), "\n".join(
348        [c for c in map_blitshader_cases]), "\n".join([i for i in shader_includes]), "\n".join(
349            [e for e in blitshaderop_enums]), "\n".join([e for e in blitshadertype_enums]))
350    write_gni_file("\n".join([s for s in shader_filenames]))
351    return 0
352
353
354if __name__ == '__main__':
355    sys.exit(main())
356