glsl: Convert tuple into a class

This makes things a little more clear now, and it will make future
changes... possible.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Matt Turner <mattst88@gmail.com>
Acked-by: Dylan Baker <dylan@pnwbakers.com>
This commit is contained in:
Ian Romanick
2016-07-07 23:03:44 -07:00
parent 6ef27003ac
commit e31c72a331

View File

@@ -24,100 +24,114 @@
import mako.template import mako.template
import sys import sys
class operation(object):
def __init__(self, name, num_operands, printable_name = None):
self.name = name
self.num_operands = num_operands
if printable_name is None:
self.printable_name = name
else:
self.printable_name = printable_name
def get_enum_name(self):
return "ir_{}op_{}".format(("un", "bin", "tri", "quad")[self.num_operands-1], self.name)
ir_expression_operation = [ ir_expression_operation = [
# Name operands string operation("bit_not", 1, printable_name="~"),
("bit_not", 1, "~"), operation("logic_not", 1, printable_name="!"),
("logic_not", 1, "!"), operation("neg", 1),
("neg", 1, None), operation("abs", 1),
("abs", 1, None), operation("sign", 1),
("sign", 1, None), operation("rcp", 1),
("rcp", 1, None), operation("rsq", 1),
("rsq", 1, None), operation("sqrt", 1),
("sqrt", 1, None), operation("exp", 1), # Log base e on gentype
("exp", 1, None), # Log base e on gentype operation("log", 1), # Natural log on gentype
("log", 1, None), # Natural log on gentype operation("exp2", 1),
("exp2", 1, None), operation("log2", 1),
("log2", 1, None), operation("f2i", 1), # Float-to-integer conversion.
("f2i", 1, None), # Float-to-integer conversion. operation("f2u", 1), # Float-to-unsigned conversion.
("f2u", 1, None), # Float-to-unsigned conversion. operation("i2f", 1), # Integer-to-float conversion.
("i2f", 1, None), # Integer-to-float conversion. operation("f2b", 1), # Float-to-boolean conversion
("f2b", 1, None), # Float-to-boolean conversion operation("b2f", 1), # Boolean-to-float conversion
("b2f", 1, None), # Boolean-to-float conversion operation("i2b", 1), # int-to-boolean conversion
("i2b", 1, None), # int-to-boolean conversion operation("b2i", 1), # Boolean-to-int conversion
("b2i", 1, None), # Boolean-to-int conversion operation("u2f", 1), # Unsigned-to-float conversion.
("u2f", 1, None), # Unsigned-to-float conversion. operation("i2u", 1), # Integer-to-unsigned conversion.
("i2u", 1, None), # Integer-to-unsigned conversion. operation("u2i", 1), # Unsigned-to-integer conversion.
("u2i", 1, None), # Unsigned-to-integer conversion. operation("d2f", 1), # Double-to-float conversion.
("d2f", 1, None), # Double-to-float conversion. operation("f2d", 1), # Float-to-double conversion.
("f2d", 1, None), # Float-to-double conversion. operation("d2i", 1), # Double-to-integer conversion.
("d2i", 1, None), # Double-to-integer conversion. operation("i2d", 1), # Integer-to-double conversion.
("i2d", 1, None), # Integer-to-double conversion. operation("d2u", 1), # Double-to-unsigned conversion.
("d2u", 1, None), # Double-to-unsigned conversion. operation("u2d", 1), # Unsigned-to-double conversion.
("u2d", 1, None), # Unsigned-to-double conversion. operation("d2b", 1), # Double-to-boolean conversion.
("d2b", 1, None), # Double-to-boolean conversion. operation("bitcast_i2f", 1), # 'Bit-identical int-to-float "conversion"
("bitcast_i2f", 1, None), # 'Bit-identical int-to-float "conversion" operation("bitcast_f2i", 1), # 'Bit-identical float-to-int "conversion"
("bitcast_f2i", 1, None), # 'Bit-identical float-to-int "conversion" operation("bitcast_u2f", 1), # 'Bit-identical uint-to-float "conversion"
("bitcast_u2f", 1, None), # 'Bit-identical uint-to-float "conversion" operation("bitcast_f2u", 1), # 'Bit-identical float-to-uint "conversion"
("bitcast_f2u", 1, None), # 'Bit-identical float-to-uint "conversion"
# Unary floating-point rounding operations. # Unary floating-point rounding operations.
("trunc", 1, None), operation("trunc", 1),
("ceil", 1, None), operation("ceil", 1),
("floor", 1, None), operation("floor", 1),
("fract", 1, None), operation("fract", 1),
("round_even", 1, None), operation("round_even", 1),
# Trigonometric operations. # Trigonometric operations.
("sin", 1, None), operation("sin", 1),
("cos", 1, None), operation("cos", 1),
# Partial derivatives. # Partial derivatives.
("dFdx", 1, None), operation("dFdx", 1),
("dFdx_coarse", 1, "dFdxCoarse"), operation("dFdx_coarse", 1, printable_name="dFdxCoarse"),
("dFdx_fine", 1, "dFdxFine"), operation("dFdx_fine", 1, printable_name="dFdxFine"),
("dFdy", 1, None), operation("dFdy", 1),
("dFdy_coarse", 1, "dFdyCoarse"), operation("dFdy_coarse", 1, printable_name="dFdyCoarse"),
("dFdy_fine", 1, "dFdyFine"), operation("dFdy_fine", 1, printable_name="dFdyFine"),
# Floating point pack and unpack operations. # Floating point pack and unpack operations.
("pack_snorm_2x16", 1, "packSnorm2x16"), operation("pack_snorm_2x16", 1, printable_name="packSnorm2x16"),
("pack_snorm_4x8", 1, "packSnorm4x8"), operation("pack_snorm_4x8", 1, printable_name="packSnorm4x8"),
("pack_unorm_2x16", 1, "packUnorm2x16"), operation("pack_unorm_2x16", 1, printable_name="packUnorm2x16"),
("pack_unorm_4x8", 1, "packUnorm4x8"), operation("pack_unorm_4x8", 1, printable_name="packUnorm4x8"),
("pack_half_2x16", 1, "packHalf2x16"), operation("pack_half_2x16", 1, printable_name="packHalf2x16"),
("unpack_snorm_2x16", 1, "unpackSnorm2x16"), operation("unpack_snorm_2x16", 1, printable_name="unpackSnorm2x16"),
("unpack_snorm_4x8", 1, "unpackSnorm4x8"), operation("unpack_snorm_4x8", 1, printable_name="unpackSnorm4x8"),
("unpack_unorm_2x16", 1, "unpackUnorm2x16"), operation("unpack_unorm_2x16", 1, printable_name="unpackUnorm2x16"),
("unpack_unorm_4x8", 1, "unpackUnorm4x8"), operation("unpack_unorm_4x8", 1, printable_name="unpackUnorm4x8"),
("unpack_half_2x16", 1, "unpackHalf2x16"), operation("unpack_half_2x16", 1, printable_name="unpackHalf2x16"),
# Bit operations, part of ARB_gpu_shader5. # Bit operations, part of ARB_gpu_shader5.
("bitfield_reverse", 1, None), operation("bitfield_reverse", 1),
("bit_count", 1, None), operation("bit_count", 1),
("find_msb", 1, None), operation("find_msb", 1),
("find_lsb", 1, None), operation("find_lsb", 1),
("saturate", 1, "sat"), operation("saturate", 1, printable_name="sat"),
# Double packing, part of ARB_gpu_shader_fp64. # Double packing, part of ARB_gpu_shader_fp64.
("pack_double_2x32", 1, "packDouble2x32"), operation("pack_double_2x32", 1, printable_name="packDouble2x32"),
("unpack_double_2x32", 1, "unpackDouble2x32"), operation("unpack_double_2x32", 1, printable_name="unpackDouble2x32"),
("frexp_sig", 1, None), operation("frexp_sig", 1),
("frexp_exp", 1, None), operation("frexp_exp", 1),
("noise", 1, None), operation("noise", 1),
("subroutine_to_int", 1, None), operation("subroutine_to_int", 1),
# Interpolate fs input at centroid # Interpolate fs input at centroid
# #
# operand0 is the fs input. # operand0 is the fs input.
("interpolate_at_centroid", 1, None), operation("interpolate_at_centroid", 1),
# Ask the driver for the total size of a buffer block. # Ask the driver for the total size of a buffer block.
# operand0 is the ir_constant buffer block index in the linked shader. # operand0 is the ir_constant buffer block index in the linked shader.
("get_buffer_size", 1, None), operation("get_buffer_size", 1),
# Calculate length of an unsized array inside a buffer block. # Calculate length of an unsized array inside a buffer block.
# This opcode is going to be replaced in a lowering pass inside # This opcode is going to be replaced in a lowering pass inside
@@ -125,94 +139,95 @@ ir_expression_operation = [
# #
# operand0 is the unsized array's ir_value for the calculation # operand0 is the unsized array's ir_value for the calculation
# of its length. # of its length.
("ssbo_unsized_array_length", 1, None), operation("ssbo_unsized_array_length", 1),
# Vote among threads on the value of the boolean argument. # Vote among threads on the value of the boolean argument.
("vote_any", 1, None), operation("vote_any", 1),
("vote_all", 1, None), operation("vote_all", 1),
("vote_eq", 1, None), operation("vote_eq", 1),
("add", 2, "+"), operation("add", 2, printable_name="+"),
("sub", 2, "-"), operation("sub", 2, printable_name="-"),
("mul", 2, "*"), # "Floating-point or low 32-bit integer multiply." # "Floating-point or low 32-bit integer multiply."
("imul_high", 2, None), # Calculates the high 32-bits of a 64-bit multiply. operation("mul", 2, printable_name="*"),
("div", 2, "/"), operation("imul_high", 2), # Calculates the high 32-bits of a 64-bit multiply.
operation("div", 2, printable_name="/"),
# Returns the carry resulting from the addition of the two arguments. # Returns the carry resulting from the addition of the two arguments.
("carry", 2, None), operation("carry", 2),
# Returns the borrow resulting from the subtraction of the second argument # Returns the borrow resulting from the subtraction of the second argument
# from the first argument. # from the first argument.
("borrow", 2, None), operation("borrow", 2),
# Either (vector % vector) or (vector % scalar) # Either (vector % vector) or (vector % scalar)
("mod", 2, "%"), operation("mod", 2, printable_name="%"),
# Binary comparison operators which return a boolean vector. # Binary comparison operators which return a boolean vector.
# The type of both operands must be equal. # The type of both operands must be equal.
("less", 2, "<"), operation("less", 2, printable_name="<"),
("greater", 2, ">"), operation("greater", 2, printable_name=">"),
("lequal", 2, "<="), operation("lequal", 2, printable_name="<="),
("gequal", 2, ">="), operation("gequal", 2, printable_name=">="),
("equal", 2, "=="), operation("equal", 2, printable_name="=="),
("nequal", 2, "!="), operation("nequal", 2, printable_name="!="),
# Returns single boolean for whether all components of operands[0] # Returns single boolean for whether all components of operands[0]
# equal the components of operands[1]. # equal the components of operands[1].
("all_equal", 2, None), operation("all_equal", 2),
# Returns single boolean for whether any component of operands[0] # Returns single boolean for whether any component of operands[0]
# is not equal to the corresponding component of operands[1]. # is not equal to the corresponding component of operands[1].
("any_nequal", 2, None), operation("any_nequal", 2),
# Bit-wise binary operations. # Bit-wise binary operations.
("lshift", 2, "<<"), operation("lshift", 2, printable_name="<<"),
("rshift", 2, ">>"), operation("rshift", 2, printable_name=">>"),
("bit_and", 2, "&"), operation("bit_and", 2, printable_name="&"),
("bit_xor", 2, "^"), operation("bit_xor", 2, printable_name="^"),
("bit_or", 2, "|"), operation("bit_or", 2, printable_name="|"),
("logic_and", 2, "&&"), operation("logic_and", 2, printable_name="&&"),
("logic_xor", 2, "^^"), operation("logic_xor", 2, printable_name="^^"),
("logic_or", 2, "||"), operation("logic_or", 2, printable_name="||"),
("dot", 2, None), operation("dot", 2),
("min", 2, None), operation("min", 2),
("max", 2, None), operation("max", 2),
("pow", 2, None), operation("pow", 2),
# Load a value the size of a given GLSL type from a uniform block. # Load a value the size of a given GLSL type from a uniform block.
# #
# operand0 is the ir_constant uniform block index in the linked shader. # operand0 is the ir_constant uniform block index in the linked shader.
# operand1 is a byte offset within the uniform block. # operand1 is a byte offset within the uniform block.
("ubo_load", 2, None), operation("ubo_load", 2),
# Multiplies a number by two to a power, part of ARB_gpu_shader5. # Multiplies a number by two to a power, part of ARB_gpu_shader5.
("ldexp", 2, None), operation("ldexp", 2),
# Extract a scalar from a vector # Extract a scalar from a vector
# #
# operand0 is the vector # operand0 is the vector
# operand1 is the index of the field to read from operand0 # operand1 is the index of the field to read from operand0
("vector_extract", 2, None), operation("vector_extract", 2),
# Interpolate fs input at offset # Interpolate fs input at offset
# #
# operand0 is the fs input # operand0 is the fs input
# operand1 is the offset from the pixel center # operand1 is the offset from the pixel center
("interpolate_at_offset", 2, None), operation("interpolate_at_offset", 2),
# Interpolate fs input at sample position # Interpolate fs input at sample position
# #
# operand0 is the fs input # operand0 is the fs input
# operand1 is the sample ID # operand1 is the sample ID
("interpolate_at_sample", 2, None), operation("interpolate_at_sample", 2),
# Fused floating-point multiply-add, part of ARB_gpu_shader5. # Fused floating-point multiply-add, part of ARB_gpu_shader5.
("fma", 3, None), operation("fma", 3),
("lrp", 3, None), operation("lrp", 3),
# Conditional Select # Conditional Select
# #
@@ -220,24 +235,22 @@ ir_expression_operation = [
# component on vectors). # component on vectors).
# #
# See also lower_instructions_visitor::ldexp_to_arith # See also lower_instructions_visitor::ldexp_to_arith
("csel", 3, None), operation("csel", 3),
("bitfield_extract", 3, None), operation("bitfield_extract", 3),
# Generate a value with one field of a vector changed # Generate a value with one field of a vector changed
# #
# operand0 is the vector # operand0 is the vector
# operand1 is the value to write into the vector result # operand1 is the value to write into the vector result
# operand2 is the index in operand0 to be modified # operand2 is the index in operand0 to be modified
("vector_insert", 3, None), operation("vector_insert", 3),
("bitfield_insert", 4, None), operation("bitfield_insert", 4),
("vector", 4, None), operation("vector", 4),
] ]
def name_from_item(item):
return "ir_{}op_{}".format(("un", "bin", "tri", "quad")[item[1]-1], item[0])
if __name__ == "__main__": if __name__ == "__main__":
copyright = """/* copyright = """/*
@@ -266,33 +279,31 @@ if __name__ == "__main__":
enum_template = mako.template.Template(copyright + """ enum_template = mako.template.Template(copyright + """
enum ir_expression_operation { enum ir_expression_operation {
% for item in values: % for item in values:
${name_from_item(item)}, ${item.get_enum_name()},
% endfor % endfor
/* Sentinels marking the last of each kind of operation. */ /* Sentinels marking the last of each kind of operation. */
% for (name, i) in lasts: % for item in lasts:
ir_last_${("un", "bin", "tri", "quad")[i]}op = ${name_from_item((name, i+1))}, ir_last_${("un", "bin", "tri", "quad")[item.num_operands - 1]}op = ${item.get_enum_name()},
% endfor % endfor
ir_last_opcode = ir_quadop_${lasts[3][0]} ir_last_opcode = ir_quadop_${lasts[3].name}
};""") };""")
strings_template = mako.template.Template(copyright + """ strings_template = mako.template.Template(copyright + """
const char *const ir_expression_operation_strings[] = { const char *const ir_expression_operation_strings[] = {
% for item in values: % for item in values:
"${item[2] if item[2] is not None else item[0]}", "${item.printable_name}",
% endfor % endfor
};""") };""")
if sys.argv[1] == "enum": if sys.argv[1] == "enum":
lasts = [None, None, None, None] lasts = [None, None, None, None]
for item in reversed(ir_expression_operation): for item in reversed(ir_expression_operation):
i = item[1] - 1 i = item.num_operands - 1
if lasts[i] is None: if lasts[i] is None:
lasts[i] = (item[0], i) lasts[i] = item
print(enum_template.render(values=ir_expression_operation, print(enum_template.render(values=ir_expression_operation,
lasts=lasts, lasts=lasts))
name_from_item=name_from_item))
elif sys.argv[1] == "strings": elif sys.argv[1] == "strings":
print(strings_template.render(values=ir_expression_operation, print(strings_template.render(values=ir_expression_operation))
name_from_item=name_from_item))