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
) {
51 switch (deref
->deref_type
) {
52 case nir_deref_type_array
:
53 deref_array
= nir_deref_as_array(deref
);
54 if (deref_array
->has_indirect
) {
55 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
56 * while GLSL 1.30 requires that the array indices be
57 * constant integer expressions. We don't expect any driver
58 * to actually work with a really variable array index, so
59 * all that would work would be an unrolled loop counter that
60 * ends up being constant.
62 ralloc_strcat(&shader_program
->InfoLog
,
63 "warning: Variable sampler array index unsupported.\n"
64 "This feature of the language was removed in GLSL 1.20 "
65 "and is unlikely to be supported for 1.10 in Mesa.\n");
67 if (deref
->child
== NULL
) {
68 return deref_array
->base_offset
;
70 ralloc_asprintf_append(name
, "[%u]", deref_array
->base_offset
);
73 case nir_deref_type_struct
:
74 deref_struct
= nir_deref_as_struct(deref
);
75 ralloc_asprintf_append(name
, ".%s", deref_struct
->elem
);
88 get_sampler_index(nir_deref_var
*sampler
,
89 struct gl_shader_program
*shader_program
,
90 const struct gl_program
*prog
)
92 void *mem_ctx
= ralloc_context(NULL
);
94 unsigned offset
= get_deref_name_offset(sampler
, shader_program
, &name
,
97 GLuint shader
= _mesa_program_enum_to_shader_stage(prog
->Target
);
100 if (!shader_program
->UniformHash
->get(location
, name
)) {
101 linker_error(shader_program
,
102 "failed to find sampler named %s.\n", name
);
106 if (!shader_program
->UniformStorage
[location
].sampler
[shader
].active
) {
107 assert(0 && "cannot return a sampler");
108 linker_error(shader_program
,
109 "cannot return a sampler named %s, because it is not "
110 "used in this shader stage. This is a driver bug.\n",
115 ralloc_free(mem_ctx
);
117 return shader_program
->UniformStorage
[location
].sampler
[shader
].index
+
122 lower_sampler(nir_tex_instr
*instr
, struct gl_shader_program
*shader_program
,
123 const struct gl_program
*prog
)
125 if (instr
->sampler
) {
126 instr
->sampler_index
= get_sampler_index(instr
->sampler
, shader_program
,
128 instr
->sampler
= NULL
;
133 struct gl_shader_program
*shader_program
;
134 struct gl_program
*prog
;
138 lower_block_cb(nir_block
*block
, void *_state
)
140 lower_state
*state
= (lower_state
*) _state
;
142 nir_foreach_instr(block
, instr
) {
143 if (instr
->type
== nir_instr_type_texture
) {
144 nir_tex_instr
*tex_instr
= nir_instr_as_texture(instr
);
145 lower_sampler(tex_instr
, state
->shader_program
, state
->prog
);
153 lower_impl(nir_function_impl
*impl
, struct gl_shader_program
*shader_program
,
154 struct gl_program
*prog
)
157 state
.shader_program
= shader_program
;
159 nir_foreach_block(impl
, lower_block_cb
, &state
);
163 nir_lower_samplers(nir_shader
*shader
, struct gl_shader_program
*shader_program
,
164 struct gl_program
*prog
)
166 nir_foreach_overload(shader
, overload
) {
168 lower_impl(overload
->impl
, shader_program
, prog
);