#include "etnaviv_compiler.h"
#include "etnaviv_context.h"
#include "etnaviv_debug.h"
+#include "etnaviv_screen.h"
#include "etnaviv_util.h"
#include "tgsi/tgsi_parse.h"
+#include "nir/tgsi_to_nir.h"
#include "util/u_math.h"
#include "util/u_memory.h"
+/* Upload shader code to bo, if not already done */
+static bool etna_icache_upload_shader(struct etna_context *ctx, struct etna_shader_variant *v)
+{
+ if (v->bo)
+ return true;
+ v->bo = etna_bo_new(ctx->screen->dev, v->code_size*4, DRM_ETNA_GEM_CACHE_WC);
+ if (!v->bo)
+ return false;
+
+ void *buf = etna_bo_map(v->bo);
+ etna_bo_cpu_prep(v->bo, DRM_ETNA_PREP_WRITE);
+ memcpy(buf, v->code, v->code_size*4);
+ etna_bo_cpu_fini(v->bo);
+ DBG("Uploaded %s of %u words to bo %p", v->stage == MESA_SHADER_FRAGMENT ? "fs":"vs", v->code_size, v->bo);
+ return true;
+}
+
/* Link vs and fs together: fill in shader_state from vs and fs
* as this function is called every time a new fs or vs is bound, the goal is to
* do little processing as possible here, and to precompute as much as possible in
*/
static bool
etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
- const struct etna_shader_variant *vs, const struct etna_shader_variant *fs)
+ struct etna_shader_variant *vs, struct etna_shader_variant *fs)
{
struct etna_shader_link_info link = { };
+ bool failed;
- assert(vs->processor == PIPE_SHADER_VERTEX);
- assert(fs->processor == PIPE_SHADER_FRAGMENT);
+ assert(vs->stage == MESA_SHADER_VERTEX);
+ assert(fs->stage == MESA_SHADER_FRAGMENT);
#ifdef DEBUG
if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS)) {
- etna_dump_shader(vs);
- etna_dump_shader(fs);
+ if (DBG_ENABLED(ETNA_DBG_NIR)) {
+ etna_dump_shader_nir(vs);
+ etna_dump_shader_nir(fs);
+ } else {
+ etna_dump_shader(vs);
+ etna_dump_shader(fs);
+ }
}
#endif
- if (etna_link_shader(&link, vs, fs)) {
+ if (DBG_ENABLED(ETNA_DBG_NIR))
+ failed = etna_link_shader_nir(&link, vs, fs);
+ else
+ failed = etna_link_shader(&link, vs, fs);
+
+ if (failed) {
/* linking failed: some fs inputs do not have corresponding
* vs outputs */
assert(0);
cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT;
}
+ /* if fragment shader doesn't read pointcoord, disable it */
+ if (link.pcoord_varying_comp_ofs == -1)
+ cs->PA_CONFIG &= ~VIVS_PA_CONFIG_POINT_SPRITE_ENABLE;
+
cs->VS_LOAD_BALANCING = vs->vs_load_balancing;
cs->VS_START_PC = 0;
VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
cs->PS_TEMP_REGISTER_CONTROL =
VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 1));
- cs->PS_CONTROL = VIVS_PS_CONTROL_UNK1; /* XXX when can we set BYPASS? */
cs->PS_START_PC = 0;
/* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
cs->GL_VARYING_TOTAL_COMPONENTS =
VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components, 2));
- cs->GL_VARYING_NUM_COMPONENTS = num_components[0];
+ cs->GL_VARYING_NUM_COMPONENTS[0] = num_components[0];
+ cs->GL_VARYING_NUM_COMPONENTS[1] = num_components[1];
cs->GL_VARYING_COMPONENT_USE[0] = component_use[0];
cs->GL_VARYING_COMPONENT_USE[1] = component_use[1];
+ cs->GL_HALTI5_SH_SPECIALS =
+ 0x7f7f0000 | /* unknown bits, probably other PS inputs */
+ /* pointsize is last (see above) */
+ VIVS_GL_HALTI5_SH_SPECIALS_VS_PSIZE_OUT((vs->vs_pointsize_out_reg != -1) ?
+ cs->VS_OUTPUT_COUNT * 4 : 0x00) |
+ VIVS_GL_HALTI5_SH_SPECIALS_PS_PCOORD_IN((link.pcoord_varying_comp_ofs != -1) ?
+ link.pcoord_varying_comp_ofs : 0x7f);
+
+ /* mask out early Z bit when frag depth is written */
+ cs->PE_DEPTH_CONFIG = ~COND(fs->ps_depth_out_reg >= 0, VIVS_PE_DEPTH_CONFIG_EARLY_Z);
+
/* reference instruction memory */
cs->vs_inst_mem_size = vs->code_size;
cs->VS_INST_MEM = vs->code;
+
cs->ps_inst_mem_size = fs->code_size;
cs->PS_INST_MEM = fs->code;
+ if (vs->needs_icache || fs->needs_icache) {
+ /* If either of the shaders needs ICACHE, we use it for both. It is
+ * either switched on or off for the entire shader processor.
+ */
+ if (!etna_icache_upload_shader(ctx, vs) ||
+ !etna_icache_upload_shader(ctx, fs)) {
+ assert(0);
+ return false;
+ }
+
+ cs->VS_INST_ADDR.bo = vs->bo;
+ cs->VS_INST_ADDR.offset = 0;
+ cs->VS_INST_ADDR.flags = ETNA_RELOC_READ;
+ cs->PS_INST_ADDR.bo = fs->bo;
+ cs->PS_INST_ADDR.offset = 0;
+ cs->PS_INST_ADDR.flags = ETNA_RELOC_READ;
+ } else {
+ /* clear relocs */
+ memset(&cs->VS_INST_ADDR, 0, sizeof(cs->VS_INST_ADDR));
+ memset(&cs->PS_INST_ADDR, 0, sizeof(cs->PS_INST_ADDR));
+ }
+
return true;
}
}
static bool
-etna_shader_update_vs_inputs(struct etna_context *ctx,
- struct compiled_shader_state *cs,
+etna_shader_update_vs_inputs(struct compiled_shader_state *cs,
const struct etna_shader_variant *vs,
const struct compiled_vertex_elements_state *ves)
{
num_vs_inputs = MAX2(ves->num_elements, vs->infile.num_reg);
if (num_vs_inputs != ves->num_elements) {
BUG("Number of elements %u does not match the number of VS inputs %zu",
- ctx->vertex_elements->num_elements, ctx->shader.vs->infile.num_reg);
+ ves->num_elements, vs->infile.num_reg);
return false;
}
etna_bitarray_set(vs_input, 8, idx, cur_temp++);
}
+ if (vs->vs_id_in_reg >= 0) {
+ cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs + 1) |
+ VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8) |
+ VIVS_VS_INPUT_COUNT_ID_ENABLE;
+
+ etna_bitarray_set(vs_input, 8, num_vs_inputs, vs->vs_id_in_reg);
+
+ cs->FE_HALTI5_ID_CONFIG =
+ VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_ENABLE |
+ VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_ENABLE |
+ VIVS_FE_HALTI5_ID_CONFIG_VERTEX_ID_REG(vs->vs_id_in_reg * 4) |
+ VIVS_FE_HALTI5_ID_CONFIG_INSTANCE_ID_REG(vs->vs_id_in_reg * 4 + 1);
+ }
+
for (int idx = 0; idx < ARRAY_SIZE(cs->VS_INPUT); ++idx)
cs->VS_INPUT[idx] = vs_input[idx];
static inline const char *
etna_shader_stage(struct etna_shader_variant *shader)
{
- switch (shader->processor) {
- case PIPE_SHADER_VERTEX: return "VERT";
- case PIPE_SHADER_FRAGMENT: return "FRAG";
- case PIPE_SHADER_COMPUTE: return "CL";
+ switch (shader->stage) {
+ case MESA_SHADER_VERTEX: return "VERT";
+ case MESA_SHADER_FRAGMENT: return "FRAG";
+ case MESA_SHADER_COMPUTE: return "CL";
default:
unreachable("invalid type");
return NULL;
if (!unlikely(etna_mesa_debug & ETNA_DBG_SHADERDB))
return;
- pipe_debug_message(debug, SHADER_INFO, "\n"
- "SHADER-DB: %s prog %d/%d: %u instructions %u temps\n"
- "SHADER-DB: %s prog %d/%d: %u immediates %u consts\n"
- "SHADER-DB: %s prog %d/%d: %u loops\n",
+ pipe_debug_message(debug, SHADER_INFO,
+ "%s shader: %u instructions, %u temps, "
+ "%u immediates, %u loops",
etna_shader_stage(v),
- v->shader->id, v->id,
v->code_size,
v->num_temps,
- etna_shader_stage(v),
- v->shader->id, v->id,
v->uniforms.imm_count,
- v->uniforms.const_count,
- etna_shader_stage(v),
- v->shader->id, v->id,
v->num_loops);
}
bool
etna_shader_update_vertex(struct etna_context *ctx)
{
- return etna_shader_update_vs_inputs(ctx, &ctx->shader_state, ctx->shader.vs,
+ return etna_shader_update_vs_inputs(&ctx->shader_state, ctx->shader.vs,
ctx->vertex_elements);
}
const struct pipe_shader_state *pss)
{
struct etna_context *ctx = etna_context(pctx);
+ struct etna_screen *screen = ctx->screen;
struct etna_shader *shader = CALLOC_STRUCT(etna_shader);
if (!shader)
static uint32_t id;
shader->id = id++;
- shader->specs = &ctx->specs;
- shader->tokens = tgsi_dup_tokens(pss->tokens);
+ shader->specs = &screen->specs;
+
+ if (DBG_ENABLED(ETNA_DBG_NIR))
+ shader->nir = (pss->type == PIPE_SHADER_IR_NIR) ? pss->ir.nir :
+ tgsi_to_nir(pss->tokens, pctx->screen);
+ else
+ shader->tokens = tgsi_dup_tokens(pss->tokens);
+
+
if (etna_mesa_debug & ETNA_DBG_SHADERDB) {
/* if shader-db run, create a standard variant immediately
while (v) {
t = v;
v = v->next;
- etna_destroy_shader(t);
+ if (t->bo)
+ etna_bo_del(t->bo);
+
+ if (DBG_ENABLED(ETNA_DBG_NIR))
+ etna_destroy_shader_nir(t);
+ else
+ etna_destroy_shader(t);
}
- FREE(shader->tokens);
+ ralloc_free(shader->nir);
FREE(shader);
}