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