nir: Make nir_lower_samplers take a gl_shader_stage, not a gl_program *.
[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,
40 gl_shader_stage stage, const char *name)
41 {
42 unsigned location;
43 if (!shader_program->UniformHash->get(location, name)) {
44 linker_error(shader_program,
45 "failed to find sampler named %s.\n", name);
46 return 0;
47 }
48
49 if (!shader_program->UniformStorage[location].sampler[stage].active) {
50 assert(0 && "cannot return a sampler");
51 linker_error(shader_program,
52 "cannot return a sampler named %s, because it is not "
53 "used in this shader stage. This is a driver bug.\n",
54 name);
55 return 0;
56 }
57
58 return shader_program->UniformStorage[location].sampler[stage].index;
59 }
60
61 static void
62 lower_sampler(nir_tex_instr *instr, struct gl_shader_program *shader_program,
63 gl_shader_stage stage, void *mem_ctx)
64 {
65 if (instr->sampler == NULL)
66 return;
67
68 /* Get the name and the offset */
69 instr->sampler_index = 0;
70 char *name = ralloc_strdup(mem_ctx, instr->sampler->var->name);
71
72 for (nir_deref *deref = &instr->sampler->deref;
73 deref->child; deref = deref->child) {
74 switch (deref->child->deref_type) {
75 case nir_deref_type_array: {
76 nir_deref_array *deref_array = nir_deref_as_array(deref->child);
77
78 /* XXX: We're assuming here that the indirect is the last array
79 * thing we have. This should be ok for now as we don't support
80 * arrays_of_arrays yet.
81 */
82
83 instr->sampler_index *= glsl_get_length(deref->type);
84 switch (deref_array->deref_array_type) {
85 case nir_deref_array_type_direct:
86 instr->sampler_index += deref_array->base_offset;
87 if (deref_array->deref.child)
88 ralloc_asprintf_append(&name, "[%u]", deref_array->base_offset);
89 break;
90 case nir_deref_array_type_indirect: {
91 instr->src = reralloc(instr, instr->src, nir_tex_src,
92 instr->num_srcs + 1);
93 memset(&instr->src[instr->num_srcs], 0, sizeof *instr->src);
94 instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
95 instr->num_srcs++;
96
97 nir_instr_rewrite_src(&instr->instr,
98 &instr->src[instr->num_srcs - 1].src,
99 deref_array->indirect);
100
101 instr->sampler_array_size = glsl_get_length(deref->type);
102
103 nir_src empty;
104 memset(&empty, 0, sizeof empty);
105 nir_instr_rewrite_src(&instr->instr, &deref_array->indirect, empty);
106
107 if (deref_array->deref.child)
108 ralloc_strcat(&name, "[0]");
109 break;
110 }
111
112 case nir_deref_array_type_wildcard:
113 unreachable("Cannot copy samplers");
114 default:
115 unreachable("Invalid deref array type");
116 }
117 break;
118 }
119
120 case nir_deref_type_struct: {
121 nir_deref_struct *deref_struct = nir_deref_as_struct(deref->child);
122 const char *field = glsl_get_struct_elem_name(deref->type,
123 deref_struct->index);
124 ralloc_asprintf_append(&name, ".%s", field);
125 break;
126 }
127
128 default:
129 unreachable("Invalid deref type");
130 break;
131 }
132 }
133
134 instr->sampler_index += get_sampler_index(shader_program, stage, name);
135
136 instr->sampler = NULL;
137 }
138
139 typedef struct {
140 void *mem_ctx;
141 struct gl_shader_program *shader_program;
142 gl_shader_stage stage;
143 } lower_state;
144
145 static bool
146 lower_block_cb(nir_block *block, void *_state)
147 {
148 lower_state *state = (lower_state *) _state;
149
150 nir_foreach_instr(block, instr) {
151 if (instr->type == nir_instr_type_tex) {
152 nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
153 lower_sampler(tex_instr, state->shader_program, state->stage,
154 state->mem_ctx);
155 }
156 }
157
158 return true;
159 }
160
161 static void
162 lower_impl(nir_function_impl *impl, struct gl_shader_program *shader_program,
163 gl_shader_stage stage)
164 {
165 lower_state state;
166
167 state.mem_ctx = ralloc_parent(impl);
168 state.shader_program = shader_program;
169 state.stage = stage;
170
171 nir_foreach_block(impl, lower_block_cb, &state);
172 }
173
174 extern "C" void
175 nir_lower_samplers(nir_shader *shader, struct gl_shader_program *shader_program,
176 gl_shader_stage stage)
177 {
178 nir_foreach_overload(shader, overload) {
179 if (overload->impl)
180 lower_impl(overload->impl, shader_program, stage);
181 }
182 }