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 <[email protected]> 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 int num_components; 65 }; 66 67 struct etna_shader_io_file { 68 size_t num_reg; 69 struct etna_shader_inout reg[ETNA_NUM_INPUTS]; 70 }; 71 72 /* shader object, for linking */ 73 struct etna_shader_variant { 74 uint32_t id; /* for debug */ 75 76 /* shader variants form a linked list */ 77 struct etna_shader_variant *next; 78 79 /* replicated here to avoid passing extra ptrs everywhere */ 80 struct etna_shader *shader; 81 struct etna_shader_key key; 82 83 struct etna_bo *bo; /* cached code memory bo handle (for icache) */ 84 85 /* 86 * Below here is serialized when written to disk cache: 87 */ 88 uint32_t *code; 89 struct etna_shader_uniform_info uniforms; 90 91 /* 92 * The following macros are used by the shader disk cache save/ 93 * restore paths to serialize/deserialize the variant. Any 94 * pointers that require special handling in store_variant() 95 * and retrieve_variant() should go above here. 96 */ 97 #define VARIANT_CACHE_START offsetof(struct etna_shader_variant, stage) 98 #define VARIANT_CACHE_PTR(v) (((char *)v) + VARIANT_CACHE_START) 99 #define VARIANT_CACHE_SIZE (sizeof(struct etna_shader_variant) - VARIANT_CACHE_START) 100 101 gl_shader_stage stage; 102 uint32_t code_size; /* code size in uint32 words */ 103 unsigned num_loops; 104 unsigned num_temps; 105 106 /* ETNA_DIRTY_* flags that, when set in context dirty, mean that the 107 * uniforms have to get (partial) reloaded. */ 108 uint32_t uniforms_dirty_bits; 109 110 /* inputs (for linking) for fs, the inputs must be in register 1..N */ 111 struct etna_shader_io_file infile; 112 113 /* outputs (for linking) */ 114 struct etna_shader_io_file outfile; 115 116 /* special inputs/outputs (vs only) */ 117 int vs_id_in_reg; /* vertexid+instanceid input */ 118 int vs_pos_out_reg; /* VS position output */ 119 int vs_pointsize_out_reg; /* VS point size output */ 120 uint32_t vs_load_balancing; 121 122 /* special outputs (ps only) */ 123 int ps_color_out_reg; /* color output register */ 124 int ps_depth_out_reg; /* depth output register */ 125 126 /* unknown input property (XX_INPUT_COUNT, field UNK8) */ 127 uint32_t input_count_unk8; 128 129 /* shader is larger than GPU instruction limit, thus needs icache */ 130 bool needs_icache; 131 132 /* shader uses pixel kill/discard */ 133 bool uses_discard; 134 }; 135 136 struct etna_varying { 137 uint32_t pa_attributes; 138 uint8_t num_components; 139 uint8_t use[4]; 140 uint8_t reg; 141 }; 142 143 struct etna_shader_link_info { 144 /* each PS input is annotated with the VS output reg */ 145 unsigned num_varyings; 146 struct etna_varying varyings[ETNA_NUM_INPUTS]; 147 int pcoord_varying_comp_ofs; 148 }; 149 150 struct etna_compiler * 151 etna_compiler_create(const char *renderer, const struct etna_core_info *info); 152 153 void 154 etna_compiler_destroy(const struct etna_compiler *compiler); 155 156 const nir_shader_compiler_options * 157 etna_compiler_get_options(struct etna_compiler *compiler); 158 159 bool 160 etna_compile_shader(struct etna_shader_variant *shader); 161 162 void 163 etna_dump_shader(const struct etna_shader_variant *shader); 164 165 void 166 etna_link_shader(struct etna_shader_link_info *info, 167 const struct etna_shader_variant *vs, 168 const struct etna_shader_variant *fs); 169 170 void 171 etna_destroy_shader(struct etna_shader_variant *shader); 172 173 #endif 174