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_inlines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_pstipple.h"
32 #include "pipe/p_shader_tokens.h"
33 #include "draw/draw_context.h"
34 #include "draw/draw_vertex.h"
35 #include "sp_context.h"
36 #include "sp_screen.h"
38 #include "sp_texture.h"
39 #include "sp_tex_sample.h"
40 #include "sp_tex_tile_cache.h"
44 * Mark the current vertex layout as "invalid".
45 * We'll validate the vertex layout later, when we start to actually
46 * render a point or line or tri.
49 invalidate_vertex_layout(struct softpipe_context
*softpipe
)
51 softpipe
->setup_info
.valid
= 0;
56 * The vertex info describes how to convert the post-transformed vertices
57 * (simple float[][4]) used by the 'draw' module into vertices for
60 * This function validates the vertex layout.
63 softpipe_compute_vertex_info(struct softpipe_context
*softpipe
)
65 struct sp_setup_info
*sinfo
= &softpipe
->setup_info
;
67 if (sinfo
->valid
== 0) {
68 const struct tgsi_shader_info
*fsInfo
= &softpipe
->fs_variant
->info
;
69 struct vertex_info
*vinfo
= &softpipe
->vertex_info
;
73 * This doesn't quite work right (wrt face injection, prim id,
74 * wide points) - hit a couple assertions, misrenderings plus
75 * memory corruption. Albeit could fix (the former two) by calling
76 * this "more often" (rasterizer changes etc.). (The latter would
77 * need to be included in draw_prepare_shader_outputs, but it looks
78 * like that would potentially allocate quite some unused additional
80 * draw_prepare_shader_outputs(softpipe->draw);
84 * Those can't actually be 0 (because pos is always at 0).
85 * But use ints anyway to avoid confusion (in vs outputs, they
86 * can very well be at pos 0).
88 softpipe
->viewport_index_slot
= -1;
89 softpipe
->layer_slot
= -1;
90 softpipe
->psize_slot
= -1;
92 vinfo
->num_attribs
= 0;
95 * Put position always first (setup needs it there).
97 vs_index
= draw_find_shader_output(softpipe
->draw
,
98 TGSI_SEMANTIC_POSITION
, 0);
100 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
103 * Match FS inputs against VS outputs, emitting the necessary
106 for (i
= 0; i
< fsInfo
->num_inputs
; i
++) {
107 enum sp_interp_mode interp
= SP_INTERP_LINEAR
;
109 switch (fsInfo
->input_interpolate
[i
]) {
110 case TGSI_INTERPOLATE_CONSTANT
:
111 interp
= SP_INTERP_CONSTANT
;
113 case TGSI_INTERPOLATE_LINEAR
:
114 interp
= SP_INTERP_LINEAR
;
116 case TGSI_INTERPOLATE_PERSPECTIVE
:
117 interp
= SP_INTERP_PERSPECTIVE
;
119 case TGSI_INTERPOLATE_COLOR
:
120 assert(fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
);
126 switch (fsInfo
->input_semantic_name
[i
]) {
127 case TGSI_SEMANTIC_POSITION
:
128 interp
= SP_INTERP_POS
;
131 case TGSI_SEMANTIC_COLOR
:
132 if (fsInfo
->input_interpolate
[i
] == TGSI_INTERPOLATE_COLOR
) {
133 if (softpipe
->rasterizer
->flatshade
)
134 interp
= SP_INTERP_CONSTANT
;
136 interp
= SP_INTERP_PERSPECTIVE
;
142 * Search for each input in current vs output:
144 vs_index
= draw_find_shader_output(softpipe
->draw
,
145 fsInfo
->input_semantic_name
[i
],
146 fsInfo
->input_semantic_index
[i
]);
148 if (fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
&&
151 * try and find a bcolor.
152 * Note that if there's both front and back color, draw will
153 * have copied back to front color already.
155 vs_index
= draw_find_shader_output(softpipe
->draw
,
156 TGSI_SEMANTIC_BCOLOR
,
157 fsInfo
->input_semantic_index
[i
]);
160 sinfo
->attrib
[i
].interp
= interp
;
161 /* extremely pointless index map */
162 sinfo
->attrib
[i
].src_index
= i
+ 1;
164 * For vp index and layer, if the fs requires them but the vs doesn't
165 * provide them, draw (vbuf) will give us the required 0 (slot -1).
166 * (This means in this case we'll also use those slots in setup, which
167 * isn't necessary but they'll contain the correct (0) value.)
169 if (fsInfo
->input_semantic_name
[i
] ==
170 TGSI_SEMANTIC_VIEWPORT_INDEX
) {
171 softpipe
->viewport_index_slot
= (int)vinfo
->num_attribs
;
172 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
173 } else if (fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_LAYER
) {
174 softpipe
->layer_slot
= (int)vinfo
->num_attribs
;
175 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
177 * Note that we'd actually want to skip position (as we won't use
178 * the attribute in the fs) but can't. The reason is that we don't
179 * actually have a input/output map for setup (even though it looks
180 * like we do...). Could adjust for this though even without a map.
184 * Note that we'd actually want to skip position (as we won't use
185 * the attribute in the fs) but can't. The reason is that we don't
186 * actually have a input/output map for setup (even though it looks
187 * like we do...). Could adjust for this though even without a map.
189 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
193 /* Figure out if we need pointsize as well.
195 vs_index
= draw_find_shader_output(softpipe
->draw
,
196 TGSI_SEMANTIC_PSIZE
, 0);
199 softpipe
->psize_slot
= (int)vinfo
->num_attribs
;
200 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
203 /* Figure out if we need viewport index (if it wasn't already in fs input) */
204 if (softpipe
->viewport_index_slot
< 0) {
205 vs_index
= draw_find_shader_output(softpipe
->draw
,
206 TGSI_SEMANTIC_VIEWPORT_INDEX
,
209 softpipe
->viewport_index_slot
=(int)vinfo
->num_attribs
;
210 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
214 /* Figure out if we need layer (if it wasn't already in fs input) */
215 if (softpipe
->layer_slot
< 0) {
216 vs_index
= draw_find_shader_output(softpipe
->draw
,
220 softpipe
->layer_slot
= (int)vinfo
->num_attribs
;
221 draw_emit_vertex_attr(vinfo
, EMIT_4F
, vs_index
);
225 draw_compute_vertex_size(vinfo
);
226 softpipe
->setup_info
.valid
= 1;
233 * Called from vbuf module.
235 * This will trigger validation of the vertex layout (and also compute
236 * the required information for setup).
239 softpipe_get_vbuf_vertex_info(struct softpipe_context
*softpipe
)
241 softpipe_compute_vertex_info(softpipe
);
242 return &softpipe
->vertex_info
;
247 * Recompute cliprect from scissor bounds, scissor enable and surface size.
250 compute_cliprect(struct softpipe_context
*sp
)
253 /* SP_NEW_FRAMEBUFFER
255 uint surfWidth
= sp
->framebuffer
.width
;
256 uint surfHeight
= sp
->framebuffer
.height
;
258 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; i
++) {
261 if (sp
->rasterizer
->scissor
) {
265 * clip to scissor rect:
267 sp
->cliprect
[i
].minx
= MAX2(sp
->scissors
[i
].minx
, 0);
268 sp
->cliprect
[i
].miny
= MAX2(sp
->scissors
[i
].miny
, 0);
269 sp
->cliprect
[i
].maxx
= MIN2(sp
->scissors
[i
].maxx
, surfWidth
);
270 sp
->cliprect
[i
].maxy
= MIN2(sp
->scissors
[i
].maxy
, surfHeight
);
273 /* clip to surface bounds */
274 sp
->cliprect
[i
].minx
= 0;
275 sp
->cliprect
[i
].miny
= 0;
276 sp
->cliprect
[i
].maxx
= surfWidth
;
277 sp
->cliprect
[i
].maxy
= surfHeight
;
284 set_shader_sampler(struct softpipe_context
*softpipe
,
289 for (i
= 0; i
<= max_sampler
; i
++) {
290 softpipe
->tgsi
.sampler
[shader
]->sp_sampler
[i
] =
291 (struct sp_sampler
*)(softpipe
->samplers
[shader
][i
]);
296 softpipe_update_compute_samplers(struct softpipe_context
*softpipe
)
298 set_shader_sampler(softpipe
, PIPE_SHADER_COMPUTE
, softpipe
->cs
->max_sampler
);
302 update_tgsi_samplers( struct softpipe_context
*softpipe
)
306 set_shader_sampler(softpipe
, PIPE_SHADER_VERTEX
,
307 softpipe
->vs
->max_sampler
);
308 set_shader_sampler(softpipe
, PIPE_SHADER_FRAGMENT
,
309 softpipe
->fs_variant
->info
.file_max
[TGSI_FILE_SAMPLER
]);
311 set_shader_sampler(softpipe
, PIPE_SHADER_GEOMETRY
,
312 softpipe
->gs
->max_sampler
);
315 /* XXX is this really necessary here??? */
316 for (sh
= 0; sh
< Elements(softpipe
->tex_cache
); sh
++) {
317 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
318 struct softpipe_tex_tile_cache
*tc
= softpipe
->tex_cache
[sh
][i
];
319 if (tc
&& tc
->texture
) {
320 struct softpipe_resource
*spt
= softpipe_resource(tc
->texture
);
321 if (spt
->timestamp
!= tc
->timestamp
) {
322 sp_tex_tile_cache_validate_texture( tc
);
324 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
326 tc
->timestamp
= spt
->timestamp
;
335 update_fragment_shader(struct softpipe_context
*softpipe
, unsigned prim
)
337 struct sp_fragment_shader_variant_key key
;
339 memset(&key
, 0, sizeof(key
));
341 if (prim
== PIPE_PRIM_TRIANGLES
)
342 key
.polygon_stipple
= softpipe
->rasterizer
->poly_stipple_enable
;
345 softpipe
->fs_variant
= softpipe_find_fs_variant(softpipe
,
348 /* prepare the TGSI interpreter for FS execution */
349 softpipe
->fs_variant
->prepare(softpipe
->fs_variant
,
350 softpipe
->fs_machine
,
351 (struct tgsi_sampler
*) softpipe
->
352 tgsi
.sampler
[PIPE_SHADER_FRAGMENT
],
353 (struct tgsi_image
*)softpipe
->tgsi
.image
[PIPE_SHADER_FRAGMENT
],
354 (struct tgsi_buffer
*)softpipe
->tgsi
.buffer
[PIPE_SHADER_FRAGMENT
]);
357 softpipe
->fs_variant
= NULL
;
360 /* This would be the logical place to pass the fragment shader
361 * to the draw module. However, doing this here, during state
362 * validation, causes problems with the 'draw' module helpers for
363 * wide/AA/stippled lines.
364 * In principle, the draw's fragment shader should be per-variant
365 * but that doesn't work. So we use a single draw fragment shader
366 * per fragment shader, not per variant.
369 if (softpipe
->fs_variant
) {
370 draw_bind_fragment_shader(softpipe
->draw
,
371 softpipe
->fs_variant
->draw_shader
);
374 draw_bind_fragment_shader(softpipe
->draw
, NULL
);
381 * This should be called when the polygon stipple pattern changes.
382 * We create a new texture from the stipple pattern and create a new
386 update_polygon_stipple_pattern(struct softpipe_context
*softpipe
)
388 struct pipe_resource
*tex
;
389 struct pipe_sampler_view
*view
;
391 tex
= util_pstipple_create_stipple_texture(&softpipe
->pipe
,
392 softpipe
->poly_stipple
.stipple
);
393 pipe_resource_reference(&softpipe
->pstipple
.texture
, tex
);
394 pipe_resource_reference(&tex
, NULL
);
396 view
= util_pstipple_create_sampler_view(&softpipe
->pipe
,
397 softpipe
->pstipple
.texture
);
398 pipe_sampler_view_reference(&softpipe
->pstipple
.sampler_view
, view
);
399 pipe_sampler_view_reference(&view
, NULL
);
404 * Should be called when polygon stipple is enabled/disabled or when
405 * the fragment shader changes.
406 * We add/update the fragment sampler and sampler views to sample from
407 * the polygon stipple texture. The texture unit that we use depends on
408 * the fragment shader (we need to use a unit not otherwise used by the
412 update_polygon_stipple_enable(struct softpipe_context
*softpipe
, unsigned prim
)
414 if (prim
== PIPE_PRIM_TRIANGLES
&&
415 softpipe
->fs_variant
->key
.polygon_stipple
) {
416 const unsigned unit
= softpipe
->fs_variant
->stipple_sampler_unit
;
419 softpipe
->samplers
[PIPE_SHADER_FRAGMENT
][unit
] = softpipe
->pstipple
.sampler
;
421 /* sampler view state */
422 softpipe_set_sampler_views(&softpipe
->pipe
, PIPE_SHADER_FRAGMENT
,
423 unit
, 1, &softpipe
->pstipple
.sampler_view
);
425 softpipe
->dirty
|= SP_NEW_SAMPLER
;
430 /* Hopefully this will remain quite simple, otherwise need to pull in
431 * something like the state tracker mechanism.
434 softpipe_update_derived(struct softpipe_context
*softpipe
, unsigned prim
)
436 struct softpipe_screen
*sp_screen
= softpipe_screen(softpipe
->pipe
.screen
);
438 /* Check for updated textures.
440 if (softpipe
->tex_timestamp
!= sp_screen
->timestamp
) {
441 softpipe
->tex_timestamp
= sp_screen
->timestamp
;
442 softpipe
->dirty
|= SP_NEW_TEXTURE
;
445 #if DO_PSTIPPLE_IN_HELPER_MODULE
446 if (softpipe
->dirty
& SP_NEW_STIPPLE
)
447 /* before updating samplers! */
448 update_polygon_stipple_pattern(softpipe
);
451 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
453 update_fragment_shader(softpipe
, prim
);
455 #if DO_PSTIPPLE_IN_HELPER_MODULE
456 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
459 update_polygon_stipple_enable(softpipe
, prim
);
462 /* TODO: this looks suboptimal */
463 if (softpipe
->dirty
& (SP_NEW_SAMPLER
|
467 update_tgsi_samplers( softpipe
);
469 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
472 invalidate_vertex_layout( softpipe
);
474 if (softpipe
->dirty
& (SP_NEW_SCISSOR
|
477 compute_cliprect(softpipe
);
479 if (softpipe
->dirty
& (SP_NEW_BLEND
|
480 SP_NEW_DEPTH_STENCIL_ALPHA
|
484 sp_build_quad_pipeline(softpipe
);