tgsi: add support for buffer/atomic operations to tgsi_exec.
[mesa.git] / src / gallium / drivers / softpipe / sp_fs_exec.c
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 * Execute fragment shader using the TGSI interpreter.
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_parse.h"
42
43
44 /**
45 * Subclass of sp_fragment_shader_variant
46 */
47 struct sp_exec_fragment_shader
48 {
49 struct sp_fragment_shader_variant base;
50 /* No other members for now */
51 };
52
53
54 /** cast wrapper */
55 static inline struct sp_exec_fragment_shader *
56 sp_exec_fragment_shader(const struct sp_fragment_shader_variant *var)
57 {
58 return (struct sp_exec_fragment_shader *) var;
59 }
60
61
62 static void
63 exec_prepare( const struct sp_fragment_shader_variant *var,
64 struct tgsi_exec_machine *machine,
65 struct tgsi_sampler *sampler,
66 struct tgsi_image *image )
67 {
68 /*
69 * Bind tokens/shader to the interpreter's machine state.
70 */
71 tgsi_exec_machine_bind_shader(machine,
72 var->tokens,
73 sampler, image, NULL);
74 }
75
76
77
78 /**
79 * Compute quad X,Y,Z,W for the four fragments in a quad.
80 *
81 * This should really be part of the compiled shader.
82 */
83 static void
84 setup_pos_vector(const struct tgsi_interp_coef *coef,
85 float x, float y,
86 struct tgsi_exec_vector *quadpos)
87 {
88 uint chan;
89 /* do X */
90 quadpos->xyzw[0].f[0] = x;
91 quadpos->xyzw[0].f[1] = x + 1;
92 quadpos->xyzw[0].f[2] = x;
93 quadpos->xyzw[0].f[3] = x + 1;
94
95 /* do Y */
96 quadpos->xyzw[1].f[0] = y;
97 quadpos->xyzw[1].f[1] = y;
98 quadpos->xyzw[1].f[2] = y + 1;
99 quadpos->xyzw[1].f[3] = y + 1;
100
101 /* do Z and W for all fragments in the quad */
102 for (chan = 2; chan < 4; chan++) {
103 const float dadx = coef->dadx[chan];
104 const float dady = coef->dady[chan];
105 const float a0 = coef->a0[chan] + dadx * x + dady * y;
106 quadpos->xyzw[chan].f[0] = a0;
107 quadpos->xyzw[chan].f[1] = a0 + dadx;
108 quadpos->xyzw[chan].f[2] = a0 + dady;
109 quadpos->xyzw[chan].f[3] = a0 + dadx + dady;
110 }
111 }
112
113
114 /* TODO: hide the machine struct in here somewhere, remove from this
115 * interface:
116 */
117 static unsigned
118 exec_run( const struct sp_fragment_shader_variant *var,
119 struct tgsi_exec_machine *machine,
120 struct quad_header *quad,
121 bool early_depth_test )
122 {
123 /* Compute X, Y, Z, W vals for this quad */
124 setup_pos_vector(quad->posCoef,
125 (float)quad->input.x0, (float)quad->input.y0,
126 &machine->QuadPos);
127
128 /* convert 0 to 1.0 and 1 to -1.0 */
129 machine->Face = (float) (quad->input.facing * -2 + 1);
130
131 machine->NonHelperMask = quad->inout.mask;
132 quad->inout.mask &= tgsi_exec_machine_run( machine );
133 if (quad->inout.mask == 0)
134 return FALSE;
135
136 /* store outputs */
137 {
138 const ubyte *sem_name = var->info.output_semantic_name;
139 const ubyte *sem_index = var->info.output_semantic_index;
140 const uint n = var->info.num_outputs;
141 uint i;
142 for (i = 0; i < n; i++) {
143 switch (sem_name[i]) {
144 case TGSI_SEMANTIC_COLOR:
145 {
146 uint cbuf = sem_index[i];
147
148 assert(sizeof(quad->output.color[cbuf]) ==
149 sizeof(machine->Outputs[i]));
150
151 /* copy float[4][4] result */
152 memcpy(quad->output.color[cbuf],
153 &machine->Outputs[i],
154 sizeof(quad->output.color[0]) );
155 }
156 break;
157 case TGSI_SEMANTIC_POSITION:
158 {
159 uint j;
160
161 if (!early_depth_test) {
162 for (j = 0; j < 4; j++)
163 quad->output.depth[j] = machine->Outputs[i].xyzw[2].f[j];
164 }
165 }
166 break;
167 case TGSI_SEMANTIC_STENCIL:
168 {
169 uint j;
170 if (!early_depth_test) {
171 for (j = 0; j < 4; j++)
172 quad->output.stencil[j] = (unsigned)machine->Outputs[i].xyzw[1].u[j];
173 }
174 }
175 break;
176 }
177 }
178 }
179
180 return TRUE;
181 }
182
183
184 static void
185 exec_delete(struct sp_fragment_shader_variant *var,
186 struct tgsi_exec_machine *machine)
187 {
188 if (machine->Tokens == var->tokens) {
189 tgsi_exec_machine_bind_shader(machine, NULL, NULL, NULL, NULL);
190 }
191
192 FREE( (void *) var->tokens );
193 FREE(var);
194 }
195
196
197 struct sp_fragment_shader_variant *
198 softpipe_create_fs_variant_exec(struct softpipe_context *softpipe)
199 {
200 struct sp_exec_fragment_shader *shader;
201
202 shader = CALLOC_STRUCT(sp_exec_fragment_shader);
203 if (!shader)
204 return NULL;
205
206 shader->base.prepare = exec_prepare;
207 shader->base.run = exec_run;
208 shader->base.delete = exec_delete;
209
210 return &shader->base;
211 }