blob: 2cddb97038d4625c3fce74a04560fc137f9d13d4 [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
24#include "r300_fs.h"
25
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020026#include "r300_tgsi_to_rc.h"
27
28#include "radeon_compiler.h"
29
30static void find_output_registers(struct r300_fragment_program_compiler * compiler,
31 struct r300_fragment_shader * fs)
Joakim Sindholt62285882009-06-26 01:08:13 +020032{
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020033 unsigned i;
34
35 /* Mark the outputs as not present initially */
36 compiler->OutputColor = fs->info.num_outputs;
37 compiler->OutputDepth = fs->info.num_outputs;
38
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;
44 break;
45 case TGSI_SEMANTIC_POSITION:
46 compiler->OutputDepth = i;
47 break;
48 }
49 }
50}
51
52static void allocate_hardware_inputs(
53 struct r300_fragment_program_compiler * c,
54 void (*allocate)(void * data, unsigned input, unsigned hwreg),
55 void * mydata)
56{
57 struct tgsi_shader_info* info = &((struct r300_fragment_shader*)c->UserData)->info;
58 int total_colors = 0;
59 int colors = 0;
60 int total_generic = 0;
61 int generic = 0;
Joakim Sindholt62285882009-06-26 01:08:13 +020062 int i;
Joakim Sindholt62285882009-06-26 01:08:13 +020063
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020064 for (i = 0; i < info->num_inputs; i++) {
65 switch (info->input_semantic_name[i]) {
66 case TGSI_SEMANTIC_COLOR:
67 total_colors++;
Joakim Sindholt62285882009-06-26 01:08:13 +020068 break;
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020069 case TGSI_SEMANTIC_FOG:
70 case TGSI_SEMANTIC_GENERIC:
71 total_generic++;
Joakim Sindholt62285882009-06-26 01:08:13 +020072 break;
73 }
74 }
75
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020076 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);
80 colors++;
81 break;
82 case TGSI_SEMANTIC_FOG:
83 case TGSI_SEMANTIC_GENERIC:
84 allocate(mydata, i, total_colors + generic);
85 generic++;
86 break;
87 }
88 }
89}
Joakim Sindholt62285882009-06-26 01:08:13 +020090
Nicolai Hähnled0c398a2009-07-30 23:45:34 +020091void r300_translate_fragment_shader(struct r300_context* r300,
92 struct r300_fragment_shader* fs)
93{
94 struct r300_fragment_program_compiler compiler;
95 struct tgsi_to_rc ttr;
96
97 memset(&compiler, 0, sizeof(compiler));
98 rc_init(&compiler.Base);
99 compiler.Base.Debug = 1;
100
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;
105
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.
109 *
110 * This is not yet handled correctly.
111 */
112
113 find_output_registers(&compiler, fs);
114
115 if (compiler.Base.Debug) {
116 debug_printf("r300: Initial vertex program\n");
117 tgsi_dump(fs->state.tokens, 0);
Joakim Sindholt62285882009-06-26 01:08:13 +0200118 }
119
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200120 /* Translate TGSI to our internal representation */
121 ttr.compiler = &compiler.Base;
122 ttr.info = &fs->info;
Joakim Sindholt62285882009-06-26 01:08:13 +0200123
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200124 r300_tgsi_to_rc(&ttr, fs->state.tokens);
125
126 /* Invoke the compiler */
127 r3xx_compile_fragment_program(&compiler);
128 if (compiler.Base.Error) {
129 /* Todo: Fail gracefully */
130 fprintf(stderr, "r300 FP: Compiler error\n");
131 abort();
132 }
Corbin Simpson07961bb2009-07-22 23:58:35 -0700133
134 /* And, finally... */
Nicolai Hähnled0c398a2009-07-30 23:45:34 +0200135 rc_destroy(&compiler.Base);
Corbin Simpson07961bb2009-07-22 23:58:35 -0700136 fs->translated = TRUE;
Joakim Sindholt62285882009-06-26 01:08:13 +0200137}