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"
48 #include "sp_texture.h"
49 #include "sp_tex_sample.h"
52 struct quad_shade_stage
54 struct quad_stage stage
; /**< base class */
55 struct tgsi_exec_machine
*machine
;
56 struct tgsi_exec_vector
*inputs
, *outputs
;
61 static INLINE
struct quad_shade_stage
*
62 quad_shade_stage(struct quad_stage
*qs
)
64 return (struct quad_shade_stage
*) qs
;
69 * Execute fragment shader for the four fragments in the quad.
72 shade_quad(struct quad_stage
*qs
, struct quad_header
*quad
)
74 struct quad_shade_stage
*qss
= quad_shade_stage( qs
);
75 struct softpipe_context
*softpipe
= qs
->softpipe
;
76 struct tgsi_exec_machine
*machine
= qss
->machine
;
79 /* Consts do not require 16 byte alignment. */
80 machine
->Consts
= softpipe
->mapped_constants
[PIPE_SHADER_FRAGMENT
];
82 machine
->InterpCoefs
= quad
->coef
;
85 quad
->inout
.mask
&= softpipe
->fs
->run( softpipe
->fs
, machine
, quad
);
90 const ubyte
*sem_name
= softpipe
->fs
->info
.output_semantic_name
;
91 const ubyte
*sem_index
= softpipe
->fs
->info
.output_semantic_index
;
92 const uint n
= qss
->stage
.softpipe
->fs
->info
.num_outputs
;
94 for (i
= 0; i
< n
; i
++) {
95 switch (sem_name
[i
]) {
96 case TGSI_SEMANTIC_COLOR
:
98 uint cbuf
= sem_index
[i
];
99 memcpy(quad
->output
.color
[cbuf
],
100 &machine
->Outputs
[i
].xyzw
[0].f
[0],
101 sizeof(quad
->output
.color
[0]) );
104 case TGSI_SEMANTIC_POSITION
:
107 for (j
= 0; j
< 4; j
++) {
108 quad
->output
.depth
[j
] = machine
->Outputs
[0].xyzw
[2].f
[j
];
118 /* compute Z values now, as in the quad earlyz stage */
119 /* XXX we should really only do this if the earlyz stage is not used */
120 const float fx
= (float) quad
->input
.x0
;
121 const float fy
= (float) quad
->input
.y0
;
122 const float dzdx
= quad
->posCoef
->dadx
[2];
123 const float dzdy
= quad
->posCoef
->dady
[2];
124 const float z0
= quad
->posCoef
->a0
[2] + dzdx
* fx
+ dzdy
* fy
;
126 quad
->output
.depth
[0] = z0
;
127 quad
->output
.depth
[1] = z0
+ dzdx
;
128 quad
->output
.depth
[2] = z0
+ dzdy
;
129 quad
->output
.depth
[3] = z0
+ dzdx
+ dzdy
;
132 /* shader may cull fragments */
133 if (quad
->inout
.mask
) {
134 qs
->next
->run( qs
->next
, quad
);
140 * Per-primitive (or per-begin?) setup
143 shade_begin(struct quad_stage
*qs
)
145 struct quad_shade_stage
*qss
= quad_shade_stage(qs
);
146 struct softpipe_context
*softpipe
= qs
->softpipe
;
148 softpipe
->fs
->prepare( softpipe
->fs
,
150 (struct tgsi_sampler
**)
151 softpipe
->tgsi
.frag_samplers_list
);
153 qs
->next
->begin(qs
->next
);
158 shade_destroy(struct quad_stage
*qs
)
160 struct quad_shade_stage
*qss
= (struct quad_shade_stage
*) qs
;
162 tgsi_exec_machine_destroy(qss
->machine
);
169 sp_quad_shade_stage( struct softpipe_context
*softpipe
)
171 struct quad_shade_stage
*qss
= CALLOC_STRUCT(quad_shade_stage
);
175 qss
->stage
.softpipe
= softpipe
;
176 qss
->stage
.begin
= shade_begin
;
177 qss
->stage
.run
= shade_quad
;
178 qss
->stage
.destroy
= shade_destroy
;
180 qss
->machine
= tgsi_exec_machine_create();
187 if (qss
&& qss
->machine
)
188 tgsi_exec_machine_destroy(qss
->machine
);