mesa/main: ff-fragshader to nir

There's little point in emitting GLSL IR for the fixed-function fragment
shaders, when we can emit NIR directly instead.

This simplifies things a bit, and makes the fixed-function vertex and
fragment shaders look a lot more alike.

The reason the old code did the splats, was that TEXENV_SRC_ZERO and
TEXENV_SRC_ONE returned scalars. I decided to keep it vector, and let
the nir optimization passes clean this up instead when needed, as that
keeps the code a bit more straight forward.

Reviewed-by: Adam Jackson <ajax@redhat.com>
Acked-by: Marek Olšák <marek.olsak@amd.com>
Reviewed-by: Emma Anholt <emma@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22755>
This commit is contained in:
Erik Faye-Lund
2023-04-18 22:05:28 +02:00
committed by Marge Bot
parent a37051304a
commit e607a89f22
5 changed files with 324 additions and 365 deletions
+1 -1
View File
@@ -251,7 +251,7 @@ traces:
minetest/minetest-v2.trace: minetest/minetest-v2.trace:
freedreno-a306: freedreno-a306:
checksum: 37081a69137df415cfda13dac62966c4 checksum: 620ee59628e66684033bb6a44e333d89
freedreno-a530: freedreno-a530:
checksum: 824672b1eef72c0223ac754dfc46e2d5 checksum: 824672b1eef72c0223ac754dfc46e2d5
freedreno-a618: freedreno-a618:
+319 -358
View File
@@ -31,28 +31,20 @@
#include "main/context.h" #include "main/context.h"
#include "main/macros.h" #include "main/macros.h"
#include "main/samplerobj.h"
#include "main/shaderobj.h"
#include "main/state.h" #include "main/state.h"
#include "main/texenvprogram.h" #include "main/texenvprogram.h"
#include "main/texobj.h" #include "main/texobj.h"
#include "main/uniforms.h"
#include "compiler/glsl/ir_builder.h"
#include "compiler/glsl/ir_optimization.h"
#include "compiler/glsl/glsl_parser_extras.h"
#include "compiler/glsl/glsl_symbol_table.h"
#include "compiler/glsl_types.h"
#include "program/link_program.h"
#include "program/program.h" #include "program/program.h"
#include "program/programopt.h"
#include "program/prog_cache.h" #include "program/prog_cache.h"
#include "program/prog_instruction.h"
#include "program/prog_parameter.h"
#include "program/prog_print.h"
#include "program/prog_statevars.h" #include "program/prog_statevars.h"
#include "program/prog_to_nir.h"
#include "util/bitscan.h" #include "util/bitscan.h"
using namespace ir_builder; #include "state_tracker/st_context.h"
#include "state_tracker/st_program.h"
#include "compiler/nir/nir_builder.h"
#include "compiler/nir/nir_builtin_builder.h"
/* /*
* Note on texture units: * Note on texture units:
@@ -332,58 +324,107 @@ static GLuint make_state_key( struct gl_context *ctx, struct state_key *key )
/** State used to build the fragment program: /** State used to build the fragment program:
*/ */
class texenv_fragment_program : public ir_factory { struct texenv_fragment_program {
public: nir_builder *b;
struct gl_shader_program *shader_program; struct gl_program_parameter_list *state_params;
struct gl_shader *shader;
exec_list *top_instructions;
struct state_key *state; struct state_key *state;
ir_variable *src_texture[MAX_TEXTURE_COORD_UNITS]; nir_variable *sampler_vars[MAX_TEXTURE_COORD_UNITS];
/* Reg containing each texture unit's sampled texture color,
* else undef. nir_ssa_def *src_texture[MAX_TEXTURE_COORD_UNITS];
/* ssa-def containing each texture unit's sampled texture color,
* else NULL.
*/ */
ir_rvalue *src_previous; /**< Reg containing color from previous nir_ssa_def *src_previous; /**< Color from previous stage */
* stage. May need to be decl'd.
*/
}; };
static ir_rvalue * static nir_variable *
get_current_attrib(texenv_fragment_program *p, GLuint attrib) register_state_var(texenv_fragment_program *p,
gl_state_index s0,
gl_state_index s1,
gl_state_index s2,
gl_state_index s3,
const struct glsl_type *type)
{ {
ir_variable *current; gl_state_index16 tokens[STATE_LENGTH];
char name[128]; tokens[0] = s0;
tokens[1] = s1;
tokens[2] = s2;
tokens[3] = s3;
nir_variable *var = nir_find_state_variable(p->b->shader, tokens);
if (var)
return var;
snprintf(name, sizeof(name), "gl_CurrentAttribFrag%uMESA", attrib); int loc = _mesa_add_state_reference(p->state_params, tokens);
current = p->shader->symbols->get_variable(name); char *name = _mesa_program_state_string(tokens);
assert(current); var = nir_variable_create(p->b->shader, nir_var_uniform, type, name);
return new(p->mem_ctx) ir_dereference_variable(current); free(name);
var->num_state_slots = 1;
var->state_slots = ralloc_array(var, nir_state_slot, 1);
var->data.driver_location = loc;
memcpy(var->state_slots[0].tokens, tokens,
sizeof(var->state_slots[0].tokens));
p->b->shader->num_uniforms++;
return var;
} }
static ir_rvalue * static nir_ssa_def *
load_state_var(texenv_fragment_program *p,
gl_state_index s0,
gl_state_index s1,
gl_state_index s2,
gl_state_index s3,
const struct glsl_type *type)
{
nir_variable *var = register_state_var(p, s0, s1, s2, s3, type);
return nir_load_var(p->b, var);
}
static nir_ssa_def *
load_input(texenv_fragment_program *p, gl_varying_slot slot,
const struct glsl_type *type)
{
nir_variable *var =
nir_get_variable_with_location(p->b->shader,
nir_var_shader_in,
slot,
type);
var->data.interpolation = INTERP_MODE_NONE;
return nir_load_var(p->b, var);
}
static nir_ssa_def *
get_current_attrib(texenv_fragment_program *p, GLuint attrib)
{
return load_state_var(p, STATE_CURRENT_ATTRIB_MAYBE_VP_CLAMPED,
(gl_state_index)attrib,
STATE_NOT_STATE_VAR,
STATE_NOT_STATE_VAR,
glsl_vec4_type());
}
static nir_ssa_def *
get_gl_Color(texenv_fragment_program *p) get_gl_Color(texenv_fragment_program *p)
{ {
if (p->state->inputs_available & VARYING_BIT_COL0) { if (p->state->inputs_available & VARYING_BIT_COL0) {
ir_variable *var = p->shader->symbols->get_variable("gl_Color"); return load_input(p, VARYING_SLOT_COL0, glsl_vec4_type());
assert(var);
return new(p->mem_ctx) ir_dereference_variable(var);
} else { } else {
return get_current_attrib(p, VERT_ATTRIB_COLOR0); return get_current_attrib(p, VERT_ATTRIB_COLOR0);
} }
} }
static ir_rvalue * static nir_ssa_def *
get_source(texenv_fragment_program *p, get_source(texenv_fragment_program *p,
GLuint src, GLuint unit) GLuint src, GLuint unit)
{ {
ir_variable *var;
ir_dereference *deref;
switch (src) { switch (src) {
case TEXENV_SRC_TEXTURE: case TEXENV_SRC_TEXTURE:
return new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]); return p->src_texture[unit];
case TEXENV_SRC_TEXTURE0: case TEXENV_SRC_TEXTURE0:
case TEXENV_SRC_TEXTURE1: case TEXENV_SRC_TEXTURE1:
@@ -393,33 +434,29 @@ get_source(texenv_fragment_program *p,
case TEXENV_SRC_TEXTURE5: case TEXENV_SRC_TEXTURE5:
case TEXENV_SRC_TEXTURE6: case TEXENV_SRC_TEXTURE6:
case TEXENV_SRC_TEXTURE7: case TEXENV_SRC_TEXTURE7:
return new(p->mem_ctx) return p->src_texture[src - TEXENV_SRC_TEXTURE0];
ir_dereference_variable(p->src_texture[src - TEXENV_SRC_TEXTURE0]);
case TEXENV_SRC_CONSTANT: case TEXENV_SRC_CONSTANT:
var = p->shader->symbols->get_variable("gl_TextureEnvColor"); return load_state_var(p, STATE_TEXENV_COLOR,
assert(var); (gl_state_index)unit,
deref = new(p->mem_ctx) ir_dereference_variable(var); STATE_NOT_STATE_VAR,
var->data.max_array_access = MAX2(var->data.max_array_access, (int)unit); STATE_NOT_STATE_VAR,
return new(p->mem_ctx) ir_dereference_array(deref, glsl_vec4_type());
new(p->mem_ctx) ir_constant(unit));
case TEXENV_SRC_PRIMARY_COLOR: case TEXENV_SRC_PRIMARY_COLOR:
var = p->shader->symbols->get_variable("gl_Color"); return get_gl_Color(p);
assert(var);
return new(p->mem_ctx) ir_dereference_variable(var);
case TEXENV_SRC_ZERO: case TEXENV_SRC_ZERO:
return new(p->mem_ctx) ir_constant(0.0f); return nir_imm_zero(p->b, 4, 32);
case TEXENV_SRC_ONE: case TEXENV_SRC_ONE:
return new(p->mem_ctx) ir_constant(1.0f); return nir_imm_vec4(p->b, 1.0f, 1.0f, 1.0f, 1.0f);
case TEXENV_SRC_PREVIOUS: case TEXENV_SRC_PREVIOUS:
if (!p->src_previous) { if (!p->src_previous) {
return get_gl_Color(p); return get_gl_Color(p);
} else { } else {
return p->src_previous->clone(p->mem_ctx, NULL); return p->src_previous;
} }
default: default:
@@ -428,27 +465,28 @@ get_source(texenv_fragment_program *p,
} }
} }
static ir_rvalue * static nir_ssa_def *
emit_combine_source(texenv_fragment_program *p, emit_combine_source(texenv_fragment_program *p,
GLuint unit, GLuint unit,
GLuint source, GLuint source,
GLuint operand) GLuint operand)
{ {
ir_rvalue *src; nir_ssa_def *src;
src = get_source(p, source, unit); src = get_source(p, source, unit);
switch (operand) { switch (operand) {
case TEXENV_OPR_ONE_MINUS_COLOR: case TEXENV_OPR_ONE_MINUS_COLOR:
return sub(new(p->mem_ctx) ir_constant(1.0f), src); return nir_fsub(p->b, nir_imm_float(p->b, 1.0), src);
case TEXENV_OPR_ALPHA: case TEXENV_OPR_ALPHA:
return src->type->is_scalar() ? src : swizzle_w(src); return src->num_components == 1 ? src : nir_channel(p->b, src, 3);
case TEXENV_OPR_ONE_MINUS_ALPHA: { case TEXENV_OPR_ONE_MINUS_ALPHA: {
ir_rvalue *const scalar = src->type->is_scalar() ? src : swizzle_w(src); nir_ssa_def *scalar =
src->num_components == 1 ? src : nir_channel(p->b, src, 3);
return sub(new(p->mem_ctx) ir_constant(1.0f), scalar); return nir_fsub(p->b, nir_imm_float(p->b, 1.0), scalar);
} }
case TEXENV_OPR_COLOR: case TEXENV_OPR_COLOR:
@@ -500,24 +538,24 @@ static GLboolean args_match( const struct state_key *key, GLuint unit )
return GL_TRUE; return GL_TRUE;
} }
static ir_rvalue * static nir_ssa_def *
smear(ir_rvalue *val) smear(nir_builder *b, nir_ssa_def *val)
{ {
if (!val->type->is_scalar()) if (val->num_components != 1)
return val; return val;
return swizzle_xxxx(val); return nir_vec4(b, val, val, val, val);
} }
static ir_rvalue * static nir_ssa_def *
emit_combine(texenv_fragment_program *p, emit_combine(struct texenv_fragment_program *p,
GLuint unit, GLuint unit,
GLuint nr, GLuint nr,
GLuint mode, GLuint mode,
const struct gl_tex_env_argument *opt) const struct gl_tex_env_argument *opt)
{ {
ir_rvalue *src[MAX_COMBINER_TERMS]; nir_ssa_def *src[MAX_COMBINER_TERMS];
ir_rvalue *tmp0, *tmp1; nir_ssa_def *tmp0, *tmp1;
GLuint i; GLuint i;
assert(nr <= MAX_COMBINER_TERMS); assert(nr <= MAX_COMBINER_TERMS);
@@ -530,52 +568,51 @@ emit_combine(texenv_fragment_program *p,
return src[0]; return src[0];
case TEXENV_MODE_MODULATE: case TEXENV_MODE_MODULATE:
return mul(src[0], src[1]); return nir_fmul(p->b, src[0], src[1]);
case TEXENV_MODE_ADD: case TEXENV_MODE_ADD:
return add(src[0], src[1]); return nir_fadd(p->b, src[0], src[1]);
case TEXENV_MODE_ADD_SIGNED: case TEXENV_MODE_ADD_SIGNED:
return add(add(src[0], src[1]), new(p->mem_ctx) ir_constant(-0.5f)); return nir_fadd_imm(p->b, nir_fadd(p->b, src[0], src[1]), -0.5f);
case TEXENV_MODE_INTERPOLATE: case TEXENV_MODE_INTERPOLATE:
/* Arg0 * (Arg2) + Arg1 * (1-Arg2) */ return nir_flrp(p->b, src[1], src[0], src[2]);
tmp0 = mul(src[0], src[2]);
tmp1 = mul(src[1], sub(new(p->mem_ctx) ir_constant(1.0f),
src[2]->clone(p->mem_ctx, NULL)));
return add(tmp0, tmp1);
case TEXENV_MODE_SUBTRACT: case TEXENV_MODE_SUBTRACT:
return sub(src[0], src[1]); return nir_fsub(p->b, src[0], src[1]);
case TEXENV_MODE_DOT3_RGBA: case TEXENV_MODE_DOT3_RGBA:
case TEXENV_MODE_DOT3_RGBA_EXT: case TEXENV_MODE_DOT3_RGBA_EXT:
case TEXENV_MODE_DOT3_RGB_EXT: case TEXENV_MODE_DOT3_RGB_EXT:
case TEXENV_MODE_DOT3_RGB: { case TEXENV_MODE_DOT3_RGB:
tmp0 = mul(src[0], new(p->mem_ctx) ir_constant(2.0f)); tmp0 = nir_fadd_imm(p->b, nir_fmul_imm(p->b, src[0], 2.0f), -1.0f);
tmp0 = add(tmp0, new(p->mem_ctx) ir_constant(-1.0f)); tmp1 = nir_fadd_imm(p->b, nir_fmul_imm(p->b, src[1], 2.0f), -1.0f);
return nir_fdot3(p->b, smear(p->b, tmp0), smear(p->b, tmp1));
tmp1 = mul(src[1], new(p->mem_ctx) ir_constant(2.0f));
tmp1 = add(tmp1, new(p->mem_ctx) ir_constant(-1.0f));
return dot(swizzle_xyz(smear(tmp0)), swizzle_xyz(smear(tmp1)));
}
case TEXENV_MODE_MODULATE_ADD_ATI: case TEXENV_MODE_MODULATE_ADD_ATI:
return add(mul(src[0], src[2]), src[1]); return nir_fmad(p->b, src[0], src[2], src[1]);
case TEXENV_MODE_MODULATE_SIGNED_ADD_ATI: case TEXENV_MODE_MODULATE_SIGNED_ADD_ATI:
return add(add(mul(src[0], src[2]), src[1]), return nir_fadd_imm(p->b,
new(p->mem_ctx) ir_constant(-0.5f)); nir_fadd(p->b,
nir_fmul(p->b, src[0], src[2]),
src[1]),
-0.5f);
case TEXENV_MODE_MODULATE_SUBTRACT_ATI: case TEXENV_MODE_MODULATE_SUBTRACT_ATI:
return sub(mul(src[0], src[2]), src[1]); return nir_fsub(p->b, nir_fmul(p->b, src[0], src[2]), src[1]);
case TEXENV_MODE_ADD_PRODUCTS_NV: case TEXENV_MODE_ADD_PRODUCTS_NV:
return add(mul(src[0], src[1]), mul(src[2], src[3])); return nir_fadd(p->b, nir_fmul(p->b, src[0], src[1]),
nir_fmul(p->b, src[2], src[3]));
case TEXENV_MODE_ADD_PRODUCTS_SIGNED_NV: case TEXENV_MODE_ADD_PRODUCTS_SIGNED_NV:
return add(add(mul(src[0], src[1]), mul(src[2], src[3])), return nir_fadd_imm(p->b,
new(p->mem_ctx) ir_constant(-0.5f)); nir_fadd(p->b,
nir_fmul(p->b, src[0], src[1]),
nir_fmul(p->b, src[2], src[3])),
-0.5f);
default: default:
assert(0); assert(0);
return src[0]; return src[0];
@@ -585,7 +622,7 @@ emit_combine(texenv_fragment_program *p,
/** /**
* Generate instructions for one texture unit's env/combiner mode. * Generate instructions for one texture unit's env/combiner mode.
*/ */
static ir_rvalue * static nir_ssa_def *
emit_texenv(texenv_fragment_program *p, GLuint unit) emit_texenv(texenv_fragment_program *p, GLuint unit)
{ {
const struct state_key *key = p->state; const struct state_key *key = p->state;
@@ -628,9 +665,7 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
else else
alpha_saturate = GL_FALSE; alpha_saturate = GL_FALSE;
ir_variable *temp_var = p->make_temp(glsl_type::vec4_type, "texenv_combine"); nir_ssa_def *val;
ir_dereference *deref;
ir_rvalue *val;
/* Emit the RGB and A combine ops /* Emit the RGB and A combine ops
*/ */
@@ -640,22 +675,19 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
key->unit[unit].NumArgsRGB, key->unit[unit].NumArgsRGB,
key->unit[unit].ModeRGB, key->unit[unit].ModeRGB,
key->unit[unit].ArgsRGB); key->unit[unit].ArgsRGB);
val = smear(val); val = smear(p->b, val);
if (rgb_saturate) if (rgb_saturate)
val = saturate(val); val = nir_fsat(p->b, val);
p->emit(assign(temp_var, val));
} }
else if (key->unit[unit].ModeRGB == TEXENV_MODE_DOT3_RGBA_EXT || else if (key->unit[unit].ModeRGB == TEXENV_MODE_DOT3_RGBA_EXT ||
key->unit[unit].ModeRGB == TEXENV_MODE_DOT3_RGBA) { key->unit[unit].ModeRGB == TEXENV_MODE_DOT3_RGBA) {
ir_rvalue *val = emit_combine(p, unit, val = emit_combine(p, unit,
key->unit[unit].NumArgsRGB, key->unit[unit].NumArgsRGB,
key->unit[unit].ModeRGB, key->unit[unit].ModeRGB,
key->unit[unit].ArgsRGB); key->unit[unit].ArgsRGB);
val = smear(val); val = smear(p->b, val);
if (rgb_saturate) if (rgb_saturate)
val = saturate(val); val = nir_fsat(p->b, val);
p->emit(assign(temp_var, val));
} }
else { else {
/* Need to do something to stop from re-emitting identical /* Need to do something to stop from re-emitting identical
@@ -665,47 +697,46 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
key->unit[unit].NumArgsRGB, key->unit[unit].NumArgsRGB,
key->unit[unit].ModeRGB, key->unit[unit].ModeRGB,
key->unit[unit].ArgsRGB); key->unit[unit].ArgsRGB);
val = swizzle_xyz(smear(val)); val = smear(p->b, val);
if (rgb_saturate) if (rgb_saturate)
val = saturate(val); val = nir_fsat(p->b, val);
p->emit(assign(temp_var, val, WRITEMASK_XYZ)); nir_ssa_def *rgb = val;
val = emit_combine(p, unit, val = emit_combine(p, unit,
key->unit[unit].NumArgsA, key->unit[unit].NumArgsA,
key->unit[unit].ModeA, key->unit[unit].ModeA,
key->unit[unit].ArgsA); key->unit[unit].ArgsA);
val = swizzle_w(smear(val));
if (alpha_saturate)
val = saturate(val);
p->emit(assign(temp_var, val, WRITEMASK_W));
}
deref = new(p->mem_ctx) ir_dereference_variable(temp_var); if (val->num_components != 1)
val = nir_channel(p->b, val, 3);
if (alpha_saturate)
val = nir_fsat(p->b, val);
nir_ssa_def *a = val;
val = nir_vector_insert_imm(p->b, rgb, a, 3);
}
/* Deal with the final shift: /* Deal with the final shift:
*/ */
if (alpha_shift || rgb_shift) { if (alpha_shift || rgb_shift) {
ir_constant *shift; nir_ssa_def *shift;
if (rgb_shift == alpha_shift) { if (rgb_shift == alpha_shift) {
shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift)); shift = nir_imm_float(p->b, (float)(1 << rgb_shift));
} }
else { else {
ir_constant_data const_data; shift = nir_imm_vec4(p->b,
(float)(1 << rgb_shift),
const_data.f[0] = float(1 << rgb_shift); (float)(1 << rgb_shift),
const_data.f[1] = float(1 << rgb_shift); (float)(1 << rgb_shift),
const_data.f[2] = float(1 << rgb_shift); (float)(1 << alpha_shift));
const_data.f[3] = float(1 << alpha_shift);
shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
&const_data);
} }
return saturate(mul(deref, shift)); return nir_fsat(p->b, nir_fmul(p->b, val, shift));
} }
else else
return deref; return val;
} }
@@ -714,127 +745,90 @@ emit_texenv(texenv_fragment_program *p, GLuint unit)
*/ */
static void load_texture( texenv_fragment_program *p, GLuint unit ) static void load_texture( texenv_fragment_program *p, GLuint unit )
{ {
ir_dereference *deref;
if (p->src_texture[unit]) if (p->src_texture[unit])
return; return;
const GLuint texTarget = p->state->unit[unit].source_index; const GLuint texTarget = p->state->unit[unit].source_index;
ir_rvalue *texcoord; nir_ssa_def *texcoord;
if (!(p->state->inputs_available & (VARYING_BIT_TEX0 << unit))) { if (!(p->state->inputs_available & (VARYING_BIT_TEX0 << unit))) {
texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit); texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit);
} else { } else {
ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord"); texcoord = load_input(p,
assert(tc_array); (gl_varying_slot)(VARYING_SLOT_TEX0 + unit),
texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array); glsl_vec4_type());
ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
tc_array->data.max_array_access = MAX2(tc_array->data.max_array_access, (int)unit);
} }
if (!p->state->unit[unit].enabled) { if (!p->state->unit[unit].enabled) {
p->src_texture[unit] = p->make_temp(glsl_type::vec4_type, p->src_texture[unit] = nir_imm_zero(p->b, 4, 32);
"dummy_tex");
p->emit(p->src_texture[unit]);
p->emit(assign(p->src_texture[unit], new(p->mem_ctx) ir_constant(0.0f)));
return ; return ;
} }
const glsl_type *sampler_type = NULL; unsigned num_srcs = 4;
int coords = 0; if (p->state->unit[unit].shadow)
num_srcs++;
switch (texTarget) { nir_tex_instr *tex = nir_tex_instr_create(p->b->shader, num_srcs);
case TEXTURE_1D_INDEX: tex->op = nir_texop_tex;
if (p->state->unit[unit].shadow) tex->dest_type = nir_type_float32;
sampler_type = glsl_type::sampler1DShadow_type; tex->texture_index = unit;
else tex->sampler_index = unit;
sampler_type = glsl_type::sampler1D_type;
coords = 1; tex->sampler_dim =
break; _mesa_texture_index_to_sampler_dim((gl_texture_index)texTarget,
case TEXTURE_1D_ARRAY_INDEX: &tex->is_array);
if (p->state->unit[unit].shadow)
sampler_type = glsl_type::sampler1DArrayShadow_type; tex->coord_components =
else glsl_get_sampler_dim_coordinate_components(tex->sampler_dim);
sampler_type = glsl_type::sampler1DArray_type; if (tex->is_array)
coords = 2; tex->coord_components++;
break;
case TEXTURE_2D_INDEX: nir_variable *var = p->sampler_vars[unit];
if (p->state->unit[unit].shadow) if (!var) {
sampler_type = glsl_type::sampler2DShadow_type; const struct glsl_type *sampler_type =
else glsl_sampler_type(tex->sampler_dim,
sampler_type = glsl_type::sampler2D_type; p->state->unit[unit].shadow,
coords = 2; tex->is_array, GLSL_TYPE_FLOAT);
break;
case TEXTURE_2D_ARRAY_INDEX: var = nir_variable_create(p->b->shader, nir_var_uniform,
if (p->state->unit[unit].shadow) sampler_type,
sampler_type = glsl_type::sampler2DArrayShadow_type; ralloc_asprintf(p->b->shader,
else "sampler_%d", unit));
sampler_type = glsl_type::sampler2DArray_type; var->data.binding = unit;
coords = 3; var->data.explicit_binding = true;
break;
case TEXTURE_RECT_INDEX: p->sampler_vars[unit] = var;
if (p->state->unit[unit].shadow)
sampler_type = glsl_type::sampler2DRectShadow_type;
else
sampler_type = glsl_type::sampler2DRect_type;
coords = 2;
break;
case TEXTURE_3D_INDEX:
assert(!p->state->unit[unit].shadow);
sampler_type = glsl_type::sampler3D_type;
coords = 3;
break;
case TEXTURE_CUBE_INDEX:
if (p->state->unit[unit].shadow)
sampler_type = glsl_type::samplerCubeShadow_type;
else
sampler_type = glsl_type::samplerCube_type;
coords = 3;
break;
case TEXTURE_EXTERNAL_INDEX:
assert(!p->state->unit[unit].shadow);
sampler_type = glsl_type::samplerExternalOES_type;
coords = 2;
break;
} }
p->src_texture[unit] = p->make_temp(glsl_type::vec4_type, nir_deref_instr *deref = nir_build_deref_var(p->b, var);
"tex"); tex->src[0].src = nir_src_for_ssa(&deref->dest.ssa);
tex->src[0].src_type = nir_tex_src_texture_deref;
tex->src[1].src = nir_src_for_ssa(&deref->dest.ssa);
tex->src[1].src_type = nir_tex_src_sampler_deref;
ir_texture *tex = new(p->mem_ctx) ir_texture(ir_tex); nir_ssa_def *src2 =
nir_channels(p->b, texcoord,
nir_component_mask(tex->coord_components));
tex->src[2].src_type = nir_tex_src_coord;
tex->src[2].src = nir_src_for_ssa(src2);
tex->src[3].src_type = nir_tex_src_projector;
char *sampler_name = ralloc_asprintf(p->mem_ctx, "sampler_%d", unit); tex->src[3].src = nir_src_for_ssa(nir_channel(p->b, texcoord, 3));
ir_variable *sampler = new(p->mem_ctx) ir_variable(sampler_type,
sampler_name,
ir_var_uniform);
p->top_instructions->push_head(sampler);
/* Set the texture unit for this sampler in the same way that
* layout(binding=X) would.
*/
sampler->data.explicit_binding = true;
sampler->data.binding = unit;
deref = new(p->mem_ctx) ir_dereference_variable(sampler);
tex->set_sampler(deref, glsl_type::vec4_type);
tex->coordinate = new(p->mem_ctx) ir_swizzle(texcoord, 0, 1, 2, 3, coords);
if (p->state->unit[unit].shadow) { if (p->state->unit[unit].shadow) {
texcoord = texcoord->clone(p->mem_ctx, NULL); tex->is_shadow = true;
tex->shadow_comparator = new(p->mem_ctx) ir_swizzle(texcoord, nir_ssa_def *src4 =
coords, 0, 0, 0, nir_channel(p->b, texcoord, tex->coord_components);
1); tex->src[4].src_type = nir_tex_src_comparator;
coords++; tex->src[4].src = nir_src_for_ssa(src4);
} }
texcoord = texcoord->clone(p->mem_ctx, NULL); nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32);
tex->projector = swizzle_w(texcoord); p->src_texture[unit] = &tex->dest.ssa;
p->emit(assign(p->src_texture[unit], tex)); nir_builder_instr_insert(p->b, &tex->instr);
BITSET_SET(p->b->shader->info.textures_used, unit);
BITSET_SET(p->b->shader->info.samplers_used, unit);
} }
static void static void
@@ -891,34 +885,38 @@ load_texunit_sources( texenv_fragment_program *p, GLuint unit )
* that ffvertex_prog.c produces fogcoord for us when * that ffvertex_prog.c produces fogcoord for us when
* GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT. * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
*/ */
static ir_rvalue * static nir_ssa_def *
emit_fog_instructions(texenv_fragment_program *p, emit_fog_instructions(texenv_fragment_program *p,
ir_rvalue *fragcolor) nir_ssa_def *fragcolor)
{ {
struct state_key *key = p->state; struct state_key *key = p->state;
ir_rvalue *f, *temp; nir_ssa_def *color, *oparams;
ir_variable *params, *oparams; nir_ssa_def *fogcoord;
ir_variable *fogcoord;
/* Temporary storage for the whole fog result. Fog calculations /* Temporary storage for the whole fog result. Fog calculations
* only affect rgb so we're hanging on to the .a value of fragcolor * only affect rgb so we're hanging on to the .a value of fragcolor
* this way. * this way.
*/ */
ir_variable *fog_result = p->make_temp(glsl_type::vec4_type, "fog_result"); nir_ssa_def *fog_alpha = nir_channel(p->b, fragcolor, 3);
p->emit(assign(fog_result, fragcolor));
fragcolor = swizzle_xyz(fog_result); oparams = load_state_var(p, STATE_FOG_PARAMS_OPTIMIZED,
STATE_NOT_STATE_VAR,
oparams = p->shader->symbols->get_variable("gl_FogParamsOptimizedMESA"); STATE_NOT_STATE_VAR,
STATE_NOT_STATE_VAR,
glsl_vec4_type());
assert(oparams); assert(oparams);
fogcoord = p->shader->symbols->get_variable("gl_FogFragCoord");
fogcoord = load_input(p, VARYING_SLOT_FOGC, glsl_float_type());
assert(fogcoord); assert(fogcoord);
params = p->shader->symbols->get_variable("gl_Fog");
assert(params);
f = new(p->mem_ctx) ir_dereference_variable(fogcoord);
ir_variable *f_var = p->make_temp(glsl_type::float_type, "fog_factor"); color = load_state_var(p, STATE_FOG_COLOR,
STATE_NOT_STATE_VAR,
STATE_NOT_STATE_VAR,
STATE_NOT_STATE_VAR,
glsl_vec4_type());
assert(color);
nir_ssa_def *f = fogcoord;
switch (key->fog_mode) { switch (key->fog_mode) {
case FOG_LINEAR: case FOG_LINEAR:
/* f = (end - z) / (end - start) /* f = (end - z) / (end - start)
@@ -926,7 +924,9 @@ emit_fog_instructions(texenv_fragment_program *p,
* gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
* (end / (end - start)) so we can generate a single MAD. * (end / (end - start)) so we can generate a single MAD.
*/ */
f = add(mul(f, swizzle_x(oparams)), swizzle_y(oparams)); f = nir_fadd(p->b, nir_fmul(p->b, f,
nir_channel(p->b, oparams, 0)),
nir_channel(p->b, oparams, 1));
break; break;
case FOG_EXP: case FOG_EXP:
/* f = e^(-(density * fogcoord)) /* f = e^(-(density * fogcoord))
@@ -935,9 +935,8 @@ emit_fog_instructions(texenv_fragment_program *p,
* use EXP2 which is generally the native instruction without * use EXP2 which is generally the native instruction without
* having to do any further math on the fog density uniform. * having to do any further math on the fog density uniform.
*/ */
f = mul(f, swizzle_z(oparams)); f = nir_fmul(p->b, f, nir_channel(p->b, oparams, 2));
f = new(p->mem_ctx) ir_expression(ir_unop_neg, f); f = nir_fexp2(p->b, nir_fneg(p->b, f));
f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
break; break;
case FOG_EXP2: case FOG_EXP2:
/* f = e^(-(density * fogcoord)^2) /* f = e^(-(density * fogcoord)^2)
@@ -946,25 +945,15 @@ emit_fog_instructions(texenv_fragment_program *p,
* can do this like FOG_EXP but with a squaring after the * can do this like FOG_EXP but with a squaring after the
* multiply by density. * multiply by density.
*/ */
ir_variable *temp_var = p->make_temp(glsl_type::float_type, "fog_temp"); f = nir_fmul(p->b, f, nir_channel(p->b, oparams, 3));
p->emit(assign(temp_var, mul(f, swizzle_w(oparams)))); f = nir_fmul(p->b, f, f);
f = nir_fexp2(p->b, nir_fneg(p->b, f));
f = mul(temp_var, temp_var);
f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
break; break;
} }
p->emit(assign(f_var, saturate(f))); f = nir_fsat(p->b, f);
nir_ssa_def *fog_rgb = nir_flrp(p->b, color, fragcolor, f);
f = sub(new(p->mem_ctx) ir_constant(1.0f), f_var); return nir_vector_insert_imm(p->b, fog_rgb, fog_alpha, 3);
temp = new(p->mem_ctx) ir_dereference_variable(params);
temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
temp = mul(swizzle_xyz(temp), f);
p->emit(assign(fog_result, add(temp, mul(fragcolor, f_var)), WRITEMASK_XYZ));
return new(p->mem_ctx) ir_dereference_variable(fog_result);
} }
static void static void
@@ -992,124 +981,77 @@ emit_instructions(texenv_fragment_program *p)
} }
} }
ir_rvalue *cf = get_source(p, TEXENV_SRC_PREVIOUS, 0); nir_ssa_def *cf = get_source(p, TEXENV_SRC_PREVIOUS, 0);
if (key->separate_specular) { if (key->separate_specular) {
ir_variable *spec_result = p->make_temp(glsl_type::vec4_type, nir_ssa_def *spec_result = cf;
"specular_add");
p->emit(assign(spec_result, cf));
ir_rvalue *secondary; nir_ssa_def *secondary;
if (p->state->inputs_available & VARYING_BIT_COL1) { if (p->state->inputs_available & VARYING_BIT_COL1)
ir_variable *var = secondary = load_input(p, VARYING_SLOT_COL1, glsl_vec4_type());
p->shader->symbols->get_variable("gl_SecondaryColor"); else
assert(var); secondary = get_current_attrib(p, VERT_ATTRIB_COLOR1);
secondary = swizzle_xyz(var);
} else {
secondary = swizzle_xyz(get_current_attrib(p, VERT_ATTRIB_COLOR1));
}
p->emit(assign(spec_result, add(swizzle_xyz(spec_result), secondary), secondary = nir_vector_insert_imm(p->b, secondary,
WRITEMASK_XYZ)); nir_imm_zero(p->b, 1, 32), 3);
cf = nir_fadd(p->b, spec_result, secondary);
cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
} }
if (key->fog_mode) { if (key->fog_mode) {
cf = emit_fog_instructions(p, cf); cf = emit_fog_instructions(p, cf);
} }
ir_variable *frag_color = p->shader->symbols->get_variable("gl_FragColor"); const char *name =
assert(frag_color); gl_frag_result_name(FRAG_RESULT_COLOR);
p->emit(assign(frag_color, cf)); nir_variable *var =
nir_variable_create(p->b->shader, nir_var_shader_out,
glsl_vec4_type(), name);
var->data.location = FRAG_RESULT_COLOR;
var->data.driver_location = p->b->shader->num_outputs++;
p->b->shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_COLOR);
nir_store_var(p->b, var, cf, 0xf);
} }
/** /**
* Generate a new fragment program which implements the context's * Generate a new fragment program which implements the context's
* current texture env/combine mode. * current texture env/combine mode.
*/ */
static struct gl_shader_program * static nir_shader *
create_new_program(struct gl_context *ctx, struct state_key *key) create_new_program(struct state_key *key,
struct gl_program *program,
const nir_shader_compiler_options *options)
{ {
texenv_fragment_program p; texenv_fragment_program p;
unsigned int unit;
_mesa_glsl_parse_state *state;
p.mem_ctx = ralloc_context(NULL); memset(&p, 0, sizeof(p));
p.shader = _mesa_new_shader(0, MESA_SHADER_FRAGMENT);
p.shader->ir = new(p.shader) exec_list;
state = new(p.shader) _mesa_glsl_parse_state(ctx, MESA_SHADER_FRAGMENT,
p.shader);
p.shader->symbols = state->symbols;
p.top_instructions = p.shader->ir;
p.instructions = p.shader->ir;
p.state = key; p.state = key;
p.shader_program = _mesa_new_shader_program(0);
/* Tell the linker to ignore the fact that we're building a program->Parameters = _mesa_new_parameter_list();
* separate shader, in case we're in a GLES2 context that would p.state_params = _mesa_new_parameter_list();
* normally reject that. The real problem is that we're building a
* fixed function program in a GLES2 context at all, but that's a
* big mess to clean up.
*/
p.shader_program->SeparateShader = GL_TRUE;
/* The legacy GLSL shadow functions follow the depth texture nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT,
* mode and return vec4. The GLSL 1.30 shadow functions return float and options,
* ignore the depth texture mode. That's a shader and state dependency "ff-fs");
* that's difficult to deal with. st/mesa uses a simple but not
* completely correct solution: if the shader declares GLSL >= 1.30 and
* the depth texture mode is GL_ALPHA (000X), it sets the XXXX swizzle
* instead. Thus, the GLSL 1.30 shadow function will get the result in .x
* and legacy shadow functions will get it in .w as expected.
* For the fixed-function fragment shader, use 120 to get correct behavior
* for GL_ALPHA.
*/
state->language_version = 120;
state->es_shader = false; nir_shader *s = b.shader;
if (_mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external)
state->OES_EGL_image_external_enable = true;
_mesa_glsl_initialize_types(state);
_mesa_glsl_initialize_variables(p.instructions, state);
for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) s->info.separate_shader = true;
p.src_texture[unit] = NULL; s->info.subgroup_size = SUBGROUP_SIZE_UNIFORM;
p.src_previous = NULL; p.b = &b;
ir_function *main_f = new(p.mem_ctx) ir_function("main");
p.emit(main_f);
state->symbols->add_function(main_f);
ir_function_signature *main_sig =
new(p.mem_ctx) ir_function_signature(glsl_type::void_type);
main_sig->is_defined = true;
main_f->add_signature(main_sig);
p.instructions = &main_sig->body;
if (key->num_draw_buffers) if (key->num_draw_buffers)
emit_instructions(&p); emit_instructions(&p);
validate_ir_tree(p.shader->ir); nir_validate_shader(b.shader, "after generating ff-vertex shader");
reparent_ir(p.shader->ir, p.shader->ir); _mesa_add_separate_state_parameters(program, p.state_params);
_mesa_free_parameter_list(p.state_params);
p.shader->CompileStatus = COMPILE_SUCCESS; return s;
p.shader->Version = state->language_version;
p.shader_program->Shaders =
(gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
p.shader_program->Shaders[0] = p.shader;
p.shader_program->NumShaders = 1;
_mesa_glsl_link_shader(ctx, p.shader_program);
if (!p.shader_program->data->LinkStatus)
_mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
p.shader_program->data->InfoLog);
ralloc_free(p.mem_ctx);
return p.shader_program;
} }
extern "C" { extern "C" {
@@ -1118,27 +1060,46 @@ extern "C" {
* Return a fragment program which implements the current * Return a fragment program which implements the current
* fixed-function texture, fog and color-sum operations. * fixed-function texture, fog and color-sum operations.
*/ */
struct gl_shader_program * struct gl_program *
_mesa_get_fixed_func_fragment_program(struct gl_context *ctx) _mesa_get_fixed_func_fragment_program(struct gl_context *ctx)
{ {
struct gl_shader_program *shader_program; struct gl_program *prog;
struct state_key key; struct state_key key;
GLuint keySize; GLuint keySize;
keySize = make_state_key(ctx, &key); keySize = make_state_key(ctx, &key);
shader_program = (struct gl_shader_program *) prog = (struct gl_program *)
_mesa_search_program_cache(ctx->FragmentProgram.Cache, _mesa_search_program_cache(ctx->FragmentProgram.Cache,
&key, keySize); &key, keySize);
if (!shader_program) { if (!prog) {
shader_program = create_new_program(ctx, &key); prog = ctx->Driver.NewProgram(ctx, MESA_SHADER_FRAGMENT, 0, false);
if (!prog)
return NULL;
_mesa_shader_cache_insert(ctx, ctx->FragmentProgram.Cache, const struct nir_shader_compiler_options *options =
&key, keySize, shader_program); st_get_nir_compiler_options(ctx->st, MESA_SHADER_FRAGMENT);
nir_shader *s =
create_new_program(&key, prog, options);
prog->state.type = PIPE_SHADER_IR_NIR;
prog->nir = s;
prog->SamplersUsed = s->info.samplers_used[0];
/* default mapping from samplers to texture units */
for (unsigned i = 0; i < MAX_SAMPLERS; i++)
prog->SamplerUnits[i] = i;
st_program_string_notify(ctx, GL_FRAGMENT_PROGRAM_ARB, prog);
_mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
&key, keySize, prog);
} }
return shader_program; return prog;
} }
} }
+2 -4
View File
@@ -256,12 +256,10 @@ update_program(struct gl_context *ctx)
NULL); NULL);
} else { } else {
/* Use fragment program generated from fixed-function state */ /* Use fragment program generated from fixed-function state */
struct gl_shader_program *f = _mesa_get_fixed_func_fragment_program(ctx);
_mesa_reference_program(ctx, &ctx->FragmentProgram._Current, _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program); _mesa_get_fixed_func_fragment_program(ctx));
_mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram, _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
f->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program); ctx->FragmentProgram._Current);
} }
/* Examine vertex program after fragment program as /* Examine vertex program after fragment program as
+1 -1
View File
@@ -34,7 +34,7 @@ extern "C" {
struct gl_context; struct gl_context;
extern struct gl_shader_program * extern struct gl_program *
_mesa_get_fixed_func_fragment_program(struct gl_context *ctx); _mesa_get_fixed_func_fragment_program(struct gl_context *ctx);
+1 -1
View File
@@ -123,7 +123,7 @@ _mesa_free_program_data(struct gl_context *ctx)
_mesa_reference_program(ctx, &ctx->VertexProgram.Current, NULL); _mesa_reference_program(ctx, &ctx->VertexProgram.Current, NULL);
_mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache); _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
_mesa_reference_program(ctx, &ctx->FragmentProgram.Current, NULL); _mesa_reference_program(ctx, &ctx->FragmentProgram.Current, NULL);
_mesa_delete_shader_cache(ctx, ctx->FragmentProgram.Cache); _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
/* XXX probably move this stuff */ /* XXX probably move this stuff */
if (ctx->ATIFragmentShader.Current) { if (ctx->ATIFragmentShader.Current) {