2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Joakim Sindholt <opensource@zhasha.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
26 #include "r300_tgsi_to_rc.h"
28 #include "radeon_compiler.h"
30 static void find_output_registers(struct r300_fragment_program_compiler
* compiler
,
31 struct r300_fragment_shader
* fs
)
35 /* Mark the outputs as not present initially */
36 compiler
->OutputColor
= fs
->info
.num_outputs
;
37 compiler
->OutputDepth
= fs
->info
.num_outputs
;
39 /* Now see where they really are. */
40 for(i
= 0; i
< fs
->info
.num_outputs
; ++i
) {
41 switch(fs
->info
.output_semantic_name
[i
]) {
42 case TGSI_SEMANTIC_COLOR
:
43 compiler
->OutputColor
= i
;
45 case TGSI_SEMANTIC_POSITION
:
46 compiler
->OutputDepth
= i
;
52 static void allocate_hardware_inputs(
53 struct r300_fragment_program_compiler
* c
,
54 void (*allocate
)(void * data
, unsigned input
, unsigned hwreg
),
57 struct tgsi_shader_info
* info
= &((struct r300_fragment_shader
*)c
->UserData
)->info
;
60 int total_generic
= 0;
64 for (i
= 0; i
< info
->num_inputs
; i
++) {
65 switch (info
->input_semantic_name
[i
]) {
66 case TGSI_SEMANTIC_COLOR
:
69 case TGSI_SEMANTIC_FOG
:
70 case TGSI_SEMANTIC_GENERIC
:
76 for(i
= 0; i
< info
->num_inputs
; i
++) {
77 switch (info
->input_semantic_name
[i
]) {
78 case TGSI_SEMANTIC_COLOR
:
79 allocate(mydata
, i
, colors
);
82 case TGSI_SEMANTIC_FOG
:
83 case TGSI_SEMANTIC_GENERIC
:
84 allocate(mydata
, i
, total_colors
+ generic
);
91 void r300_translate_fragment_shader(struct r300_context
* r300
,
92 struct r300_fragment_shader
* fs
)
94 struct r300_fragment_program_compiler compiler
;
95 struct tgsi_to_rc ttr
;
97 memset(&compiler
, 0, sizeof(compiler
));
98 rc_init(&compiler
.Base
);
99 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_FP
);
101 compiler
.code
= &fs
->code
;
102 compiler
.is_r500
= r300_screen(r300
->context
.screen
)->caps
->is_r500
;
103 compiler
.AllocateHwInputs
= &allocate_hardware_inputs
;
104 compiler
.UserData
= fs
;
106 /* TODO: Program compilation depends on texture compare modes,
107 * which are sampler state. Therefore, programs need to be recompiled
108 * depending on this state as in the classic Mesa driver.
110 * This is not yet handled correctly.
113 find_output_registers(&compiler
, fs
);
115 if (compiler
.Base
.Debug
) {
116 debug_printf("r300: Initial fragment program\n");
117 tgsi_dump(fs
->state
.tokens
, 0);
120 /* Translate TGSI to our internal representation */
121 ttr
.compiler
= &compiler
.Base
;
122 ttr
.info
= &fs
->info
;
124 r300_tgsi_to_rc(&ttr
, fs
->state
.tokens
);
126 /* Invoke the compiler */
127 r3xx_compile_fragment_program(&compiler
);
128 if (compiler
.Base
.Error
) {
129 /* Todo: Fallback to software rendering gracefully? */
130 fprintf(stderr
, "r300 FP: Compiler error: %s\n", compiler
.Base
.ErrorMsg
);
132 if (compiler
.is_r500
) {
133 memcpy(compiler
.code
, &r5xx_passthrough_fragment_shader
, sizeof(r5xx_passthrough_fragment_shader
));
135 memcpy(compiler
.code
, &r3xx_passthrough_fragment_shader
, sizeof(r3xx_passthrough_fragment_shader
));
139 /* And, finally... */
140 rc_destroy(&compiler
.Base
);
141 fs
->translated
= TRUE
;