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