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"
35 #include "r300_tgsi_to_rc.h"
37 #include "radeon_code.h"
38 #include "radeon_compiler.h"
40 /* Convert info about FS input semantics to r300_shader_semantics. */
41 void r300_shader_read_fs_inputs(struct tgsi_shader_info
* info
,
42 struct r300_shader_semantics
* fs_inputs
)
47 r300_shader_semantics_reset(fs_inputs
);
49 for (i
= 0; i
< info
->num_inputs
; i
++) {
50 index
= info
->input_semantic_index
[i
];
52 switch (info
->input_semantic_name
[i
]) {
53 case TGSI_SEMANTIC_COLOR
:
54 assert(index
< ATTR_COLOR_COUNT
);
55 fs_inputs
->color
[index
] = i
;
58 case TGSI_SEMANTIC_GENERIC
:
59 assert(index
< ATTR_GENERIC_COUNT
);
60 fs_inputs
->generic
[index
] = i
;
63 case TGSI_SEMANTIC_FOG
:
68 case TGSI_SEMANTIC_POSITION
:
74 fprintf(stderr
, "r300: FP: Unknown input semantic: %i\n",
75 info
->input_semantic_name
[i
]);
80 static void find_output_registers(struct r300_fragment_program_compiler
* compiler
,
81 struct r300_fragment_shader_code
*shader
)
83 unsigned i
, colorbuf_count
= 0;
85 /* Mark the outputs as not present initially */
86 compiler
->OutputColor
[0] = shader
->info
.num_outputs
;
87 compiler
->OutputColor
[1] = shader
->info
.num_outputs
;
88 compiler
->OutputColor
[2] = shader
->info
.num_outputs
;
89 compiler
->OutputColor
[3] = shader
->info
.num_outputs
;
90 compiler
->OutputDepth
= shader
->info
.num_outputs
;
92 /* Now see where they really are. */
93 for(i
= 0; i
< shader
->info
.num_outputs
; ++i
) {
94 switch(shader
->info
.output_semantic_name
[i
]) {
95 case TGSI_SEMANTIC_COLOR
:
96 compiler
->OutputColor
[colorbuf_count
] = i
;
99 case TGSI_SEMANTIC_POSITION
:
100 compiler
->OutputDepth
= i
;
106 static void allocate_hardware_inputs(
107 struct r300_fragment_program_compiler
* c
,
108 void (*allocate
)(void * data
, unsigned input
, unsigned hwreg
),
111 struct r300_shader_semantics
* inputs
=
112 (struct r300_shader_semantics
*)c
->UserData
;
115 /* Allocate input registers. */
116 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
117 if (inputs
->color
[i
] != ATTR_UNUSED
) {
118 allocate(mydata
, inputs
->color
[i
], reg
++);
121 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
122 if (inputs
->generic
[i
] != ATTR_UNUSED
) {
123 allocate(mydata
, inputs
->generic
[i
], reg
++);
126 if (inputs
->fog
!= ATTR_UNUSED
) {
127 allocate(mydata
, inputs
->fog
, reg
++);
129 if (inputs
->wpos
!= ATTR_UNUSED
) {
130 allocate(mydata
, inputs
->wpos
, reg
++);
134 static void get_external_state(
135 struct r300_context
* r300
,
136 struct r300_fragment_program_external_state
* state
)
138 struct r300_textures_state
*texstate
= r300
->textures_state
.state
;
141 for (i
= 0; i
< texstate
->sampler_state_count
; i
++) {
142 struct r300_sampler_state
* s
= texstate
->sampler_states
[i
];
143 struct r300_texture
*t
;
145 if (!s
|| !texstate
->sampler_views
[i
]) {
149 t
= (struct r300_texture
*)texstate
->sampler_views
[i
]->base
.texture
;
151 if (s
->state
.compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
152 /* XXX Gallium doesn't provide us with any information regarding
153 * this mode, so we are screwed. I'm setting 0 = LUMINANCE. */
154 state
->unit
[i
].depth_texture_mode
= 0;
156 /* Fortunately, no need to translate this. */
157 state
->unit
[i
].texture_compare_func
= s
->state
.compare_func
;
160 state
->unit
[i
].fake_npot
= t
->uses_pitch
;
161 state
->unit
[i
].non_normalized_coords
= !s
->state
.normalized_coords
;
163 /* XXX this should probably take into account STR, not just S. */
164 switch (s
->state
.wrap_s
) {
165 case PIPE_TEX_WRAP_REPEAT
:
166 state
->unit
[i
].wrap_mode
= RC_WRAP_REPEAT
;
168 case PIPE_TEX_WRAP_CLAMP
:
169 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
170 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
171 state
->unit
[i
].wrap_mode
= RC_WRAP_CLAMP
;
173 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
174 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
175 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
176 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
177 state
->unit
[i
].wrap_mode
= RC_WRAP_MIRROR
;
180 state
->unit
[i
].wrap_mode
= RC_WRAP_NONE
;
186 static void r300_translate_fragment_shader(
187 struct r300_context
* r300
,
188 struct r300_fragment_shader_code
* shader
,
189 const struct tgsi_token
*tokens
);
191 static void r300_dummy_fragment_shader(
192 struct r300_context
* r300
,
193 struct r300_fragment_shader_code
* shader
)
195 struct pipe_shader_state state
;
196 struct ureg_program
*ureg
;
200 /* Make a simple fragment shader which outputs (0, 0, 0, 1) */
201 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
202 out
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
203 imm
= ureg_imm4f(ureg
, 0, 0, 0, 1);
205 ureg_MOV(ureg
, out
, imm
);
208 state
.tokens
= ureg_finalize(ureg
);
210 shader
->dummy
= TRUE
;
211 r300_translate_fragment_shader(r300
, shader
, state
.tokens
);
216 static void r300_translate_fragment_shader(
217 struct r300_context
* r300
,
218 struct r300_fragment_shader_code
* shader
,
219 const struct tgsi_token
*tokens
)
221 struct r300_fragment_program_compiler compiler
;
222 struct tgsi_to_rc ttr
;
226 tgsi_scan_shader(tokens
, &shader
->info
);
227 r300_shader_read_fs_inputs(&shader
->info
, &shader
->inputs
);
229 wpos
= shader
->inputs
.wpos
;
231 /* Setup the compiler. */
232 memset(&compiler
, 0, sizeof(compiler
));
233 rc_init(&compiler
.Base
);
234 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_FP
);
236 compiler
.code
= &shader
->code
;
237 compiler
.state
= shader
->compare_state
;
238 compiler
.is_r500
= r300
->screen
->caps
.is_r500
;
239 compiler
.max_temp_regs
= compiler
.is_r500
? 128 : 32;
240 compiler
.AllocateHwInputs
= &allocate_hardware_inputs
;
241 compiler
.UserData
= &shader
->inputs
;
243 find_output_registers(&compiler
, shader
);
245 if (compiler
.Base
.Debug
) {
246 debug_printf("r300: Initial fragment program\n");
247 tgsi_dump(tokens
, 0);
250 /* Translate TGSI to our internal representation */
251 ttr
.compiler
= &compiler
.Base
;
252 ttr
.info
= &shader
->info
;
253 ttr
.use_half_swizzles
= TRUE
;
255 r300_tgsi_to_rc(&ttr
, tokens
);
258 * Transform the program to support WPOS.
260 * Introduce a small fragment at the start of the program that will be
261 * the only code that directly reads the WPOS input.
262 * All other code pieces that reference that input will be rewritten
263 * to read from a newly allocated temporary. */
264 if (wpos
!= ATTR_UNUSED
) {
265 /* Moving the input to some other reg is not really necessary. */
266 rc_transform_fragment_wpos(&compiler
.Base
, wpos
, wpos
, TRUE
);
269 /* Invoke the compiler */
270 r3xx_compile_fragment_program(&compiler
);
272 if (compiler
.Base
.Error
) {
273 fprintf(stderr
, "r300 FP: Compiler Error:\n%sUsing a dummy shader"
274 " instead.\n", compiler
.Base
.ErrorMsg
);
277 fprintf(stderr
, "r300 FP: Cannot compile the dummy shader! "
281 r300_dummy_fragment_shader(r300
, shader
);
284 /* Initialize numbers of constants for each type. */
285 shader
->externals_count
= ttr
.immediate_offset
;
286 shader
->immediates_count
= 0;
287 shader
->rc_state_count
= 0;
289 for (i
= shader
->externals_count
; i
< shader
->code
.constants
.Count
; i
++) {
290 switch (shader
->code
.constants
.Constants
[i
].Type
) {
291 case RC_CONSTANT_IMMEDIATE
:
292 ++shader
->immediates_count
;
294 case RC_CONSTANT_STATE
:
295 ++shader
->rc_state_count
;
302 /* Setup shader depth output. */
303 if (shader
->code
.writes_depth
) {
304 shader
->fg_depth_src
= R300_FG_DEPTH_SRC_SHADER
;
305 shader
->us_out_w
= R300_W_FMT_W24
| R300_W_SRC_US
;
307 shader
->fg_depth_src
= R300_FG_DEPTH_SRC_SCAN
;
308 shader
->us_out_w
= R300_W_FMT_W0
| R300_W_SRC_US
;
311 /* And, finally... */
312 rc_destroy(&compiler
.Base
);
315 boolean
r300_pick_fragment_shader(struct r300_context
* r300
)
317 struct r300_fragment_shader
* fs
= r300_fs(r300
);
318 struct r300_fragment_program_external_state state
= {{{ 0 }}};
319 struct r300_fragment_shader_code
* ptr
;
321 get_external_state(r300
, &state
);
324 /* Build the fragment shader for the first time. */
325 fs
->first
= fs
->shader
= CALLOC_STRUCT(r300_fragment_shader_code
);
327 memcpy(&fs
->shader
->compare_state
, &state
,
328 sizeof(struct r300_fragment_program_external_state
));
329 r300_translate_fragment_shader(r300
, fs
->shader
, fs
->state
.tokens
);
333 /* Check if the currently-bound shader has been compiled
334 * with the texture-compare state we need. */
335 if (memcmp(&fs
->shader
->compare_state
, &state
, sizeof(state
)) != 0) {
336 /* Search for the right shader. */
339 if (memcmp(&ptr
->compare_state
, &state
, sizeof(state
)) == 0) {
346 /* Not found, gotta compile a new one. */
347 ptr
= CALLOC_STRUCT(r300_fragment_shader_code
);
348 ptr
->next
= fs
->first
;
349 fs
->first
= fs
->shader
= ptr
;
351 ptr
->compare_state
= state
;
352 r300_translate_fragment_shader(r300
, ptr
, fs
->state
.tokens
);