1 /**************************************************************************
3 * Copyright 2007 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 **************************************************************************/
29 * Execute fragment shader using the TGSI interpreter.
32 #include "sp_context.h"
37 #include "pipe/p_state.h"
38 #include "pipe/p_defines.h"
39 #include "util/u_memory.h"
40 #include "tgsi/tgsi_exec.h"
41 #include "tgsi/tgsi_parse.h"
45 * Subclass of sp_fragment_shader_variant
47 struct sp_exec_fragment_shader
49 struct sp_fragment_shader_variant base
;
50 /* No other members for now */
55 exec_prepare( const struct sp_fragment_shader_variant
*var
,
56 struct tgsi_exec_machine
*machine
,
57 struct tgsi_sampler
*sampler
,
58 struct tgsi_image
*image
,
59 struct tgsi_buffer
*buffer
)
62 * Bind tokens/shader to the interpreter's machine state.
64 tgsi_exec_machine_bind_shader(machine
,
66 sampler
, image
, buffer
);
72 * Compute quad X,Y,Z,W for the four fragments in a quad.
74 * This should really be part of the compiled shader.
77 setup_pos_vector(const struct tgsi_interp_coef
*coef
,
79 struct tgsi_exec_vector
*quadpos
)
83 quadpos
->xyzw
[0].f
[0] = x
;
84 quadpos
->xyzw
[0].f
[1] = x
+ 1;
85 quadpos
->xyzw
[0].f
[2] = x
;
86 quadpos
->xyzw
[0].f
[3] = x
+ 1;
89 quadpos
->xyzw
[1].f
[0] = y
;
90 quadpos
->xyzw
[1].f
[1] = y
;
91 quadpos
->xyzw
[1].f
[2] = y
+ 1;
92 quadpos
->xyzw
[1].f
[3] = y
+ 1;
94 /* do Z and W for all fragments in the quad */
95 for (chan
= 2; chan
< 4; chan
++) {
96 const float dadx
= coef
->dadx
[chan
];
97 const float dady
= coef
->dady
[chan
];
98 const float a0
= coef
->a0
[chan
] + dadx
* x
+ dady
* y
;
99 quadpos
->xyzw
[chan
].f
[0] = a0
;
100 quadpos
->xyzw
[chan
].f
[1] = a0
+ dadx
;
101 quadpos
->xyzw
[chan
].f
[2] = a0
+ dady
;
102 quadpos
->xyzw
[chan
].f
[3] = a0
+ dadx
+ dady
;
107 /* TODO: hide the machine struct in here somewhere, remove from this
111 exec_run( const struct sp_fragment_shader_variant
*var
,
112 struct tgsi_exec_machine
*machine
,
113 struct quad_header
*quad
,
114 bool early_depth_test
)
116 /* Compute X, Y, Z, W vals for this quad */
117 setup_pos_vector(quad
->posCoef
,
118 (float)quad
->input
.x0
, (float)quad
->input
.y0
,
121 /* convert 0 to 1.0 and 1 to -1.0 */
122 machine
->Face
= (float) (quad
->input
.facing
* -2 + 1);
124 machine
->NonHelperMask
= quad
->inout
.mask
;
125 quad
->inout
.mask
&= tgsi_exec_machine_run( machine
, 0 );
126 if (quad
->inout
.mask
== 0)
131 const ubyte
*sem_name
= var
->info
.output_semantic_name
;
132 const ubyte
*sem_index
= var
->info
.output_semantic_index
;
133 const uint n
= var
->info
.num_outputs
;
135 for (i
= 0; i
< n
; i
++) {
136 switch (sem_name
[i
]) {
137 case TGSI_SEMANTIC_COLOR
:
139 uint cbuf
= sem_index
[i
];
141 assert(sizeof(quad
->output
.color
[cbuf
]) ==
142 sizeof(machine
->Outputs
[i
]));
144 /* copy float[4][4] result */
145 memcpy(quad
->output
.color
[cbuf
],
146 &machine
->Outputs
[i
],
147 sizeof(quad
->output
.color
[0]) );
150 case TGSI_SEMANTIC_POSITION
:
154 if (!early_depth_test
) {
155 for (j
= 0; j
< 4; j
++)
156 quad
->output
.depth
[j
] = machine
->Outputs
[i
].xyzw
[2].f
[j
];
160 case TGSI_SEMANTIC_STENCIL
:
163 if (!early_depth_test
) {
164 for (j
= 0; j
< 4; j
++)
165 quad
->output
.stencil
[j
] = (unsigned)machine
->Outputs
[i
].xyzw
[1].u
[j
];
178 exec_delete(struct sp_fragment_shader_variant
*var
,
179 struct tgsi_exec_machine
*machine
)
181 if (machine
->Tokens
== var
->tokens
) {
182 tgsi_exec_machine_bind_shader(machine
, NULL
, NULL
, NULL
, NULL
);
185 FREE( (void *) var
->tokens
);
190 struct sp_fragment_shader_variant
*
191 softpipe_create_fs_variant_exec(struct softpipe_context
*softpipe
)
193 struct sp_exec_fragment_shader
*shader
;
195 shader
= CALLOC_STRUCT(sp_exec_fragment_shader
);
199 shader
->base
.prepare
= exec_prepare
;
200 shader
->base
.run
= exec_run
;
201 shader
->base
.delete = exec_delete
;
203 return &shader
->base
;