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