nir/tex_instr: Add a nir_tex_src struct and dynamically allocate the src array
[mesa.git] / src / glsl / nir / nir_lower_samplers.cpp
1 /*
2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2014 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "nir.h"
27 #include "../program.h"
28 #include "program/hash_table.h"
29 #include "ir_uniform.h"
30
31 extern "C" {
32 #include "main/compiler.h"
33 #include "main/mtypes.h"
34 #include "program/prog_parameter.h"
35 #include "program/program.h"
36 }
37
38 static unsigned
39 get_sampler_index(struct gl_shader_program *shader_program, const char *name,
40 const struct gl_program *prog)
41 {
42 GLuint shader = _mesa_program_enum_to_shader_stage(prog->Target);
43
44 unsigned location;
45 if (!shader_program->UniformHash->get(location, name)) {
46 linker_error(shader_program,
47 "failed to find sampler named %s.\n", name);
48 return 0;
49 }
50
51 if (!shader_program->UniformStorage[location].sampler[shader].active) {
52 assert(0 && "cannot return a sampler");
53 linker_error(shader_program,
54 "cannot return a sampler named %s, because it is not "
55 "used in this shader stage. This is a driver bug.\n",
56 name);
57 return 0;
58 }
59
60 return shader_program->UniformStorage[location].sampler[shader].index;
61 }
62
63 static void
64 lower_sampler(nir_tex_instr *instr, struct gl_shader_program *shader_program,
65 const struct gl_program *prog, void *mem_ctx)
66 {
67 if (instr->sampler == NULL)
68 return;
69
70 /* Get the name and the offset */
71 instr->sampler_index = 0;
72 bool has_indirect = false;
73 char *name = ralloc_strdup(mem_ctx, instr->sampler->var->name);
74
75 for (nir_deref *deref = &instr->sampler->deref;
76 deref->child; deref = deref->child) {
77 switch (deref->child->deref_type) {
78 case nir_deref_type_array: {
79 nir_deref_array *deref_array = nir_deref_as_array(deref->child);
80
81 /* XXX: We're assuming here that the indirect is the last array
82 * thing we have. This should be ok for now as we don't support
83 * arrays_of_arrays yet.
84 */
85 assert(!has_indirect);
86
87 instr->sampler_index *= glsl_get_length(deref->type);
88 switch (deref_array->deref_array_type) {
89 case nir_deref_array_type_direct:
90 instr->sampler_index += deref_array->base_offset;
91 if (deref_array->deref.child)
92 ralloc_asprintf_append(&name, "[%u]", deref_array->base_offset);
93 break;
94 case nir_deref_array_type_indirect: {
95 assert(!has_indirect);
96
97 instr->src = reralloc(mem_ctx, instr->src, nir_tex_src,
98 instr->num_srcs + 1);
99 memset(&instr->src[instr->num_srcs], 0, sizeof *instr->src);
100 instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
101 instr->num_srcs++;
102
103 nir_instr_rewrite_src(&instr->instr,
104 &instr->src[instr->num_srcs - 1].src,
105 nir_src_copy(deref_array->indirect, mem_ctx));
106
107 instr->sampler_array_size = glsl_get_length(deref->type);
108
109 nir_src empty;
110 memset(&empty, 0, sizeof empty);
111 nir_instr_rewrite_src(&instr->instr, &deref_array->indirect, empty);
112
113 if (deref_array->deref.child)
114 ralloc_strcat(&name, "[0]");
115 break;
116 }
117
118 case nir_deref_array_type_wildcard:
119 unreachable("Cannot copy samplers");
120 default:
121 unreachable("Invalid deref array type");
122 }
123 break;
124 }
125
126 case nir_deref_type_struct: {
127 nir_deref_struct *deref_struct = nir_deref_as_struct(deref->child);
128 const char *field = glsl_get_struct_elem_name(deref->type,
129 deref_struct->index);
130 ralloc_asprintf_append(&name, ".%s", field);
131 break;
132 }
133
134 default:
135 unreachable("Invalid deref type");
136 break;
137 }
138 }
139
140 instr->sampler_index += get_sampler_index(shader_program, name, prog);
141
142 instr->sampler = NULL;
143 }
144
145 typedef struct {
146 void *mem_ctx;
147 struct gl_shader_program *shader_program;
148 struct gl_program *prog;
149 } lower_state;
150
151 static bool
152 lower_block_cb(nir_block *block, void *_state)
153 {
154 lower_state *state = (lower_state *) _state;
155
156 nir_foreach_instr(block, instr) {
157 if (instr->type == nir_instr_type_tex) {
158 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
159 lower_sampler(tex_instr, state->shader_program, state->prog,
160 state->mem_ctx);
161 }
162 }
163
164 return true;
165 }
166
167 static void
168 lower_impl(nir_function_impl *impl, struct gl_shader_program *shader_program,
169 struct gl_program *prog)
170 {
171 lower_state state;
172
173 state.mem_ctx = ralloc_parent(impl);
174 state.shader_program = shader_program;
175 state.prog = prog;
176
177 nir_foreach_block(impl, lower_block_cb, &state);
178 }
179
180 extern "C" void
181 nir_lower_samplers(nir_shader *shader, struct gl_shader_program *shader_program,
182 struct gl_program *prog)
183 {
184 nir_foreach_overload(shader, overload) {
185 if (overload->impl)
186 lower_impl(overload->impl, shader_program, prog);
187 }
188 }