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"
30 #include "r300_context.h"
31 #include "r300_screen.h"
33 #include "r300_tgsi_to_rc.h"
35 #include "radeon_code.h"
36 #include "radeon_compiler.h"
38 /* Convert info about FS input semantics to r300_shader_semantics. */
39 void r300_shader_read_fs_inputs(struct tgsi_shader_info
* info
,
40 struct r300_shader_semantics
* fs_inputs
)
45 r300_shader_semantics_reset(fs_inputs
);
47 for (i
= 0; i
< info
->num_inputs
; i
++) {
48 index
= info
->input_semantic_index
[i
];
50 switch (info
->input_semantic_name
[i
]) {
51 case TGSI_SEMANTIC_COLOR
:
52 assert(index
<= ATTR_COLOR_COUNT
);
53 fs_inputs
->color
[index
] = i
;
56 case TGSI_SEMANTIC_GENERIC
:
57 assert(index
<= ATTR_GENERIC_COUNT
);
58 fs_inputs
->generic
[index
] = i
;
61 case TGSI_SEMANTIC_FOG
:
72 static void find_output_registers(struct r300_fragment_program_compiler
* compiler
,
73 struct r300_fragment_shader
* fs
)
77 /* Mark the outputs as not present initially */
78 compiler
->OutputColor
= fs
->info
.num_outputs
;
79 compiler
->OutputDepth
= fs
->info
.num_outputs
;
81 /* Now see where they really are. */
82 for(i
= 0; i
< fs
->info
.num_outputs
; ++i
) {
83 switch(fs
->info
.output_semantic_name
[i
]) {
84 case TGSI_SEMANTIC_COLOR
:
85 compiler
->OutputColor
= i
;
87 case TGSI_SEMANTIC_POSITION
:
88 compiler
->OutputDepth
= i
;
94 static void allocate_hardware_inputs(
95 struct r300_fragment_program_compiler
* c
,
96 void (*allocate
)(void * data
, unsigned input
, unsigned hwreg
),
99 struct r300_shader_semantics
* inputs
=
100 (struct r300_shader_semantics
*)c
->UserData
;
103 /* Allocate input registers. */
104 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
105 if (inputs
->color
[i
] != ATTR_UNUSED
) {
106 allocate(mydata
, inputs
->color
[i
], reg
++);
109 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
110 if (inputs
->generic
[i
] != ATTR_UNUSED
) {
111 allocate(mydata
, inputs
->generic
[i
], reg
++);
114 if (inputs
->fog
!= ATTR_UNUSED
) {
115 allocate(mydata
, inputs
->fog
, reg
++);
119 static void get_compare_state(
120 struct r300_context
* r300
,
121 struct r300_fragment_program_external_state
* state
,
122 unsigned shadow_samplers
)
124 memset(state
, 0, sizeof(*state
));
126 for (int i
= 0; i
< r300
->sampler_count
; i
++) {
127 struct r300_sampler_state
* s
= r300
->sampler_states
[i
];
129 if (s
&& s
->state
.compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
130 /* XXX Gallium doesn't provide us with any information regarding
131 * this mode, so we are screwed. I'm setting 0 = LUMINANCE. */
132 state
->unit
[i
].depth_texture_mode
= 0;
134 /* Fortunately, no need to translate this. */
135 state
->unit
[i
].texture_compare_func
= s
->state
.compare_func
;
140 static void r300_translate_fragment_shader(
141 struct r300_context
* r300
,
142 struct r300_fragment_shader_code
* shader
)
144 struct r300_fragment_shader
* fs
= r300
->fs
;
145 struct r300_fragment_program_compiler compiler
;
146 struct tgsi_to_rc ttr
;
148 /* Setup the compiler. */
149 memset(&compiler
, 0, sizeof(compiler
));
150 rc_init(&compiler
.Base
);
151 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_FP
);
153 compiler
.code
= &shader
->code
;
154 compiler
.state
= shader
->compare_state
;
155 compiler
.is_r500
= r300_screen(r300
->context
.screen
)->caps
->is_r500
;
156 compiler
.AllocateHwInputs
= &allocate_hardware_inputs
;
157 compiler
.UserData
= &fs
->inputs
;
159 find_output_registers(&compiler
, fs
);
161 if (compiler
.Base
.Debug
) {
162 debug_printf("r300: Initial fragment program\n");
163 tgsi_dump(fs
->state
.tokens
, 0);
166 /* Translate TGSI to our internal representation */
167 ttr
.compiler
= &compiler
.Base
;
168 ttr
.info
= &fs
->info
;
170 r300_tgsi_to_rc(&ttr
, fs
->state
.tokens
);
172 fs
->shadow_samplers
= compiler
.Base
.Program
.ShadowSamplers
;
174 /* Invoke the compiler */
175 r3xx_compile_fragment_program(&compiler
);
176 if (compiler
.Base
.Error
) {
177 /* XXX failover maybe? */
178 DBG(r300
, DBG_FP
, "r300: Error compiling fragment program: %s\n",
179 compiler
.Base
.ErrorMsg
);
183 /* And, finally... */
184 rc_destroy(&compiler
.Base
);
187 boolean
r300_pick_fragment_shader(struct r300_context
* r300
)
189 struct r300_fragment_shader
* fs
= r300
->fs
;
190 struct r300_fragment_program_external_state state
;
191 struct r300_fragment_shader_code
* ptr
;
194 /* Build the fragment shader for the first time. */
195 fs
->first
= fs
->shader
= CALLOC_STRUCT(r300_fragment_shader_code
);
197 /* BTW shadow samplers will be known after the first translation,
198 * therefore we set ~0, which means it should look at all sampler
199 * states. This choice doesn't have any impact on the correctness. */
200 get_compare_state(r300
, &fs
->shader
->compare_state
, ~0);
201 r300_translate_fragment_shader(r300
, fs
->shader
);
204 } else if (fs
->shadow_samplers
) {
205 get_compare_state(r300
, &state
, fs
->shadow_samplers
);
207 /* Check if the currently-bound shader has been compiled
208 * with the texture-compare state we need. */
209 if (memcmp(&fs
->shader
->compare_state
, &state
, sizeof(state
)) != 0) {
210 /* Search for the right shader. */
213 if (memcmp(&ptr
->compare_state
, &state
, sizeof(state
)) == 0) {
220 /* Not found, gotta compile a new one. */
221 ptr
= CALLOC_STRUCT(r300_fragment_shader_code
);
222 ptr
->next
= fs
->first
;
223 fs
->first
= fs
->shader
= ptr
;
225 ptr
->compare_state
= state
;
226 r300_translate_fragment_shader(r300
, ptr
);