Merge branch '7.8'
[mesa.git] / src / gallium / drivers / svga / svga_state_constants.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28
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"
34
35 #include "svga_hw_reg.h"
36
37 /***********************************************************************
38 * Hardware update
39 */
40
41 /* Convert from PIPE_SHADER_* to SVGA3D_SHADERTYPE_*
42 */
43 static int svga_shader_type( int unit )
44 {
45 return unit + 1;
46 }
47
48
49 static int emit_const( struct svga_context *svga,
50 int unit,
51 int i,
52 const float *value )
53 {
54 int ret = PIPE_OK;
55
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",
61 i,
62 value[0],
63 value[1],
64 value[2],
65 value[3]);
66
67 ret = SVGA3D_SetShaderConst( svga->swc,
68 i,
69 svga_shader_type(unit),
70 SVGA3D_CONST_TYPE_FLOAT,
71 value );
72 if (ret)
73 return ret;
74
75 memcpy(svga->state.hw_draw.cb[unit][i], value, 4 * sizeof(float));
76 }
77
78 return ret;
79 }
80
81 static int emit_consts( struct svga_context *svga,
82 int offset,
83 int unit )
84 {
85 struct pipe_transfer *transfer = NULL;
86 unsigned count;
87 const float (*data)[4] = NULL;
88 unsigned i;
89 int ret = PIPE_OK;
90
91 if (svga->curr.cb[unit] == NULL)
92 goto done;
93
94 count = svga->curr.cb[unit]->width0 / (4 * sizeof(float));
95
96 data = (const float (*)[4])pipe_buffer_map(&svga->pipe,
97 svga->curr.cb[unit],
98 PIPE_TRANSFER_READ,
99 &transfer);
100 if (data == NULL) {
101 ret = PIPE_ERROR_OUT_OF_MEMORY;
102 goto done;
103 }
104
105 for (i = 0; i < count; i++) {
106 ret = emit_const( svga, unit, offset + i, data[i] );
107 if (ret)
108 goto done;
109 }
110
111 done:
112 if (data)
113 pipe_buffer_unmap(&svga->pipe, svga->curr.cb[unit], transfer);
114
115 return ret;
116 }
117
118 static int emit_fs_consts( struct svga_context *svga,
119 unsigned dirty )
120 {
121 const struct svga_shader_result *result = svga->state.hw_draw.fs;
122 const struct svga_fs_compile_key *key = &result->key.fkey;
123 int ret = 0;
124
125 ret = emit_consts( svga, 0, PIPE_SHADER_FRAGMENT );
126 if (ret)
127 return ret;
128
129 /* The internally generated fragment shader for xor blending
130 * doesn't have a 'result' struct. It should be fixed to avoid
131 * this special case, but work around it with a NULL check:
132 */
133 if (result != NULL &&
134 key->num_unnormalized_coords)
135 {
136 unsigned offset = result->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
137 int i;
138
139 for (i = 0; i < key->num_textures; i++) {
140 if (key->tex[i].unnormalized) {
141 struct pipe_resource *tex = svga->curr.sampler_views[i]->texture;
142 float data[4];
143
144 data[0] = 1.0 / (float)tex->width0;
145 data[1] = 1.0 / (float)tex->height0;
146 data[2] = 1.0;
147 data[3] = 1.0;
148
149 ret = emit_const( svga,
150 PIPE_SHADER_FRAGMENT,
151 key->tex[i].width_height_idx + offset,
152 data );
153 if (ret)
154 return ret;
155 }
156 }
157
158 offset += key->num_unnormalized_coords;
159 }
160
161 return 0;
162 }
163
164
165 struct svga_tracked_state svga_hw_fs_parameters =
166 {
167 "hw fs params",
168 (SVGA_NEW_FS_CONST_BUFFER |
169 SVGA_NEW_FS_RESULT |
170 SVGA_NEW_TEXTURE_BINDING),
171 emit_fs_consts
172 };
173
174 /***********************************************************************
175 */
176
177 static int emit_vs_consts( struct svga_context *svga,
178 unsigned dirty )
179 {
180 const struct svga_shader_result *result = svga->state.hw_draw.vs;
181 const struct svga_vs_compile_key *key = &result->key.vkey;
182 int ret = 0;
183 unsigned offset;
184
185 /* SVGA_NEW_VS_RESULT
186 */
187 if (result == NULL)
188 return 0;
189
190 /* SVGA_NEW_VS_CONST_BUFFER
191 */
192 ret = emit_consts( svga, 0, PIPE_SHADER_VERTEX );
193 if (ret)
194 return ret;
195
196 offset = result->shader->info.file_max[TGSI_FILE_CONSTANT] + 1;
197
198 /* SVGA_NEW_VS_RESULT
199 */
200 if (key->need_prescale) {
201 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
202 svga->state.hw_clear.prescale.scale );
203 if (ret)
204 return ret;
205
206 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
207 svga->state.hw_clear.prescale.translate );
208 if (ret)
209 return ret;
210 }
211
212 /* SVGA_NEW_ZERO_STRIDE
213 */
214 if (key->zero_stride_vertex_elements) {
215 unsigned i, curr_zero_stride = 0;
216 for (i = 0; i < PIPE_MAX_ATTRIBS; ++i) {
217 if (key->zero_stride_vertex_elements & (1 << i)) {
218 ret = emit_const( svga, PIPE_SHADER_VERTEX, offset++,
219 svga->curr.zero_stride_constants +
220 4 * curr_zero_stride );
221 if (ret)
222 return ret;
223 ++curr_zero_stride;
224 }
225 }
226 }
227
228 return 0;
229 }
230
231
232 struct svga_tracked_state svga_hw_vs_parameters =
233 {
234 "hw vs params",
235 (SVGA_NEW_PRESCALE |
236 SVGA_NEW_VS_CONST_BUFFER |
237 SVGA_NEW_ZERO_STRIDE |
238 SVGA_NEW_VS_RESULT),
239 emit_vs_consts
240 };
241