Merge commit 'origin/master' into gallium-0.2
[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 #include "sp_context.h"
30 #include "sp_state.h"
31 #include "sp_fs.h"
32 #include "sp_headers.h"
33
34
35 #include "pipe/p_state.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_memory.h"
38 #include "pipe/p_inlines.h"
39 #include "tgsi/tgsi_exec.h"
40 #include "tgsi/tgsi_parse.h"
41
42 struct sp_exec_fragment_shader
43 {
44 struct sp_fragment_shader base;
45 const struct tgsi_token *machine_tokens;
46 };
47
48
49 /** cast wrapper */
50 static INLINE struct sp_exec_fragment_shader *
51 sp_exec_fragment_shader(const struct sp_fragment_shader *base)
52 {
53 return (struct sp_exec_fragment_shader *) base;
54 }
55
56
57 /**
58 * Compute quad X,Y,Z,W for the four fragments in a quad.
59 *
60 * This should really be part of the compiled shader.
61 */
62 void
63 sp_setup_pos_vector(const struct tgsi_interp_coef *coef,
64 float x, float y,
65 struct tgsi_exec_vector *quadpos)
66 {
67 uint chan;
68 /* do X */
69 quadpos->xyzw[0].f[0] = x;
70 quadpos->xyzw[0].f[1] = x + 1;
71 quadpos->xyzw[0].f[2] = x;
72 quadpos->xyzw[0].f[3] = x + 1;
73
74 /* do Y */
75 quadpos->xyzw[1].f[0] = y;
76 quadpos->xyzw[1].f[1] = y;
77 quadpos->xyzw[1].f[2] = y + 1;
78 quadpos->xyzw[1].f[3] = y + 1;
79
80 /* do Z and W for all fragments in the quad */
81 for (chan = 2; chan < 4; chan++) {
82 const float dadx = coef->dadx[chan];
83 const float dady = coef->dady[chan];
84 const float a0 = coef->a0[chan] + dadx * x + dady * y;
85 quadpos->xyzw[chan].f[0] = a0;
86 quadpos->xyzw[chan].f[1] = a0 + dadx;
87 quadpos->xyzw[chan].f[2] = a0 + dady;
88 quadpos->xyzw[chan].f[3] = a0 + dadx + dady;
89 }
90 }
91
92
93 static void
94 exec_prepare( const struct sp_fragment_shader *base,
95 struct tgsi_exec_machine *machine,
96 struct tgsi_sampler *samplers )
97 {
98 struct sp_exec_fragment_shader *spefs =
99 sp_exec_fragment_shader(base);
100
101 /*
102 * Bind tokens/shader to the interpreter's machine state.
103 * Avoid redundant binding.
104 */
105 if (spefs->machine_tokens != base->shader.tokens) {
106 tgsi_exec_machine_bind_shader( machine,
107 base->shader.tokens,
108 PIPE_MAX_SAMPLERS,
109 samplers );
110 spefs->machine_tokens = base->shader.tokens;
111 }
112 }
113
114
115
116
117 /* TODO: hide the machine struct in here somewhere, remove from this
118 * interface:
119 */
120 static unsigned
121 exec_run( const struct sp_fragment_shader *base,
122 struct tgsi_exec_machine *machine,
123 struct quad_header *quad )
124 {
125
126 /* Compute X, Y, Z, W vals for this quad */
127 sp_setup_pos_vector(quad->posCoef,
128 (float)quad->input.x0, (float)quad->input.y0,
129 &machine->QuadPos);
130
131 return tgsi_exec_machine_run( machine );
132 }
133
134
135
136 static void
137 exec_delete( struct sp_fragment_shader *base )
138 {
139 FREE((void *) base->shader.tokens);
140 FREE(base);
141 }
142
143
144
145
146
147 struct sp_fragment_shader *
148 softpipe_create_fs_exec(struct softpipe_context *softpipe,
149 const struct pipe_shader_state *templ)
150 {
151 struct sp_exec_fragment_shader *shader;
152
153 /* Decide whether we'll be codegenerating this shader and if so do
154 * that now.
155 */
156
157 shader = CALLOC_STRUCT(sp_exec_fragment_shader);
158 if (!shader)
159 return NULL;
160
161 /* we need to keep a local copy of the tokens */
162 shader->base.shader.tokens = tgsi_dup_tokens(templ->tokens);
163 shader->base.prepare = exec_prepare;
164 shader->base.run = exec_run;
165 shader->base.delete = exec_delete;
166
167 return &shader->base;
168 }
169