1 /* 2 * Copyright (c) 2012-2015 Etnaviv Project 3 * 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 * the rights to use, copy, modify, merge, publish, distribute, sub license, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the 12 * next paragraph) shall be included in all copies or substantial portions 13 * of the Software. 14 * 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: 24 * Wladimir J. van der Laan <laanwj@gmail.com> 25 */ 26 27 #ifndef H_ETNAVIV_COMPILER 28 #define H_ETNAVIV_COMPILER 29 30 #include "etna_core_info.h" 31 #include "etnaviv_context.h" 32 #include "etnaviv_internal.h" 33 #include "etnaviv_shader.h" 34 #include "util/compiler.h" 35 #include "pipe/p_shader_tokens.h" 36 #include "compiler/shader_enums.h" 37 #include "util/disk_cache.h" 38 39 /* XXX some of these are pretty arbitrary limits, may be better to switch 40 * to dynamic allocation at some point. 41 */ 42 #define ETNA_MAX_TEMPS (64) /* max temp register count of all Vivante hw */ 43 #define ETNA_MAX_TOKENS (2048) 44 #define ETNA_MAX_IMM (1024) /* max const+imm in 32-bit words */ 45 #define ETNA_MAX_DEPTH (32) 46 #define ETNA_MAX_INSTRUCTIONS (2048) 47 48 /** 49 * Compiler state saved across compiler invocations, for any expensive global 50 * setup. 51 */ 52 struct etna_compiler { 53 uint32_t shader_count; 54 struct ra_regs *regs; 55 56 nir_shader_compiler_options options; 57 struct disk_cache *disk_cache; 58 }; 59 60 /* compiler output per input/output */ 61 struct etna_shader_inout { 62 int reg; /* native register */ 63 int slot; /* nir: gl_varying_slot or gl_vert_attrib */ 64 uint8_t interpolation; 65 uint8_t num_components; 66 }; 67 68 struct etna_shader_io_file { 69 size_t num_reg; 70 struct etna_shader_inout reg[ETNA_NUM_INPUTS]; 71 }; 72 73 /* shader object, for linking */ 74 struct etna_shader_variant { 75 uint32_t id; /* for debug */ 76 77 /* shader variants form a linked list */ 78 struct etna_shader_variant *next; 79 80 /* replicated here to avoid passing extra ptrs everywhere */ 81 struct etna_shader *shader; 82 struct etna_shader_key key; 83 84 struct etna_bo *bo; /* cached code memory bo handle (for icache) */ 85 86 /* 87 * Below here is serialized when written to disk cache: 88 */ 89 uint32_t *code; 90 struct etna_shader_uniform_info uniforms; 91 92 /* 93 * The following macros are used by the shader disk cache save/ 94 * restore paths to serialize/deserialize the variant. Any 95 * pointers that require special handling in store_variant() 96 * and retrieve_variant() should go above here. 97 */ 98 #define VARIANT_CACHE_START offsetof(struct etna_shader_variant, stage) 99 #define VARIANT_CACHE_PTR(v) (((char *)v) + VARIANT_CACHE_START) 100 #define VARIANT_CACHE_SIZE (sizeof(struct etna_shader_variant) - VARIANT_CACHE_START) 101 102 gl_shader_stage stage; 103 uint32_t code_size; /* code size in uint32 words */ 104 unsigned num_loops; 105 unsigned num_temps; 106 107 /* ETNA_DIRTY_* flags that, when set in context dirty, mean that the 108 * uniforms have to get (partial) reloaded. */ 109 uint32_t uniforms_dirty_bits; 110 111 /* inputs (for linking) for fs, the inputs must be in register 1..N */ 112 struct etna_shader_io_file infile; 113 114 /* outputs (for linking) */ 115 struct etna_shader_io_file outfile; 116 117 /* special inputs/outputs (vs only) */ 118 int vs_id_in_reg; /* vertexid+instanceid input */ 119 int vs_pos_out_reg; /* VS position output */ 120 int vs_pointsize_out_reg; /* VS point size output */ 121 uint32_t vs_load_balancing; 122 123 /* special outputs (ps only) */ 124 int ps_color_out_reg[PIPE_MAX_COLOR_BUFS]; /* color output register */ 125 int ps_depth_out_reg; /* depth output register */ 126 127 /* unknown input property (XX_INPUT_COUNT, field UNK8) */ 128 uint32_t input_count_unk8; 129 130 /* shader is larger than GPU instruction limit, thus needs icache */ 131 bool needs_icache; 132 133 /* shader uses pixel kill/discard */ 134 bool uses_discard; 135 }; 136 137 struct etna_varying { 138 uint32_t pa_attributes; 139 uint8_t num_components; 140 uint8_t use[4]; 141 uint8_t semantic; 142 uint8_t reg; 143 }; 144 145 struct etna_shader_link_info { 146 /* each PS input is annotated with the VS output reg */ 147 unsigned num_varyings; 148 struct etna_varying varyings[ETNA_NUM_INPUTS]; 149 int pcoord_varying_comp_ofs; 150 }; 151 152 struct etna_compiler * 153 etna_compiler_create(const char *renderer, const struct etna_core_info *info); 154 155 void 156 etna_compiler_destroy(const struct etna_compiler *compiler); 157 158 const nir_shader_compiler_options * 159 etna_compiler_get_options(struct etna_compiler *compiler); 160 161 bool 162 etna_compile_shader(struct etna_shader_variant *shader); 163 164 void 165 etna_dump_shader(const struct etna_shader_variant *shader); 166 167 void 168 etna_link_shader(struct etna_shader_link_info *info, 169 const struct etna_shader_variant *vs, 170 const struct etna_shader_variant *fs); 171 172 void 173 etna_destroy_shader(struct etna_shader_variant *shader); 174 175 #endif 176