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
;
68 if (sinfo
->valid
== 0) {
69 /* compute vertex layout for vbuf now */
70 const struct tgsi_shader_info
*fsInfo
= &softpipe
->fs_variant
->info
;
71 struct vertex_info
*vinfo_vbuf
= &softpipe
->vertex_info_vbuf
;
72 const uint num
= draw_num_shader_outputs(softpipe
->draw
);
75 /* Tell draw_vbuf to simply emit the whole post-xform vertex
76 * as-is. No longer any need to try and emit draw vertex_header
79 vinfo_vbuf
->num_attribs
= 0;
80 for (i
= 0; i
< num
; i
++) {
81 draw_emit_vertex_attr(vinfo_vbuf
, EMIT_4F
, i
);
83 draw_compute_vertex_size(vinfo_vbuf
);
85 softpipe
->viewport_index_slot
= 0;
86 softpipe
->layer_slot
= 0;
87 softpipe
->psize_slot
= 0;
90 * Loop over fragment shader inputs, searching for the matching output
91 * from the vertex shader.
93 for (i
= 0; i
< fsInfo
->num_inputs
; i
++) {
95 enum sp_interp_mode interp
= SP_INTERP_LINEAR
;
97 switch (fsInfo
->input_interpolate
[i
]) {
98 case TGSI_INTERPOLATE_CONSTANT
:
99 interp
= SP_INTERP_CONSTANT
;
101 case TGSI_INTERPOLATE_LINEAR
:
102 interp
= SP_INTERP_LINEAR
;
104 case TGSI_INTERPOLATE_PERSPECTIVE
:
105 interp
= SP_INTERP_PERSPECTIVE
;
107 case TGSI_INTERPOLATE_COLOR
:
108 assert(fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
);
114 switch (fsInfo
->input_semantic_name
[i
]) {
115 case TGSI_SEMANTIC_POSITION
:
116 interp
= SP_INTERP_POS
;
119 case TGSI_SEMANTIC_COLOR
:
120 if (fsInfo
->input_interpolate
[i
] == TGSI_INTERPOLATE_COLOR
) {
121 if (softpipe
->rasterizer
->flatshade
)
122 interp
= SP_INTERP_CONSTANT
;
124 interp
= SP_INTERP_PERSPECTIVE
;
129 /* this includes texcoords and varying vars */
130 src
= draw_find_shader_output(softpipe
->draw
,
131 fsInfo
->input_semantic_name
[i
],
132 fsInfo
->input_semantic_index
[i
]);
133 if (fsInfo
->input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
&& src
== -1)
135 * try and find a bcolor.
136 * Note that if there's both front and back color, draw will
137 * have copied back to front color already.
139 src
= draw_find_shader_output(softpipe
->draw
,
140 TGSI_SEMANTIC_BCOLOR
,
141 fsInfo
->input_semantic_index
[i
]);
143 sinfo
->attrib
[i
].interp
= interp
;
145 * note src can be -1 if not found. Would need special handling,
146 * (as we don't tell draw anything about it) just force to 0.
147 * It's wrong either way but should be safer...
151 sinfo
->attrib
[i
].src_index
= src
;
154 /* Figure out if we need pointsize as well. */
155 vs_index
= draw_find_shader_output(softpipe
->draw
,
156 TGSI_SEMANTIC_PSIZE
, 0);
159 softpipe
->psize_slot
= vs_index
;
162 /* Figure out if we need viewport index */
163 vs_index
= draw_find_shader_output(softpipe
->draw
,
164 TGSI_SEMANTIC_VIEWPORT_INDEX
,
167 softpipe
->viewport_index_slot
= vs_index
;
170 /* Figure out if we need layer */
171 vs_index
= draw_find_shader_output(softpipe
->draw
,
175 softpipe
->layer_slot
= vs_index
;
177 softpipe
->setup_info
.valid
= 1;
185 * Called from vbuf module.
187 * Note the vertex layout used for vbuf is simply telling it to pass
188 * through everything as is. The mapping actually used for setup is
189 * stored separately (but calculated here too at the same time).
192 softpipe_get_vbuf_vertex_info(struct softpipe_context
*softpipe
)
194 softpipe_compute_vertex_info(softpipe
);
195 return &softpipe
->vertex_info_vbuf
;
200 * Recompute cliprect from scissor bounds, scissor enable and surface size.
203 compute_cliprect(struct softpipe_context
*sp
)
206 /* SP_NEW_FRAMEBUFFER
208 uint surfWidth
= sp
->framebuffer
.width
;
209 uint surfHeight
= sp
->framebuffer
.height
;
211 for (i
= 0; i
< PIPE_MAX_VIEWPORTS
; i
++) {
214 if (sp
->rasterizer
->scissor
) {
218 * clip to scissor rect:
220 sp
->cliprect
[i
].minx
= MAX2(sp
->scissors
[i
].minx
, 0);
221 sp
->cliprect
[i
].miny
= MAX2(sp
->scissors
[i
].miny
, 0);
222 sp
->cliprect
[i
].maxx
= MIN2(sp
->scissors
[i
].maxx
, surfWidth
);
223 sp
->cliprect
[i
].maxy
= MIN2(sp
->scissors
[i
].maxy
, surfHeight
);
226 /* clip to surface bounds */
227 sp
->cliprect
[i
].minx
= 0;
228 sp
->cliprect
[i
].miny
= 0;
229 sp
->cliprect
[i
].maxx
= surfWidth
;
230 sp
->cliprect
[i
].maxy
= surfHeight
;
237 set_shader_sampler(struct softpipe_context
*softpipe
,
242 for (i
= 0; i
<= max_sampler
; i
++) {
243 softpipe
->tgsi
.sampler
[shader
]->sp_sampler
[i
] =
244 (struct sp_sampler
*)(softpipe
->samplers
[shader
][i
]);
249 update_tgsi_samplers( struct softpipe_context
*softpipe
)
253 set_shader_sampler(softpipe
, PIPE_SHADER_VERTEX
,
254 softpipe
->vs
->max_sampler
);
255 set_shader_sampler(softpipe
, PIPE_SHADER_FRAGMENT
,
256 softpipe
->fs_variant
->info
.file_max
[TGSI_FILE_SAMPLER
]);
258 set_shader_sampler(softpipe
, PIPE_SHADER_GEOMETRY
,
259 softpipe
->gs
->max_sampler
);
262 /* XXX is this really necessary here??? */
263 for (sh
= 0; sh
< Elements(softpipe
->tex_cache
); sh
++) {
264 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
265 struct softpipe_tex_tile_cache
*tc
= softpipe
->tex_cache
[sh
][i
];
266 if (tc
&& tc
->texture
) {
267 struct softpipe_resource
*spt
= softpipe_resource(tc
->texture
);
268 if (spt
->timestamp
!= tc
->timestamp
) {
269 sp_tex_tile_cache_validate_texture( tc
);
271 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
273 tc
->timestamp
= spt
->timestamp
;
282 update_fragment_shader(struct softpipe_context
*softpipe
, unsigned prim
)
284 struct sp_fragment_shader_variant_key key
;
286 memset(&key
, 0, sizeof(key
));
288 if (prim
== PIPE_PRIM_TRIANGLES
)
289 key
.polygon_stipple
= softpipe
->rasterizer
->poly_stipple_enable
;
292 softpipe
->fs_variant
= softpipe_find_fs_variant(softpipe
,
295 /* prepare the TGSI interpreter for FS execution */
296 softpipe
->fs_variant
->prepare(softpipe
->fs_variant
,
297 softpipe
->fs_machine
,
298 (struct tgsi_sampler
*) softpipe
->
299 tgsi
.sampler
[PIPE_SHADER_FRAGMENT
]);
302 softpipe
->fs_variant
= NULL
;
305 /* This would be the logical place to pass the fragment shader
306 * to the draw module. However, doing this here, during state
307 * validation, causes problems with the 'draw' module helpers for
308 * wide/AA/stippled lines.
309 * In principle, the draw's fragment shader should be per-variant
310 * but that doesn't work. So we use a single draw fragment shader
311 * per fragment shader, not per variant.
314 if (softpipe
->fs_variant
) {
315 draw_bind_fragment_shader(softpipe
->draw
,
316 softpipe
->fs_variant
->draw_shader
);
319 draw_bind_fragment_shader(softpipe
->draw
, NULL
);
326 * This should be called when the polygon stipple pattern changes.
327 * We create a new texture from the stipple pattern and create a new
331 update_polygon_stipple_pattern(struct softpipe_context
*softpipe
)
333 struct pipe_resource
*tex
;
334 struct pipe_sampler_view
*view
;
336 tex
= util_pstipple_create_stipple_texture(&softpipe
->pipe
,
337 softpipe
->poly_stipple
.stipple
);
338 pipe_resource_reference(&softpipe
->pstipple
.texture
, tex
);
339 pipe_resource_reference(&tex
, NULL
);
341 view
= util_pstipple_create_sampler_view(&softpipe
->pipe
,
342 softpipe
->pstipple
.texture
);
343 pipe_sampler_view_reference(&softpipe
->pstipple
.sampler_view
, view
);
344 pipe_sampler_view_reference(&view
, NULL
);
349 * Should be called when polygon stipple is enabled/disabled or when
350 * the fragment shader changes.
351 * We add/update the fragment sampler and sampler views to sample from
352 * the polygon stipple texture. The texture unit that we use depends on
353 * the fragment shader (we need to use a unit not otherwise used by the
357 update_polygon_stipple_enable(struct softpipe_context
*softpipe
, unsigned prim
)
359 if (prim
== PIPE_PRIM_TRIANGLES
&&
360 softpipe
->fs_variant
->key
.polygon_stipple
) {
361 const unsigned unit
= softpipe
->fs_variant
->stipple_sampler_unit
;
364 softpipe
->samplers
[PIPE_SHADER_FRAGMENT
][unit
] = softpipe
->pstipple
.sampler
;
366 /* sampler view state */
367 softpipe_set_sampler_views(&softpipe
->pipe
, PIPE_SHADER_FRAGMENT
,
368 unit
, 1, &softpipe
->pstipple
.sampler_view
);
370 softpipe
->dirty
|= SP_NEW_SAMPLER
;
375 /* Hopefully this will remain quite simple, otherwise need to pull in
376 * something like the state tracker mechanism.
379 softpipe_update_derived(struct softpipe_context
*softpipe
, unsigned prim
)
381 struct softpipe_screen
*sp_screen
= softpipe_screen(softpipe
->pipe
.screen
);
383 /* Check for updated textures.
385 if (softpipe
->tex_timestamp
!= sp_screen
->timestamp
) {
386 softpipe
->tex_timestamp
= sp_screen
->timestamp
;
387 softpipe
->dirty
|= SP_NEW_TEXTURE
;
390 #if DO_PSTIPPLE_IN_HELPER_MODULE
391 if (softpipe
->dirty
& SP_NEW_STIPPLE
)
392 /* before updating samplers! */
393 update_polygon_stipple_pattern(softpipe
);
396 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
398 update_fragment_shader(softpipe
, prim
);
400 #if DO_PSTIPPLE_IN_HELPER_MODULE
401 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
404 update_polygon_stipple_enable(softpipe
, prim
);
407 /* TODO: this looks suboptimal */
408 if (softpipe
->dirty
& (SP_NEW_SAMPLER
|
412 update_tgsi_samplers( softpipe
);
414 if (softpipe
->dirty
& (SP_NEW_RASTERIZER
|
417 invalidate_vertex_layout( softpipe
);
419 if (softpipe
->dirty
& (SP_NEW_SCISSOR
|
422 compute_cliprect(softpipe
);
424 if (softpipe
->dirty
& (SP_NEW_BLEND
|
425 SP_NEW_DEPTH_STENCIL_ALPHA
|
429 sp_build_quad_pipeline(softpipe
);