Joakim Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 1 | /* |
| 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ähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 26 | #include "r300_tgsi_to_rc.h" |
| 27 | |
| 28 | #include "radeon_compiler.h" |
| 29 | |
| 30 | static void find_output_registers(struct r300_fragment_program_compiler * compiler, |
| 31 | struct r300_fragment_shader * fs) |
Joakim Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 32 | { |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 33 | 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 | |
| 52 | static 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 Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 62 | int i; |
Joakim Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 63 | |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 64 | for (i = 0; i < info->num_inputs; i++) { |
| 65 | switch (info->input_semantic_name[i]) { |
| 66 | case TGSI_SEMANTIC_COLOR: |
| 67 | total_colors++; |
Joakim Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 68 | break; |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 69 | case TGSI_SEMANTIC_FOG: |
| 70 | case TGSI_SEMANTIC_GENERIC: |
| 71 | total_generic++; |
Joakim Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 76 | 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 Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 90 | |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 91 | void 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); |
Nicolai Hähnle | 4b01e6f | 2009-09-06 15:03:51 +0200 | [diff] [blame] | 99 | compiler.Base.Debug = DBG_ON(r300, DBG_FP); |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 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) { |
Cooper Yuan | 2cbd3fc | 2009-08-11 14:39:58 +0800 | [diff] [blame] | 116 | debug_printf("r300: Initial fragment program\n"); |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 117 | tgsi_dump(fs->state.tokens, 0); |
Joakim Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 120 | /* Translate TGSI to our internal representation */ |
| 121 | ttr.compiler = &compiler.Base; |
| 122 | ttr.info = &fs->info; |
Joakim Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 123 | |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 124 | r300_tgsi_to_rc(&ttr, fs->state.tokens); |
| 125 | |
| 126 | /* Invoke the compiler */ |
| 127 | r3xx_compile_fragment_program(&compiler); |
| 128 | if (compiler.Base.Error) { |
Nicolai Hähnle | 81e5188 | 2009-10-03 19:20:31 +0200 | [diff] [blame] | 129 | /* Todo: Fallback to software rendering gracefully? */ |
| 130 | fprintf(stderr, "r300 FP: Compiler error: %s\n", compiler.Base.ErrorMsg); |
| 131 | |
| 132 | if (compiler.is_r500) { |
| 133 | memcpy(compiler.code, &r5xx_passthrough_fragment_shader, sizeof(r5xx_passthrough_fragment_shader)); |
| 134 | } else { |
| 135 | memcpy(compiler.code, &r3xx_passthrough_fragment_shader, sizeof(r3xx_passthrough_fragment_shader)); |
| 136 | } |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 137 | } |
Corbin Simpson | 07961bb | 2009-07-22 23:58:35 -0700 | [diff] [blame] | 138 | |
| 139 | /* And, finally... */ |
Nicolai Hähnle | d0c398a | 2009-07-30 23:45:34 +0200 | [diff] [blame] | 140 | rc_destroy(&compiler.Base); |
Corbin Simpson | 07961bb | 2009-07-22 23:58:35 -0700 | [diff] [blame] | 141 | fs->translated = TRUE; |
Joakim Sindholt | 6228588 | 2009-06-26 01:08:13 +0200 | [diff] [blame] | 142 | } |