594db1e9f12566468d470e75a3194935b686a693
[mesa.git] / src / mesa / state_tracker / st_atom_constbuf.c
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 <keithw@vmware.com>
31 * Brian Paul
32 */
33
34 #include "main/imports.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37
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 "st_debug.h"
45 #include "st_context.h"
46 #include "st_atom.h"
47 #include "st_atom_constbuf.h"
48 #include "st_program.h"
49 #include "st_cb_bufferobjects.h"
50
51 /**
52 * Pass the given program parameters to the graphics pipe as a
53 * constant buffer.
54 * \param shader_type either PIPE_SHADER_VERTEX or PIPE_SHADER_FRAGMENT
55 */
56 void st_upload_constants( struct st_context *st,
57 struct gl_program_parameter_list *params,
58 unsigned shader_type)
59 {
60 assert(shader_type == PIPE_SHADER_VERTEX ||
61 shader_type == PIPE_SHADER_FRAGMENT ||
62 shader_type == PIPE_SHADER_GEOMETRY ||
63 shader_type == PIPE_SHADER_TESS_CTRL ||
64 shader_type == PIPE_SHADER_TESS_EVAL ||
65 shader_type == PIPE_SHADER_COMPUTE);
66
67 /* update the ATI constants before rendering */
68 if (shader_type == PIPE_SHADER_FRAGMENT && st->fp->ati_fs) {
69 struct ati_fragment_shader *ati_fs = st->fp->ati_fs;
70 unsigned c;
71
72 for (c = 0; c < MAX_NUM_FRAGMENT_CONSTANTS_ATI; c++) {
73 if (ati_fs->LocalConstDef & (1 << c))
74 memcpy(params->ParameterValues[c],
75 ati_fs->Constants[c], sizeof(GLfloat) * 4);
76 else
77 memcpy(params->ParameterValues[c],
78 st->ctx->ATIFragmentShader.GlobalConstants[c], sizeof(GLfloat) * 4);
79 }
80 }
81
82 /* update constants */
83 if (params && params->NumParameters) {
84 struct pipe_constant_buffer cb;
85 const uint paramBytes = params->NumParameters * sizeof(GLfloat) * 4;
86
87 /* Update the constants which come from fixed-function state, such as
88 * transformation matrices, fog factors, etc. The rest of the values in
89 * the parameters list are explicitly set by the user with glUniform,
90 * glProgramParameter(), etc.
91 */
92 if (params->StateFlags)
93 _mesa_load_state_parameters(st->ctx, params);
94
95 /* We always need to get a new buffer, to keep the drivers simple and
96 * avoid gratuitous rendering synchronization.
97 * Let's use a user buffer to avoid an unnecessary copy.
98 */
99 if (st->constbuf_uploader) {
100 cb.buffer = NULL;
101 cb.user_buffer = NULL;
102 u_upload_data(st->constbuf_uploader, 0, paramBytes,
103 st->ctx->Const.UniformBufferOffsetAlignment,
104 params->ParameterValues, &cb.buffer_offset, &cb.buffer);
105 u_upload_unmap(st->constbuf_uploader);
106 } else {
107 cb.buffer = NULL;
108 cb.user_buffer = params->ParameterValues;
109 cb.buffer_offset = 0;
110 }
111 cb.buffer_size = paramBytes;
112
113 if (ST_DEBUG & DEBUG_CONSTANTS) {
114 debug_printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n",
115 __func__, shader_type, params->NumParameters,
116 params->StateFlags);
117 _mesa_print_parameter_list(params);
118 }
119
120 cso_set_constant_buffer(st->cso_context, shader_type, 0, &cb);
121 pipe_resource_reference(&cb.buffer, NULL);
122
123 st->state.constants[shader_type].ptr = params->ParameterValues;
124 st->state.constants[shader_type].size = paramBytes;
125 }
126 else if (st->state.constants[shader_type].ptr) {
127 /* Unbind. */
128 st->state.constants[shader_type].ptr = NULL;
129 st->state.constants[shader_type].size = 0;
130 cso_set_constant_buffer(st->cso_context, shader_type, 0, NULL);
131 }
132 }
133
134
135 /**
136 * Vertex shader:
137 */
138 static void update_vs_constants(struct st_context *st )
139 {
140 struct st_vertex_program *vp = st->vp;
141 struct gl_program_parameter_list *params = vp->Base.Base.Parameters;
142
143 st_upload_constants( st, params, PIPE_SHADER_VERTEX );
144 }
145
146
147 const struct st_tracked_state st_update_vs_constants = {
148 "st_update_vs_constants", /* name */
149 { /* dirty */
150 _NEW_PROGRAM_CONSTANTS, /* mesa */
151 ST_NEW_VERTEX_PROGRAM, /* st */
152 },
153 update_vs_constants /* update */
154 };
155
156
157
158 /**
159 * Fragment shader:
160 */
161 static void update_fs_constants(struct st_context *st )
162 {
163 struct st_fragment_program *fp = st->fp;
164 struct gl_program_parameter_list *params = fp->Base.Base.Parameters;
165
166 st_upload_constants( st, params, PIPE_SHADER_FRAGMENT );
167 }
168
169
170 const struct st_tracked_state st_update_fs_constants = {
171 "st_update_fs_constants", /* name */
172 { /* dirty */
173 _NEW_PROGRAM_CONSTANTS, /* mesa */
174 ST_NEW_FRAGMENT_PROGRAM, /* st */
175 },
176 update_fs_constants /* update */
177 };
178
179 /* Geometry shader:
180 */
181 static void update_gs_constants(struct st_context *st )
182 {
183 struct st_geometry_program *gp = st->gp;
184 struct gl_program_parameter_list *params;
185
186 if (gp) {
187 params = gp->Base.Base.Parameters;
188 st_upload_constants( st, params, PIPE_SHADER_GEOMETRY );
189 }
190 }
191
192 const struct st_tracked_state st_update_gs_constants = {
193 "st_update_gs_constants", /* name */
194 { /* dirty */
195 _NEW_PROGRAM_CONSTANTS, /* mesa */
196 ST_NEW_GEOMETRY_PROGRAM, /* st */
197 },
198 update_gs_constants /* update */
199 };
200
201 /* Tessellation control shader:
202 */
203 static void update_tcs_constants(struct st_context *st )
204 {
205 struct st_tessctrl_program *tcp = st->tcp;
206 struct gl_program_parameter_list *params;
207
208 if (tcp) {
209 params = tcp->Base.Base.Parameters;
210 st_upload_constants( st, params, PIPE_SHADER_TESS_CTRL );
211 }
212 }
213
214 const struct st_tracked_state st_update_tcs_constants = {
215 "st_update_tcs_constants", /* name */
216 { /* dirty */
217 _NEW_PROGRAM_CONSTANTS, /* mesa */
218 ST_NEW_TESSCTRL_PROGRAM, /* st */
219 },
220 update_tcs_constants /* update */
221 };
222
223 /* Tessellation evaluation shader:
224 */
225 static void update_tes_constants(struct st_context *st )
226 {
227 struct st_tesseval_program *tep = st->tep;
228 struct gl_program_parameter_list *params;
229
230 if (tep) {
231 params = tep->Base.Base.Parameters;
232 st_upload_constants( st, params, PIPE_SHADER_TESS_EVAL );
233 }
234 }
235
236 const struct st_tracked_state st_update_tes_constants = {
237 "st_update_tes_constants", /* name */
238 { /* dirty */
239 _NEW_PROGRAM_CONSTANTS, /* mesa */
240 ST_NEW_TESSEVAL_PROGRAM, /* st */
241 },
242 update_tes_constants /* update */
243 };
244
245 /* Compute shader:
246 */
247 static void update_cs_constants(struct st_context *st )
248 {
249 struct st_compute_program *cp = st->cp;
250 struct gl_program_parameter_list *params;
251
252 if (cp) {
253 params = cp->Base.Base.Parameters;
254 st_upload_constants( st, params, PIPE_SHADER_COMPUTE );
255 }
256 }
257
258 const struct st_tracked_state st_update_cs_constants = {
259 "st_update_cs_constants", /* name */
260 { /* dirty */
261 _NEW_PROGRAM_CONSTANTS, /* mesa */
262 ST_NEW_COMPUTE_PROGRAM, /* st */
263 },
264 update_cs_constants /* update */
265 };
266
267 static void st_bind_ubos(struct st_context *st,
268 struct gl_linked_shader *shader,
269 unsigned shader_type)
270 {
271 unsigned i;
272 struct pipe_constant_buffer cb = { 0 };
273
274 if (!shader)
275 return;
276
277 for (i = 0; i < shader->NumUniformBlocks; i++) {
278 struct gl_uniform_buffer_binding *binding;
279 struct st_buffer_object *st_obj;
280
281 binding = &st->ctx->UniformBufferBindings[shader->UniformBlocks[i]->Binding];
282 st_obj = st_buffer_object(binding->BufferObject);
283
284 cb.buffer = st_obj->buffer;
285
286 if (cb.buffer) {
287 cb.buffer_offset = binding->Offset;
288 cb.buffer_size = cb.buffer->width0 - binding->Offset;
289
290 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
291 * Take the minimum just to be sure.
292 */
293 if (!binding->AutomaticSize)
294 cb.buffer_size = MIN2(cb.buffer_size, (unsigned) binding->Size);
295 }
296 else {
297 cb.buffer_offset = 0;
298 cb.buffer_size = 0;
299 }
300
301 cso_set_constant_buffer(st->cso_context, shader_type, 1 + i, &cb);
302 }
303 }
304
305 static void bind_vs_ubos(struct st_context *st)
306 {
307 struct gl_shader_program *prog =
308 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
309
310 if (!prog)
311 return;
312
313 st_bind_ubos(st, prog->_LinkedShaders[MESA_SHADER_VERTEX], PIPE_SHADER_VERTEX);
314 }
315
316 const struct st_tracked_state st_bind_vs_ubos = {
317 "st_bind_vs_ubos",
318 {
319 0,
320 ST_NEW_VERTEX_PROGRAM | ST_NEW_UNIFORM_BUFFER,
321 },
322 bind_vs_ubos
323 };
324
325 static void bind_fs_ubos(struct st_context *st)
326 {
327 struct gl_shader_program *prog =
328 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
329
330 if (!prog)
331 return;
332
333 st_bind_ubos(st, prog->_LinkedShaders[MESA_SHADER_FRAGMENT], PIPE_SHADER_FRAGMENT);
334 }
335
336 const struct st_tracked_state st_bind_fs_ubos = {
337 "st_bind_fs_ubos",
338 {
339 0,
340 ST_NEW_FRAGMENT_PROGRAM | ST_NEW_UNIFORM_BUFFER,
341 },
342 bind_fs_ubos
343 };
344
345 static void bind_gs_ubos(struct st_context *st)
346 {
347 struct gl_shader_program *prog =
348 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
349
350 if (!prog)
351 return;
352
353 st_bind_ubos(st, prog->_LinkedShaders[MESA_SHADER_GEOMETRY], PIPE_SHADER_GEOMETRY);
354 }
355
356 const struct st_tracked_state st_bind_gs_ubos = {
357 "st_bind_gs_ubos",
358 {
359 0,
360 ST_NEW_GEOMETRY_PROGRAM | ST_NEW_UNIFORM_BUFFER,
361 },
362 bind_gs_ubos
363 };
364
365 static void bind_tcs_ubos(struct st_context *st)
366 {
367 struct gl_shader_program *prog =
368 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
369
370 if (!prog)
371 return;
372
373 st_bind_ubos(st, prog->_LinkedShaders[MESA_SHADER_TESS_CTRL], PIPE_SHADER_TESS_CTRL);
374 }
375
376 const struct st_tracked_state st_bind_tcs_ubos = {
377 "st_bind_tcs_ubos",
378 {
379 0,
380 ST_NEW_TESSCTRL_PROGRAM | ST_NEW_UNIFORM_BUFFER,
381 },
382 bind_tcs_ubos
383 };
384
385 static void bind_tes_ubos(struct st_context *st)
386 {
387 struct gl_shader_program *prog =
388 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
389
390 if (!prog)
391 return;
392
393 st_bind_ubos(st, prog->_LinkedShaders[MESA_SHADER_TESS_EVAL], PIPE_SHADER_TESS_EVAL);
394 }
395
396 const struct st_tracked_state st_bind_tes_ubos = {
397 "st_bind_tes_ubos",
398 {
399 0,
400 ST_NEW_TESSEVAL_PROGRAM | ST_NEW_UNIFORM_BUFFER,
401 },
402 bind_tes_ubos
403 };
404
405 static void bind_cs_ubos(struct st_context *st)
406 {
407 struct gl_shader_program *prog =
408 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
409
410 if (!prog)
411 return;
412
413 st_bind_ubos(st, prog->_LinkedShaders[MESA_SHADER_COMPUTE],
414 PIPE_SHADER_COMPUTE);
415 }
416
417 const struct st_tracked_state st_bind_cs_ubos = {
418 "st_bind_cs_ubos",
419 {
420 0,
421 ST_NEW_COMPUTE_PROGRAM | ST_NEW_UNIFORM_BUFFER,
422 },
423 bind_cs_ubos
424 };