1c9f03a381c8d5e32e077c25b1cc103bbf12f6ec
[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 unsigned vs_index;
54 uint i;
55
56 /*
57 * Match FS inputs against VS outputs, emitting the necessary
58 * attributes. Could cache these structs and look them up with a
59 * combination of fragment shader, vertex shader ids.
60 */
61
62 vinfo->num_attribs = 0;
63
64 vs_index = draw_find_shader_output(llvmpipe->draw,
65 TGSI_SEMANTIC_POSITION,
66 0);
67
68 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
69
70 for (i = 0; i < lpfs->info.base.num_inputs; i++) {
71 /*
72 * Search for each input in current vs output:
73 */
74
75 vs_index = draw_find_shader_output(llvmpipe->draw,
76 lpfs->info.base.input_semantic_name[i],
77 lpfs->info.base.input_semantic_index[i]);
78 if (lpfs->info.base.input_semantic_name[i]==TGSI_SEMANTIC_COLOR){
79 llvmpipe->color_slot = vinfo->num_attribs;
80 }
81 /*
82 * Emit the requested fs attribute for all but position.
83 */
84 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
85 }
86
87 /* Figure out if we need bcolor as well.
88 */
89 vs_index = draw_find_shader_output(llvmpipe->draw,
90 TGSI_SEMANTIC_BCOLOR, 0);
91
92 if (vs_index > 0) {
93 llvmpipe->bcolor_slot = vinfo->num_attribs;
94 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_PERSPECTIVE, vs_index);
95 }
96
97 /* Figure out if we need pointsize as well.
98 */
99 vs_index = draw_find_shader_output(llvmpipe->draw,
100 TGSI_SEMANTIC_PSIZE, 0);
101
102 if (vs_index > 0) {
103 llvmpipe->psize_slot = vinfo->num_attribs;
104 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
105 }
106
107 draw_compute_vertex_size(vinfo);
108 lp_setup_set_vertex_info(llvmpipe->setup, vinfo);
109 }
110
111
112 /**
113 * Handle state changes.
114 * Called just prior to drawing anything (pipe::draw_arrays(), etc).
115 *
116 * Hopefully this will remain quite simple, otherwise need to pull in
117 * something like the state tracker mechanism.
118 */
119 void llvmpipe_update_derived( struct llvmpipe_context *llvmpipe )
120 {
121 struct llvmpipe_screen *lp_screen = llvmpipe_screen(llvmpipe->pipe.screen);
122
123 /* Check for updated textures.
124 */
125 if (llvmpipe->tex_timestamp != lp_screen->timestamp) {
126 llvmpipe->tex_timestamp = lp_screen->timestamp;
127 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
128 }
129
130 if (llvmpipe->dirty & (LP_NEW_RASTERIZER |
131 LP_NEW_FS |
132 LP_NEW_VS))
133 compute_vertex_info( llvmpipe );
134
135 if (llvmpipe->dirty & (LP_NEW_FS |
136 LP_NEW_BLEND |
137 LP_NEW_SCISSOR |
138 LP_NEW_DEPTH_STENCIL_ALPHA |
139 LP_NEW_RASTERIZER |
140 LP_NEW_SAMPLER |
141 LP_NEW_SAMPLER_VIEW |
142 LP_NEW_QUERY))
143 llvmpipe_update_fs( llvmpipe );
144
145 if (llvmpipe->dirty & (LP_NEW_FS |
146 LP_NEW_RASTERIZER))
147 llvmpipe_update_setup( llvmpipe );
148
149 if (llvmpipe->dirty & LP_NEW_BLEND_COLOR)
150 lp_setup_set_blend_color(llvmpipe->setup,
151 &llvmpipe->blend_color);
152
153 if (llvmpipe->dirty & LP_NEW_SCISSOR)
154 lp_setup_set_scissor(llvmpipe->setup, &llvmpipe->scissor);
155
156 if (llvmpipe->dirty & LP_NEW_DEPTH_STENCIL_ALPHA) {
157 lp_setup_set_alpha_ref_value(llvmpipe->setup,
158 llvmpipe->depth_stencil->alpha.ref_value);
159 lp_setup_set_stencil_ref_values(llvmpipe->setup,
160 llvmpipe->stencil_ref.ref_value);
161 }
162
163 if (llvmpipe->dirty & LP_NEW_CONSTANTS)
164 lp_setup_set_fs_constants(llvmpipe->setup,
165 llvmpipe->constants[PIPE_SHADER_FRAGMENT][0]);
166
167 if (llvmpipe->dirty & (LP_NEW_SAMPLER_VIEW))
168 lp_setup_set_fragment_sampler_views(llvmpipe->setup,
169 llvmpipe->num_fragment_sampler_views,
170 llvmpipe->fragment_sampler_views);
171
172 if (llvmpipe->dirty & (LP_NEW_SAMPLER))
173 lp_setup_set_fragment_sampler_state(llvmpipe->setup,
174 llvmpipe->num_samplers,
175 llvmpipe->sampler);
176
177 llvmpipe->dirty = 0;
178 }
179