78d046985b9dc50e0426da573483fe1277753e11
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_derived.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "pipe/p_shader_tokens.h"
31 #include "draw/draw_context.h"
32 #include "draw/draw_vertex.h"
33 #include "draw/draw_private.h"
34 #include "lp_context.h"
35 #include "lp_screen.h"
36 #include "lp_setup.h"
37 #include "lp_state.h"
38
39
40
41 /**
42 * The vertex info describes how to convert the post-transformed vertices
43 * (simple float[][4]) used by the 'draw' module into vertices for
44 * rasterization.
45 *
46 * This function validates the vertex layout.
47 */
48 static void
49 compute_vertex_info(struct llvmpipe_context *llvmpipe)
50 {
51 const struct lp_fragment_shader *lpfs = llvmpipe->fs;
52 struct vertex_info *vinfo = &llvmpipe->vertex_info;
53 const uint num = draw_num_shader_outputs(llvmpipe->draw);
54 uint i;
55
56 /* Tell setup to tell the draw module to simply emit the whole
57 * post-xform vertex as-is.
58 *
59 * Not really sure if this is the best approach.
60 */
61 vinfo->num_attribs = 0;
62 for (i = 0; i < num; i++) {
63 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, i);
64 }
65 draw_compute_vertex_size(vinfo);
66
67
68 lp_setup_set_vertex_info(llvmpipe->setup, vinfo);
69
70 /*
71 llvmpipe->psize_slot = draw_find_vs_output(llvmpipe->draw,
72 TGSI_SEMANTIC_PSIZE, 0);
73 */
74
75 /* Now match FS inputs against emitted vertex data. It's also
76 * entirely possible to just have a fixed layout for FS input,
77 * determined by the fragment shader itself, and adjust the draw
78 * outputs to match that.
79 */
80 {
81 struct lp_shader_input inputs[PIPE_MAX_SHADER_INPUTS];
82
83 for (i = 0; i < lpfs->info.num_inputs; i++) {
84
85 /* This can be precomputed, except for flatshade:
86 */
87 switch (lpfs->info.input_semantic_name[i]) {
88 case TGSI_SEMANTIC_FACE:
89 inputs[i].interp = LP_INTERP_FACING;
90 break;
91 case TGSI_SEMANTIC_POSITION:
92 inputs[i].interp = LP_INTERP_POSITION;
93 break;
94 case TGSI_SEMANTIC_COLOR:
95 /* Colors are linearly interpolated in the fragment shader
96 * even when flatshading is active. This just tells the
97 * setup module to use coefficients with ddx==0 and
98 * ddy==0.
99 */
100 if (llvmpipe->rasterizer->flatshade)
101 inputs[i].interp = LP_INTERP_CONSTANT;
102 else
103 inputs[i].interp = LP_INTERP_LINEAR;
104 break;
105
106 default:
107 switch (lpfs->info.input_interpolate[i]) {
108 case TGSI_INTERPOLATE_CONSTANT:
109 inputs[i].interp = LP_INTERP_CONSTANT;
110 break;
111 case TGSI_INTERPOLATE_LINEAR:
112 inputs[i].interp = LP_INTERP_LINEAR;
113 break;
114 case TGSI_INTERPOLATE_PERSPECTIVE:
115 inputs[i].interp = LP_INTERP_PERSPECTIVE;
116 break;
117 default:
118 assert(0);
119 break;
120 }
121 }
122
123 /* Search for each input in current vs output:
124 */
125 inputs[i].src_index =
126 draw_find_shader_output(llvmpipe->draw,
127 lpfs->info.input_semantic_name[i],
128 lpfs->info.input_semantic_index[i]);
129 }
130
131 lp_setup_set_fs_inputs(llvmpipe->setup,
132 inputs,
133 lpfs->info.num_inputs);
134 }
135 }
136
137
138
139 /**
140 * Recompute cliprect from scissor bounds, scissor enable and surface size.
141 */
142 static void
143 compute_cliprect(struct llvmpipe_context *lp)
144 {
145 /* LP_NEW_FRAMEBUFFER
146 */
147 uint surfWidth = lp->framebuffer.width;
148 uint surfHeight = lp->framebuffer.height;
149
150 /* LP_NEW_RASTERIZER
151 */
152 if (lp->rasterizer->scissor) {
153
154 /* LP_NEW_SCISSOR
155 *
156 * clip to scissor rect:
157 */
158 lp->cliprect.minx = MAX2(lp->scissor.minx, 0);
159 lp->cliprect.miny = MAX2(lp->scissor.miny, 0);
160 lp->cliprect.maxx = MIN2(lp->scissor.maxx, surfWidth);
161 lp->cliprect.maxy = MIN2(lp->scissor.maxy, surfHeight);
162 }
163 else {
164 /* clip to surface bounds */
165 lp->cliprect.minx = 0;
166 lp->cliprect.miny = 0;
167 lp->cliprect.maxx = surfWidth;
168 lp->cliprect.maxy = surfHeight;
169 }
170 }
171
172
173 /* Hopefully this will remain quite simple, otherwise need to pull in
174 * something like the state tracker mechanism.
175 */
176 void llvmpipe_update_derived( struct llvmpipe_context *llvmpipe )
177 {
178 struct llvmpipe_screen *lp_screen = llvmpipe_screen(llvmpipe->pipe.screen);
179
180 /* Check for updated textures.
181 */
182 if (llvmpipe->tex_timestamp != lp_screen->timestamp) {
183 llvmpipe->tex_timestamp = lp_screen->timestamp;
184 llvmpipe->dirty |= LP_NEW_TEXTURE;
185 }
186
187 if (llvmpipe->dirty & (LP_NEW_RASTERIZER |
188 LP_NEW_FS |
189 LP_NEW_VS))
190 compute_vertex_info( llvmpipe );
191
192 if (llvmpipe->dirty & (LP_NEW_SCISSOR |
193 LP_NEW_RASTERIZER |
194 LP_NEW_FRAMEBUFFER))
195 compute_cliprect(llvmpipe);
196
197 if (llvmpipe->dirty & (LP_NEW_FS |
198 LP_NEW_BLEND |
199 LP_NEW_DEPTH_STENCIL_ALPHA |
200 LP_NEW_SAMPLER |
201 LP_NEW_TEXTURE))
202 llvmpipe_update_fs( llvmpipe );
203
204 if (llvmpipe->dirty & LP_NEW_BLEND_COLOR)
205 lp_setup_set_blend_color(llvmpipe->setup,
206 &llvmpipe->blend_color);
207
208 if (llvmpipe->dirty & LP_NEW_DEPTH_STENCIL_ALPHA)
209 lp_setup_set_alpha_ref_value(llvmpipe->setup,
210 llvmpipe->depth_stencil->alpha.ref_value);
211
212 if (llvmpipe->dirty & LP_NEW_CONSTANTS)
213 lp_setup_set_fs_constants(llvmpipe->setup,
214 llvmpipe->constants[PIPE_SHADER_FRAGMENT].buffer);
215
216 if (llvmpipe->dirty & LP_NEW_TEXTURE)
217 lp_setup_set_sampler_textures(llvmpipe->setup,
218 llvmpipe->num_textures,
219 llvmpipe->texture);
220
221 llvmpipe->dirty = 0;
222 }
223