2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "r300_state_shader.h"
25 static void r300_copy_passthrough_shader(struct r300_fragment_shader
* fs
)
27 struct r300_fragment_shader
* pt
= &r300_passthrough_fragment_shader
;
28 fs
->shader
.stack_size
= pt
->shader
.stack_size
;
29 fs
->alu_instruction_count
= pt
->alu_instruction_count
;
30 fs
->tex_instruction_count
= pt
->tex_instruction_count
;
31 fs
->indirections
= pt
->indirections
;
32 fs
->instructions
[0] = pt
->instructions
[0];
35 static void r500_copy_passthrough_shader(struct r500_fragment_shader
* fs
)
37 struct r500_fragment_shader
* pt
= &r500_passthrough_fragment_shader
;
38 fs
->shader
.stack_size
= pt
->shader
.stack_size
;
39 fs
->instruction_count
= pt
->instruction_count
;
40 fs
->instructions
[0] = pt
->instructions
[0];
43 static void r300_fs_declare(struct r300_fs_asm
* assembler
,
44 struct tgsi_full_declaration
* decl
)
46 switch (decl
->Declaration
.File
) {
48 switch (decl
->Semantic
.SemanticName
) {
49 case TGSI_SEMANTIC_COLOR
:
50 assembler
->color_count
++;
52 case TGSI_SEMANTIC_GENERIC
:
53 assembler
->tex_count
++;
56 debug_printf("r300: fs: Bad semantic declaration %d\n",
57 decl
->Semantic
.SemanticName
);
61 case TGSI_FILE_OUTPUT
:
64 debug_printf("r300: fs: Bad file %d\n", decl
->Declaration
.File
);
68 assembler
->temp_offset
= assembler
->color_count
+ assembler
->tex_count
;
71 /* XXX cover extended cases */
72 static INLINE
uint32_t r500_rgb_swiz(struct tgsi_src_register
* reg
)
74 uint32_t temp
= reg
->SwizzleX
| (reg
->SwizzleY
<< 3) |
79 static INLINE
uint32_t r500_alpha_swiz(struct tgsi_src_register
* reg
)
84 static INLINE
void r500_emit_mov(struct r500_fragment_shader
* fs
,
85 struct r300_fs_asm
* assembler
,
86 struct tgsi_full_src_register
* src
,
87 struct tgsi_full_dst_register
* dst
)
89 int i
= fs
->instruction_count
;
90 fs
->instructions
[i
].inst0
= R500_INST_TYPE_OUT
|
91 R500_INST_TEX_SEM_WAIT
| R500_INST_LAST
|
92 R500_INST_RGB_OMASK_RGB
| R500_INST_ALPHA_OMASK
|
93 R500_INST_RGB_CLAMP
| R500_INST_ALPHA_CLAMP
;
94 fs
->instructions
[i
].inst1
=
95 R500_RGB_ADDR0(0) | R500_RGB_ADDR1(0) | R500_RGB_ADDR1_CONST
|
96 R500_RGB_ADDR2(0) | R500_RGB_ADDR2_CONST
;
97 fs
->instructions
[i
].inst2
=
98 R500_ALPHA_ADDR0(0) | R500_ALPHA_ADDR1(0) | R500_ALPHA_ADDR1_CONST
|
99 R500_ALPHA_ADDR2(0) | R500_ALPHA_ADDR2_CONST
;
100 fs
->instructions
[i
].inst3
= R500_ALU_RGB_SEL_A_SRC0
|
101 R500_SWIZ_RGB_A(r500_rgb_swiz(&src
->SrcRegister
)) |
102 R500_ALU_RGB_SEL_B_SRC0
|
103 R500_SWIZ_RGB_B(r500_rgb_swiz(&src
->SrcRegister
));
104 fs
->instructions
[i
].inst4
= R500_ALPHA_OP_CMP
|
105 R500_SWIZ_ALPHA_A(r500_alpha_swiz(&src
->SrcRegister
)) |
106 R500_SWIZ_ALPHA_B(r500_alpha_swiz(&src
->SrcRegister
));
107 fs
->instructions
[i
].inst5
=
108 R500_ALU_RGBA_OP_CMP
| R500_ALU_RGBA_R_SWIZ_0
|
109 R500_ALU_RGBA_G_SWIZ_0
| R500_ALU_RGBA_B_SWIZ_0
|
110 R500_ALU_RGBA_A_SWIZ_0
;
112 fs
->instruction_count
++;
115 static void r500_fs_instruction(struct r500_fragment_shader
* fs
,
116 struct r300_fs_asm
* assembler
,
117 struct tgsi_full_instruction
* inst
)
119 /* Switch between opcodes. When possible, prefer using the official
120 * AMD/ATI names for opcodes, please, as it facilitates using the
122 switch (inst
->Instruction
.Opcode
) {
123 case TGSI_OPCODE_MOV
:
124 r500_emit_mov(fs
, assembler
, &inst
->FullSrcRegisters
[0],
125 &inst
->FullDstRegisters
[0]);
127 case TGSI_OPCODE_END
:
130 debug_printf("r300: fs: Bad opcode %d\n",
131 inst
->Instruction
.Opcode
);
136 void r300_translate_fragment_shader(struct r300_context
* r300
,
137 struct r300_fragment_shader
* fs
)
139 struct tgsi_parse_context parser
;
141 tgsi_parse_init(&parser
, fs
->shader
.state
.tokens
);
143 while (!tgsi_parse_end_of_tokens(&parser
)) {
144 tgsi_parse_token(&parser
);
147 r300_copy_passthrough_shader(fs
);
150 void r500_translate_fragment_shader(struct r300_context
* r300
,
151 struct r500_fragment_shader
* fs
)
153 struct r300_fs_asm
* assembler
= CALLOC_STRUCT(r300_fs_asm
);
154 if (assembler
== NULL
) {
157 struct tgsi_parse_context parser
;
159 tgsi_parse_init(&parser
, fs
->shader
.state
.tokens
);
161 while (!tgsi_parse_end_of_tokens(&parser
)) {
162 tgsi_parse_token(&parser
);
164 /* This is seriously the lamest way to create fragment programs ever.
166 switch (parser
.FullToken
.Token
.Type
) {
167 case TGSI_TOKEN_TYPE_DECLARATION
:
168 /* Allocated registers sitting at the beginning
170 r300_fs_declare(assembler
, &parser
.FullToken
.FullDeclaration
);
172 case TGSI_TOKEN_TYPE_INSTRUCTION
:
173 r500_fs_instruction(fs
, assembler
,
174 &parser
.FullToken
.FullInstruction
);
179 debug_printf("%d texs and %d colors, first free reg is %d\n",
180 assembler
->tex_count
, assembler
->color_count
,
181 assembler
->tex_count
+ assembler
->color_count
);
183 /* XXX subtly wrong */
184 fs
->shader
.stack_size
= assembler
->temp_offset
;
186 tgsi_dump(fs
->shader
.state
.tokens
);
188 //r500_copy_passthrough_shader(fs);
190 tgsi_parse_free(&parser
);