blob: 2db185fd80eb05283b244f589eccd7e7b6c83d67 [file] [log] [blame]
Joakim Sindholt62285882009-06-26 01:08:13 +02001/*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Joakim Sindholt <opensource@zhasha.com>
4 *
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:
11 *
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
14 * Software.
15 *
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. */
23
Corbin Simpson6a448a52009-10-21 06:31:36 -070024#include "tgsi/tgsi_dump.h"
Joakim Sindholt62285882009-06-26 01:08:13 +020025
Corbin Simpson6a448a52009-10-21 06:31:36 -070026#include "r300_context.h"
27#include "r300_screen.h"
28#include "r300_fs.h"
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020029#include "r300_tgsi_to_rc.h"
30
Corbin Simpson6a448a52009-10-21 06:31:36 -070031#include "radeon_code.h"
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020032#include "radeon_compiler.h"
33
34static void find_output_registers(struct r300_fragment_program_compiler * compiler,
35 struct r300_fragment_shader * fs)
Joakim Sindholt62285882009-06-26 01:08:13 +020036{
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020037 unsigned i;
38
39 /* Mark the outputs as not present initially */
40 compiler->OutputColor = fs->info.num_outputs;
41 compiler->OutputDepth = fs->info.num_outputs;
42
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;
48 break;
49 case TGSI_SEMANTIC_POSITION:
50 compiler->OutputDepth = i;
51 break;
52 }
53 }
54}
55
56static void allocate_hardware_inputs(
57 struct r300_fragment_program_compiler * c,
58 void (*allocate)(void * data, unsigned input, unsigned hwreg),
59 void * mydata)
60{
61 struct tgsi_shader_info* info = &((struct r300_fragment_shader*)c->UserData)->info;
62 int total_colors = 0;
63 int colors = 0;
64 int total_generic = 0;
65 int generic = 0;
Joakim Sindholt62285882009-06-26 01:08:13 +020066 int i;
Joakim Sindholt62285882009-06-26 01:08:13 +020067
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020068 for (i = 0; i < info->num_inputs; i++) {
69 switch (info->input_semantic_name[i]) {
70 case TGSI_SEMANTIC_COLOR:
71 total_colors++;
Joakim Sindholt62285882009-06-26 01:08:13 +020072 break;
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020073 case TGSI_SEMANTIC_FOG:
74 case TGSI_SEMANTIC_GENERIC:
75 total_generic++;
Joakim Sindholt62285882009-06-26 01:08:13 +020076 break;
77 }
78 }
79
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020080 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);
84 colors++;
85 break;
86 case TGSI_SEMANTIC_FOG:
87 case TGSI_SEMANTIC_GENERIC:
88 allocate(mydata, i, total_colors + generic);
89 generic++;
90 break;
91 }
92 }
93}
Joakim Sindholt62285882009-06-26 01:08:13 +020094
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020095void r300_translate_fragment_shader(struct r300_context* r300,
96 struct r300_fragment_shader* fs)
97{
98 struct r300_fragment_program_compiler compiler;
99 struct tgsi_to_rc ttr;
100
101 memset(&compiler, 0, sizeof(compiler));
102 rc_init(&compiler.Base);
Nicolai Hähnle4b01e6f2009-09-06 15:03:51 +0200103 compiler.Base.Debug = DBG_ON(r300, DBG_FP);
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200104
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;
109
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.
113 *
114 * This is not yet handled correctly.
115 */
116
117 find_output_registers(&compiler, fs);
118
119 if (compiler.Base.Debug) {
Cooper Yuan2cbd3fc2009-08-11 14:39:58 +0800120 debug_printf("r300: Initial fragment program\n");
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200121 tgsi_dump(fs->state.tokens, 0);
Joakim Sindholt62285882009-06-26 01:08:13 +0200122 }
123
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200124 /* Translate TGSI to our internal representation */
125 ttr.compiler = &compiler.Base;
126 ttr.info = &fs->info;
Joakim Sindholt62285882009-06-26 01:08:13 +0200127
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200128 r300_tgsi_to_rc(&ttr, fs->state.tokens);
129
130 /* Invoke the compiler */
131 r3xx_compile_fragment_program(&compiler);
132 if (compiler.Base.Error) {
Nicolai Hähnle81e51882009-10-03 19:20:31 +0200133 /* Todo: Fallback to software rendering gracefully? */
134 fprintf(stderr, "r300 FP: Compiler error: %s\n", compiler.Base.ErrorMsg);
135
136 if (compiler.is_r500) {
137 memcpy(compiler.code, &r5xx_passthrough_fragment_shader, sizeof(r5xx_passthrough_fragment_shader));
138 } else {
139 memcpy(compiler.code, &r3xx_passthrough_fragment_shader, sizeof(r3xx_passthrough_fragment_shader));
140 }
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200141 }
Corbin Simpson07961bb2009-07-22 23:58:35 -0700142
143 /* And, finally... */
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200144 rc_destroy(&compiler.Base);
Corbin Simpson07961bb2009-07-22 23:58:35 -0700145 fs->translated = TRUE;
Joakim Sindholt62285882009-06-26 01:08:13 +0200146}