1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
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"
42 * The vertex info describes how to convert the post-transformed vertices
43 * (simple float[][4]) used by the 'draw' module into vertices for
46 * This function validates the vertex layout.
49 compute_vertex_info(struct llvmpipe_context
*llvmpipe
)
51 const struct tgsi_shader_info
*fsInfo
= &llvmpipe
->fs
->info
.base
;
52 struct vertex_info
*vinfo
= &llvmpipe
->vertex_info
;
56 draw_prepare_shader_outputs(llvmpipe
->draw
);
59 * Those can't actually be 0 (because pos is always at 0).
60 * But use ints anyway to avoid confusion (in vs outputs, they
61 * can very well be at pos 0).
63 llvmpipe
->color_slot
[0] = -1;
64 llvmpipe
->color_slot
[1] = -1;
65 llvmpipe
->bcolor_slot
[0] = -1;
66 llvmpipe
->bcolor_slot
[1] = -1;
67 llvmpipe
->viewport_index_slot
= -1;
68 llvmpipe
->layer_slot
= -1;
69 llvmpipe
->face_slot
= -1;
70 llvmpipe
->psize_slot
= -1;
73 * Match FS inputs against VS outputs, emitting the necessary
74 * attributes. Could cache these structs and look them up with a
75 * combination of fragment shader, vertex shader ids.
78 vinfo
->num_attribs
= 0;
80 vs_index
= draw_find_shader_output(llvmpipe
->draw
,
81 TGSI_SEMANTIC_POSITION
, 0);
83 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
85 for (i
= 0; i
< fsInfo
->num_inputs
; i
++) {
87 * Search for each input in current vs output:
89 vs_index
= draw_find_shader_output(llvmpipe
->draw
,
90 fsInfo
->input_semantic_name
[i
],
91 fsInfo
->input_semantic_index
[i
]);
93 if (fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
&&
94 fsInfo
->input_semantic_index
[i
] < 2) {
95 int idx
= fsInfo
->input_semantic_index
[i
];
96 llvmpipe
->color_slot
[idx
] = (int)vinfo
->num_attribs
;
99 if (fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_FACE
) {
100 llvmpipe
->face_slot
= (int)vinfo
->num_attribs
;
101 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
103 * For vp index and layer, if the fs requires them but the vs doesn't
104 * provide them, draw (vbuf) will give us the required 0 (slot -1).
105 * (This means in this case we'll also use those slots in setup, which
106 * isn't necessary but they'll contain the correct (0) value.)
108 } else if (fsInfo
->input_semantic_name
[i
] ==
109 TGSI_SEMANTIC_VIEWPORT_INDEX
) {
110 llvmpipe
->viewport_index_slot
= (int)vinfo
->num_attribs
;
111 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
112 } else if (fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
) {
113 llvmpipe
->layer_slot
= (int)vinfo
->num_attribs
;
114 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
117 * Note that we'd actually want to skip position (as we won't use
118 * the attribute in the fs) but can't. The reason is that we don't
119 * actually have an input/output map for setup (even though it looks
120 * like we do...). Could adjust for this though even without a map
121 * (in llvmpipe_create_fs_state()).
123 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
127 /* Figure out if we need bcolor as well.
129 for (i
= 0; i
< 2; i
++) {
130 vs_index
= draw_find_shader_output(llvmpipe
->draw
,
131 TGSI_SEMANTIC_BCOLOR
, i
);
134 llvmpipe
->bcolor_slot
[i
] = (int)vinfo
->num_attribs
;
135 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
139 /* Figure out if we need pointsize as well.
141 vs_index
= draw_find_shader_output(llvmpipe
->draw
,
142 TGSI_SEMANTIC_PSIZE
, 0);
145 llvmpipe
->psize_slot
= (int)vinfo
->num_attribs
;
146 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
149 /* Figure out if we need viewport index (if it wasn't already in fs input) */
150 if (llvmpipe
->viewport_index_slot
< 0) {
151 vs_index
= draw_find_shader_output(llvmpipe
->draw
,
152 TGSI_SEMANTIC_VIEWPORT_INDEX
,
155 llvmpipe
->viewport_index_slot
=(int)vinfo
->num_attribs
;
156 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
160 /* Figure out if we need layer (if it wasn't already in fs input) */
161 if (llvmpipe
->layer_slot
< 0) {
162 vs_index
= draw_find_shader_output(llvmpipe
->draw
,
166 llvmpipe
->layer_slot
= (int)vinfo
->num_attribs
;
167 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
171 draw_compute_vertex_size(vinfo
);
172 lp_setup_set_vertex_info(llvmpipe
->setup
, vinfo
);
177 * Handle state changes.
178 * Called just prior to drawing anything (pipe::draw_arrays(), etc).
180 * Hopefully this will remain quite simple, otherwise need to pull in
181 * something like the state tracker mechanism.
183 void llvmpipe_update_derived( struct llvmpipe_context
*llvmpipe
)
185 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(llvmpipe
->pipe
.screen
);
187 /* Check for updated textures.
189 if (llvmpipe
->tex_timestamp
!= lp_screen
->timestamp
) {
190 llvmpipe
->tex_timestamp
= lp_screen
->timestamp
;
191 llvmpipe
->dirty
|= LP_NEW_SAMPLER_VIEW
;
194 /* This needs LP_NEW_RASTERIZER because of draw_prepare_shader_outputs(). */
195 if (llvmpipe
->dirty
& (LP_NEW_RASTERIZER
|
199 compute_vertex_info(llvmpipe
);
201 if (llvmpipe
->dirty
& (LP_NEW_FS
|
205 LP_NEW_DEPTH_STENCIL_ALPHA
|
208 LP_NEW_SAMPLER_VIEW
|
209 LP_NEW_OCCLUSION_QUERY
))
210 llvmpipe_update_fs(llvmpipe
);
212 if (llvmpipe
->dirty
& (LP_NEW_FS
|
215 LP_NEW_DEPTH_STENCIL_ALPHA
)) {
218 * Rasterization is disabled if there is no pixel shader and
219 * both depth and stencil testing are disabled:
220 * http://msdn.microsoft.com/en-us/library/windows/desktop/bb205125
221 * FIXME: set rasterizer_discard in state tracker instead.
223 boolean null_fs
= !llvmpipe
->fs
||
224 llvmpipe
->fs
->info
.base
.num_instructions
<= 1;
226 (llvmpipe
->sample_mask
& 1) == 0 ||
227 (llvmpipe
->rasterizer
? llvmpipe
->rasterizer
->rasterizer_discard
: FALSE
) ||
229 !llvmpipe
->depth_stencil
->depth
.enabled
&&
230 !llvmpipe
->depth_stencil
->stencil
[0].enabled
);
231 lp_setup_set_rasterizer_discard(llvmpipe
->setup
, discard
);
234 if (llvmpipe
->dirty
& (LP_NEW_FS
|
237 llvmpipe_update_setup( llvmpipe
);
239 if (llvmpipe
->dirty
& LP_NEW_BLEND_COLOR
)
240 lp_setup_set_blend_color(llvmpipe
->setup
,
241 &llvmpipe
->blend_color
);
243 if (llvmpipe
->dirty
& LP_NEW_SCISSOR
)
244 lp_setup_set_scissors(llvmpipe
->setup
, llvmpipe
->scissors
);
246 if (llvmpipe
->dirty
& LP_NEW_DEPTH_STENCIL_ALPHA
) {
247 lp_setup_set_alpha_ref_value(llvmpipe
->setup
,
248 llvmpipe
->depth_stencil
->alpha
.ref_value
);
249 lp_setup_set_stencil_ref_values(llvmpipe
->setup
,
250 llvmpipe
->stencil_ref
.ref_value
);
253 if (llvmpipe
->dirty
& LP_NEW_FS_CONSTANTS
)
254 lp_setup_set_fs_constants(llvmpipe
->setup
,
255 ARRAY_SIZE(llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
]),
256 llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
]);
258 if (llvmpipe
->dirty
& LP_NEW_FS_SSBOS
)
259 lp_setup_set_fs_ssbos(llvmpipe
->setup
,
260 ARRAY_SIZE(llvmpipe
->ssbos
[PIPE_SHADER_FRAGMENT
]),
261 llvmpipe
->ssbos
[PIPE_SHADER_FRAGMENT
]);
263 if (llvmpipe
->dirty
& LP_NEW_FS_IMAGES
)
264 lp_setup_set_fs_images(llvmpipe
->setup
,
265 ARRAY_SIZE(llvmpipe
->images
[PIPE_SHADER_FRAGMENT
]),
266 llvmpipe
->images
[PIPE_SHADER_FRAGMENT
]);
268 if (llvmpipe
->dirty
& (LP_NEW_SAMPLER_VIEW
))
269 lp_setup_set_fragment_sampler_views(llvmpipe
->setup
,
270 llvmpipe
->num_sampler_views
[PIPE_SHADER_FRAGMENT
],
271 llvmpipe
->sampler_views
[PIPE_SHADER_FRAGMENT
]);
273 if (llvmpipe
->dirty
& (LP_NEW_SAMPLER
))
274 lp_setup_set_fragment_sampler_state(llvmpipe
->setup
,
275 llvmpipe
->num_samplers
[PIPE_SHADER_FRAGMENT
],
276 llvmpipe
->samplers
[PIPE_SHADER_FRAGMENT
]);
278 if (llvmpipe
->dirty
& LP_NEW_VIEWPORT
) {
280 * Update setup and fragment's view of the active viewport state.
282 * XXX TODO: It is possible to only loop over the active viewports
283 * instead of all viewports (PIPE_MAX_VIEWPORTS).
285 lp_setup_set_viewports(llvmpipe
->setup
,
287 llvmpipe
->viewports
);