91e04687c5b7255b940a21f54729902f6e494ec3
[mesa.git] / src / gallium / drivers / softpipe / sp_fs_exec.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 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
46 */
47 struct sp_exec_fragment_shader
48 {
49 struct sp_fragment_shader 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 *base)
57 {
58 return (struct sp_exec_fragment_shader *) base;
59 }
60
61
62 /**
63 * Compute quad X,Y,Z,W for the four fragments in a quad.
64 *
65 * This should really be part of the compiled shader.
66 */
67 void
68 sp_setup_pos_vector(const struct tgsi_interp_coef *coef,
69 float x, float y,
70 struct tgsi_exec_vector *quadpos)
71 {
72 uint chan;
73 /* do X */
74 quadpos->xyzw[0].f[0] = x;
75 quadpos->xyzw[0].f[1] = x + 1;
76 quadpos->xyzw[0].f[2] = x;
77 quadpos->xyzw[0].f[3] = x + 1;
78
79 /* do Y */
80 quadpos->xyzw[1].f[0] = y;
81 quadpos->xyzw[1].f[1] = y;
82 quadpos->xyzw[1].f[2] = y + 1;
83 quadpos->xyzw[1].f[3] = y + 1;
84
85 /* do Z and W for all fragments in the quad */
86 for (chan = 2; chan < 4; chan++) {
87 const float dadx = coef->dadx[chan];
88 const float dady = coef->dady[chan];
89 const float a0 = coef->a0[chan] + dadx * x + dady * y;
90 quadpos->xyzw[chan].f[0] = a0;
91 quadpos->xyzw[chan].f[1] = a0 + dadx;
92 quadpos->xyzw[chan].f[2] = a0 + dady;
93 quadpos->xyzw[chan].f[3] = a0 + dadx + dady;
94 }
95 }
96
97
98 static void
99 exec_prepare( const struct sp_fragment_shader *base,
100 struct tgsi_exec_machine *machine,
101 struct tgsi_sampler **samplers )
102 {
103 /*
104 * Bind tokens/shader to the interpreter's machine state.
105 * Avoid redundant binding.
106 */
107 if (machine->Tokens != base->shader.tokens) {
108 tgsi_exec_machine_bind_shader( machine,
109 base->shader.tokens,
110 PIPE_MAX_SAMPLERS,
111 samplers );
112 }
113 }
114
115
116 /* TODO: hide the machine struct in here somewhere, remove from this
117 * interface:
118 */
119 static unsigned
120 exec_run( const struct sp_fragment_shader *base,
121 struct tgsi_exec_machine *machine,
122 struct quad_header *quad )
123 {
124 /* Compute X, Y, Z, W vals for this quad */
125 sp_setup_pos_vector(quad->posCoef,
126 (float)quad->input.x0, (float)quad->input.y0,
127 &machine->QuadPos);
128
129 quad->inout.mask &= tgsi_exec_machine_run( machine );
130 if (quad->inout.mask == 0)
131 return FALSE;
132
133 /* store outputs */
134 {
135 const ubyte *sem_name = base->info.output_semantic_name;
136 const ubyte *sem_index = base->info.output_semantic_index;
137 const uint n = base->info.num_outputs;
138 uint i;
139 for (i = 0; i < n; i++) {
140 switch (sem_name[i]) {
141 case TGSI_SEMANTIC_COLOR:
142 {
143 uint cbuf = sem_index[i];
144 memcpy(quad->output.color[cbuf],
145 &machine->Outputs[i].xyzw[0].f[0],
146 sizeof(quad->output.color[0]) );
147 }
148 break;
149 case TGSI_SEMANTIC_POSITION:
150 {
151 uint j;
152 for (j = 0; j < 4; j++) {
153 quad->output.depth[j] = machine->Outputs[0].xyzw[2].f[j];
154 }
155 }
156 break;
157 }
158 }
159 }
160
161 return TRUE;
162 }
163
164
165 static void
166 exec_delete( struct sp_fragment_shader *base )
167 {
168 FREE((void *) base->shader.tokens);
169 FREE(base);
170 }
171
172
173 struct sp_fragment_shader *
174 softpipe_create_fs_exec(struct softpipe_context *softpipe,
175 const struct pipe_shader_state *templ)
176 {
177 struct sp_exec_fragment_shader *shader;
178
179 /* Decide whether we'll be codegenerating this shader and if so do
180 * that now.
181 */
182
183 shader = CALLOC_STRUCT(sp_exec_fragment_shader);
184 if (!shader)
185 return NULL;
186
187 /* we need to keep a local copy of the tokens */
188 shader->base.shader.tokens = tgsi_dup_tokens(templ->tokens);
189 shader->base.prepare = exec_prepare;
190 shader->base.run = exec_run;
191 shader->base.delete = exec_delete;
192
193 return &shader->base;
194 }