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