1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2008 VMware, Inc. All rights reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 /* Vertices are just an array of floats, with all the attributes
30 * packed. We currently assume a layout like:
32 * attr[0][0..3] - window position
33 * attr[1..n][0..3] - remaining attributes.
35 * Attributes are assumed to be 4 floats wide but are packed so that
36 * all the enabled attributes run contiguously.
39 #include "util/u_math.h"
40 #include "util/u_memory.h"
41 #include "pipe/p_defines.h"
42 #include "pipe/p_shader_tokens.h"
44 #include "sp_context.h"
47 #include "sp_quad_pipe.h"
50 struct quad_shade_stage
52 struct quad_stage stage
; /**< base class */
53 struct tgsi_exec_machine
*machine
;
54 struct tgsi_exec_vector
*inputs
, *outputs
;
59 static INLINE
struct quad_shade_stage
*
60 quad_shade_stage(struct quad_stage
*qs
)
62 return (struct quad_shade_stage
*) qs
;
67 * Execute fragment shader for the four fragments in the quad.
68 * \return TRUE if quad is alive, FALSE if all four pixels are killed
71 shade_quad(struct quad_stage
*qs
, struct quad_header
*quad
)
73 struct quad_shade_stage
*qss
= quad_shade_stage( qs
);
74 struct softpipe_context
*softpipe
= qs
->softpipe
;
75 struct tgsi_exec_machine
*machine
= qss
->machine
;
78 return softpipe
->fs
->run( softpipe
->fs
, machine
, quad
);
84 coverage_quad(struct quad_stage
*qs
, struct quad_header
*quad
)
86 struct softpipe_context
*softpipe
= qs
->softpipe
;
89 /* loop over colorbuffer outputs */
90 for (cbuf
= 0; cbuf
< softpipe
->framebuffer
.nr_cbufs
; cbuf
++) {
91 float (*quadColor
)[4] = quad
->output
.color
[cbuf
];
93 for (j
= 0; j
< QUAD_SIZE
; j
++) {
94 assert(quad
->input
.coverage
[j
] >= 0.0);
95 assert(quad
->input
.coverage
[j
] <= 1.0);
96 quadColor
[3][j
] *= quad
->input
.coverage
[j
];
103 * Shade/write an array of quads
104 * Called via quad_stage::run()
107 shade_quads(struct quad_stage
*qs
,
108 struct quad_header
*quads
[],
111 struct quad_shade_stage
*qss
= quad_shade_stage( qs
);
112 struct softpipe_context
*softpipe
= qs
->softpipe
;
113 struct tgsi_exec_machine
*machine
= qss
->machine
;
114 unsigned i
, pass
= 0;
116 for (i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
117 machine
->Consts
[i
] = softpipe
->mapped_constants
[PIPE_SHADER_FRAGMENT
][i
];
119 machine
->InterpCoefs
= quads
[0]->coef
;
121 for (i
= 0; i
< nr
; i
++) {
122 if (!shade_quad(qs
, quads
[i
]))
123 continue; /* quad totally culled/killed */
125 if (/*do_coverage*/ 0)
126 coverage_quad( qs
, quads
[i
] );
128 quads
[pass
++] = quads
[i
];
132 qs
->next
->run(qs
->next
, quads
, pass
);
137 * Per-primitive (or per-begin?) setup
140 shade_begin(struct quad_stage
*qs
)
142 struct quad_shade_stage
*qss
= quad_shade_stage(qs
);
143 struct softpipe_context
*softpipe
= qs
->softpipe
;
145 softpipe
->fs
->prepare( softpipe
->fs
,
147 (struct tgsi_sampler
**)
148 softpipe
->tgsi
.frag_samplers_list
);
150 qs
->next
->begin(qs
->next
);
155 shade_destroy(struct quad_stage
*qs
)
157 struct quad_shade_stage
*qss
= (struct quad_shade_stage
*) qs
;
159 tgsi_exec_machine_destroy(qss
->machine
);
166 sp_quad_shade_stage( struct softpipe_context
*softpipe
)
168 struct quad_shade_stage
*qss
= CALLOC_STRUCT(quad_shade_stage
);
172 qss
->stage
.softpipe
= softpipe
;
173 qss
->stage
.begin
= shade_begin
;
174 qss
->stage
.run
= shade_quads
;
175 qss
->stage
.destroy
= shade_destroy
;
177 qss
->machine
= tgsi_exec_machine_create();
184 if (qss
&& qss
->machine
)
185 tgsi_exec_machine_destroy(qss
->machine
);