st_atom_constbuf.c revision 693fac8ae2e5812265222b1335695bd33b90bd8a
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 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 TUNGSTEN GRAPHICS 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 * Authors: 29 * Keith Whitwell <keith@tungstengraphics.com> 30 * Brian Paul 31 */ 32 33#include "main/imports.h" 34#include "shader/prog_parameter.h" 35#include "shader/prog_print.h" 36 37#include "pipe/p_context.h" 38#include "pipe/p_defines.h" 39#include "pipe/p_inlines.h" 40 41#include "st_context.h" 42#include "st_atom.h" 43#include "st_atom_constbuf.h" 44#include "st_program.h" 45 46 47/** 48 * Pass the given program parameters to the graphics pipe as a 49 * constant buffer. 50 * \param id either PIPE_SHADER_VERTEX or PIPE_SHADER_FRAGMENT 51 */ 52void st_upload_constants( struct st_context *st, 53 struct gl_program_parameter_list *params, 54 unsigned id) 55{ 56 struct pipe_context *pipe = st->pipe; 57 struct pipe_constant_buffer *cbuf = &st->state.constants[id]; 58 59 assert(id == PIPE_SHADER_VERTEX || id == PIPE_SHADER_FRAGMENT); 60 61 /* update constants */ 62 if (params && params->NumParameters) { 63 const uint paramBytes = params->NumParameters * sizeof(GLfloat) * 4; 64 65 /* Update our own dependency flags. This works because this 66 * function will also be called whenever the program changes. 67 */ 68 st->constants.tracked_state[id].dirty.mesa = 69 (params->StateFlags | _NEW_PROGRAM); 70 71 _mesa_load_state_parameters(st->ctx, params); 72 73 /* We always need to get a new buffer, to keep the drivers simple and 74 * avoid gratuitous rendering synchronization. 75 */ 76 pipe_buffer_reference(pipe->screen, &cbuf->buffer, NULL ); 77 cbuf->buffer = pipe_buffer_create(pipe->screen, 16, PIPE_BUFFER_USAGE_CONSTANT, 78 paramBytes ); 79 80 if (0) 81 { 82 printf("%s(shader=%d, numParams=%d, stateFlags=0x%x)\n", 83 __FUNCTION__, id, params->NumParameters, params->StateFlags); 84 _mesa_print_parameter_list(params); 85 } 86 87 /* load Mesa constants into the constant buffer */ 88 if (cbuf->buffer) 89 pipe_buffer_write(pipe->screen, cbuf->buffer, 90 0, paramBytes, 91 params->ParameterValues); 92 93 st->pipe->set_constant_buffer(st->pipe, id, 0, cbuf); 94 } 95 else { 96 st->constants.tracked_state[id].dirty.mesa = 0; 97 // st->pipe->set_constant_buffer(st->pipe, id, 0, NULL); 98 } 99} 100 101/* Vertex shader: 102 */ 103static void update_vs_constants(struct st_context *st ) 104{ 105 struct st_vertex_program *vp = st->vp; 106 struct gl_program_parameter_list *params = vp->Base.Base.Parameters; 107 108 st_upload_constants( st, params, PIPE_SHADER_VERTEX ); 109} 110 111const struct st_tracked_state st_update_vs_constants = { 112 "st_update_vs_constants", /* name */ 113 { /* dirty */ 114 0, /* set dynamically above */ /* mesa */ 115 ST_NEW_VERTEX_PROGRAM, /* st */ 116 }, 117 update_vs_constants /* update */ 118}; 119 120/* Fragment shader: 121 */ 122static void update_fs_constants(struct st_context *st ) 123{ 124 struct st_fragment_program *fp = st->fp; 125 struct gl_program_parameter_list *params = fp->Base.Base.Parameters; 126 127 st_upload_constants( st, params, PIPE_SHADER_FRAGMENT ); 128} 129 130const struct st_tracked_state st_update_fs_constants = { 131 "st_update_fs_constants", /* name */ 132 { /* dirty */ 133 0, /* set dynamically above */ /* mesa */ 134 ST_NEW_FRAGMENT_PROGRAM, /* st */ 135 }, 136 update_fs_constants /* update */ 137}; 138 139