1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
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_tile_cache.h"
43 * Mark the current vertex layout as "invalid".
44 * We'll validate the vertex layout later, when we start to actually
45 * render a point or line or tri.
48 invalidate_vertex_layout(struct softpipe_context
*softpipe
)
50 softpipe
->vertex_info
.num_attribs
= 0;
55 * The vertex info describes how to convert the post-transformed vertices
56 * (simple float[][4]) used by the 'draw' module into vertices for
59 * This function validates the vertex layout and returns a pointer to a
63 softpipe_get_vertex_info(struct softpipe_context
*softpipe
)
65 struct vertex_info
*vinfo
= &softpipe
->vertex_info
;
67 if (vinfo
->num_attribs
== 0) {
68 /* compute vertex layout now */
69 const struct tgsi_shader_info
*fsInfo
= &softpipe
->fs_variant
->info
;
70 struct vertex_info
*vinfo_vbuf
= &softpipe
->vertex_info_vbuf
;
71 const uint num
= draw_num_shader_outputs(softpipe
->draw
);
74 /* Tell draw_vbuf to simply emit the whole post-xform vertex
75 * as-is. No longer any need to try and emit draw vertex_header
78 vinfo_vbuf
->num_attribs
= 0;
79 for (i
= 0; i
< num
; i
++) {
80 draw_emit_vertex_attr(vinfo_vbuf
, EMIT_4F
, INTERP_PERSPECTIVE
, i
);
82 draw_compute_vertex_size(vinfo_vbuf
);
85 * Loop over fragment shader inputs, searching for the matching output
86 * from the vertex shader.
88 vinfo
->num_attribs
= 0;
89 for (i
= 0; i
< fsInfo
->num_inputs
; i
++) {
91 enum interp_mode interp
;
93 switch (fsInfo
->input_interpolate
[i
]) {
94 case TGSI_INTERPOLATE_CONSTANT
:
95 interp
= INTERP_CONSTANT
;
97 case TGSI_INTERPOLATE_LINEAR
:
98 interp
= INTERP_LINEAR
;
100 case TGSI_INTERPOLATE_PERSPECTIVE
:
101 interp
= INTERP_PERSPECTIVE
;
105 interp
= INTERP_LINEAR
;
108 switch (fsInfo
->input_semantic_name
[i
]) {
109 case TGSI_SEMANTIC_POSITION
:
113 case TGSI_SEMANTIC_COLOR
:
114 if (softpipe
->rasterizer
->flatshade
) {
115 interp
= INTERP_CONSTANT
;
120 /* this includes texcoords and varying vars */
121 src
= draw_find_shader_output(softpipe
->draw
,
122 fsInfo
->input_semantic_name
[i
],
123 fsInfo
->input_semantic_index
[i
]);
124 if (fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
&& src
== 0)
125 /* try and find a bcolor */
126 src
= draw_find_shader_output(softpipe
->draw
,
127 TGSI_SEMANTIC_BCOLOR
, fsInfo
->input_semantic_index
[i
]);
129 draw_emit_vertex_attr(vinfo
, EMIT_4F
, interp
, src
);
132 softpipe
->psize_slot
= draw_find_shader_output(softpipe
->draw
,
133 TGSI_SEMANTIC_PSIZE
, 0);
134 if (softpipe
->psize_slot
> 0) {
135 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_CONSTANT
,
136 softpipe
->psize_slot
);
139 draw_compute_vertex_size(vinfo
);
147 * Called from vbuf module.
149 * Note that there's actually two different vertex layouts in softpipe.
151 * The normal one is computed in softpipe_get_vertex_info() above and is
152 * used by the point/line/tri "setup" code.
154 * The other one (this one) is only used by the vbuf module (which is
155 * not normally used by default but used in testing). For the vbuf module,
156 * we basically want to pass-through the draw module's vertex layout as-is.
157 * When the softpipe vbuf code begins drawing, the normal vertex layout
158 * will come into play again.
161 softpipe_get_vbuf_vertex_info(struct softpipe_context
*softpipe
)
163 (void) softpipe_get_vertex_info(softpipe
);
164 return &softpipe
->vertex_info_vbuf
;
169 * Recompute cliprect from scissor bounds, scissor enable and surface size.
172 compute_cliprect(struct softpipe_context
*sp
)
174 /* SP_NEW_FRAMEBUFFER
176 uint surfWidth
= sp
->framebuffer
.width
;
177 uint surfHeight
= sp
->framebuffer
.height
;
181 if (sp
->rasterizer
->scissor
) {
185 * clip to scissor rect:
187 sp
->cliprect
.minx
= MAX2(sp
->scissor
.minx
, 0);
188 sp
->cliprect
.miny
= MAX2(sp
->scissor
.miny
, 0);
189 sp
->cliprect
.maxx
= MIN2(sp
->scissor
.maxx
, surfWidth
);
190 sp
->cliprect
.maxy
= MIN2(sp
->scissor
.maxy
, surfHeight
);
193 /* clip to surface bounds */
194 sp
->cliprect
.minx
= 0;
195 sp
->cliprect
.miny
= 0;
196 sp
->cliprect
.maxx
= surfWidth
;
197 sp
->cliprect
.maxy
= surfHeight
;
203 update_tgsi_samplers( struct softpipe_context
*softpipe
)
207 softpipe_reset_sampler_variants( softpipe
);
209 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
210 struct softpipe_tex_tile_cache
*tc
= softpipe
->fragment_tex_cache
[i
];
211 if (tc
&& tc
->texture
) {
212 struct softpipe_resource
*spt
= softpipe_resource(tc
->texture
);
213 if (spt
->timestamp
!= tc
->timestamp
) {
214 sp_tex_tile_cache_validate_texture( tc
);
216 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
218 tc
->timestamp
= spt
->timestamp
;
223 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
224 struct softpipe_tex_tile_cache
*tc
= softpipe
->vertex_tex_cache
[i
];
226 if (tc
&& tc
->texture
) {
227 struct softpipe_resource
*spt
= softpipe_resource(tc
->texture
);
229 if (spt
->timestamp
!= tc
->timestamp
) {
230 sp_tex_tile_cache_validate_texture(tc
);
231 tc
->timestamp
= spt
->timestamp
;
236 for (i
= 0; i
< PIPE_MAX_GEOMETRY_SAMPLERS
; i
++) {
237 struct softpipe_tex_tile_cache
*tc
= softpipe
->geometry_tex_cache
[i
];
239 if (tc
&& tc
->texture
) {
240 struct softpipe_resource
*spt
= softpipe_resource(tc
->texture
);
242 if (spt
->timestamp
!= tc
->timestamp
) {
243 sp_tex_tile_cache_validate_texture(tc
);
244 tc
->timestamp
= spt
->timestamp
;
252 update_fragment_shader(struct softpipe_context
*softpipe
, unsigned prim
)
254 struct sp_fragment_shader_variant_key key
;
256 memset(&key
, 0, sizeof(key
));
258 if (prim
== PIPE_PRIM_TRIANGLES
)
259 key
.polygon_stipple
= softpipe
->rasterizer
->poly_stipple_enable
;
262 softpipe
->fs_variant
= softpipe_find_fs_variant(softpipe
,
266 softpipe
->fs_variant
= NULL
;
269 /* This would be the logical place to pass the fragment shader
270 * to the draw module. However, doing this here, during state
271 * validation, causes problems with the 'draw' module helpers for
272 * wide/AA/stippled lines.
273 * In principle, the draw's fragment shader should be per-variant
274 * but that doesn't work. So we use a single draw fragment shader
275 * per fragment shader, not per variant.
278 if (softpipe
->fs_variant
) {
279 draw_bind_fragment_shader(softpipe
->draw
,
280 softpipe
->fs_variant
->draw_shader
);
283 draw_bind_fragment_shader(softpipe
->draw
, NULL
);
290 * This should be called when the polygon stipple pattern changes.
291 * We create a new texture from the stipple pattern and create a new
295 update_polygon_stipple_pattern(struct softpipe_context
*softpipe
)
297 struct pipe_resource
*tex
;
298 struct pipe_sampler_view
*view
;
300 tex
= util_pstipple_create_stipple_texture(&softpipe
->pipe
,
301 softpipe
->poly_stipple
.stipple
);
302 pipe_resource_reference(&softpipe
->pstipple
.texture
, tex
);
303 pipe_resource_reference(&tex
, NULL
);
305 view
= util_pstipple_create_sampler_view(&softpipe
->pipe
,
306 softpipe
->pstipple
.texture
);
307 pipe_sampler_view_reference(&softpipe
->pstipple
.sampler_view
, view
);
308 pipe_sampler_view_reference(&view
, NULL
);
313 * Should be called when polygon stipple is enabled/disabled or when
314 * the fragment shader changes.
315 * We add/update the fragment sampler and sampler views to sample from
316 * the polygon stipple texture. The texture unit that we use depends on
317 * the fragment shader (we need to use a unit not otherwise used by the
321 update_polygon_stipple_enable(struct softpipe_context
*softpipe
, unsigned prim
)
323 if (prim
== PIPE_PRIM_TRIANGLES
&&
324 softpipe
->fs_variant
->key
.polygon_stipple
) {
325 const unsigned unit
= softpipe
->fs_variant
->stipple_sampler_unit
;
327 assert(unit
>= softpipe
->num_fragment_samplers
);
330 softpipe
->fragment_samplers
[unit
] = softpipe
->pstipple
.sampler
;
333 pipe_sampler_view_reference(&softpipe
->fragment_sampler_views
[unit
],
334 softpipe
->pstipple
.sampler_view
);
336 sp_tex_tile_cache_set_sampler_view(softpipe
->fragment_tex_cache
[unit
],
337 softpipe
->pstipple
.sampler_view
);
339 softpipe
->dirty
|= SP_NEW_SAMPLER
;
344 /* Hopefully this will remain quite simple, otherwise need to pull in
345 * something like the state tracker mechanism.
348 softpipe_update_derived(struct softpipe_context
*softpipe
, unsigned prim
)
350 struct softpipe_screen
*sp_screen
= softpipe_screen(softpipe
->pipe
.screen
);
352 /* Check for updated textures.
354 if (softpipe
->tex_timestamp
!= sp_screen
->timestamp
) {
355 softpipe
->tex_timestamp
= sp_screen
->timestamp
;
356 softpipe
->dirty
|= SP_NEW_TEXTURE
;
359 #if DO_PSTIPPLE_IN_HELPER_MODULE
360 if (softpipe
->dirty
& SP_NEW_STIPPLE
)
361 /* before updating samplers! */
362 update_polygon_stipple_pattern(softpipe
);
365 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
367 update_fragment_shader(softpipe
, prim
);
369 #if DO_PSTIPPLE_IN_HELPER_MODULE
370 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
373 update_polygon_stipple_enable(softpipe
, prim
);
376 if (softpipe
->dirty
& (SP_NEW_SAMPLER
|
380 update_tgsi_samplers( softpipe
);
382 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
385 invalidate_vertex_layout( softpipe
);
387 if (softpipe
->dirty
& (SP_NEW_SCISSOR
|
390 compute_cliprect(softpipe
);
392 if (softpipe
->dirty
& (SP_NEW_BLEND
|
393 SP_NEW_DEPTH_STENCIL_ALPHA
|
396 sp_build_quad_pipeline(softpipe
);