1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <[email protected]>
31 * Brian Paul
32 */
33
34
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37 #include "main/shaderapi.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "util/u_inlines.h"
41 #include "util/u_upload_mgr.h"
42 #include "cso_cache/cso_context.h"
43
44 #include "main/bufferobj.h"
45 #include "st_debug.h"
46 #include "st_context.h"
47 #include "st_atom.h"
48 #include "st_atom_constbuf.h"
49 #include "st_program.h"
50
51 /* Unbinds the CB0 if it's not used by the current program to avoid leaving
52 * dangling pointers to old (potentially deleted) shaders in the driver.
53 */
54 static void
st_unbind_unused_cb0(struct st_context * st,enum pipe_shader_type shader_type)55 st_unbind_unused_cb0(struct st_context *st, enum pipe_shader_type shader_type)
56 {
57 if (st->state.constbuf0_enabled_shader_mask & (1 << shader_type)) {
58 struct pipe_context *pipe = st->pipe;
59
60 pipe->set_constant_buffer(pipe, shader_type, 0, false, NULL);
61 st->state.constbuf0_enabled_shader_mask &= ~(1 << shader_type);
62 }
63 }
64
65 /**
66 * Pass the given program parameters to the graphics pipe as a
67 * constant buffer.
68 */
69 void
st_upload_constants(struct st_context * st,struct gl_program * prog,gl_shader_stage stage)70 st_upload_constants(struct st_context *st, struct gl_program *prog, gl_shader_stage stage)
71 {
72 enum pipe_shader_type shader_type = pipe_shader_type_from_mesa(stage);
73 if (!prog) {
74 st_unbind_unused_cb0(st, shader_type);
75 return;
76 }
77
78 struct gl_program_parameter_list *params = prog->Parameters;
79
80 assert(shader_type == PIPE_SHADER_VERTEX ||
81 shader_type == PIPE_SHADER_FRAGMENT ||
82 shader_type == PIPE_SHADER_GEOMETRY ||
83 shader_type == PIPE_SHADER_TESS_CTRL ||
84 shader_type == PIPE_SHADER_TESS_EVAL ||
85 shader_type == PIPE_SHADER_COMPUTE);
86
87 /* update the ATI constants before rendering */
88 if (shader_type == PIPE_SHADER_FRAGMENT && prog->ati_fs) {
89 struct ati_fragment_shader *ati_fs = prog->ati_fs;
90 unsigned c;
91
92 for (c = 0; c < MAX_NUM_FRAGMENT_CONSTANTS_ATI; c++) {
93 unsigned offset = params->Parameters[c].ValueOffset;
94 if (ati_fs->LocalConstDef & (1 << c))
95 memcpy(params->ParameterValues + offset,
96 ati_fs->Constants[c], sizeof(GLfloat) * 4);
97 else
98 memcpy(params->ParameterValues + offset,
99 st->ctx->ATIFragmentShader.GlobalConstants[c],
100 sizeof(GLfloat) * 4);
101 }
102 }
103
104 /* Make all bindless samplers/images bound texture/image units resident in
105 * the context.
106 */
107 st_make_bound_samplers_resident(st, prog);
108 st_make_bound_images_resident(st, prog);
109
110 /* update constants */
111 if (params && params->NumParameters) {
112 struct pipe_constant_buffer cb;
113 const uint paramBytes = params->NumParameterValues * sizeof(GLfloat);
114
115 _mesa_shader_write_subroutine_indices(st->ctx, stage);
116
117 cb.buffer = NULL;
118 cb.user_buffer = NULL;
119 cb.buffer_offset = 0;
120 cb.buffer_size = paramBytes;
121
122 if (st->prefer_real_buffer_in_constbuf0) {
123 struct pipe_context *pipe = st->pipe;
124 uint32_t *ptr;
125
126 const unsigned alignment = MAX2(
127 st->ctx->Const.UniformBufferOffsetAlignment, 64);
128
129 /* fetch_state always stores 4 components (16 bytes) per matrix row,
130 * but matrix rows are sometimes allocated partially, so add 12
131 * to compensate for the fetch_state defect.
132 */
133 u_upload_alloc(pipe->const_uploader, 0, paramBytes + 12,
134 alignment, &cb.buffer_offset, &cb.buffer, (void**)&ptr);
135
136 int uniform_bytes = params->UniformBytes;
137 if (uniform_bytes)
138 memcpy(ptr, params->ParameterValues, uniform_bytes);
139
140 /* Upload the constants which come from fixed-function state, such as
141 * transformation matrices, fog factors, etc.
142 */
143 if (params->StateFlags)
144 _mesa_upload_state_parameters(st->ctx, params, ptr);
145
146 u_upload_unmap(pipe->const_uploader);
147 pipe->set_constant_buffer(pipe, shader_type, 0, true, &cb);
148
149 /* Set inlinable constants. This is more involved because state
150 * parameters are uploaded directly above instead of being loaded
151 * into gl_program_parameter_list. The easiest way to get their values
152 * is to load them.
153 */
154 unsigned num_inlinable_uniforms = prog->info.num_inlinable_uniforms;
155 if (num_inlinable_uniforms) {
156 uint32_t values[MAX_INLINABLE_UNIFORMS];
157 gl_constant_value *constbuf = params->ParameterValues;
158 bool loaded_state_vars = false;
159
160 for (unsigned i = 0; i < num_inlinable_uniforms; i++) {
161 unsigned dw_offset = prog->info.inlinable_uniform_dw_offsets[i];
162
163 if (dw_offset * 4 >= uniform_bytes && !loaded_state_vars) {
164 _mesa_load_state_parameters(st->ctx, params);
165 loaded_state_vars = true;
166 }
167
168 values[i] = constbuf[prog->info.inlinable_uniform_dw_offsets[i]].u;
169 }
170
171 pipe->set_inlinable_constants(pipe, shader_type,
172 prog->info.num_inlinable_uniforms,
173 values);
174 }
175 } else {
176 struct pipe_context *pipe = st->pipe;
177
178 cb.user_buffer = params->ParameterValues;
179
180 /* Update the constants which come from fixed-function state, such as
181 * transformation matrices, fog factors, etc.
182 */
183 if (params->StateFlags)
184 _mesa_load_state_parameters(st->ctx, params);
185
186 pipe->set_constant_buffer(pipe, shader_type, 0, false, &cb);
187
188 /* Set inlinable constants. */
189 unsigned num_inlinable_uniforms = prog->info.num_inlinable_uniforms;
190 if (num_inlinable_uniforms) {
191 uint32_t values[MAX_INLINABLE_UNIFORMS];
192 gl_constant_value *constbuf = params->ParameterValues;
193
194 for (unsigned i = 0; i < num_inlinable_uniforms; i++)
195 values[i] = constbuf[prog->info.inlinable_uniform_dw_offsets[i]].u;
196
197 pipe->set_inlinable_constants(pipe, shader_type,
198 prog->info.num_inlinable_uniforms,
199 values);
200 }
201 }
202
203 st->state.constbuf0_enabled_shader_mask |= 1 << shader_type;
204 } else {
205 st_unbind_unused_cb0(st, shader_type);
206 }
207 }
208
209
210 /**
211 * Vertex shader:
212 */
213 void
st_update_vs_constants(struct st_context * st)214 st_update_vs_constants(struct st_context *st)
215 {
216 st_upload_constants(st, st->ctx->VertexProgram._Current,
217 MESA_SHADER_VERTEX);
218 }
219
220 /**
221 * Fragment shader:
222 */
223 void
st_update_fs_constants(struct st_context * st)224 st_update_fs_constants(struct st_context *st)
225 {
226 st_upload_constants(st, st->ctx->FragmentProgram._Current,
227 MESA_SHADER_FRAGMENT);
228 }
229
230
231 /* Geometry shader:
232 */
233 void
st_update_gs_constants(struct st_context * st)234 st_update_gs_constants(struct st_context *st)
235 {
236 st_upload_constants(st, st->ctx->GeometryProgram._Current,
237 MESA_SHADER_GEOMETRY);
238 }
239
240 /* Tessellation control shader:
241 */
242 void
st_update_tcs_constants(struct st_context * st)243 st_update_tcs_constants(struct st_context *st)
244 {
245 st_upload_constants(st, st->ctx->TessCtrlProgram._Current,
246 MESA_SHADER_TESS_CTRL);
247 }
248
249 /* Tessellation evaluation shader:
250 */
251 void
st_update_tes_constants(struct st_context * st)252 st_update_tes_constants(struct st_context *st)
253 {
254 st_upload_constants(st, st->ctx->TessEvalProgram._Current,
255 MESA_SHADER_TESS_EVAL);
256 }
257
258 /* Compute shader:
259 */
260 void
st_update_cs_constants(struct st_context * st)261 st_update_cs_constants(struct st_context *st)
262 {
263 st_upload_constants(st, st->ctx->ComputeProgram._Current,
264 MESA_SHADER_COMPUTE);
265 }
266
267 static void
st_bind_ubos(struct st_context * st,struct gl_program * prog,enum pipe_shader_type shader_type)268 st_bind_ubos(struct st_context *st, struct gl_program *prog,
269 enum pipe_shader_type shader_type)
270 {
271 unsigned i;
272 struct pipe_constant_buffer cb = { 0 };
273
274 if (!prog)
275 return;
276
277 struct pipe_context *pipe = st->pipe;
278
279 for (i = 0; i < prog->sh.NumUniformBlocks; i++) {
280 struct gl_buffer_binding *binding;
281
282 binding =
283 &st->ctx->UniformBufferBindings[prog->sh.UniformBlocks[i]->Binding];
284
285 if (binding->BufferObject) {
286 cb.buffer = _mesa_get_bufferobj_reference(st->ctx,
287 binding->BufferObject);
288 } else {
289 cb.buffer = NULL;
290 }
291
292 if (cb.buffer) {
293 cb.buffer_offset = binding->Offset;
294 cb.buffer_size = cb.buffer->width0 - binding->Offset;
295
296 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
297 * Take the minimum just to be sure.
298 */
299 if (!binding->AutomaticSize)
300 cb.buffer_size = MIN2(cb.buffer_size, (unsigned) binding->Size);
301 }
302 else {
303 cb.buffer_offset = 0;
304 cb.buffer_size = 0;
305 }
306
307 pipe->set_constant_buffer(pipe, shader_type, 1 + i, true, &cb);
308 }
309 }
310
311 void
st_bind_vs_ubos(struct st_context * st)312 st_bind_vs_ubos(struct st_context *st)
313 {
314 struct gl_program *prog =
315 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
316
317 st_bind_ubos(st, prog, PIPE_SHADER_VERTEX);
318 }
319
320 void
st_bind_fs_ubos(struct st_context * st)321 st_bind_fs_ubos(struct st_context *st)
322 {
323 struct gl_program *prog =
324 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
325
326 st_bind_ubos(st, prog, PIPE_SHADER_FRAGMENT);
327 }
328
329 void
st_bind_gs_ubos(struct st_context * st)330 st_bind_gs_ubos(struct st_context *st)
331 {
332 struct gl_program *prog =
333 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
334
335 st_bind_ubos(st, prog, PIPE_SHADER_GEOMETRY);
336 }
337
338 void
st_bind_tcs_ubos(struct st_context * st)339 st_bind_tcs_ubos(struct st_context *st)
340 {
341 struct gl_program *prog =
342 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
343
344 st_bind_ubos(st, prog, PIPE_SHADER_TESS_CTRL);
345 }
346
347 void
st_bind_tes_ubos(struct st_context * st)348 st_bind_tes_ubos(struct st_context *st)
349 {
350 struct gl_program *prog =
351 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
352
353 st_bind_ubos(st, prog, PIPE_SHADER_TESS_EVAL);
354 }
355
356 void
st_bind_cs_ubos(struct st_context * st)357 st_bind_cs_ubos(struct st_context *st)
358 {
359 struct gl_program *prog =
360 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
361
362 st_bind_ubos(st, prog, PIPE_SHADER_COMPUTE);
363 }
364