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/* 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 /* fetch_state always stores 4 components (16 bytes) per matrix row, 126 * but matrix rows are sometimes allocated partially, so add 12 127 * to compensate for the fetch_state defect. 128 */ 129 u_upload_alloc(pipe->const_uploader, 0, paramBytes + 12, 64, 130 &cb.buffer_offset, &cb.buffer, (void**)&ptr); 131 132 int uniform_bytes = params->UniformBytes; 133 if (uniform_bytes) 134 memcpy(ptr, params->ParameterValues, uniform_bytes); 135 136 /* Upload the constants which come from fixed-function state, such as 137 * transformation matrices, fog factors, etc. 138 */ 139 if (params->StateFlags) 140 _mesa_upload_state_parameters(st->ctx, params, ptr); 141 142 u_upload_unmap(pipe->const_uploader); 143 pipe->set_constant_buffer(pipe, shader_type, 0, true, &cb); 144 145 /* Set inlinable constants. This is more involved because state 146 * parameters are uploaded directly above instead of being loaded 147 * into gl_program_parameter_list. The easiest way to get their values 148 * is to load them. 149 */ 150 unsigned num_inlinable_uniforms = prog->info.num_inlinable_uniforms; 151 if (num_inlinable_uniforms) { 152 uint32_t values[MAX_INLINABLE_UNIFORMS]; 153 gl_constant_value *constbuf = params->ParameterValues; 154 bool loaded_state_vars = false; 155 156 for (unsigned i = 0; i < num_inlinable_uniforms; i++) { 157 unsigned dw_offset = prog->info.inlinable_uniform_dw_offsets[i]; 158 159 if (dw_offset * 4 >= uniform_bytes && !loaded_state_vars) { 160 _mesa_load_state_parameters(st->ctx, params); 161 loaded_state_vars = true; 162 } 163 164 values[i] = constbuf[prog->info.inlinable_uniform_dw_offsets[i]].u; 165 } 166 167 pipe->set_inlinable_constants(pipe, shader_type, 168 prog->info.num_inlinable_uniforms, 169 values); 170 } 171 } else { 172 struct pipe_context *pipe = st->pipe; 173 174 cb.user_buffer = params->ParameterValues; 175 176 /* Update the constants which come from fixed-function state, such as 177 * transformation matrices, fog factors, etc. 178 */ 179 if (params->StateFlags) 180 _mesa_load_state_parameters(st->ctx, params); 181 182 pipe->set_constant_buffer(pipe, shader_type, 0, false, &cb); 183 184 /* Set inlinable constants. */ 185 unsigned num_inlinable_uniforms = prog->info.num_inlinable_uniforms; 186 if (num_inlinable_uniforms) { 187 uint32_t values[MAX_INLINABLE_UNIFORMS]; 188 gl_constant_value *constbuf = params->ParameterValues; 189 190 for (unsigned i = 0; i < num_inlinable_uniforms; i++) 191 values[i] = constbuf[prog->info.inlinable_uniform_dw_offsets[i]].u; 192 193 pipe->set_inlinable_constants(pipe, shader_type, 194 prog->info.num_inlinable_uniforms, 195 values); 196 } 197 } 198 199 st->state.constbuf0_enabled_shader_mask |= 1 << shader_type; 200 } else { 201 st_unbind_unused_cb0(st, shader_type); 202 } 203} 204 205 206/** 207 * Vertex shader: 208 */ 209void 210st_update_vs_constants(struct st_context *st) 211{ 212 st_upload_constants(st, &st->vp->Base, MESA_SHADER_VERTEX); 213} 214 215/** 216 * Fragment shader: 217 */ 218void 219st_update_fs_constants(struct st_context *st) 220{ 221 st_upload_constants(st, &st->fp->Base, MESA_SHADER_FRAGMENT); 222} 223 224 225/* Geometry shader: 226 */ 227void 228st_update_gs_constants(struct st_context *st) 229{ 230 st_upload_constants(st, st->gp ? &st->gp->Base : NULL, MESA_SHADER_GEOMETRY); 231} 232 233/* Tessellation control shader: 234 */ 235void 236st_update_tcs_constants(struct st_context *st) 237{ 238 st_upload_constants(st, st->tcp ? &st->tcp->Base : NULL, MESA_SHADER_TESS_CTRL); 239} 240 241/* Tessellation evaluation shader: 242 */ 243void 244st_update_tes_constants(struct st_context *st) 245{ 246 st_upload_constants(st, st->tep ? &st->tep->Base : NULL, MESA_SHADER_TESS_EVAL); 247} 248 249/* Compute shader: 250 */ 251void 252st_update_cs_constants(struct st_context *st) 253{ 254 st_upload_constants(st, st->cp ? &st->cp->Base : NULL, MESA_SHADER_COMPUTE); 255} 256 257static void 258st_bind_ubos(struct st_context *st, struct gl_program *prog, 259 enum pipe_shader_type shader_type) 260{ 261 unsigned i; 262 struct pipe_constant_buffer cb = { 0 }; 263 264 if (!prog) 265 return; 266 267 struct pipe_context *pipe = st->pipe; 268 269 for (i = 0; i < prog->sh.NumUniformBlocks; i++) { 270 struct gl_buffer_binding *binding; 271 272 binding = 273 &st->ctx->UniformBufferBindings[prog->sh.UniformBlocks[i]->Binding]; 274 275 cb.buffer = st_get_buffer_reference(st->ctx, binding->BufferObject); 276 277 if (cb.buffer) { 278 cb.buffer_offset = binding->Offset; 279 cb.buffer_size = cb.buffer->width0 - binding->Offset; 280 281 /* AutomaticSize is FALSE if the buffer was set with BindBufferRange. 282 * Take the minimum just to be sure. 283 */ 284 if (!binding->AutomaticSize) 285 cb.buffer_size = MIN2(cb.buffer_size, (unsigned) binding->Size); 286 } 287 else { 288 cb.buffer_offset = 0; 289 cb.buffer_size = 0; 290 } 291 292 pipe->set_constant_buffer(pipe, shader_type, 1 + i, true, &cb); 293 } 294} 295 296void 297st_bind_vs_ubos(struct st_context *st) 298{ 299 struct gl_program *prog = 300 st->ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX]; 301 302 st_bind_ubos(st, prog, PIPE_SHADER_VERTEX); 303} 304 305void 306st_bind_fs_ubos(struct st_context *st) 307{ 308 struct gl_program *prog = 309 st->ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT]; 310 311 st_bind_ubos(st, prog, PIPE_SHADER_FRAGMENT); 312} 313 314void 315st_bind_gs_ubos(struct st_context *st) 316{ 317 struct gl_program *prog = 318 st->ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY]; 319 320 st_bind_ubos(st, prog, PIPE_SHADER_GEOMETRY); 321} 322 323void 324st_bind_tcs_ubos(struct st_context *st) 325{ 326 struct gl_program *prog = 327 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL]; 328 329 st_bind_ubos(st, prog, PIPE_SHADER_TESS_CTRL); 330} 331 332void 333st_bind_tes_ubos(struct st_context *st) 334{ 335 struct gl_program *prog = 336 st->ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL]; 337 338 st_bind_ubos(st, prog, PIPE_SHADER_TESS_EVAL); 339} 340 341void 342st_bind_cs_ubos(struct st_context *st) 343{ 344 struct gl_program *prog = 345 st->ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE]; 346 347 st_bind_ubos(st, prog, PIPE_SHADER_COMPUTE); 348} 349