47e413b776c17ad2a5cb32059968ea2765b85f2f
[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 int vs_index;
54 uint i;
55
56 draw_prepare_shader_outputs(llvmpipe->draw);
57
58 llvmpipe->color_slot[0] = -1;
59 llvmpipe->color_slot[1] = -1;
60 llvmpipe->bcolor_slot[0] = -1;
61 llvmpipe->bcolor_slot[1] = -1;
62
63 /*
64 * Match FS inputs against VS outputs, emitting the necessary
65 * attributes. Could cache these structs and look them up with a
66 * combination of fragment shader, vertex shader ids.
67 */
68
69 vinfo->num_attribs = 0;
70
71 vs_index = draw_find_shader_output(llvmpipe->draw,
72 TGSI_SEMANTIC_POSITION,
73 0);
74
75 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
76
77 for (i = 0; i < lpfs->info.base.num_inputs; i++) {
78 /*
79 * Search for each input in current vs output:
80 */
81
82 vs_index = draw_find_shader_output(llvmpipe->draw,
83 lpfs->info.base.input_semantic_name[i],
84 lpfs->info.base.input_semantic_index[i]);
85
86 if (lpfs->info.base.input_semantic_name[i] == TGSI_SEMANTIC_COLOR &&
87 lpfs->info.base.input_semantic_index[i] < 2) {
88 int idx = lpfs->info.base.input_semantic_index[i];
89 llvmpipe->color_slot[idx] = (int)vinfo->num_attribs;
90 }
91
92 if (lpfs->info.base.input_semantic_name[i] == TGSI_SEMANTIC_FACE) {
93 llvmpipe->face_slot = vinfo->num_attribs;
94 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
95 } else if (lpfs->info.base.input_semantic_name[i] == TGSI_SEMANTIC_PRIMID) {
96 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
97 } else {
98 /*
99 * Emit the requested fs attribute for all but position.
100 */
101 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
102 }
103 }
104 /* Figure out if we need bcolor as well.
105 */
106 for (i = 0; i < 2; i++) {
107 vs_index = draw_find_shader_output(llvmpipe->draw,
108 TGSI_SEMANTIC_BCOLOR, i);
109
110 if (vs_index >= 0) {
111 llvmpipe->bcolor_slot[i] = (int)vinfo->num_attribs;
112 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
113 }
114 }
115
116
117 /* Figure out if we need pointsize as well.
118 */
119 vs_index = draw_find_shader_output(llvmpipe->draw,
120 TGSI_SEMANTIC_PSIZE, 0);
121
122 if (vs_index >= 0) {
123 llvmpipe->psize_slot = vinfo->num_attribs;
124 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
125 }
126
127 /* Figure out if we need viewport index */
128 vs_index = draw_find_shader_output(llvmpipe->draw,
129 TGSI_SEMANTIC_VIEWPORT_INDEX,
130 0);
131 if (vs_index >= 0) {
132 llvmpipe->viewport_index_slot = vinfo->num_attribs;
133 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
134 } else {
135 llvmpipe->viewport_index_slot = 0;
136 }
137
138 /* Figure out if we need layer */
139 vs_index = draw_find_shader_output(llvmpipe->draw,
140 TGSI_SEMANTIC_LAYER,
141 0);
142 if (vs_index >= 0) {
143 llvmpipe->layer_slot = vinfo->num_attribs;
144 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
145 } else {
146 llvmpipe->layer_slot = 0;
147 }
148
149 draw_compute_vertex_size(vinfo);
150 lp_setup_set_vertex_info(llvmpipe->setup, vinfo);
151 }
152
153
154 /**
155 * Handle state changes.
156 * Called just prior to drawing anything (pipe::draw_arrays(), etc).
157 *
158 * Hopefully this will remain quite simple, otherwise need to pull in
159 * something like the state tracker mechanism.
160 */
161 void llvmpipe_update_derived( struct llvmpipe_context *llvmpipe )
162 {
163 struct llvmpipe_screen *lp_screen = llvmpipe_screen(llvmpipe->pipe.screen);
164
165 /* Check for updated textures.
166 */
167 if (llvmpipe->tex_timestamp != lp_screen->timestamp) {
168 llvmpipe->tex_timestamp = lp_screen->timestamp;
169 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
170 }
171
172 if (llvmpipe->dirty & (LP_NEW_RASTERIZER |
173 LP_NEW_FS |
174 LP_NEW_VS))
175 compute_vertex_info( llvmpipe );
176
177 if (llvmpipe->dirty & (LP_NEW_FS |
178 LP_NEW_FRAMEBUFFER |
179 LP_NEW_BLEND |
180 LP_NEW_SCISSOR |
181 LP_NEW_DEPTH_STENCIL_ALPHA |
182 LP_NEW_RASTERIZER |
183 LP_NEW_SAMPLER |
184 LP_NEW_SAMPLER_VIEW |
185 LP_NEW_OCCLUSION_QUERY))
186 llvmpipe_update_fs( llvmpipe );
187
188 if (llvmpipe->dirty & (LP_NEW_FS |
189 LP_NEW_FRAMEBUFFER |
190 LP_NEW_RASTERIZER))
191 llvmpipe_update_setup( llvmpipe );
192
193 if (llvmpipe->dirty & LP_NEW_BLEND_COLOR)
194 lp_setup_set_blend_color(llvmpipe->setup,
195 &llvmpipe->blend_color);
196
197 if (llvmpipe->dirty & LP_NEW_SCISSOR)
198 lp_setup_set_scissors(llvmpipe->setup, llvmpipe->scissors);
199
200 if (llvmpipe->dirty & LP_NEW_DEPTH_STENCIL_ALPHA) {
201 lp_setup_set_alpha_ref_value(llvmpipe->setup,
202 llvmpipe->depth_stencil->alpha.ref_value);
203 lp_setup_set_stencil_ref_values(llvmpipe->setup,
204 llvmpipe->stencil_ref.ref_value);
205 }
206
207 if (llvmpipe->dirty & LP_NEW_CONSTANTS)
208 lp_setup_set_fs_constants(llvmpipe->setup,
209 Elements(llvmpipe->constants[PIPE_SHADER_FRAGMENT]),
210 llvmpipe->constants[PIPE_SHADER_FRAGMENT]);
211
212 if (llvmpipe->dirty & (LP_NEW_SAMPLER_VIEW))
213 lp_setup_set_fragment_sampler_views(llvmpipe->setup,
214 llvmpipe->num_sampler_views[PIPE_SHADER_FRAGMENT],
215 llvmpipe->sampler_views[PIPE_SHADER_FRAGMENT]);
216
217 if (llvmpipe->dirty & (LP_NEW_SAMPLER))
218 lp_setup_set_fragment_sampler_state(llvmpipe->setup,
219 llvmpipe->num_samplers[PIPE_SHADER_FRAGMENT],
220 llvmpipe->samplers[PIPE_SHADER_FRAGMENT]);
221
222 llvmpipe->dirty = 0;
223 }
224