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