sp_quad_fs.c revision 835bab0143e11ab98551a061043f944fd6eab456
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2008 VMware, Inc.  All rights reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29/* Vertices are just an array of floats, with all the attributes
30 * packed.  We currently assume a layout like:
31 *
32 * attr[0][0..3] - window position
33 * attr[1..n][0..3] - remaining attributes.
34 *
35 * Attributes are assumed to be 4 floats wide but are packed so that
36 * all the enabled attributes run contiguously.
37 */
38
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"
43
44#include "sp_context.h"
45#include "sp_state.h"
46#include "sp_quad.h"
47#include "sp_quad_pipe.h"
48
49
50struct quad_shade_stage
51{
52   struct quad_stage stage;  /**< base class */
53   struct tgsi_exec_machine *machine;
54   struct tgsi_exec_vector *inputs, *outputs;
55};
56
57
58/** cast wrapper */
59static INLINE struct quad_shade_stage *
60quad_shade_stage(struct quad_stage *qs)
61{
62   return (struct quad_shade_stage *) qs;
63}
64
65
66/**
67 * Execute fragment shader for the four fragments in the quad.
68 */
69static INLINE boolean
70shade_quad(struct quad_stage *qs, struct quad_header *quad)
71{
72   struct quad_shade_stage *qss = quad_shade_stage( qs );
73   struct softpipe_context *softpipe = qs->softpipe;
74   struct tgsi_exec_machine *machine = qss->machine;
75
76   /* run shader */
77   return softpipe->fs->run( softpipe->fs, machine, quad );
78}
79
80
81
82static void
83coverage_quad(struct quad_stage *qs, struct quad_header *quad)
84{
85   struct softpipe_context *softpipe = qs->softpipe;
86   uint cbuf;
87
88   /* loop over colorbuffer outputs */
89   for (cbuf = 0; cbuf < softpipe->framebuffer.nr_cbufs; cbuf++) {
90      float (*quadColor)[4] = quad->output.color[cbuf];
91      unsigned j;
92      for (j = 0; j < QUAD_SIZE; j++) {
93         assert(quad->input.coverage[j] >= 0.0);
94         assert(quad->input.coverage[j] <= 1.0);
95         quadColor[3][j] *= quad->input.coverage[j];
96      }
97   }
98}
99
100
101
102static void
103shade_quads(struct quad_stage *qs,
104                 struct quad_header *quads[],
105                 unsigned nr)
106{
107   struct quad_shade_stage *qss = quad_shade_stage( qs );
108   struct softpipe_context *softpipe = qs->softpipe;
109   struct tgsi_exec_machine *machine = qss->machine;
110   unsigned i, pass = 0;
111
112   for (i = 0; i < PIPE_MAX_CONSTANT; i++) {
113      machine->Consts[i] = softpipe->mapped_constants[PIPE_SHADER_FRAGMENT][i];
114   }
115   machine->InterpCoefs = quads[0]->coef;
116
117   for (i = 0; i < nr; i++) {
118      if (!shade_quad(qs, quads[i]))
119         continue;
120
121      if (/*do_coverage*/ 0)
122         coverage_quad( qs, quads[i] );
123
124      quads[pass++] = quads[i];
125   }
126
127   if (pass)
128      qs->next->run(qs->next, quads, pass);
129}
130
131
132
133
134
135/**
136 * Per-primitive (or per-begin?) setup
137 */
138static void
139shade_begin(struct quad_stage *qs)
140{
141   struct quad_shade_stage *qss = quad_shade_stage(qs);
142   struct softpipe_context *softpipe = qs->softpipe;
143
144   softpipe->fs->prepare( softpipe->fs,
145			  qss->machine,
146			  (struct tgsi_sampler **)
147                             softpipe->tgsi.frag_samplers_list );
148
149   qs->next->begin(qs->next);
150}
151
152
153static void
154shade_destroy(struct quad_stage *qs)
155{
156   struct quad_shade_stage *qss = (struct quad_shade_stage *) qs;
157
158   tgsi_exec_machine_destroy(qss->machine);
159
160   FREE( qs );
161}
162
163
164struct quad_stage *
165sp_quad_shade_stage( struct softpipe_context *softpipe )
166{
167   struct quad_shade_stage *qss = CALLOC_STRUCT(quad_shade_stage);
168   if (!qss)
169      goto fail;
170
171   qss->stage.softpipe = softpipe;
172   qss->stage.begin = shade_begin;
173   qss->stage.run = shade_quads;
174   qss->stage.destroy = shade_destroy;
175
176   qss->machine = tgsi_exec_machine_create();
177   if (!qss->machine)
178      goto fail;
179
180   return &qss->stage;
181
182fail:
183   if (qss && qss->machine)
184      tgsi_exec_machine_destroy(qss->machine);
185
186   FREE(qss);
187   return NULL;
188}
189