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 "tgsi/tgsi_dump.h"
27 #include "r300_context.h"
28 #include "r300_screen.h"
30 #include "r300_tgsi_to_rc.h"
32 #include "radeon_code.h"
33 #include "radeon_compiler.h"
35 /* Convert info about FS input semantics to r300_shader_semantics. */
36 static void r300_shader_read_fs_inputs(struct tgsi_shader_info
* info
,
37 struct r300_shader_semantics
* fs_inputs
)
42 r300_shader_semantics_reset(fs_inputs
);
44 for (i
= 0; i
< info
->num_inputs
; i
++) {
45 index
= info
->input_semantic_index
[i
];
47 switch (info
->input_semantic_name
[i
]) {
48 case TGSI_SEMANTIC_COLOR
:
49 assert(index
<= ATTR_COLOR_COUNT
);
50 fs_inputs
->color
[index
] = i
;
53 case TGSI_SEMANTIC_GENERIC
:
54 assert(index
<= ATTR_GENERIC_COUNT
);
55 fs_inputs
->generic
[index
] = i
;
58 case TGSI_SEMANTIC_FOG
:
70 static void find_output_registers(struct r300_fragment_program_compiler
* compiler
,
71 struct r300_fragment_shader
* fs
)
75 /* Mark the outputs as not present initially */
76 compiler
->OutputColor
= fs
->info
.num_outputs
;
77 compiler
->OutputDepth
= fs
->info
.num_outputs
;
79 /* Now see where they really are. */
80 for(i
= 0; i
< fs
->info
.num_outputs
; ++i
) {
81 switch(fs
->info
.output_semantic_name
[i
]) {
82 case TGSI_SEMANTIC_COLOR
:
83 compiler
->OutputColor
= i
;
85 case TGSI_SEMANTIC_POSITION
:
86 compiler
->OutputDepth
= i
;
92 static void allocate_hardware_inputs(
93 struct r300_fragment_program_compiler
* c
,
94 void (*allocate
)(void * data
, unsigned input
, unsigned hwreg
),
97 struct r300_shader_semantics
* inputs
=
98 &((struct r300_fragment_shader
*)c
->UserData
)->inputs
;
101 /* Allocate input registers. */
102 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
103 if (inputs
->color
[i
] != ATTR_UNUSED
) {
104 allocate(mydata
, inputs
->color
[i
], reg
++);
107 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
108 if (inputs
->generic
[i
] != ATTR_UNUSED
) {
109 allocate(mydata
, inputs
->generic
[i
], reg
++);
112 if (inputs
->fog
!= ATTR_UNUSED
) {
113 allocate(mydata
, inputs
->fog
, reg
++);
117 void r300_translate_fragment_shader(struct r300_context
* r300
,
118 struct r300_fragment_shader
* fs
)
120 struct r300_fragment_program_compiler compiler
;
121 struct tgsi_to_rc ttr
;
124 r300_shader_read_fs_inputs(&fs
->info
, &fs
->inputs
);
126 /* Setup the compiler. */
127 memset(&compiler
, 0, sizeof(compiler
));
128 rc_init(&compiler
.Base
);
129 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_FP
);
131 compiler
.code
= &fs
->code
;
132 compiler
.is_r500
= r300_screen(r300
->context
.screen
)->caps
->is_r500
;
133 compiler
.AllocateHwInputs
= &allocate_hardware_inputs
;
134 compiler
.UserData
= fs
;
136 /* XXX: Program compilation depends on texture compare modes,
137 * which are sampler state. Therefore, programs need to be recompiled
138 * depending on this state as in the classic Mesa driver.
140 * This is not yet handled correctly.
143 find_output_registers(&compiler
, fs
);
145 if (compiler
.Base
.Debug
) {
146 debug_printf("r300: Initial fragment program\n");
147 tgsi_dump(fs
->state
.tokens
, 0);
150 /* Translate TGSI to our internal representation */
151 ttr
.compiler
= &compiler
.Base
;
152 ttr
.info
= &fs
->info
;
154 r300_tgsi_to_rc(&ttr
, fs
->state
.tokens
);
156 /* Invoke the compiler */
157 r3xx_compile_fragment_program(&compiler
);
158 if (compiler
.Base
.Error
) {
159 /* XXX failover maybe? */
160 DBG(r300
, DBG_FP
, "r300: Error compiling fragment program: %s\n",
161 compiler
.Base
.ErrorMsg
);
165 /* And, finally... */
166 rc_destroy(&compiler
.Base
);
167 fs
->translated
= TRUE
;