nvfx: expose GLSL
[mesa/mesa-lb.git] / src / gallium / drivers / svga / svga_state_constants.c
blob493f78a99089e1c87e0c30fa12edd3c115d4cced
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
24 **********************************************************/
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
29 #include "svga_context.h"
30 #include "svga_state.h"
31 #include "svga_cmd.h"
32 #include "svga_tgsi.h"
33 #include "svga_debug.h"
35 #include "svga_hw_reg.h"
37 /***********************************************************************
38 * Hardware update
41 /* Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
43 static int svga_shader_type( int unit )
45 return unit + 1;
49 static int emit_const( struct svga_context *svga,
50 int unit,
51 int i,
52 const float *value )
54 int ret = PIPE_OK;
56 if (memcmp(svga->state.hw_draw.cb[unit][i], value, 4 * sizeof(float)) != 0) {
57 if (SVGA_DEBUG & DEBUG_CONSTS)
58 debug_printf("%s %s %d: %f %f %f %f\n",
59 __FUNCTION__,
60 unit == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
62 value[0],
63 value[1],
64 value[2],
65 value[3]);
67 ret = SVGA3D_SetShaderConst( svga->swc,
69 svga_shader_type(unit),
70 SVGA3D_CONST_TYPE_FLOAT,
71 value );
72 if (ret)
73 return ret;
75 memcpy(svga->state.hw_draw.cb[unit][i], value, 4 * sizeof(float));
78 return ret;
81 static int emit_consts( struct svga_context *svga,
82 int offset,
83 int unit )
85 struct pipe_screen *screen = svga->pipe.screen;
86 unsigned count;
87 const float (*data)[4] = NULL;
88 unsigned i;
89 int ret = PIPE_OK;
91 if (svga->curr.cb[unit] == NULL)
92 goto done;
94 count = svga->curr.cb[unit]->size / (4 * sizeof(float));
96 data = (const float (*)[4])pipe_buffer_map(screen,
97 svga->curr.cb[unit],
98 PIPE_BUFFER_USAGE_CPU_READ);
99 if (data == NULL) {
100 ret = PIPE_ERROR_OUT_OF_MEMORY;
101 goto done;
104 for (i = 0; i < count; i++) {
105 ret = emit_const( svga, unit, offset + i, data[i] );
106 if (ret)
107 goto done;
110 done:
111 if (data)
112 pipe_buffer_unmap(screen, svga->curr.cb[unit]);
114 return ret;
117 static int emit_fs_consts( struct svga_context *svga,
118 unsigned dirty )
120 const struct svga_shader_result *result = svga->state.hw_draw.fs;
121 const struct svga_fs_compile_key *key = &result->key.fkey;
122 int ret = 0;
124 ret = emit_consts( svga, 0, PIPE_SHADER_FRAGMENT );
125 if (ret)
126 return ret;
128 /* The internally generated fragment shader for xor blending
129 * doesn't have a 'result' struct. It should be fixed to avoid
130 * this special case, but work around it with a NULL check:
132 if (result != NULL &&
133 key->num_unnormalized_coords)
135 unsigned offset = result->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
136 int i;
138 for (i = 0; i < key->num_textures; i++) {
139 if (key->tex[i].unnormalized) {
140 struct pipe_texture *tex = svga->curr.sampler_views[i]->texture;
141 float data[4];
143 data[0] = 1.0 / (float)tex->width0;
144 data[1] = 1.0 / (float)tex->height0;
145 data[2] = 1.0;
146 data[3] = 1.0;
148 ret = emit_const( svga,
149 PIPE_SHADER_FRAGMENT,
150 key->tex[i].width_height_idx + offset,
151 data );
152 if (ret)
153 return ret;
157 offset += key->num_unnormalized_coords;
160 return 0;
164 struct svga_tracked_state svga_hw_fs_parameters =
166 "hw fs params",
167 (SVGA_NEW_FS_CONST_BUFFER |
168 SVGA_NEW_FS_RESULT |
169 SVGA_NEW_TEXTURE_BINDING),
170 emit_fs_consts
173 /***********************************************************************
176 static int emit_vs_consts( struct svga_context *svga,
177 unsigned dirty )
179 const struct svga_shader_result *result = svga->state.hw_draw.vs;
180 const struct svga_vs_compile_key *key = &result->key.vkey;
181 int ret = 0;
182 unsigned offset;
184 /* SVGA_NEW_VS_RESULT
186 if (result == NULL)
187 return 0;
189 /* SVGA_NEW_VS_CONST_BUFFER
191 ret = emit_consts( svga, 0, PIPE_SHADER_VERTEX );
192 if (ret)
193 return ret;
195 offset = result->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
197 /* SVGA_NEW_VS_RESULT
199 if (key->need_prescale) {
200 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
201 svga->state.hw_clear.prescale.scale );
202 if (ret)
203 return ret;
205 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
206 svga->state.hw_clear.prescale.translate );
207 if (ret)
208 return ret;
211 /* SVGA_NEW_ZERO_STRIDE
213 if (key->zero_stride_vertex_elements) {
214 unsigned i, curr_zero_stride = 0;
215 for (i = 0; i < PIPE_MAX_ATTRIBS; ++i) {
216 if (key->zero_stride_vertex_elements & (1 << i)) {
217 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
218 svga->curr.zero_stride_constants +
219 4 * curr_zero_stride );
220 if (ret)
221 return ret;
222 ++curr_zero_stride;
227 return 0;
231 struct svga_tracked_state svga_hw_vs_parameters =
233 "hw vs params",
234 (SVGA_NEW_PRESCALE |
235 SVGA_NEW_VS_CONST_BUFFER |
236 SVGA_NEW_ZERO_STRIDE |
237 SVGA_NEW_VS_RESULT),
238 emit_vs_consts