101e04c3fSmrg/*
201e04c3fSmrg * Copyright (c) 2012-2015 Etnaviv Project
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sub license,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the
1201e04c3fSmrg * next paragraph) shall be included in all copies or substantial portions
1301e04c3fSmrg * of the Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg *
2301e04c3fSmrg * Authors:
2401e04c3fSmrg *    Wladimir J. van der Laan <laanwj@gmail.com>
2501e04c3fSmrg */
2601e04c3fSmrg
2701e04c3fSmrg#ifndef H_ETNAVIV_COMPILER
2801e04c3fSmrg#define H_ETNAVIV_COMPILER
2901e04c3fSmrg
3001e04c3fSmrg#include "etnaviv_context.h"
3101e04c3fSmrg#include "etnaviv_internal.h"
3201e04c3fSmrg#include "etnaviv_shader.h"
3301e04c3fSmrg#include "pipe/p_compiler.h"
3401e04c3fSmrg#include "pipe/p_shader_tokens.h"
357ec681f3Smrg#include "compiler/shader_enums.h"
367ec681f3Smrg#include "util/disk_cache.h"
3701e04c3fSmrg
3801e04c3fSmrg/* XXX some of these are pretty arbitrary limits, may be better to switch
3901e04c3fSmrg * to dynamic allocation at some point.
4001e04c3fSmrg */
4101e04c3fSmrg#define ETNA_MAX_TEMPS (64) /* max temp register count of all Vivante hw */
4201e04c3fSmrg#define ETNA_MAX_TOKENS (2048)
4301e04c3fSmrg#define ETNA_MAX_IMM (1024) /* max const+imm in 32-bit words */
4401e04c3fSmrg#define ETNA_MAX_DECL (2048) /* max declarations */
4501e04c3fSmrg#define ETNA_MAX_DEPTH (32)
4601e04c3fSmrg#define ETNA_MAX_INSTRUCTIONS (2048)
4701e04c3fSmrg
487ec681f3Smrg/**
497ec681f3Smrg * Compiler state saved across compiler invocations, for any expensive global
507ec681f3Smrg * setup.
517ec681f3Smrg */
527ec681f3Smrgstruct etna_compiler {
537ec681f3Smrg   uint32_t shader_count;
547ec681f3Smrg   struct ra_regs *regs;
557ec681f3Smrg
567ec681f3Smrg   struct disk_cache *disk_cache;
577ec681f3Smrg};
587ec681f3Smrg
5901e04c3fSmrg/* compiler output per input/output */
6001e04c3fSmrgstruct etna_shader_inout {
6101e04c3fSmrg   int reg; /* native register */
627ec681f3Smrg   int slot; /* nir: gl_varying_slot or gl_vert_attrib */
6301e04c3fSmrg   int num_components;
6401e04c3fSmrg};
6501e04c3fSmrg
6601e04c3fSmrgstruct etna_shader_io_file {
6701e04c3fSmrg   size_t num_reg;
6801e04c3fSmrg   struct etna_shader_inout reg[ETNA_NUM_INPUTS];
6901e04c3fSmrg};
7001e04c3fSmrg
7101e04c3fSmrg/* shader object, for linking */
7201e04c3fSmrgstruct etna_shader_variant {
7301e04c3fSmrg   uint32_t id; /* for debug */
7401e04c3fSmrg
757ec681f3Smrg   /* shader variants form a linked list */
767ec681f3Smrg   struct etna_shader_variant *next;
777ec681f3Smrg
787ec681f3Smrg   /* replicated here to avoid passing extra ptrs everywhere */
797ec681f3Smrg   struct etna_shader *shader;
807ec681f3Smrg   struct etna_shader_key key;
817ec681f3Smrg
827ec681f3Smrg   struct etna_bo *bo; /* cached code memory bo handle (for icache) */
837ec681f3Smrg
847ec681f3Smrg   /*
857ec681f3Smrg    * Below here is serialized when written to disk cache:
867ec681f3Smrg    */
8701e04c3fSmrg   uint32_t *code;
887ec681f3Smrg   struct etna_shader_uniform_info uniforms;
897ec681f3Smrg
907ec681f3Smrg   /*
917ec681f3Smrg    * The following macros are used by the shader disk cache save/
927ec681f3Smrg    * restore paths to serialize/deserialize the variant.  Any
937ec681f3Smrg    * pointers that require special handling in store_variant()
947ec681f3Smrg    * and retrieve_variant() should go above here.
957ec681f3Smrg    */
967ec681f3Smrg#define VARIANT_CACHE_START    offsetof(struct etna_shader_variant, stage)
977ec681f3Smrg#define VARIANT_CACHE_PTR(v)   (((char *)v) + VARIANT_CACHE_START)
987ec681f3Smrg#define VARIANT_CACHE_SIZE     (sizeof(struct etna_shader_variant) - VARIANT_CACHE_START)
997ec681f3Smrg
1007ec681f3Smrg   gl_shader_stage stage;
1017ec681f3Smrg   uint32_t code_size; /* code size in uint32 words */
10201e04c3fSmrg   unsigned num_loops;
10301e04c3fSmrg   unsigned num_temps;
10401e04c3fSmrg
10501e04c3fSmrg   /* ETNA_DIRTY_* flags that, when set in context dirty, mean that the
10601e04c3fSmrg    * uniforms have to get (partial) reloaded. */
10701e04c3fSmrg   uint32_t uniforms_dirty_bits;
10801e04c3fSmrg
10901e04c3fSmrg   /* inputs (for linking) for fs, the inputs must be in register 1..N */
11001e04c3fSmrg   struct etna_shader_io_file infile;
11101e04c3fSmrg
11201e04c3fSmrg   /* outputs (for linking) */
11301e04c3fSmrg   struct etna_shader_io_file outfile;
11401e04c3fSmrg
1157ec681f3Smrg   /* special inputs/outputs (vs only) */
1167ec681f3Smrg   int vs_id_in_reg; /* vertexid+instanceid input */
11701e04c3fSmrg   int vs_pos_out_reg; /* VS position output */
11801e04c3fSmrg   int vs_pointsize_out_reg; /* VS point size output */
11901e04c3fSmrg   uint32_t vs_load_balancing;
12001e04c3fSmrg
12101e04c3fSmrg   /* special outputs (ps only) */
12201e04c3fSmrg   int ps_color_out_reg; /* color output register */
12301e04c3fSmrg   int ps_depth_out_reg; /* depth output register */
12401e04c3fSmrg
12501e04c3fSmrg   /* unknown input property (XX_INPUT_COUNT, field UNK8) */
12601e04c3fSmrg   uint32_t input_count_unk8;
12701e04c3fSmrg
12801e04c3fSmrg   /* shader is larger than GPU instruction limit, thus needs icache */
12901e04c3fSmrg   bool needs_icache;
13001e04c3fSmrg
1317ec681f3Smrg   /* shader uses pixel kill/discard */
1327ec681f3Smrg   bool uses_discard;
13301e04c3fSmrg};
13401e04c3fSmrg
13501e04c3fSmrgstruct etna_varying {
13601e04c3fSmrg   uint32_t pa_attributes;
13701e04c3fSmrg   uint8_t num_components;
13801e04c3fSmrg   uint8_t use[4];
13901e04c3fSmrg   uint8_t reg;
14001e04c3fSmrg};
14101e04c3fSmrg
14201e04c3fSmrgstruct etna_shader_link_info {
14301e04c3fSmrg   /* each PS input is annotated with the VS output reg */
14401e04c3fSmrg   unsigned num_varyings;
14501e04c3fSmrg   struct etna_varying varyings[ETNA_NUM_INPUTS];
14601e04c3fSmrg   int pcoord_varying_comp_ofs;
14701e04c3fSmrg};
14801e04c3fSmrg
1497ec681f3Smrgstruct etna_compiler *
1507ec681f3Smrgetna_compiler_create(const char *renderer);
1517ec681f3Smrg
1527ec681f3Smrgvoid
1537ec681f3Smrgetna_compiler_destroy(const struct etna_compiler *compiler);
1547ec681f3Smrg
15501e04c3fSmrgbool
15601e04c3fSmrgetna_compile_shader(struct etna_shader_variant *shader);
15701e04c3fSmrg
15801e04c3fSmrgvoid
15901e04c3fSmrgetna_dump_shader(const struct etna_shader_variant *shader);
16001e04c3fSmrg
16101e04c3fSmrgbool
16201e04c3fSmrgetna_link_shader(struct etna_shader_link_info *info,
16301e04c3fSmrg                 const struct etna_shader_variant *vs, const struct etna_shader_variant *fs);
16401e04c3fSmrg
16501e04c3fSmrgvoid
16601e04c3fSmrgetna_destroy_shader(struct etna_shader_variant *shader);
16701e04c3fSmrg
1687ec681f3Smrg/* NIR compiler */
1697ec681f3Smrg
1707ec681f3Smrgbool
1717ec681f3Smrgetna_compile_shader_nir(struct etna_shader_variant *shader);
1727ec681f3Smrg
1737ec681f3Smrgbool
1747ec681f3Smrgetna_link_shader_nir(struct etna_shader_link_info *info,
1757ec681f3Smrg                     const struct etna_shader_variant *vs,
1767ec681f3Smrg                     const struct etna_shader_variant *fs);
1777ec681f3Smrg
17801e04c3fSmrg#endif
179