1c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors/*
2c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * Copyright (c) 2012-2015 Etnaviv Project
3c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors *
4c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * Permission is hereby granted, free of charge, to any person obtaining a
5c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * copy of this software and associated documentation files (the "Software"),
6c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * to deal in the Software without restriction, including without limitation
7c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * the rights to use, copy, modify, merge, publish, distribute, sub license,
8c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * and/or sell copies of the Software, and to permit persons to whom the
9c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * Software is furnished to do so, subject to the following conditions:
10c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors *
11c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * The above copyright notice and this permission notice (including the
12c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * next paragraph) shall be included in all copies or substantial portions
13c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * of the Software.
14c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors *
15c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * DEALINGS IN THE SOFTWARE.
22c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors *
23c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * Authors:
24c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors *    Wladimir J. van der Laan <laanwj@gmail.com>
25c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors */
26c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
27c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#include "etnaviv_shader.h"
28c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
29c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#include "etnaviv_compiler.h"
30c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#include "etnaviv_context.h"
31c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#include "etnaviv_debug.h"
32c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#include "etnaviv_util.h"
33c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
34c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#include "util/u_math.h"
35c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#include "util/u_memory.h"
36c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
37c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors/* Link vs and fs together: fill in shader_state from vs and fs
38c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * as this function is called every time a new fs or vs is bound, the goal is to
39c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * do little processing as possible here, and to precompute as much as possible in
40c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * the vs/fs shader_object.
41c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors *
42c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * XXX we could cache the link result for a certain set of VS/PS; usually a pair
43c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors * of VS and PS will be used together anyway.
44c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors */
45c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsstatic bool
46c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
47c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                  const struct etna_shader *vs, const struct etna_shader *fs)
48c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
49c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   struct etna_shader_link_info link = { };
50c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
51c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   assert(vs->processor == PIPE_SHADER_VERTEX);
52c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   assert(fs->processor == PIPE_SHADER_FRAGMENT);
53c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
54c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#ifdef DEBUG
55c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS)) {
56c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      etna_dump_shader(vs);
57c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      etna_dump_shader(fs);
58c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   }
59c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors#endif
60c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
61c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (etna_link_shader(&link, vs, fs)) {
62c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      /* linking failed: some fs inputs do not have corresponding
63c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors       * vs outputs */
64c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      assert(0);
65c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
66c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      return false;
67c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   }
68c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
69c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (DBG_ENABLED(ETNA_DBG_LINKER_MSGS)) {
70c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      debug_printf("link result:\n");
71c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      debug_printf("  vs  -> fs  comps use     pa_attr\n");
72c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
73c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      for (int idx = 0; idx < link.num_varyings; ++idx)
74c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors         debug_printf("  t%-2u -> t%-2u %-5.*s %u,%u,%u,%u 0x%08x\n",
75c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                      link.varyings[idx].reg, idx + 1,
76c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                      link.varyings[idx].num_components, "xyzw",
77c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                      link.varyings[idx].use[0], link.varyings[idx].use[1],
78c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                      link.varyings[idx].use[2], link.varyings[idx].use[3],
79c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                      link.varyings[idx].pa_attributes);
80c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   }
81c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
82c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   /* set last_varying_2x flag if the last varying has 1 or 2 components */
83c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   bool last_varying_2x = false;
84c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (link.num_varyings > 0 && link.varyings[link.num_varyings - 1].num_components <= 2)
85c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      last_varying_2x = true;
86c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
87c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->RA_CONTROL = VIVS_RA_CONTROL_UNK0 |
88c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                    COND(last_varying_2x, VIVS_RA_CONTROL_LAST_VARYING_2X);
89c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
90c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PA_ATTRIBUTE_ELEMENT_COUNT = VIVS_PA_ATTRIBUTE_ELEMENT_COUNT_COUNT(link.num_varyings);
91c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   for (int idx = 0; idx < link.num_varyings; ++idx)
92c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      cs->PA_SHADER_ATTRIBUTES[idx] = link.varyings[idx].pa_attributes;
93c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
94c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->VS_END_PC = vs->code_size / 4;
95c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->VS_OUTPUT_COUNT = 1 + link.num_varyings; /* position + varyings */
96c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
97c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   /* vs outputs (varyings) */
98c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   DEFINE_ETNA_BITARRAY(vs_output, 16, 8) = {0};
99c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   int varid = 0;
100c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   etna_bitarray_set(vs_output, 8, varid++, vs->vs_pos_out_reg);
101c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   for (int idx = 0; idx < link.num_varyings; ++idx)
102c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      etna_bitarray_set(vs_output, 8, varid++, link.varyings[idx].reg);
103c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (vs->vs_pointsize_out_reg >= 0)
104c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      etna_bitarray_set(vs_output, 8, varid++, vs->vs_pointsize_out_reg); /* pointsize is last */
105c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
106c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   for (int idx = 0; idx < ARRAY_SIZE(cs->VS_OUTPUT); ++idx)
107c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      cs->VS_OUTPUT[idx] = vs_output[idx];
108c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
109c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (vs->vs_pointsize_out_reg != -1) {
110c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      /* vertex shader outputs point coordinate, provide extra output and make
111c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors       * sure PA config is
112c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors       * not masked */
113c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      cs->PA_CONFIG = ~0;
114c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT + 1;
115c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   } else {
116c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      /* vertex shader does not output point coordinate, make sure thate
117c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors       * POINT_SIZE_ENABLE is masked
118c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors       * and no extra output is given */
119c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      cs->PA_CONFIG = ~VIVS_PA_CONFIG_POINT_SIZE_ENABLE;
120c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT;
121c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   }
122c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
123c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->VS_LOAD_BALANCING = vs->vs_load_balancing;
124c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->VS_START_PC = 0;
125c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
126c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_END_PC = fs->code_size / 4;
127c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_OUTPUT_REG = fs->ps_color_out_reg;
128c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_INPUT_COUNT =
129c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 1) | /* Number of inputs plus position */
130c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
131c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_TEMP_REGISTER_CONTROL =
132c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 1));
133c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_CONTROL = VIVS_PS_CONTROL_UNK1; /* XXX when can we set BYPASS? */
134c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_START_PC = 0;
135c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
136c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   /* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
137c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors    * mode, avoids some fumbling in sync_context. */
138c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_INPUT_COUNT_MSAA =
139c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 2) | /* MSAA adds another input */
140c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
141c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_TEMP_REGISTER_CONTROL_MSAA =
142c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 2));
143c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
144c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   uint32_t total_components = 0;
145c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   DEFINE_ETNA_BITARRAY(num_components, ETNA_NUM_VARYINGS, 4) = {0};
146c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   DEFINE_ETNA_BITARRAY(component_use, 4 * ETNA_NUM_VARYINGS, 2) = {0};
147c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   for (int idx = 0; idx < link.num_varyings; ++idx) {
148c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      const struct etna_varying *varying = &link.varyings[idx];
149c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
150c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      etna_bitarray_set(num_components, 4, idx, varying->num_components);
151c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      for (int comp = 0; comp < varying->num_components; ++comp) {
152c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors         etna_bitarray_set(component_use, 2, total_components, varying->use[comp]);
153c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors         total_components += 1;
154c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      }
155c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   }
156c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
157c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->GL_VARYING_TOTAL_COMPONENTS =
158c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components, 2));
159c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->GL_VARYING_NUM_COMPONENTS = num_components[0];
160c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->GL_VARYING_COMPONENT_USE[0] = component_use[0];
161c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->GL_VARYING_COMPONENT_USE[1] = component_use[1];
162c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
163c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   /* reference instruction memory */
164c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->vs_inst_mem_size = vs->code_size;
165c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->VS_INST_MEM = vs->code;
166c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->ps_inst_mem_size = fs->code_size;
167c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->PS_INST_MEM = fs->code;
168c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
169c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   return true;
170c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
171c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
172c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsbool
173c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_shader_link(struct etna_context *ctx)
174c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
175c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (!ctx->vs || !ctx->fs)
176c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      return false;
177c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
178c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   /* re-link vs and fs if needed */
179c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   return etna_link_shaders(ctx, &ctx->shader_state, ctx->vs, ctx->fs);
180c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
181c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
182c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsstatic bool
183c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_shader_update_vs_inputs(struct etna_context *ctx,
184c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                             struct compiled_shader_state *cs,
185c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                             const struct etna_shader *vs,
186c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                             const struct compiled_vertex_elements_state *ves)
187c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
188c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   unsigned num_temps, cur_temp, num_vs_inputs;
189c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
190c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (!vs)
191c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      return false;
192c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
193c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   /* Number of vertex elements determines number of VS inputs. Otherwise,
194c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors    * the GPU crashes. Allocate any unused vertex elements to VS temporary
195c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors    * registers. */
196c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   num_vs_inputs = MAX2(ves->num_elements, vs->infile.num_reg);
197c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (num_vs_inputs != ves->num_elements) {
198c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      BUG("Number of elements %u does not match the number of VS inputs %zu",
199c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors          ctx->vertex_elements->num_elements, ctx->vs->infile.num_reg);
200c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      return false;
201c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   }
202c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
203c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cur_temp = vs->num_temps;
204c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   num_temps = num_vs_inputs - vs->infile.num_reg + cur_temp;
205c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
206c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs) |
207c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                        VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8);
208c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   cs->VS_TEMP_REGISTER_CONTROL =
209c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      VIVS_VS_TEMP_REGISTER_CONTROL_NUM_TEMPS(num_temps);
210c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
211c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   /* vs inputs (attributes) */
212c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   DEFINE_ETNA_BITARRAY(vs_input, 16, 8) = {0};
213c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   for (int idx = 0; idx < num_vs_inputs; ++idx) {
214c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      if (idx < vs->infile.num_reg)
215c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors         etna_bitarray_set(vs_input, 8, idx, vs->infile.reg[idx].reg);
216c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      else
217c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors         etna_bitarray_set(vs_input, 8, idx, cur_temp++);
218c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   }
219c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
220c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   for (int idx = 0; idx < ARRAY_SIZE(cs->VS_INPUT); ++idx)
221c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      cs->VS_INPUT[idx] = vs_input[idx];
222c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
223c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   return true;
224c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
225c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
226c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsbool
227c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_shader_update_vertex(struct etna_context *ctx)
228c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
229c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   return etna_shader_update_vs_inputs(ctx, &ctx->shader_state, ctx->vs,
230c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                                       ctx->vertex_elements);
231c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
232c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
233c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsstatic void *
234c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_create_shader_state(struct pipe_context *pctx,
235c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors                         const struct pipe_shader_state *pss)
236c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
237c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   struct etna_context *ctx = etna_context(pctx);
238c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
239c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   return etna_compile_shader(&ctx->specs, pss->tokens);
240c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
241c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
242c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsstatic void
243c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_delete_shader_state(struct pipe_context *pctx, void *ss)
244c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
245c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   etna_destroy_shader(ss);
246c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
247c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
248c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsstatic void
249c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_bind_fs_state(struct pipe_context *pctx, void *fss_)
250c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
251c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   struct etna_context *ctx = etna_context(pctx);
252c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   struct etna_shader *fss = fss_;
253c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
254c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (ctx->fs == fss) /* skip if already bound */
255c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      return;
256c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
257c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   assert(fss == NULL || fss->processor == PIPE_SHADER_FRAGMENT);
258c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   ctx->fs = fss;
259c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   ctx->dirty |= ETNA_DIRTY_SHADER;
260c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
261c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
262c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsstatic void
263c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_bind_vs_state(struct pipe_context *pctx, void *vss_)
264c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
265c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   struct etna_context *ctx = etna_context(pctx);
266c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   struct etna_shader *vss = vss_;
267c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
268c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   if (ctx->vs == vss) /* skip if already bound */
269c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors      return;
270c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
271c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   assert(vss == NULL || vss->processor == PIPE_SHADER_VERTEX);
272c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   ctx->vs = vss;
273c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   ctx->dirty |= ETNA_DIRTY_SHADER;
274c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
275c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors
276c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsvoid
277c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authorsetna_shader_init(struct pipe_context *pctx)
278c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors{
279c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   pctx->create_fs_state = etna_create_shader_state;
280c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   pctx->bind_fs_state = etna_bind_fs_state;
281c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   pctx->delete_fs_state = etna_delete_shader_state;
282c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   pctx->create_vs_state = etna_create_shader_state;
283c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   pctx->bind_vs_state = etna_bind_vs_state;
284c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors   pctx->delete_vs_state = etna_delete_shader_state;
285c9e8b49b885242d84ba031dacef5aa4a5ac1e5b6The etnaviv authors}
286