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