<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Mar 26, 2016 at 2:02 PM, Rob Clark <span dir="ltr"><<a href="mailto:robdclark@gmail.com" target="_blank">robdclark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
<br>
Signed-off-by: Rob Clark <<a href="mailto:robclark@freedesktop.org">robclark@freedesktop.org</a>><br>
---<br>
src/mesa/Makefile.sources | 2 +<br>
src/mesa/state_tracker/st_nir.h | 28 +++<br>
src/mesa/state_tracker/st_nir_lower_builtin.c | 242 ++++++++++++++++++++++++++<br>
3 files changed, 272 insertions(+)<br>
create mode 100644 src/mesa/state_tracker/st_nir.h<br>
create mode 100644 src/mesa/state_tracker/st_nir_lower_builtin.c<br>
<br>
diff --git a/src/mesa/Makefile.sources b/src/mesa/Makefile.sources<br>
index 7b5b6ea..6c93d07 100644<br>
--- a/src/mesa/Makefile.sources<br>
+++ b/src/mesa/Makefile.sources<br>
@@ -495,6 +495,8 @@ STATETRACKER_FILES = \<br>
state_tracker/st_manager.h \<br>
state_tracker/st_mesa_to_tgsi.c \<br>
state_tracker/st_mesa_to_tgsi.h \<br>
+ state_tracker/st_nir.h \<br>
+ state_tracker/st_nir_lower_builtin.c \<br>
state_tracker/st_program.c \<br>
state_tracker/st_program.h \<br>
state_tracker/st_texture.c \<br>
diff --git a/src/mesa/state_tracker/st_nir.h b/src/mesa/state_tracker/st_nir.h<br>
new file mode 100644<br>
index 0000000..1c07c4c<br>
--- /dev/null<br>
+++ b/src/mesa/state_tracker/st_nir.h<br>
@@ -0,0 +1,28 @@<br>
+/*<br>
+ * Copyright © 2016 Red Hat<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,<br>
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br>
+ * SOFTWARE.<br>
+ */<br>
+<br>
+#pragma once<br>
+<br>
+typedef struct nir_shader nir_shader;<br>
+<br>
+void st_nir_lower_builtin(nir_shader *shader);<br>
diff --git a/src/mesa/state_tracker/st_nir_lower_builtin.c b/src/mesa/state_tracker/st_nir_lower_builtin.c<br>
new file mode 100644<br>
index 0000000..7f3262f<br>
--- /dev/null<br>
+++ b/src/mesa/state_tracker/st_nir_lower_builtin.c<br>
@@ -0,0 +1,242 @@<br>
+/*<br>
+ * Copyright © 2016 Red Hat<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,<br>
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE<br>
+ * SOFTWARE.<br>
+ */<br>
+<br>
+/* Lowering pass that lowers accesses to built-in uniform variables.<br>
+ * Built-in uniforms are not necessarily packed the same way that<br>
+ * normal uniform structs are, for example:<br>
+ *<br>
+ * struct gl_FogParameters {<br>
+ * vec4 color;<br>
+ * float density;<br>
+ * float start;<br>
+ * float end;<br>
+ * float scale;<br>
+ * };<br>
+ *<br>
+ * is packed into vec4[2], whereas the same struct would be packed<br>
+ * (by gallium), as vec4[5] if it where not built-in. Because of<br>
+ * this, we need to replace (for example) access like:<br>
+ *<br>
+ * vec1 ssa_1 = intrinsic load_var () (gl_Fog.start) ()<br>
+ *<br>
+ * with:<br>
+ *<br>
+ * vec4 ssa_2 = intrinsic load_var () (fog.params) ()<br>
+ * vec1 ssa_1 = ssa_2.y<br>
+ *<br>
+ * with appropriate substitutions in the uniform variables list:<br>
+ *<br>
+ * decl_var uniform INTERP_QUALIFIER_NONE gl_FogParameters gl_Fog (0, 0)<br>
+ *<br>
+ * would become:<br>
+ *<br>
+ * decl_var uniform INTERP_QUALIFIER_NONE vec4 state.fog.color (0, 0)<br>
+ * decl_var uniform INTERP_QUALIFIER_NONE vec4 state.fog.params (0, 1)<br>
+ *<br>
+ * See in particular 'struct gl_builtin_uniform_element'.<br>
+ */<br>
+<br>
+#include "compiler/nir/nir.h"<br>
+#include "compiler/nir/nir_builder.h"<br>
+#include "st_nir.h"<br>
+#include "compiler/glsl/ir.h"<br>
+#include "uniforms.h"<br>
+#include "program/prog_instruction.h"<br>
+<br>
+typedef struct {<br>
+ nir_shader *shader;<br>
+ nir_builder builder;<br>
+ void *mem_ctx;<br>
+} lower_builtin_state;<br>
+<br>
+static const struct gl_builtin_uniform_element *<br>
+get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_var *deref)<br>
+{<br>
+ nir_deref *tail = &deref->deref;<br>
+<br>
+ if ((desc->num_elements == 1) && (desc->elements[0].field == NULL))<br>
+ return NULL;<br>
+<br>
+ /* we handle array's in get_variable(): */<br>
+ if (tail->child->deref_type == nir_deref_type_array)<br>
+ tail = tail->child;<br>
+<br>
+ /* don't need to deal w/ non-struct or array of non-struct: */<br>
+ if (!tail->child)<br>
+ return NULL;<br>
+<br>
+ if (tail->child->deref_type != nir_deref_type_struct)<br>
+ return NULL;<br>
+<br>
+ nir_deref_struct *deref_struct = nir_deref_as_struct(tail->child);<br>
+<br>
+ assert(deref_struct->index < desc->num_elements);<br>
+<br>
+ return &desc->elements[deref_struct->index];<br>
+}<br>
+<br>
+static nir_variable *<br>
+get_variable(lower_builtin_state *state, nir_deref_var *deref,<br>
+ const struct gl_builtin_uniform_element *element)<br>
+{<br>
+ nir_shader *shader = state->shader;<br>
+ int tokens[STATE_LENGTH];<br>
+<br>
+ memcpy(tokens, element->tokens, sizeof(tokens));<br>
+<br>
+ if (deref->deref.child->deref_type == nir_deref_type_array) {<br>
+ nir_deref_array *darr = nir_deref_as_array(deref->deref.child);<br>
+<br>
+ assert(darr->deref_array_type == nir_deref_array_type_direct);<br>
+<br>
+ /* we need to fixup the array index slot: */<br>
+ switch (tokens[0]) {<br>
+ case STATE_MODELVIEW_MATRIX:<br>
+ case STATE_PROJECTION_MATRIX:<br>
+ case STATE_MVP_MATRIX:<br>
+ case STATE_TEXTURE_MATRIX:<br>
+ case STATE_PROGRAM_MATRIX:<br>
+ case STATE_LIGHT:<br>
+ case STATE_LIGHTPROD:<br>
+ case STATE_TEXGEN:<br>
+ case STATE_TEXENV_COLOR:<br>
+ case STATE_CLIPPLANE:<br>
+ tokens[1] = darr->base_offset;<br>
+ break;<br>
+ }<br>
+ }<br>
+<br>
+ char *name = _mesa_program_state_string((gl_state_index *)tokens);<br>
+<br>
+ nir_foreach_variable(var, &shader->uniforms)<br>
+ if (strcmp(var->name, name) == 0)<br>
+ return var;<br>
+<br>
+ /* variable doesn't exist yet, so create it: */<br>
+ nir_variable *var =<br>
+ nir_variable_create(shader, nir_var_uniform, glsl_vec4_type(), name);<br>
+<br>
+ var->num_state_slots = 1;<br>
+ var->state_slots = ralloc_array(var, nir_state_slot, 1);<br>
+ memcpy(var->state_slots[0].tokens, tokens,<br>
+ sizeof(var->state_slots[0].tokens));<br>
+<br>
+ free(name);<br>
+<br>
+ return var;<br>
+}<br>
+<br>
+static bool<br>
+lower_builtin_block(nir_block *block, void *_state)<br>
+{<br>
+ lower_builtin_state *state = _state;<br>
+<br>
+ nir_builder *b = &state->builder;<br>
+<br>
+ nir_foreach_instr_safe(block, instr) {<br>
+ if (instr->type != nir_instr_type_intrinsic)<br>
+ continue;<br>
+<br>
+ nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
+<br>
+ if (intrin->intrinsic != nir_intrinsic_load_var)<br>
+ continue;<br>
+<br>
+ nir_variable *var = intrin->variables[0]->var;<br>
+ if (var->data.mode != nir_var_uniform)<br>
+ continue;<br>
+<br>
+ /* built-in's will always start with "gl_" */<br>
+ if (strncmp(var->name, "gl_", 3) != 0)<br>
+ continue;<br>
+<br>
+ const struct gl_builtin_uniform_desc *desc =<br>
+ _mesa_glsl_get_builtin_uniform_desc(var->name);<br>
+<br>
+ /* if no descriptor, it isn't something we need to handle specially: */<br>
+ if (!desc)<br>
+ continue;<br>
+<br>
+ const struct gl_builtin_uniform_element *element =<br>
+ get_element(desc, intrin->variables[0]);<br>
+<br>
+ /* matrix elements (array_deref) do not need special handling: */<br>
+ if (!element)<br>
+ continue;<br>
+<br>
+ /* remove existing var from uniform list: */<br>
+ exec_node_remove(&var->node);<br>
+ exec_node_self_link(&var->node); /* no delinit() :-( */<br></blockquote><div><br></div><div>Are you sure you want to remove it multiple times? There can be more than one load_var for a particular uniform. I guess since you self_link, it's safe to do so. Maybe throw in a comment?<br><br></div><div>Other than that, this looks mostly ok to me. I can't really say that I know enough to properly review it though.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+ nir_variable *new_var =<br>
+ get_variable(state, intrin->variables[0], element);<br>
+<br>
+ b->cursor = nir_before_instr(instr);<br>
+<br>
+ nir_ssa_def *def = nir_load_var(b, new_var);<br>
+<br>
+ /* swizzle the result: */<br>
+ unsigned swiz[4];<br>
+ for (unsigned i = 0; i < 4; i++) {<br>
+ swiz[i] = GET_SWZ(element->swizzle, i);<br>
+ assert(swiz[i] <= SWIZZLE_W);<br>
+ }<br>
+ def = nir_swizzle(b, def, swiz, intrin->num_components, true);<br>
+<br>
+ /* and rewrite uses of original instruction: */<br>
+ assert(intrin->dest.is_ssa);<br>
+ nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(def));<br>
+<br>
+ /* at this point intrin should be unused. We need to remove it<br>
+ * (rather than waiting for DCE pass) to avoid dangling reference<br>
+ * to remove'd var. And we have to remove the original uniform<br>
+ * var since we don't want it to get uniform space allocated.<br>
+ */<br>
+ exec_node_remove(&intrin->instr.node);<br>
+ }<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+static void<br>
+lower_builtin_impl(lower_builtin_state *state, nir_function_impl *impl)<br>
+{<br>
+ nir_builder_init(&state->builder, impl);<br>
+ state->mem_ctx = ralloc_parent(impl);<br>
+<br>
+ nir_foreach_block(impl, lower_builtin_block, state);<br>
+<br>
+ nir_metadata_preserve(impl, nir_metadata_block_index |<br>
+ nir_metadata_dominance);<br>
+}<br>
+<br>
+void<br>
+st_nir_lower_builtin(nir_shader *shader)<br>
+{<br>
+ lower_builtin_state state;<br>
+ state.shader = shader;<br>
+ nir_foreach_function(shader, function) {<br>
+ if (function->impl)<br>
+ lower_builtin_impl(&state, function->impl);<br>
+ }<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.5.5<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>