2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2014 Intel Corporation
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:
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
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.
27 #include "../program.h"
28 #include "program/hash_table.h"
29 #include "ir_uniform.h"
32 #include "main/compiler.h"
33 #include "main/mtypes.h"
34 #include "program/prog_parameter.h"
35 #include "program/program.h"
39 get_deref_name_offset(nir_deref_var
*deref_var
,
40 gl_shader_program
*shader_program
, char **name
,
43 nir_deref
*deref
= &deref_var
->deref
;
44 nir_deref_array
*deref_array
;
45 nir_deref_struct
*deref_struct
;
47 *name
= ralloc_strdup(mem_ctx
, deref_var
->var
->name
);
49 while (deref
->child
!= NULL
) {
50 switch (deref
->child
->deref_type
) {
51 case nir_deref_type_array
:
52 deref_array
= nir_deref_as_array(deref
->child
);
53 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
54 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
55 * while GLSL 1.30 requires that the array indices be
56 * constant integer expressions. We don't expect any driver
57 * to actually work with a really variable array index, so
58 * all that would work would be an unrolled loop counter that
59 * ends up being constant.
61 ralloc_strcat(&shader_program
->InfoLog
,
62 "warning: Variable sampler array index unsupported.\n"
63 "This feature of the language was removed in GLSL 1.20 "
64 "and is unlikely to be supported for 1.10 in Mesa.\n");
66 if (deref_array
->deref
.child
== NULL
) {
67 return deref_array
->base_offset
;
69 ralloc_asprintf_append(name
, "[%u]", deref_array
->base_offset
);
72 case nir_deref_type_struct
: {
73 deref_struct
= nir_deref_as_struct(deref
->child
);
74 const char *field
= glsl_get_struct_elem_name(deref
->type
,
76 ralloc_asprintf_append(name
, ".%s", field
);
91 get_sampler_index(nir_deref_var
*sampler
,
92 struct gl_shader_program
*shader_program
,
93 const struct gl_program
*prog
)
95 void *mem_ctx
= ralloc_context(NULL
);
97 unsigned offset
= get_deref_name_offset(sampler
, shader_program
, &name
,
100 GLuint shader
= _mesa_program_enum_to_shader_stage(prog
->Target
);
103 if (!shader_program
->UniformHash
->get(location
, name
)) {
104 linker_error(shader_program
,
105 "failed to find sampler named %s.\n", name
);
109 if (!shader_program
->UniformStorage
[location
].sampler
[shader
].active
) {
110 assert(0 && "cannot return a sampler");
111 linker_error(shader_program
,
112 "cannot return a sampler named %s, because it is not "
113 "used in this shader stage. This is a driver bug.\n",
118 ralloc_free(mem_ctx
);
120 return shader_program
->UniformStorage
[location
].sampler
[shader
].index
+
125 lower_sampler(nir_tex_instr
*instr
, struct gl_shader_program
*shader_program
,
126 const struct gl_program
*prog
)
128 if (instr
->sampler
) {
129 instr
->sampler_index
= get_sampler_index(instr
->sampler
, shader_program
,
132 memset(&empty_src
, 0, sizeof empty_src
);
133 for (nir_deref
*deref
= &instr
->sampler
->deref
; deref
; deref
= deref
->child
) {
134 if (deref
->deref_type
== nir_deref_type_array
) {
135 nir_deref_array
*arr
= nir_deref_as_array(deref
);
136 nir_instr_rewrite_src(&instr
->instr
, &arr
->indirect
, empty_src
);
140 instr
->sampler
= NULL
;
145 struct gl_shader_program
*shader_program
;
146 struct gl_program
*prog
;
150 lower_block_cb(nir_block
*block
, void *_state
)
152 lower_state
*state
= (lower_state
*) _state
;
154 nir_foreach_instr(block
, instr
) {
155 if (instr
->type
== nir_instr_type_tex
) {
156 nir_tex_instr
*tex_instr
= nir_instr_as_tex(instr
);
157 lower_sampler(tex_instr
, state
->shader_program
, state
->prog
);
165 lower_impl(nir_function_impl
*impl
, struct gl_shader_program
*shader_program
,
166 struct gl_program
*prog
)
169 state
.shader_program
= shader_program
;
171 nir_foreach_block(impl
, lower_block_cb
, &state
);
175 nir_lower_samplers(nir_shader
*shader
, struct gl_shader_program
*shader_program
,
176 struct gl_program
*prog
)
178 nir_foreach_overload(shader
, overload
) {
180 lower_impl(overload
->impl
, shader_program
, prog
);