glsl: fix crash in loop analysis when some controls can't be determined
[mesa.git] / src / gallium / drivers / softpipe / sp_fs_sse.c
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 /**
29 * Execute fragment shader using runtime SSE code generation.
30 */
31
32 #include "sp_context.h"
33 #include "sp_state.h"
34 #include "sp_fs.h"
35 #include "sp_quad.h"
36
37 #include "pipe/p_state.h"
38 #include "pipe/p_defines.h"
39 #include "util/u_memory.h"
40 #include "tgsi/tgsi_exec.h"
41 #include "tgsi/tgsi_sse2.h"
42
43
44 #if defined(PIPE_ARCH_X86)
45
46 #include "rtasm/rtasm_x86sse.h"
47
48
49
50 /**
51 * Subclass of sp_fragment_shader
52 */
53 struct sp_sse_fragment_shader
54 {
55 struct sp_fragment_shader base;
56 struct x86_function sse2_program;
57 tgsi_sse2_fs_function func;
58 float immediates[TGSI_EXEC_NUM_IMMEDIATES][4];
59 };
60
61
62 /** cast wrapper */
63 static INLINE struct sp_sse_fragment_shader *
64 sp_sse_fragment_shader(const struct sp_fragment_shader *base)
65 {
66 return (struct sp_sse_fragment_shader *) base;
67 }
68
69
70 static void
71 fs_sse_prepare( const struct sp_fragment_shader *base,
72 struct tgsi_exec_machine *machine,
73 struct tgsi_sampler **samplers )
74 {
75 machine->Samplers = samplers;
76 }
77
78
79
80 /**
81 * Compute quad X,Y,Z,W for the four fragments in a quad.
82 *
83 * This should really be part of the compiled shader.
84 */
85 static void
86 setup_pos_vector(const struct tgsi_interp_coef *coef,
87 float x, float y,
88 struct tgsi_exec_vector *quadpos)
89 {
90 uint chan;
91 /* do X */
92 quadpos->xyzw[0].f[0] = x;
93 quadpos->xyzw[0].f[1] = x + 1;
94 quadpos->xyzw[0].f[2] = x;
95 quadpos->xyzw[0].f[3] = x + 1;
96
97 /* do Y */
98 quadpos->xyzw[1].f[0] = y;
99 quadpos->xyzw[1].f[1] = y;
100 quadpos->xyzw[1].f[2] = y + 1;
101 quadpos->xyzw[1].f[3] = y + 1;
102
103 /* do Z and W for all fragments in the quad */
104 for (chan = 2; chan < 4; chan++) {
105 const float dadx = coef->dadx[chan];
106 const float dady = coef->dady[chan];
107 const float a0 = coef->a0[chan] + dadx * x + dady * y;
108 quadpos->xyzw[chan].f[0] = a0;
109 quadpos->xyzw[chan].f[1] = a0 + dadx;
110 quadpos->xyzw[chan].f[2] = a0 + dady;
111 quadpos->xyzw[chan].f[3] = a0 + dadx + dady;
112 }
113 }
114
115
116 /* TODO: codegenerate the whole run function, skip this wrapper.
117 * TODO: break dependency on tgsi_exec_machine struct
118 * TODO: push Position calculation into the generated shader
119 * TODO: process >1 quad at a time
120 */
121 static unsigned
122 fs_sse_run( const struct sp_fragment_shader *base,
123 struct tgsi_exec_machine *machine,
124 struct quad_header *quad )
125 {
126 struct sp_sse_fragment_shader *shader = sp_sse_fragment_shader(base);
127
128 /* Compute X, Y, Z, W vals for this quad -- place in temp[0] for now */
129 setup_pos_vector(quad->posCoef,
130 (float)quad->input.x0, (float)quad->input.y0,
131 machine->Temps);
132
133 /* init kill mask */
134 tgsi_set_kill_mask(machine, 0x0);
135 tgsi_set_exec_mask(machine, 1, 1, 1, 1);
136
137 shader->func( machine,
138 (const float (*)[4])machine->Consts[0],
139 (const float (*)[4])shader->immediates,
140 machine->InterpCoefs
141 /*, &machine->QuadPos*/
142 );
143
144 quad->inout.mask &= ~(machine->Temps[TGSI_EXEC_TEMP_KILMASK_I].xyzw[TGSI_EXEC_TEMP_KILMASK_C].u[0]);
145 if (quad->inout.mask == 0)
146 return FALSE;
147
148 /* store outputs */
149 {
150 const ubyte *sem_name = base->info.output_semantic_name;
151 const ubyte *sem_index = base->info.output_semantic_index;
152 const uint n = base->info.num_outputs;
153 uint i;
154 for (i = 0; i < n; i++) {
155 switch (sem_name[i]) {
156 case TGSI_SEMANTIC_COLOR:
157 {
158 uint cbuf = sem_index[i];
159
160 assert(sizeof(quad->output.color[cbuf]) ==
161 sizeof(machine->Outputs[i]));
162
163 /* copy float[4][4] result */
164 memcpy(quad->output.color[cbuf],
165 &machine->Outputs[i],
166 sizeof(quad->output.color[0]) );
167 }
168 break;
169 case TGSI_SEMANTIC_POSITION:
170 {
171 uint j;
172 for (j = 0; j < 4; j++)
173 quad->output.depth[j] = machine->Outputs[i].xyzw[2].f[j];
174 }
175 break;
176 case TGSI_SEMANTIC_STENCIL:
177 {
178 uint j;
179 for (j = 0; j < 4; j++)
180 quad->output.stencil[j] = machine->Outputs[i].xyzw[1].f[j];
181 }
182 break;
183 }
184 }
185 }
186
187 return TRUE;
188 }
189
190
191 static void
192 fs_sse_delete( struct sp_fragment_shader *base )
193 {
194 struct sp_sse_fragment_shader *shader = sp_sse_fragment_shader(base);
195
196 x86_release_func( &shader->sse2_program );
197 FREE(shader);
198 }
199
200
201 struct sp_fragment_shader *
202 softpipe_create_fs_sse(struct softpipe_context *softpipe,
203 const struct pipe_shader_state *templ)
204 {
205 struct sp_sse_fragment_shader *shader;
206
207 if (!softpipe->use_sse)
208 return NULL;
209
210 shader = CALLOC_STRUCT(sp_sse_fragment_shader);
211 if (!shader)
212 return NULL;
213
214 x86_init_func( &shader->sse2_program );
215
216 if (!tgsi_emit_sse2( templ->tokens, &shader->sse2_program,
217 shader->immediates, FALSE )) {
218 FREE(shader);
219 return NULL;
220 }
221
222 shader->func = (tgsi_sse2_fs_function) x86_get_func( &shader->sse2_program );
223 if (!shader->func) {
224 x86_release_func( &shader->sse2_program );
225 FREE(shader);
226 return NULL;
227 }
228
229 shader->base.shader.tokens = NULL; /* don't hold reference to templ->tokens */
230 shader->base.prepare = fs_sse_prepare;
231 shader->base.run = fs_sse_run;
232 shader->base.delete = fs_sse_delete;
233
234 return &shader->base;
235 }
236
237
238 #else
239
240 /* Maybe put this variant in the header file.
241 */
242 struct sp_fragment_shader *
243 softpipe_create_fs_sse(struct softpipe_context *softpipe,
244 const struct pipe_shader_state *templ)
245 {
246 return NULL;
247 }
248
249 #endif