1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
34 #include "main/imports.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.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"
45 #include "st_context.h"
47 #include "st_atom_constbuf.h"
48 #include "st_program.h"
49 #include "st_cb_bufferobjects.h"
52 * Pass the given program parameters to the graphics pipe as a
54 * \param shader_type either PIPE_SHADER_VERTEX or PIPE_SHADER_FRAGMENT
56 void st_upload_constants( struct st_context
*st
,
57 struct gl_program_parameter_list
*params
,
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 shader_type
== PIPE_SHADER_COMPUTE
);
67 /* update the ATI constants before rendering */
68 if (shader_type
== PIPE_SHADER_FRAGMENT
&& st
->fp
->ati_fs
) {
69 struct ati_fragment_shader
*ati_fs
= st
->fp
->ati_fs
;
72 for (c
= 0; c
< MAX_NUM_FRAGMENT_CONSTANTS_ATI
; c
++) {
73 if (ati_fs
->LocalConstDef
& (1 << c
))
74 memcpy(params
->ParameterValues
[c
],
75 ati_fs
->Constants
[c
], sizeof(GLfloat
) * 4);
77 memcpy(params
->ParameterValues
[c
],
78 st
->ctx
->ATIFragmentShader
.GlobalConstants
[c
], sizeof(GLfloat
) * 4);
82 /* update constants */
83 if (params
&& params
->NumParameters
) {
84 struct pipe_constant_buffer cb
;
85 const uint paramBytes
= params
->NumParameters
* sizeof(GLfloat
) * 4;
87 /* Update the constants which come from fixed-function state, such as
88 * transformation matrices, fog factors, etc. The rest of the values in
89 * the parameters list are explicitly set by the user with glUniform,
90 * glProgramParameter(), etc.
92 if (params
->StateFlags
)
93 _mesa_load_state_parameters(st
->ctx
, params
);
95 /* We always need to get a new buffer, to keep the drivers simple and
96 * avoid gratuitous rendering synchronization.
97 * Let's use a user buffer to avoid an unnecessary copy.
99 if (st
->constbuf_uploader
) {
101 cb
.user_buffer
= NULL
;
102 u_upload_data(st
->constbuf_uploader
, 0, paramBytes
,
103 st
->ctx
->Const
.UniformBufferOffsetAlignment
,
104 params
->ParameterValues
, &cb
.buffer_offset
, &cb
.buffer
);
105 u_upload_unmap(st
->constbuf_uploader
);
108 cb
.user_buffer
= params
->ParameterValues
;
109 cb
.buffer_offset
= 0;
111 cb
.buffer_size
= paramBytes
;
113 if (ST_DEBUG
& DEBUG_CONSTANTS
) {
114 debug_printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n",
115 __func__
, shader_type
, params
->NumParameters
,
117 _mesa_print_parameter_list(params
);
120 cso_set_constant_buffer(st
->cso_context
, shader_type
, 0, &cb
);
121 pipe_resource_reference(&cb
.buffer
, NULL
);
123 st
->state
.constants
[shader_type
].ptr
= params
->ParameterValues
;
124 st
->state
.constants
[shader_type
].size
= paramBytes
;
126 else if (st
->state
.constants
[shader_type
].ptr
) {
128 st
->state
.constants
[shader_type
].ptr
= NULL
;
129 st
->state
.constants
[shader_type
].size
= 0;
130 cso_set_constant_buffer(st
->cso_context
, shader_type
, 0, NULL
);
138 static void update_vs_constants(struct st_context
*st
)
140 struct st_vertex_program
*vp
= st
->vp
;
141 struct gl_program_parameter_list
*params
= vp
->Base
.Base
.Parameters
;
143 st_upload_constants( st
, params
, PIPE_SHADER_VERTEX
);
147 const struct st_tracked_state st_update_vs_constants
= {
149 _NEW_PROGRAM_CONSTANTS
, /* mesa */
150 ST_NEW_VERTEX_PROGRAM
, /* st */
152 update_vs_constants
/* update */
160 static void update_fs_constants(struct st_context
*st
)
162 struct st_fragment_program
*fp
= st
->fp
;
163 struct gl_program_parameter_list
*params
= fp
->Base
.Base
.Parameters
;
165 st_upload_constants( st
, params
, PIPE_SHADER_FRAGMENT
);
169 const struct st_tracked_state st_update_fs_constants
= {
171 _NEW_PROGRAM_CONSTANTS
, /* mesa */
172 ST_NEW_FRAGMENT_PROGRAM
, /* st */
174 update_fs_constants
/* update */
179 static void update_gs_constants(struct st_context
*st
)
181 struct st_geometry_program
*gp
= st
->gp
;
182 struct gl_program_parameter_list
*params
;
185 params
= gp
->Base
.Base
.Parameters
;
186 st_upload_constants( st
, params
, PIPE_SHADER_GEOMETRY
);
190 const struct st_tracked_state st_update_gs_constants
= {
192 _NEW_PROGRAM_CONSTANTS
, /* mesa */
193 ST_NEW_GEOMETRY_PROGRAM
, /* st */
195 update_gs_constants
/* update */
198 /* Tessellation control shader:
200 static void update_tcs_constants(struct st_context
*st
)
202 struct st_tessctrl_program
*tcp
= st
->tcp
;
203 struct gl_program_parameter_list
*params
;
206 params
= tcp
->Base
.Base
.Parameters
;
207 st_upload_constants( st
, params
, PIPE_SHADER_TESS_CTRL
);
211 const struct st_tracked_state st_update_tcs_constants
= {
213 _NEW_PROGRAM_CONSTANTS
, /* mesa */
214 ST_NEW_TESSCTRL_PROGRAM
, /* st */
216 update_tcs_constants
/* update */
219 /* Tessellation evaluation shader:
221 static void update_tes_constants(struct st_context
*st
)
223 struct st_tesseval_program
*tep
= st
->tep
;
224 struct gl_program_parameter_list
*params
;
227 params
= tep
->Base
.Base
.Parameters
;
228 st_upload_constants( st
, params
, PIPE_SHADER_TESS_EVAL
);
232 const struct st_tracked_state st_update_tes_constants
= {
234 _NEW_PROGRAM_CONSTANTS
, /* mesa */
235 ST_NEW_TESSEVAL_PROGRAM
, /* st */
237 update_tes_constants
/* update */
242 static void update_cs_constants(struct st_context
*st
)
244 struct st_compute_program
*cp
= st
->cp
;
245 struct gl_program_parameter_list
*params
;
248 params
= cp
->Base
.Base
.Parameters
;
249 st_upload_constants( st
, params
, PIPE_SHADER_COMPUTE
);
253 const struct st_tracked_state st_update_cs_constants
= {
255 _NEW_PROGRAM_CONSTANTS
, /* mesa */
256 ST_NEW_COMPUTE_PROGRAM
, /* st */
258 update_cs_constants
/* update */
261 static void st_bind_ubos(struct st_context
*st
,
262 struct gl_linked_shader
*shader
,
263 unsigned shader_type
)
266 struct pipe_constant_buffer cb
= { 0 };
271 for (i
= 0; i
< shader
->NumUniformBlocks
; i
++) {
272 struct gl_uniform_buffer_binding
*binding
;
273 struct st_buffer_object
*st_obj
;
275 binding
= &st
->ctx
->UniformBufferBindings
[shader
->UniformBlocks
[i
]->Binding
];
276 st_obj
= st_buffer_object(binding
->BufferObject
);
278 cb
.buffer
= st_obj
->buffer
;
281 cb
.buffer_offset
= binding
->Offset
;
282 cb
.buffer_size
= cb
.buffer
->width0
- binding
->Offset
;
284 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange.
285 * Take the minimum just to be sure.
287 if (!binding
->AutomaticSize
)
288 cb
.buffer_size
= MIN2(cb
.buffer_size
, (unsigned) binding
->Size
);
291 cb
.buffer_offset
= 0;
295 cso_set_constant_buffer(st
->cso_context
, shader_type
, 1 + i
, &cb
);
299 static void bind_vs_ubos(struct st_context
*st
)
301 struct gl_shader_program
*prog
=
302 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
307 st_bind_ubos(st
, prog
->_LinkedShaders
[MESA_SHADER_VERTEX
], PIPE_SHADER_VERTEX
);
310 const struct st_tracked_state st_bind_vs_ubos
= {
313 ST_NEW_VERTEX_PROGRAM
| ST_NEW_UNIFORM_BUFFER
,
318 static void bind_fs_ubos(struct st_context
*st
)
320 struct gl_shader_program
*prog
=
321 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
];
326 st_bind_ubos(st
, prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
], PIPE_SHADER_FRAGMENT
);
329 const struct st_tracked_state st_bind_fs_ubos
= {
332 ST_NEW_FRAGMENT_PROGRAM
| ST_NEW_UNIFORM_BUFFER
,
337 static void bind_gs_ubos(struct st_context
*st
)
339 struct gl_shader_program
*prog
=
340 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
345 st_bind_ubos(st
, prog
->_LinkedShaders
[MESA_SHADER_GEOMETRY
], PIPE_SHADER_GEOMETRY
);
348 const struct st_tracked_state st_bind_gs_ubos
= {
351 ST_NEW_GEOMETRY_PROGRAM
| ST_NEW_UNIFORM_BUFFER
,
356 static void bind_tcs_ubos(struct st_context
*st
)
358 struct gl_shader_program
*prog
=
359 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
];
364 st_bind_ubos(st
, prog
->_LinkedShaders
[MESA_SHADER_TESS_CTRL
], PIPE_SHADER_TESS_CTRL
);
367 const struct st_tracked_state st_bind_tcs_ubos
= {
370 ST_NEW_TESSCTRL_PROGRAM
| ST_NEW_UNIFORM_BUFFER
,
375 static void bind_tes_ubos(struct st_context
*st
)
377 struct gl_shader_program
*prog
=
378 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
383 st_bind_ubos(st
, prog
->_LinkedShaders
[MESA_SHADER_TESS_EVAL
], PIPE_SHADER_TESS_EVAL
);
386 const struct st_tracked_state st_bind_tes_ubos
= {
389 ST_NEW_TESSEVAL_PROGRAM
| ST_NEW_UNIFORM_BUFFER
,
394 static void bind_cs_ubos(struct st_context
*st
)
396 struct gl_shader_program
*prog
=
397 st
->ctx
->_Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
402 st_bind_ubos(st
, prog
->_LinkedShaders
[MESA_SHADER_COMPUTE
],
403 PIPE_SHADER_COMPUTE
);
406 const struct st_tracked_state st_bind_cs_ubos
= {
409 ST_NEW_COMPUTE_PROGRAM
| ST_NEW_UNIFORM_BUFFER
,