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 "main/bufferobj.h" 45#include "st_debug.h" 46#include "st_context.h" 47#include "st_atom.h" 48#include "st_atom_constbuf.h" 49#include "st_program.h" 50 51/* Unbinds the CB0 if it's not used by the current program to avoid leaving 52 * dangling pointers to old (potentially deleted) shaders in the driver. 53 */ 54static void 55st_unbind_unused_cb0(struct st_context *st, enum pipe_shader_type shader_type) 56{ 57 if (st->state.constbuf0_enabled_shader_mask & (1 << shader_type)) { 58 struct pipe_context *pipe = st->pipe; 59 60 pipe->set_constant_buffer(pipe, shader_type, 0, false, NULL); 61 st->state.constbuf0_enabled_shader_mask &= ~(1 << shader_type); 62 } 63} 64 65/** 66 * Pass the given program parameters to the graphics pipe as a 67 * constant buffer. 68 */ 69void 70st_upload_constants(struct st_context *st, struct gl_program *prog, gl_shader_stage stage) 71{ 72 enum pipe_shader_type shader_type = pipe_shader_type_from_mesa(stage); 73 if (!prog) { 74 st_unbind_unused_cb0(st, shader_type); 75 return; 76 } 77 78 struct gl_program_parameter_list *params = prog->Parameters; 79 80 assert(shader_type == PIPE_SHADER_VERTEX || 81 shader_type == PIPE_SHADER_FRAGMENT || 82 shader_type == PIPE_SHADER_GEOMETRY || 83 shader_type == PIPE_SHADER_TESS_CTRL || 84 shader_type == PIPE_SHADER_TESS_EVAL || 85 shader_type == PIPE_SHADER_COMPUTE); 86 87 /* update the ATI constants before rendering */ 88 if (shader_type == PIPE_SHADER_FRAGMENT && st->fp->ati_fs) { 89 struct ati_fragment_shader *ati_fs = st->fp->ati_fs; 90 unsigned c; 91 92 for (c = 0; c < MAX_NUM_FRAGMENT_CONSTANTS_ATI; c++) { 93 unsigned offset = params->Parameters[c].ValueOffset; 94 if (ati_fs->LocalConstDef & (1 << c)) 95 memcpy(params->ParameterValues + offset, 96 ati_fs->Constants[c], sizeof(GLfloat) * 4); 97 else 98 memcpy(params->ParameterValues + offset, 99 st->ctx->ATIFragmentShader.GlobalConstants[c], 100 sizeof(GLfloat) * 4); 101 } 102 } 103 104 /* Make all bindless samplers/images bound texture/image units resident in 105 * the context. 106 */ 107 st_make_bound_samplers_resident(st, prog); 108 st_make_bound_images_resident(st, prog); 109 110 /* update constants */ 111 if (params && params->NumParameters) { 112 struct pipe_constant_buffer cb; 113 const uint paramBytes = params->NumParameterValues * sizeof(GLfloat); 114 115 _mesa_shader_write_subroutine_indices(st->ctx, stage); 116 117 cb.buffer = NULL; 118 cb.user_buffer = NULL; 119 cb.buffer_offset = 0; 120 cb.buffer_size = paramBytes; 121 122 if (st->prefer_real_buffer_in_constbuf0) { 123 struct pipe_context *pipe = st->pipe; 124 uint32_t *ptr; 125 126 const unsigned alignment = MAX2( 127 st->ctx->Const.UniformBufferOffsetAlignment, 64); 128 129 /* fetch_state always stores 4 components (16 bytes) per matrix row, 130 * but matrix rows are sometimes allocated partially, so add 12 131 * to compensate for the fetch_state defect. 132 */ 133 u_upload_alloc(pipe->const_uploader, 0, paramBytes + 12, 134 alignment, &cb.buffer_offset, &cb.buffer, (void**)&ptr); 135 136 int uniform_bytes = params->UniformBytes; 137 if (uniform_bytes) 138 memcpy(ptr, params->ParameterValues, uniform_bytes); 139 140 /* Upload the constants which come from fixed-function state, such as 141 * transformation matrices, fog factors, etc. 142 */ 143 if (params->StateFlags) 144 _mesa_upload_state_parameters(st->ctx, params, ptr); 145 146 u_upload_unmap(pipe->const_uploader); 147 pipe->set_constant_buffer(pipe, shader_type, 0, true, &cb); 148 149 /* Set inlinable constants. This is more involved because state 150 * parameters are uploaded directly above instead of being loaded 151 * into gl_program_parameter_list. The easiest way to get their values 152 * is to load them. 153 */ 154 unsigned num_inlinable_uniforms = prog->info.num_inlinable_uniforms; 155 if (num_inlinable_uniforms) { 156 uint32_t values[MAX_INLINABLE_UNIFORMS]; 157 gl_constant_value *constbuf = params->ParameterValues; 158 bool loaded_state_vars = false; 159 160 for (unsigned i = 0; i < num_inlinable_uniforms; i++) { 161 unsigned dw_offset = prog->info.inlinable_uniform_dw_offsets[i]; 162 163 if (dw_offset * 4 >= uniform_bytes && !loaded_state_vars) { 164 _mesa_load_state_parameters(st->ctx, params); 165 loaded_state_vars = true; 166 } 167 168 values[i] = constbuf[prog->info.inlinable_uniform_dw_offsets[i]].u; 169 } 170 171 pipe->set_inlinable_constants(pipe, shader_type, 172 prog->info.num_inlinable_uniforms, 173 values); 174 } 175 } else { 176 struct pipe_context *pipe = st->pipe; 177 178 cb.user_buffer = params->ParameterValues; 179 180 /* Update the constants which come from fixed-function state, such as 181 * transformation matrices, fog factors, etc. 182 */ 183 if (params->StateFlags) 184 _mesa_load_state_parameters(st->ctx, params); 185 186 pipe->set_constant_buffer(pipe, shader_type, 0, false, &cb); 187 188 /* Set inlinable constants. */ 189 unsigned num_inlinable_uniforms = prog->info.num_inlinable_uniforms; 190 if (num_inlinable_uniforms) { 191 uint32_t values[MAX_INLINABLE_UNIFORMS]; 192 gl_constant_value *constbuf = params->ParameterValues; 193 194 for (unsigned i = 0; i < num_inlinable_uniforms; i++) 195 values[i] = constbuf[prog->info.inlinable_uniform_dw_offsets[i]].u; 196 197 pipe->set_inlinable_constants(pipe, shader_type, 198 prog->info.num_inlinable_uniforms, 199 values); 200 } 201 } 202 203 st->state.constbuf0_enabled_shader_mask |= 1 << shader_type; 204 } else { 205 st_unbind_unused_cb0(st, shader_type); 206 } 207} 208 209 210/** 211 * Vertex shader: 212 */ 213void 214st_update_vs_constants(struct st_context *st) 215{ 216 st_upload_constants(st, st->vp, MESA_SHADER_VERTEX); 217} 218 219/** 220 * Fragment shader: 221 */ 222void 223st_update_fs_constants(struct st_context *st) 224{ 225 st_upload_constants(st, st->fp, MESA_SHADER_FRAGMENT); 226} 227 228 229/* Geometry shader: 230 */ 231void 232st_update_gs_constants(struct st_context *st) 233{ 234 st_upload_constants(st, st->gp, MESA_SHADER_GEOMETRY); 235} 236 237/* Tessellation control shader: 238 */ 239void 240st_update_tcs_constants(struct st_context *st) 241{ 242 st_upload_constants(st, st->tcp, MESA_SHADER_TESS_CTRL); 243} 244 245/* Tessellation evaluation shader: 246 */ 247void 248st_update_tes_constants(struct st_context *st) 249{ 250 st_upload_constants(st, st->tep, MESA_SHADER_TESS_EVAL); 251} 252 253/* Compute shader: 254 */ 255void 256st_update_cs_constants(struct st_context *st) 257{ 258 st_upload_constants(st, st->cp, MESA_SHADER_COMPUTE); 259} 260 261static void 262st_bind_ubos(struct st_context *st, struct gl_program *prog, 263 enum pipe_shader_type shader_type) 264{ 265 unsigned i; 266 struct pipe_constant_buffer cb = { 0 }; 267 268 if (!prog) 269 return; 270 271 struct pipe_context *pipe = st->pipe; 272 273 for (i = 0; i < prog->sh.NumUniformBlocks; i++) { 274 struct gl_buffer_binding *binding; 275 276 binding = 277 &st->ctx->UniformBufferBindings[prog->sh.UniformBlocks[i]->Binding]; 278 279 cb.buffer = _mesa_get_bufferobj_reference(st->ctx, binding->BufferObject); 280 281 if (cb.buffer) { 282 cb.buffer_offset = binding->Offset; 283 cb.buffer_size = cb.buffer->width0 - binding->Offset; 284 285 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange. 286 * Take the minimum just to be sure. 287 */ 288 if (!binding->AutomaticSize) 289 cb.buffer_size = MIN2(cb.buffer_size, (unsigned) binding->Size); 290 } 291 else { 292 cb.buffer_offset = 0; 293 cb.buffer_size = 0; 294 } 295 296 pipe->set_constant_buffer(pipe, shader_type, 1 + i, true, &cb); 297 } 298} 299 300void 301st_bind_vs_ubos(struct st_context *st) 302{ 303 struct gl_program *prog = 304 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]; 305 306 st_bind_ubos(st, prog, PIPE_SHADER_VERTEX); 307} 308 309void 310st_bind_fs_ubos(struct st_context *st) 311{ 312 struct gl_program *prog = 313 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT]; 314 315 st_bind_ubos(st, prog, PIPE_SHADER_FRAGMENT); 316} 317 318void 319st_bind_gs_ubos(struct st_context *st) 320{ 321 struct gl_program *prog = 322 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]; 323 324 st_bind_ubos(st, prog, PIPE_SHADER_GEOMETRY); 325} 326 327void 328st_bind_tcs_ubos(struct st_context *st) 329{ 330 struct gl_program *prog = 331 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL]; 332 333 st_bind_ubos(st, prog, PIPE_SHADER_TESS_CTRL); 334} 335 336void 337st_bind_tes_ubos(struct st_context *st) 338{ 339 struct gl_program *prog = 340 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL]; 341 342 st_bind_ubos(st, prog, PIPE_SHADER_TESS_EVAL); 343} 344 345void 346st_bind_cs_ubos(struct st_context *st) 347{ 348 struct gl_program *prog = 349 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE]; 350 351 st_bind_ubos(st, prog, PIPE_SHADER_COMPUTE); 352} 353