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