0a6b23aff3b09eae1ba66c3b40f3e4ed1afa80a0
[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 #include "main/shaderapi.h"
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 */
55 void st_upload_constants(struct st_context *st, struct gl_program *prog)
56 {
57 gl_shader_stage stage = prog->info.stage;
58 struct gl_program_parameter_list *params = prog->Parameters;
59 enum pipe_shader_type shader_type = pipe_shader_type_from_mesa(stage);
60
61 assert(shader_type == PIPE_SHADER_VERTEX ||
62 shader_type == PIPE_SHADER_FRAGMENT ||
63 shader_type == PIPE_SHADER_GEOMETRY ||
64 shader_type == PIPE_SHADER_TESS_CTRL ||
65 shader_type == PIPE_SHADER_TESS_EVAL ||
66 shader_type == PIPE_SHADER_COMPUTE);
67
68 /* update the ATI constants before rendering */
69 if (shader_type == PIPE_SHADER_FRAGMENT && st->fp->ati_fs) {
70 struct ati_fragment_shader *ati_fs = st->fp->ati_fs;
71 unsigned c;
72
73 for (c = 0; c < MAX_NUM_FRAGMENT_CONSTANTS_ATI; c++) {
74 if (ati_fs->LocalConstDef & (1 << c))
75 memcpy(params->ParameterValues[c],
76 ati_fs->Constants[c], sizeof(GLfloat) * 4);
77 else
78 memcpy(params->ParameterValues[c],
79 st->ctx->ATIFragmentShader.GlobalConstants[c], sizeof(GLfloat) * 4);
80 }
81 }
82
83 /* Make all bindless samplers/images bound texture/image units resident in
84 * the context.
85 */
86 st_make_bound_samplers_resident(st, prog);
87 st_make_bound_images_resident(st, prog);
88
89 /* update constants */
90 if (params && params->NumParameters) {
91 struct pipe_constant_buffer cb;
92 const uint paramBytes = params->NumParameters * sizeof(GLfloat) * 4;
93
94 /* Update the constants which come from fixed-function state, such as
95 * transformation matrices, fog factors, etc. The rest of the values in
96 * the parameters list are explicitly set by the user with glUniform,
97 * glProgramParameter(), etc.
98 */
99 if (params->StateFlags)
100 _mesa_load_state_parameters(st->ctx, params);
101
102 _mesa_shader_write_subroutine_indices(st->ctx, stage);
103
104 cb.buffer = NULL;
105 cb.user_buffer = params->ParameterValues;
106 cb.buffer_offset = 0;
107 cb.buffer_size = paramBytes;
108
109 if (ST_DEBUG & DEBUG_CONSTANTS) {
110 debug_printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n",
111 __func__, shader_type, params->NumParameters,
112 params->StateFlags);
113 _mesa_print_parameter_list(params);
114 }
115
116 cso_set_constant_buffer(st->cso_context, shader_type, 0, &cb);
117 pipe_resource_reference(&cb.buffer, NULL);
118
119 st->state.constants[shader_type].ptr = params->ParameterValues;
120 st->state.constants[shader_type].size = paramBytes;
121 }
122 else if (st->state.constants[shader_type].ptr) {
123 /* Unbind. */
124 st->state.constants[shader_type].ptr = NULL;
125 st->state.constants[shader_type].size = 0;
126 cso_set_constant_buffer(st->cso_context, shader_type, 0, NULL);
127 }
128 }
129
130
131 /**
132 * Vertex shader:
133 */
134 void st_update_vs_constants(struct st_context *st )
135 {
136 st_upload_constants(st, &st->vp->Base);
137 }
138
139 /**
140 * Fragment shader:
141 */
142 void st_update_fs_constants(struct st_context *st )
143 {
144 st_upload_constants(st, &st->fp->Base);
145 }
146
147
148 /* Geometry shader:
149 */
150 void st_update_gs_constants(struct st_context *st )
151 {
152 struct st_common_program *gp = st->gp;
153
154 if (gp)
155 st_upload_constants(st, &gp->Base);
156 }
157
158 /* Tessellation control shader:
159 */
160 void st_update_tcs_constants(struct st_context *st )
161 {
162 struct st_common_program *tcp = st->tcp;
163
164 if (tcp)
165 st_upload_constants(st, &tcp->Base);
166 }
167
168 /* Tessellation evaluation shader:
169 */
170 void st_update_tes_constants(struct st_context *st )
171 {
172 struct st_common_program *tep = st->tep;
173
174 if (tep)
175 st_upload_constants(st, &tep->Base);
176 }
177
178 /* Compute shader:
179 */
180 void st_update_cs_constants(struct st_context *st )
181 {
182 struct st_compute_program *cp = st->cp;
183
184 if (cp)
185 st_upload_constants(st, &cp->Base);
186 }
187
188 static void st_bind_ubos(struct st_context *st, struct gl_program *prog,
189 unsigned shader_type)
190 {
191 unsigned i;
192 struct pipe_constant_buffer cb = { 0 };
193
194 if (!prog)
195 return;
196
197 for (i = 0; i < prog->info.num_ubos; i++) {
198 struct gl_buffer_binding *binding;
199 struct st_buffer_object *st_obj;
200
201 binding =
202 &st->ctx->UniformBufferBindings[prog->sh.UniformBlocks[i]->Binding];
203 st_obj = st_buffer_object(binding->BufferObject);
204
205 cb.buffer = st_obj->buffer;
206
207 if (cb.buffer) {
208 cb.buffer_offset = binding->Offset;
209 cb.buffer_size = cb.buffer->width0 - binding->Offset;
210
211 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
212 * Take the minimum just to be sure.
213 */
214 if (!binding->AutomaticSize)
215 cb.buffer_size = MIN2(cb.buffer_size, (unsigned) binding->Size);
216 }
217 else {
218 cb.buffer_offset = 0;
219 cb.buffer_size = 0;
220 }
221
222 cso_set_constant_buffer(st->cso_context, shader_type, 1 + i, &cb);
223 }
224 }
225
226 void st_bind_vs_ubos(struct st_context *st)
227 {
228 struct gl_program *prog =
229 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
230
231 st_bind_ubos(st, prog, PIPE_SHADER_VERTEX);
232 }
233
234 void st_bind_fs_ubos(struct st_context *st)
235 {
236 struct gl_program *prog =
237 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
238
239 st_bind_ubos(st, prog, PIPE_SHADER_FRAGMENT);
240 }
241
242 void st_bind_gs_ubos(struct st_context *st)
243 {
244 struct gl_program *prog =
245 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
246
247 st_bind_ubos(st, prog, PIPE_SHADER_GEOMETRY);
248 }
249
250 void st_bind_tcs_ubos(struct st_context *st)
251 {
252 struct gl_program *prog =
253 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
254
255 st_bind_ubos(st, prog, PIPE_SHADER_TESS_CTRL);
256 }
257
258 void st_bind_tes_ubos(struct st_context *st)
259 {
260 struct gl_program *prog =
261 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
262
263 st_bind_ubos(st, prog, PIPE_SHADER_TESS_EVAL);
264 }
265
266 void st_bind_cs_ubos(struct st_context *st)
267 {
268 struct gl_program *prog =
269 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
270
271 st_bind_ubos(st, prog, PIPE_SHADER_COMPUTE);
272 }