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 "etnaviv_context.h"
31#include "etnaviv_internal.h"
32#include "etnaviv_shader.h"
33#include "pipe/p_compiler.h"
34#include "pipe/p_shader_tokens.h"
35#include "compiler/shader_enums.h"
36#include "util/disk_cache.h"
37
38/* XXX some of these are pretty arbitrary limits, may be better to switch
39 * to dynamic allocation at some point.
40 */
41#define ETNA_MAX_TEMPS (64) /* max temp register count of all Vivante hw */
42#define ETNA_MAX_TOKENS (2048)
43#define ETNA_MAX_IMM (1024) /* max const+imm in 32-bit words */
44#define ETNA_MAX_DEPTH (32)
45#define ETNA_MAX_INSTRUCTIONS (2048)
46
47/**
48 * Compiler state saved across compiler invocations, for any expensive global
49 * setup.
50 */
51struct etna_compiler {
52   uint32_t shader_count;
53   struct ra_regs *regs;
54
55   nir_shader_compiler_options options;
56   struct disk_cache *disk_cache;
57};
58
59/* compiler output per input/output */
60struct etna_shader_inout {
61   int reg; /* native register */
62   int slot; /* nir: gl_varying_slot or gl_vert_attrib */
63   int num_components;
64};
65
66struct etna_shader_io_file {
67   size_t num_reg;
68   struct etna_shader_inout reg[ETNA_NUM_INPUTS];
69};
70
71/* shader object, for linking */
72struct etna_shader_variant {
73   uint32_t id; /* for debug */
74
75   /* shader variants form a linked list */
76   struct etna_shader_variant *next;
77
78   /* replicated here to avoid passing extra ptrs everywhere */
79   struct etna_shader *shader;
80   struct etna_shader_key key;
81
82   struct etna_bo *bo; /* cached code memory bo handle (for icache) */
83
84   /*
85    * Below here is serialized when written to disk cache:
86    */
87   uint32_t *code;
88   struct etna_shader_uniform_info uniforms;
89
90   /*
91    * The following macros are used by the shader disk cache save/
92    * restore paths to serialize/deserialize the variant.  Any
93    * pointers that require special handling in store_variant()
94    * and retrieve_variant() should go above here.
95    */
96#define VARIANT_CACHE_START    offsetof(struct etna_shader_variant, stage)
97#define VARIANT_CACHE_PTR(v)   (((char *)v) + VARIANT_CACHE_START)
98#define VARIANT_CACHE_SIZE     (sizeof(struct etna_shader_variant) - VARIANT_CACHE_START)
99
100   gl_shader_stage stage;
101   uint32_t code_size; /* code size in uint32 words */
102   unsigned num_loops;
103   unsigned num_temps;
104
105   /* ETNA_DIRTY_* flags that, when set in context dirty, mean that the
106    * uniforms have to get (partial) reloaded. */
107   uint32_t uniforms_dirty_bits;
108
109   /* inputs (for linking) for fs, the inputs must be in register 1..N */
110   struct etna_shader_io_file infile;
111
112   /* outputs (for linking) */
113   struct etna_shader_io_file outfile;
114
115   /* special inputs/outputs (vs only) */
116   int vs_id_in_reg; /* vertexid+instanceid input */
117   int vs_pos_out_reg; /* VS position output */
118   int vs_pointsize_out_reg; /* VS point size output */
119   uint32_t vs_load_balancing;
120
121   /* special outputs (ps only) */
122   int ps_color_out_reg; /* color output register */
123   int ps_depth_out_reg; /* depth output register */
124
125   /* unknown input property (XX_INPUT_COUNT, field UNK8) */
126   uint32_t input_count_unk8;
127
128   /* shader is larger than GPU instruction limit, thus needs icache */
129   bool needs_icache;
130
131   /* shader uses pixel kill/discard */
132   bool uses_discard;
133};
134
135struct etna_varying {
136   uint32_t pa_attributes;
137   uint8_t num_components;
138   uint8_t use[4];
139   uint8_t reg;
140};
141
142struct etna_shader_link_info {
143   /* each PS input is annotated with the VS output reg */
144   unsigned num_varyings;
145   struct etna_varying varyings[ETNA_NUM_INPUTS];
146   int pcoord_varying_comp_ofs;
147};
148
149struct etna_compiler *
150etna_compiler_create(const char *renderer, const struct etna_specs *specs);
151
152void
153etna_compiler_destroy(const struct etna_compiler *compiler);
154
155const nir_shader_compiler_options *
156etna_compiler_get_options(struct etna_compiler *compiler);
157
158bool
159etna_compile_shader(struct etna_shader_variant *shader);
160
161void
162etna_dump_shader(const struct etna_shader_variant *shader);
163
164bool
165etna_link_shader(struct etna_shader_link_info *info,
166                 const struct etna_shader_variant *vs,
167                 const struct etna_shader_variant *fs);
168
169void
170etna_destroy_shader(struct etna_shader_variant *shader);
171
172#endif
173