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. */
24 #include "tgsi/tgsi_dump.h"
26 #include "r300_context.h"
27 #include "r300_screen.h"
29 #include "r300_tgsi_to_rc.h"
31 #include "radeon_code.h"
32 #include "radeon_compiler.h"
34 static void find_output_registers(struct r300_fragment_program_compiler
* compiler
,
35 struct r300_fragment_shader
* fs
)
39 /* Mark the outputs as not present initially */
40 compiler
->OutputColor
= fs
->info
.num_outputs
;
41 compiler
->OutputDepth
= fs
->info
.num_outputs
;
43 /* Now see where they really are. */
44 for(i
= 0; i
< fs
->info
.num_outputs
; ++i
) {
45 switch(fs
->info
.output_semantic_name
[i
]) {
46 case TGSI_SEMANTIC_COLOR
:
47 compiler
->OutputColor
= i
;
49 case TGSI_SEMANTIC_POSITION
:
50 compiler
->OutputDepth
= i
;
56 static void allocate_hardware_inputs(
57 struct r300_fragment_program_compiler
* c
,
58 void (*allocate
)(void * data
, unsigned input
, unsigned hwreg
),
61 struct tgsi_shader_info
* info
= &((struct r300_fragment_shader
*)c
->UserData
)->info
;
64 int total_generic
= 0;
68 for (i
= 0; i
< info
->num_inputs
; i
++) {
69 switch (info
->input_semantic_name
[i
]) {
70 case TGSI_SEMANTIC_COLOR
:
73 case TGSI_SEMANTIC_FOG
:
74 case TGSI_SEMANTIC_GENERIC
:
80 for(i
= 0; i
< info
->num_inputs
; i
++) {
81 switch (info
->input_semantic_name
[i
]) {
82 case TGSI_SEMANTIC_COLOR
:
83 allocate(mydata
, i
, colors
);
86 case TGSI_SEMANTIC_FOG
:
87 case TGSI_SEMANTIC_GENERIC
:
88 allocate(mydata
, i
, total_colors
+ generic
);
95 void r300_translate_fragment_shader(struct r300_context
* r300
,
96 struct r300_fragment_shader
* fs
)
98 struct r300_fragment_program_compiler compiler
;
99 struct tgsi_to_rc ttr
;
101 memset(&compiler
, 0, sizeof(compiler
));
102 rc_init(&compiler
.Base
);
103 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_FP
);
105 compiler
.code
= &fs
->code
;
106 compiler
.is_r500
= r300_screen(r300
->context
.screen
)->caps
->is_r500
;
107 compiler
.AllocateHwInputs
= &allocate_hardware_inputs
;
108 compiler
.UserData
= fs
;
110 /* TODO: Program compilation depends on texture compare modes,
111 * which are sampler state. Therefore, programs need to be recompiled
112 * depending on this state as in the classic Mesa driver.
114 * This is not yet handled correctly.
117 find_output_registers(&compiler
, fs
);
119 if (compiler
.Base
.Debug
) {
120 debug_printf("r300: Initial fragment program\n");
121 tgsi_dump(fs
->state
.tokens
, 0);
124 /* Translate TGSI to our internal representation */
125 ttr
.compiler
= &compiler
.Base
;
126 ttr
.info
= &fs
->info
;
128 r300_tgsi_to_rc(&ttr
, fs
->state
.tokens
);
130 /* Invoke the compiler */
131 r3xx_compile_fragment_program(&compiler
);
132 if (compiler
.Base
.Error
) {
133 /* XXX failover maybe? */
134 DBG(r300
, DBG_FP
, "r300: Error compiling fragment program: %s\n",
135 compiler
.Base
.ErrorMsg
);
138 /* And, finally... */
139 rc_destroy(&compiler
.Base
);
140 fs
->translated
= TRUE
;