r300_fs.c revision d2ac3d5e79bdf5a32a2dca135403d963ac6c83e4
1/* 2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com> 3 * Joakim Sindholt <opensource@zhasha.com> 4 * Copyright 2009 Marek Olšák <maraeo@gmail.com> 5 * 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: 12 * 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 15 * Software. 16 * 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. */ 24 25#include "util/u_math.h" 26#include "util/u_memory.h" 27 28#include "tgsi/tgsi_dump.h" 29 30#include "r300_context.h" 31#include "r300_screen.h" 32#include "r300_fs.h" 33#include "r300_tgsi_to_rc.h" 34 35#include "radeon_code.h" 36#include "radeon_compiler.h" 37 38/* Convert info about FS input semantics to r300_shader_semantics. */ 39void r300_shader_read_fs_inputs(struct tgsi_shader_info* info, 40 struct r300_shader_semantics* fs_inputs) 41{ 42 int i; 43 unsigned index; 44 45 r300_shader_semantics_reset(fs_inputs); 46 47 for (i = 0; i < info->num_inputs; i++) { 48 index = info->input_semantic_index[i]; 49 50 switch (info->input_semantic_name[i]) { 51 case TGSI_SEMANTIC_COLOR: 52 assert(index < ATTR_COLOR_COUNT); 53 fs_inputs->color[index] = i; 54 break; 55 56 case TGSI_SEMANTIC_GENERIC: 57 assert(index < ATTR_GENERIC_COUNT); 58 fs_inputs->generic[index] = i; 59 break; 60 61 case TGSI_SEMANTIC_FOG: 62 assert(index == 0); 63 fs_inputs->fog = i; 64 break; 65 66 case TGSI_SEMANTIC_POSITION: 67 assert(index == 0); 68 fs_inputs->wpos = i; 69 break; 70 71 default: 72 assert(0); 73 } 74 } 75} 76 77static void find_output_registers(struct r300_fragment_program_compiler * compiler, 78 struct r300_fragment_shader * fs) 79{ 80 unsigned i, colorbuf_count = 0; 81 82 /* Mark the outputs as not present initially */ 83 compiler->OutputColor[0] = fs->info.num_outputs; 84 compiler->OutputColor[1] = fs->info.num_outputs; 85 compiler->OutputColor[2] = fs->info.num_outputs; 86 compiler->OutputColor[3] = fs->info.num_outputs; 87 compiler->OutputDepth = fs->info.num_outputs; 88 89 /* Now see where they really are. */ 90 for(i = 0; i < fs->info.num_outputs; ++i) { 91 switch(fs->info.output_semantic_name[i]) { 92 case TGSI_SEMANTIC_COLOR: 93 compiler->OutputColor[colorbuf_count] = i; 94 colorbuf_count++; 95 break; 96 case TGSI_SEMANTIC_POSITION: 97 compiler->OutputDepth = i; 98 break; 99 } 100 } 101} 102 103static void allocate_hardware_inputs( 104 struct r300_fragment_program_compiler * c, 105 void (*allocate)(void * data, unsigned input, unsigned hwreg), 106 void * mydata) 107{ 108 struct r300_shader_semantics* inputs = 109 (struct r300_shader_semantics*)c->UserData; 110 int i, reg = 0; 111 112 /* Allocate input registers. */ 113 for (i = 0; i < ATTR_COLOR_COUNT; i++) { 114 if (inputs->color[i] != ATTR_UNUSED) { 115 allocate(mydata, inputs->color[i], reg++); 116 } 117 } 118 for (i = 0; i < ATTR_GENERIC_COUNT; i++) { 119 if (inputs->generic[i] != ATTR_UNUSED) { 120 allocate(mydata, inputs->generic[i], reg++); 121 } 122 } 123 if (inputs->fog != ATTR_UNUSED) { 124 allocate(mydata, inputs->fog, reg++); 125 } 126 if (inputs->wpos != ATTR_UNUSED) { 127 allocate(mydata, inputs->wpos, reg++); 128 } 129} 130 131static void get_compare_state( 132 struct r300_context* r300, 133 struct r300_fragment_program_external_state* state, 134 unsigned shadow_samplers) 135{ 136 struct r300_textures_state *texstate = 137 (struct r300_textures_state*)r300->textures_state.state; 138 139 memset(state, 0, sizeof(*state)); 140 141 for (int i = 0; i < texstate->sampler_count; i++) { 142 struct r300_sampler_state* s = texstate->sampler_states[i]; 143 144 if (s && s->state.compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) { 145 /* XXX Gallium doesn't provide us with any information regarding 146 * this mode, so we are screwed. I'm setting 0 = LUMINANCE. */ 147 state->unit[i].depth_texture_mode = 0; 148 149 /* Fortunately, no need to translate this. */ 150 state->unit[i].texture_compare_func = s->state.compare_func; 151 } 152 } 153} 154 155static void r300_translate_fragment_shader( 156 struct r300_context* r300, 157 struct r300_fragment_shader_code* shader) 158{ 159 struct r300_fragment_shader* fs = r300->fs; 160 struct r300_fragment_program_compiler compiler; 161 struct tgsi_to_rc ttr; 162 int wpos = fs->inputs.wpos; 163 164 /* Setup the compiler. */ 165 memset(&compiler, 0, sizeof(compiler)); 166 rc_init(&compiler.Base); 167 compiler.Base.Debug = DBG_ON(r300, DBG_FP); 168 169 compiler.code = &shader->code; 170 compiler.state = shader->compare_state; 171 compiler.is_r500 = r300_screen(r300->context.screen)->caps->is_r500; 172 compiler.AllocateHwInputs = &allocate_hardware_inputs; 173 compiler.UserData = &fs->inputs; 174 175 find_output_registers(&compiler, fs); 176 177 if (compiler.Base.Debug) { 178 debug_printf("r300: Initial fragment program\n"); 179 tgsi_dump(fs->state.tokens, 0); 180 } 181 182 /* Translate TGSI to our internal representation */ 183 ttr.compiler = &compiler.Base; 184 ttr.info = &fs->info; 185 ttr.use_half_swizzles = TRUE; 186 187 r300_tgsi_to_rc(&ttr, fs->state.tokens); 188 189 fs->shadow_samplers = compiler.Base.Program.ShadowSamplers; 190 191 /** 192 * Transform the program to support WPOS. 193 * 194 * Introduce a small fragment at the start of the program that will be 195 * the only code that directly reads the WPOS input. 196 * All other code pieces that reference that input will be rewritten 197 * to read from a newly allocated temporary. */ 198 if (wpos != ATTR_UNUSED) { 199 /* Moving the input to some other reg is not really necessary. */ 200 rc_transform_fragment_wpos(&compiler.Base, wpos, wpos, TRUE); 201 } 202 203 /* Invoke the compiler */ 204 r3xx_compile_fragment_program(&compiler); 205 if (compiler.Base.Error) { 206 /* XXX failover maybe? */ 207 DBG(r300, DBG_FP, "r300: Error compiling fragment program: %s\n", 208 compiler.Base.ErrorMsg); 209 assert(0); 210 } 211 212 /* And, finally... */ 213 rc_destroy(&compiler.Base); 214} 215 216boolean r300_pick_fragment_shader(struct r300_context* r300) 217{ 218 struct r300_fragment_shader* fs = r300->fs; 219 struct r300_fragment_program_external_state state; 220 struct r300_fragment_shader_code* ptr; 221 222 if (!fs->first) { 223 /* Build the fragment shader for the first time. */ 224 fs->first = fs->shader = CALLOC_STRUCT(r300_fragment_shader_code); 225 226 /* BTW shadow samplers will be known after the first translation, 227 * therefore we set ~0, which means it should look at all sampler 228 * states. This choice doesn't have any impact on the correctness. */ 229 get_compare_state(r300, &fs->shader->compare_state, ~0); 230 r300_translate_fragment_shader(r300, fs->shader); 231 return TRUE; 232 233 } else if (fs->shadow_samplers) { 234 get_compare_state(r300, &state, fs->shadow_samplers); 235 236 /* Check if the currently-bound shader has been compiled 237 * with the texture-compare state we need. */ 238 if (memcmp(&fs->shader->compare_state, &state, sizeof(state)) != 0) { 239 /* Search for the right shader. */ 240 ptr = fs->first; 241 while (ptr) { 242 if (memcmp(&ptr->compare_state, &state, sizeof(state)) == 0) { 243 fs->shader = ptr; 244 return TRUE; 245 } 246 ptr = ptr->next; 247 } 248 249 /* Not found, gotta compile a new one. */ 250 ptr = CALLOC_STRUCT(r300_fragment_shader_code); 251 ptr->next = fs->first; 252 fs->first = fs->shader = ptr; 253 254 ptr->compare_state = state; 255 r300_translate_fragment_shader(r300, ptr); 256 return TRUE; 257 } 258 } 259 260 return FALSE; 261} 262