Re: [Mesa-dev] [PATCH 05/22] glsl: Add a lowering pass to turn complicated UBO references to vector loads.
On 07/31/2012 03:01 PM, Eric Anholt wrote: --- src/glsl/Makefile.sources|1 + src/glsl/ir_optimization.h |1 + src/glsl/lower_ubo_reference.cpp | 325 ++ 3 files changed, 327 insertions(+) create mode 100644 src/glsl/lower_ubo_reference.cpp diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources index f2743f7..765f06a 100644 --- a/src/glsl/Makefile.sources +++ b/src/glsl/Makefile.sources @@ -66,6 +66,7 @@ LIBGLSL_CXX_FILES = \ $(GLSL_SRCDIR)/lower_vec_index_to_swizzle.cpp \ $(GLSL_SRCDIR)/lower_vector.cpp \ $(GLSL_SRCDIR)/lower_output_reads.cpp \ + $(GLSL_SRCDIR)/lower_ubo_reference.cpp \ $(GLSL_SRCDIR)/opt_algebraic.cpp \ $(GLSL_SRCDIR)/opt_array_splitting.cpp \ $(GLSL_SRCDIR)/opt_constant_folding.cpp \ diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h index c435d77..2220d51 100644 --- a/src/glsl/ir_optimization.h +++ b/src/glsl/ir_optimization.h @@ -74,6 +74,7 @@ bool lower_variable_index_to_cond_assign(exec_list *instructions, bool lower_quadop_vector(exec_list *instructions, bool dont_lower_swz); bool lower_clip_distance(exec_list *instructions); void lower_output_reads(exec_list *instructions); +void lower_ubo_reference(struct gl_shader *shader, exec_list *instructions); bool optimize_redundant_jumps(exec_list *instructions); bool optimize_split_arrays(exec_list *instructions, bool linked); diff --git a/src/glsl/lower_ubo_reference.cpp b/src/glsl/lower_ubo_reference.cpp new file mode 100644 index 000..6a8d75d --- /dev/null +++ b/src/glsl/lower_ubo_reference.cpp @@ -0,0 +1,325 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the Software), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +/** + * \file lower_ubo_reference.cpp + * IR lower pass to remove noise opcodes. + */ + +#include ir.h +#include ir_builder.h +#include ir_rvalue_visitor.h +#include main/macros.h + +using namespace ir_builder; + +namespace { +class lower_ubo_reference_visitor : public ir_rvalue_enter_visitor { +public: + lower_ubo_reference_visitor(struct gl_shader *shader) + : shader(shader) + { + } + + void handle_rvalue(ir_rvalue **rvalue); + void emit_ubo_loads(ir_dereference *deref, ir_variable *offset); + ir_expression *ubo_load(const struct glsl_type *type, +ir_rvalue *offset); + + void *mem_ctx; + struct gl_shader *shader; + struct gl_uniform_buffer_variable *ubo_var; + unsigned uniform_block; + bool progress; +}; + +static inline unsigned int +align(unsigned int a, unsigned int align) +{ + return (a + align - 1) / align * align; +} + +void +lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue) +{ + if (!*rvalue) + return; + + ir_dereference *deref = (*rvalue)-as_dereference(); + if (!deref) + return; + + ir_variable *var = deref-variable_referenced(); + if (!var || var-uniform_block == -1) + return; + + mem_ctx = ralloc_parent(*rvalue); + uniform_block = var-uniform_block; + struct gl_uniform_block *block = shader-UniformBlocks[uniform_block]; + this-ubo_var = block-Uniforms[var-location]; + ir_rvalue *offset = new(mem_ctx) ir_constant(0u); + unsigned const_offset = 0; + bool row_major = ubo_var-RowMajor; Could you put a comment here describing what this large block of code is intending to do? Something like: /* Calculate the offset to the start of the dereference. */ + while (deref) { + switch (deref-ir_type) { + case ir_type_dereference_variable: { + const_offset += ubo_var-Offset; + deref = NULL; + break; + } + + case ir_type_dereference_array: { + ir_dereference_array *deref_array = (ir_dereference_array *)deref; + unsigned array_stride; + if
[Mesa-dev] [PATCH 05/22] glsl: Add a lowering pass to turn complicated UBO references to vector loads.
--- src/glsl/Makefile.sources|1 + src/glsl/ir_optimization.h |1 + src/glsl/lower_ubo_reference.cpp | 325 ++ 3 files changed, 327 insertions(+) create mode 100644 src/glsl/lower_ubo_reference.cpp diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources index f2743f7..765f06a 100644 --- a/src/glsl/Makefile.sources +++ b/src/glsl/Makefile.sources @@ -66,6 +66,7 @@ LIBGLSL_CXX_FILES = \ $(GLSL_SRCDIR)/lower_vec_index_to_swizzle.cpp \ $(GLSL_SRCDIR)/lower_vector.cpp \ $(GLSL_SRCDIR)/lower_output_reads.cpp \ + $(GLSL_SRCDIR)/lower_ubo_reference.cpp \ $(GLSL_SRCDIR)/opt_algebraic.cpp \ $(GLSL_SRCDIR)/opt_array_splitting.cpp \ $(GLSL_SRCDIR)/opt_constant_folding.cpp \ diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h index c435d77..2220d51 100644 --- a/src/glsl/ir_optimization.h +++ b/src/glsl/ir_optimization.h @@ -74,6 +74,7 @@ bool lower_variable_index_to_cond_assign(exec_list *instructions, bool lower_quadop_vector(exec_list *instructions, bool dont_lower_swz); bool lower_clip_distance(exec_list *instructions); void lower_output_reads(exec_list *instructions); +void lower_ubo_reference(struct gl_shader *shader, exec_list *instructions); bool optimize_redundant_jumps(exec_list *instructions); bool optimize_split_arrays(exec_list *instructions, bool linked); diff --git a/src/glsl/lower_ubo_reference.cpp b/src/glsl/lower_ubo_reference.cpp new file mode 100644 index 000..6a8d75d --- /dev/null +++ b/src/glsl/lower_ubo_reference.cpp @@ -0,0 +1,325 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the Software), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +/** + * \file lower_ubo_reference.cpp + * IR lower pass to remove noise opcodes. + */ + +#include ir.h +#include ir_builder.h +#include ir_rvalue_visitor.h +#include main/macros.h + +using namespace ir_builder; + +namespace { +class lower_ubo_reference_visitor : public ir_rvalue_enter_visitor { +public: + lower_ubo_reference_visitor(struct gl_shader *shader) + : shader(shader) + { + } + + void handle_rvalue(ir_rvalue **rvalue); + void emit_ubo_loads(ir_dereference *deref, ir_variable *offset); + ir_expression *ubo_load(const struct glsl_type *type, + ir_rvalue *offset); + + void *mem_ctx; + struct gl_shader *shader; + struct gl_uniform_buffer_variable *ubo_var; + unsigned uniform_block; + bool progress; +}; + +static inline unsigned int +align(unsigned int a, unsigned int align) +{ + return (a + align - 1) / align * align; +} + +void +lower_ubo_reference_visitor::handle_rvalue(ir_rvalue **rvalue) +{ + if (!*rvalue) + return; + + ir_dereference *deref = (*rvalue)-as_dereference(); + if (!deref) + return; + + ir_variable *var = deref-variable_referenced(); + if (!var || var-uniform_block == -1) + return; + + mem_ctx = ralloc_parent(*rvalue); + uniform_block = var-uniform_block; + struct gl_uniform_block *block = shader-UniformBlocks[uniform_block]; + this-ubo_var = block-Uniforms[var-location]; + ir_rvalue *offset = new(mem_ctx) ir_constant(0u); + unsigned const_offset = 0; + bool row_major = ubo_var-RowMajor; + + while (deref) { + switch (deref-ir_type) { + case ir_type_dereference_variable: { +const_offset += ubo_var-Offset; +deref = NULL; +break; + } + + case ir_type_dereference_array: { +ir_dereference_array *deref_array = (ir_dereference_array *)deref; +unsigned array_stride; +if (deref_array-array-type-is_matrix() row_major) { + /* When loading a vector out of a row major matrix, the +* step between the columns (vectors) is the size of a +* float, while the step between the rows (elements of a +* vector) is handled below in emit_ubo_loads. +