2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Joakim Sindholt <opensource@zhasha.com>
4 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
25 #include "util/u_math.h"
26 #include "util/u_memory.h"
28 #include "tgsi/tgsi_dump.h"
29 #include "tgsi/tgsi_ureg.h"
31 #include "r300_context.h"
32 #include "r300_screen.h"
34 #include "r300_tgsi_to_rc.h"
36 #include "radeon_code.h"
37 #include "radeon_compiler.h"
39 /* Convert info about FS input semantics to r300_shader_semantics. */
40 void r300_shader_read_fs_inputs(struct tgsi_shader_info
* info
,
41 struct r300_shader_semantics
* fs_inputs
)
46 r300_shader_semantics_reset(fs_inputs
);
48 for (i
= 0; i
< info
->num_inputs
; i
++) {
49 index
= info
->input_semantic_index
[i
];
51 switch (info
->input_semantic_name
[i
]) {
52 case TGSI_SEMANTIC_COLOR
:
53 assert(index
< ATTR_COLOR_COUNT
);
54 fs_inputs
->color
[index
] = i
;
57 case TGSI_SEMANTIC_GENERIC
:
58 assert(index
< ATTR_GENERIC_COUNT
);
59 fs_inputs
->generic
[index
] = i
;
62 case TGSI_SEMANTIC_FOG
:
67 case TGSI_SEMANTIC_POSITION
:
73 fprintf(stderr
, "r300: FP: Unknown input semantic: %i\n",
74 info
->input_semantic_name
[i
]);
79 static void find_output_registers(struct r300_fragment_program_compiler
* compiler
,
80 struct r300_fragment_shader_code
*shader
)
82 unsigned i
, colorbuf_count
= 0;
84 /* Mark the outputs as not present initially */
85 compiler
->OutputColor
[0] = shader
->info
.num_outputs
;
86 compiler
->OutputColor
[1] = shader
->info
.num_outputs
;
87 compiler
->OutputColor
[2] = shader
->info
.num_outputs
;
88 compiler
->OutputColor
[3] = shader
->info
.num_outputs
;
89 compiler
->OutputDepth
= shader
->info
.num_outputs
;
91 /* Now see where they really are. */
92 for(i
= 0; i
< shader
->info
.num_outputs
; ++i
) {
93 switch(shader
->info
.output_semantic_name
[i
]) {
94 case TGSI_SEMANTIC_COLOR
:
95 compiler
->OutputColor
[colorbuf_count
] = i
;
98 case TGSI_SEMANTIC_POSITION
:
99 compiler
->OutputDepth
= i
;
105 static void allocate_hardware_inputs(
106 struct r300_fragment_program_compiler
* c
,
107 void (*allocate
)(void * data
, unsigned input
, unsigned hwreg
),
110 struct r300_shader_semantics
* inputs
=
111 (struct r300_shader_semantics
*)c
->UserData
;
114 /* Allocate input registers. */
115 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
116 if (inputs
->color
[i
] != ATTR_UNUSED
) {
117 allocate(mydata
, inputs
->color
[i
], reg
++);
120 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
121 if (inputs
->generic
[i
] != ATTR_UNUSED
) {
122 allocate(mydata
, inputs
->generic
[i
], reg
++);
125 if (inputs
->fog
!= ATTR_UNUSED
) {
126 allocate(mydata
, inputs
->fog
, reg
++);
128 if (inputs
->wpos
!= ATTR_UNUSED
) {
129 allocate(mydata
, inputs
->wpos
, reg
++);
133 static void get_compare_state(
134 struct r300_context
* r300
,
135 struct r300_fragment_program_external_state
* state
,
136 unsigned shadow_samplers
)
138 struct r300_textures_state
*texstate
=
139 (struct r300_textures_state
*)r300
->textures_state
.state
;
141 memset(state
, 0, sizeof(*state
));
143 for (int i
= 0; i
< texstate
->sampler_state_count
; i
++) {
144 struct r300_sampler_state
* s
= texstate
->sampler_states
[i
];
146 if (s
&& s
->state
.compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
147 /* XXX Gallium doesn't provide us with any information regarding
148 * this mode, so we are screwed. I'm setting 0 = LUMINANCE. */
149 state
->unit
[i
].depth_texture_mode
= 0;
151 /* Fortunately, no need to translate this. */
152 state
->unit
[i
].texture_compare_func
= s
->state
.compare_func
;
157 static void r300_translate_fragment_shader(
158 struct r300_context
* r300
,
159 struct r300_fragment_shader_code
* shader
,
160 const struct tgsi_token
*tokens
);
162 static void r300_dummy_fragment_shader(
163 struct r300_context
* r300
,
164 struct r300_fragment_shader_code
* shader
)
166 struct pipe_shader_state state
;
167 struct ureg_program
*ureg
;
171 /* Make a simple fragment shader which outputs (0, 0, 0, 1) */
172 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
173 out
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
174 imm
= ureg_imm4f(ureg
, 0, 0, 0, 1);
176 ureg_MOV(ureg
, out
, imm
);
179 state
.tokens
= ureg_finalize(ureg
);
181 shader
->dummy
= TRUE
;
182 r300_translate_fragment_shader(r300
, shader
, state
.tokens
);
187 static void r300_translate_fragment_shader(
188 struct r300_context
* r300
,
189 struct r300_fragment_shader_code
* shader
,
190 const struct tgsi_token
*tokens
)
192 struct r300_fragment_program_compiler compiler
;
193 struct tgsi_to_rc ttr
;
196 tgsi_scan_shader(tokens
, &shader
->info
);
197 r300_shader_read_fs_inputs(&shader
->info
, &shader
->inputs
);
199 wpos
= shader
->inputs
.wpos
;
201 /* Setup the compiler. */
202 memset(&compiler
, 0, sizeof(compiler
));
203 rc_init(&compiler
.Base
);
204 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_FP
);
206 compiler
.code
= &shader
->code
;
207 compiler
.state
= shader
->compare_state
;
208 compiler
.is_r500
= r300
->screen
->caps
.is_r500
;
209 compiler
.max_temp_regs
= compiler
.is_r500
? 128 : 32;
210 compiler
.AllocateHwInputs
= &allocate_hardware_inputs
;
211 compiler
.UserData
= &shader
->inputs
;
213 find_output_registers(&compiler
, shader
);
215 if (compiler
.Base
.Debug
) {
216 debug_printf("r300: Initial fragment program\n");
217 tgsi_dump(tokens
, 0);
220 /* Translate TGSI to our internal representation */
221 ttr
.compiler
= &compiler
.Base
;
222 ttr
.info
= &shader
->info
;
223 ttr
.use_half_swizzles
= TRUE
;
225 r300_tgsi_to_rc(&ttr
, tokens
);
227 shader
->shadow_samplers
= compiler
.Base
.Program
.ShadowSamplers
;
230 * Transform the program to support WPOS.
232 * Introduce a small fragment at the start of the program that will be
233 * the only code that directly reads the WPOS input.
234 * All other code pieces that reference that input will be rewritten
235 * to read from a newly allocated temporary. */
236 if (wpos
!= ATTR_UNUSED
) {
237 /* Moving the input to some other reg is not really necessary. */
238 rc_transform_fragment_wpos(&compiler
.Base
, wpos
, wpos
, TRUE
);
241 /* Invoke the compiler */
242 r3xx_compile_fragment_program(&compiler
);
243 if (compiler
.Base
.Error
) {
244 fprintf(stderr
, "r300 FP: Compiler Error:\n%sUsing a dummy shader"
245 " instead.\n", compiler
.Base
.ErrorMsg
);
248 fprintf(stderr
, "r300 FP: Cannot compile the dummy shader! "
252 r300_dummy_fragment_shader(r300
, shader
);
255 /* And, finally... */
256 rc_destroy(&compiler
.Base
);
259 boolean
r300_pick_fragment_shader(struct r300_context
* r300
)
261 struct r300_fragment_shader
* fs
= r300
->fs
;
262 struct r300_fragment_program_external_state state
;
263 struct r300_fragment_shader_code
* ptr
;
266 /* Build the fragment shader for the first time. */
267 fs
->first
= fs
->shader
= CALLOC_STRUCT(r300_fragment_shader_code
);
269 /* BTW shadow samplers will be known after the first translation,
270 * therefore we set ~0, which means it should look at all sampler
271 * states. This choice doesn't have any impact on the correctness. */
272 get_compare_state(r300
, &fs
->shader
->compare_state
, ~0);
273 r300_translate_fragment_shader(r300
, fs
->shader
, fs
->state
.tokens
);
274 fs
->shadow_samplers
= fs
->shader
->shadow_samplers
;
277 } else if (fs
->shadow_samplers
) {
278 get_compare_state(r300
, &state
, fs
->shadow_samplers
);
280 /* Check if the currently-bound shader has been compiled
281 * with the texture-compare state we need. */
282 if (memcmp(&fs
->shader
->compare_state
, &state
, sizeof(state
)) != 0) {
283 /* Search for the right shader. */
286 if (memcmp(&ptr
->compare_state
, &state
, sizeof(state
)) == 0) {
293 /* Not found, gotta compile a new one. */
294 ptr
= CALLOC_STRUCT(r300_fragment_shader_code
);
295 ptr
->next
= fs
->first
;
296 fs
->first
= fs
->shader
= ptr
;
298 ptr
->compare_state
= state
;
299 r300_translate_fragment_shader(r300
, ptr
, fs
->state
.tokens
);