2015-12-21 21:27:25 -05:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2015 Red Hat
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nir.h"
|
|
|
|
|
#include "nir_builder.h"
|
|
|
|
|
|
|
|
|
|
/* Lower glDrawPixels().
|
|
|
|
|
*
|
|
|
|
|
* This is based on the logic in st_get_drawpix_shader() in TGSI compiler.
|
|
|
|
|
*
|
|
|
|
|
* Run before nir_lower_io.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
const nir_lower_drawpixels_options *options;
|
|
|
|
|
nir_shader *shader;
|
2019-09-30 22:16:55 -04:00
|
|
|
nir_variable *texcoord, *texcoord_const, *scale, *bias, *tex, *pixelmap;
|
2015-12-21 21:27:25 -05:00
|
|
|
} lower_drawpixels_state;
|
|
|
|
|
|
|
|
|
|
static nir_ssa_def *
|
2021-08-09 15:57:41 +02:00
|
|
|
get_texcoord(nir_builder *b, lower_drawpixels_state *state)
|
2015-12-21 21:27:25 -05:00
|
|
|
{
|
|
|
|
|
if (state->texcoord == NULL) {
|
2023-05-02 15:40:51 -07:00
|
|
|
state->texcoord = nir_get_variable_with_location(state->shader, nir_var_shader_in,
|
|
|
|
|
VARYING_SLOT_TEX0, glsl_vec4_type());
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
2021-08-09 15:57:41 +02:00
|
|
|
return nir_load_var(b, state->texcoord);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static nir_ssa_def *
|
2021-08-09 15:57:41 +02:00
|
|
|
get_scale(nir_builder *b, lower_drawpixels_state *state)
|
2015-12-21 21:27:25 -05:00
|
|
|
{
|
|
|
|
|
if (state->scale == NULL) {
|
2023-05-16 13:37:53 -07:00
|
|
|
state->scale = nir_state_variable_create(state->shader, glsl_vec4_type(), "gl_PTscale",
|
|
|
|
|
state->options->scale_state_tokens);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
2021-08-09 15:57:41 +02:00
|
|
|
return nir_load_var(b, state->scale);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static nir_ssa_def *
|
2021-08-09 15:57:41 +02:00
|
|
|
get_bias(nir_builder *b, lower_drawpixels_state *state)
|
2015-12-21 21:27:25 -05:00
|
|
|
{
|
|
|
|
|
if (state->bias == NULL) {
|
2023-05-16 13:37:53 -07:00
|
|
|
state->bias = nir_state_variable_create(state->shader, glsl_vec4_type(), "gl_PTbias",
|
|
|
|
|
state->options->bias_state_tokens);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
2021-08-09 15:57:41 +02:00
|
|
|
return nir_load_var(b, state->bias);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static nir_ssa_def *
|
2021-08-09 15:57:41 +02:00
|
|
|
get_texcoord_const(nir_builder *b, lower_drawpixels_state *state)
|
2015-12-21 21:27:25 -05:00
|
|
|
{
|
2019-09-30 22:16:55 -04:00
|
|
|
if (state->texcoord_const == NULL) {
|
2023-05-16 13:37:53 -07:00
|
|
|
state->texcoord_const = nir_state_variable_create(state->shader, glsl_vec4_type(),
|
|
|
|
|
"gl_MultiTexCoord0",
|
|
|
|
|
state->options->texcoord_state_tokens);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
2021-08-09 15:57:41 +02:00
|
|
|
return nir_load_var(b, state->texcoord_const);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-09 15:57:41 +02:00
|
|
|
static bool
|
|
|
|
|
lower_color(nir_builder *b, lower_drawpixels_state *state, nir_intrinsic_instr *intr)
|
2015-12-21 21:27:25 -05:00
|
|
|
{
|
|
|
|
|
nir_ssa_def *texcoord;
|
|
|
|
|
nir_tex_instr *tex;
|
|
|
|
|
nir_ssa_def *def;
|
|
|
|
|
|
|
|
|
|
assert(intr->dest.is_ssa);
|
|
|
|
|
|
|
|
|
|
b->cursor = nir_before_instr(&intr->instr);
|
|
|
|
|
|
2021-08-09 15:57:41 +02:00
|
|
|
texcoord = get_texcoord(b, state);
|
2015-12-21 21:27:25 -05:00
|
|
|
|
2018-08-24 01:34:30 -07:00
|
|
|
const struct glsl_type *sampler2D =
|
|
|
|
|
glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);
|
|
|
|
|
|
|
|
|
|
if (!state->tex) {
|
|
|
|
|
state->tex =
|
|
|
|
|
nir_variable_create(b->shader, nir_var_uniform, sampler2D, "drawpix");
|
|
|
|
|
state->tex->data.binding = state->options->drawpix_sampler;
|
2019-02-05 23:24:51 -08:00
|
|
|
state->tex->data.explicit_binding = true;
|
|
|
|
|
state->tex->data.how_declared = nir_var_hidden;
|
2018-08-24 01:34:30 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 23:24:51 -08:00
|
|
|
nir_deref_instr *tex_deref = nir_build_deref_var(b, state->tex);
|
|
|
|
|
|
2015-12-21 21:27:25 -05:00
|
|
|
/* replace load_var(gl_Color) w/ texture sample:
|
|
|
|
|
* TEX def, texcoord, drawpix_sampler, 2D
|
|
|
|
|
*/
|
2019-02-05 23:24:51 -08:00
|
|
|
tex = nir_tex_instr_create(state->shader, 3);
|
2015-12-21 21:27:25 -05:00
|
|
|
tex->op = nir_texop_tex;
|
|
|
|
|
tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
|
|
|
|
|
tex->coord_components = 2;
|
2020-12-08 13:45:55 +01:00
|
|
|
tex->dest_type = nir_type_float32;
|
2019-02-05 23:24:51 -08:00
|
|
|
tex->src[0].src_type = nir_tex_src_texture_deref;
|
|
|
|
|
tex->src[0].src = nir_src_for_ssa(&tex_deref->dest.ssa);
|
|
|
|
|
tex->src[1].src_type = nir_tex_src_sampler_deref;
|
|
|
|
|
tex->src[1].src = nir_src_for_ssa(&tex_deref->dest.ssa);
|
|
|
|
|
tex->src[2].src_type = nir_tex_src_coord;
|
|
|
|
|
tex->src[2].src =
|
2017-03-08 15:20:31 -08:00
|
|
|
nir_src_for_ssa(nir_channels(b, texcoord,
|
|
|
|
|
(1 << tex->coord_components) - 1));
|
2015-12-21 21:27:25 -05:00
|
|
|
|
nir: Drop unused name from nir_ssa_dest_init
Since 624e799cc34 ("nir: Drop nir_ssa_def::name and nir_register::name"), SSA
defs don't have names, making the name argument unused. Drop it from the
signature and fix the call sites. This was done with the help of the following
Coccinelle semantic patch:
@@
expression A, B, C, D, E;
@@
-nir_ssa_dest_init(A, B, C, D, E);
+nir_ssa_dest_init(A, B, C, D);
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Timur Kristóf <timur.kristof@gmail.com>
Reviewed-by: Emma Anholt <emma@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23078>
2023-05-17 09:08:22 -04:00
|
|
|
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32);
|
2015-12-21 21:27:25 -05:00
|
|
|
nir_builder_instr_insert(b, &tex->instr);
|
|
|
|
|
def = &tex->dest.ssa;
|
|
|
|
|
|
|
|
|
|
/* Apply the scale and bias. */
|
|
|
|
|
if (state->options->scale_and_bias) {
|
|
|
|
|
/* MAD def, def, scale, bias; */
|
2021-08-09 15:57:41 +02:00
|
|
|
def = nir_ffma(b, def, get_scale(b, state), get_bias(b, state));
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->options->pixel_maps) {
|
2018-08-24 01:34:30 -07:00
|
|
|
if (!state->pixelmap) {
|
|
|
|
|
state->pixelmap = nir_variable_create(b->shader, nir_var_uniform,
|
|
|
|
|
sampler2D, "pixelmap");
|
|
|
|
|
state->pixelmap->data.binding = state->options->pixelmap_sampler;
|
2019-02-05 23:24:51 -08:00
|
|
|
state->pixelmap->data.explicit_binding = true;
|
|
|
|
|
state->pixelmap->data.how_declared = nir_var_hidden;
|
2018-08-24 01:34:30 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 23:24:51 -08:00
|
|
|
nir_deref_instr *pixelmap_deref =
|
|
|
|
|
nir_build_deref_var(b, state->pixelmap);
|
|
|
|
|
|
2015-12-21 21:27:25 -05:00
|
|
|
/* do four pixel map look-ups with two TEX instructions: */
|
|
|
|
|
nir_ssa_def *def_xy, *def_zw;
|
|
|
|
|
|
|
|
|
|
/* TEX def.xy, def.xyyy, pixelmap_sampler, 2D; */
|
2019-02-05 23:24:51 -08:00
|
|
|
tex = nir_tex_instr_create(state->shader, 3);
|
2015-12-21 21:27:25 -05:00
|
|
|
tex->op = nir_texop_tex;
|
|
|
|
|
tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
|
|
|
|
|
tex->coord_components = 2;
|
|
|
|
|
tex->sampler_index = state->options->pixelmap_sampler;
|
|
|
|
|
tex->texture_index = state->options->pixelmap_sampler;
|
2020-12-08 13:45:55 +01:00
|
|
|
tex->dest_type = nir_type_float32;
|
2019-02-05 23:24:51 -08:00
|
|
|
tex->src[0].src_type = nir_tex_src_texture_deref;
|
|
|
|
|
tex->src[0].src = nir_src_for_ssa(&pixelmap_deref->dest.ssa);
|
|
|
|
|
tex->src[1].src_type = nir_tex_src_sampler_deref;
|
|
|
|
|
tex->src[1].src = nir_src_for_ssa(&pixelmap_deref->dest.ssa);
|
|
|
|
|
tex->src[2].src_type = nir_tex_src_coord;
|
|
|
|
|
tex->src[2].src = nir_src_for_ssa(nir_channels(b, def, 0x3));
|
2015-12-21 21:27:25 -05:00
|
|
|
|
nir: Drop unused name from nir_ssa_dest_init
Since 624e799cc34 ("nir: Drop nir_ssa_def::name and nir_register::name"), SSA
defs don't have names, making the name argument unused. Drop it from the
signature and fix the call sites. This was done with the help of the following
Coccinelle semantic patch:
@@
expression A, B, C, D, E;
@@
-nir_ssa_dest_init(A, B, C, D, E);
+nir_ssa_dest_init(A, B, C, D);
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Timur Kristóf <timur.kristof@gmail.com>
Reviewed-by: Emma Anholt <emma@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23078>
2023-05-17 09:08:22 -04:00
|
|
|
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32);
|
2015-12-21 21:27:25 -05:00
|
|
|
nir_builder_instr_insert(b, &tex->instr);
|
|
|
|
|
def_xy = &tex->dest.ssa;
|
|
|
|
|
|
|
|
|
|
/* TEX def.zw, def.zwww, pixelmap_sampler, 2D; */
|
|
|
|
|
tex = nir_tex_instr_create(state->shader, 1);
|
|
|
|
|
tex->op = nir_texop_tex;
|
|
|
|
|
tex->sampler_dim = GLSL_SAMPLER_DIM_2D;
|
|
|
|
|
tex->coord_components = 2;
|
|
|
|
|
tex->sampler_index = state->options->pixelmap_sampler;
|
2020-12-08 13:45:55 +01:00
|
|
|
tex->dest_type = nir_type_float32;
|
2016-11-03 16:28:49 -07:00
|
|
|
tex->src[0].src_type = nir_tex_src_coord;
|
2018-07-13 03:33:22 +02:00
|
|
|
tex->src[0].src = nir_src_for_ssa(nir_channels(b, def, 0xc));
|
2015-12-21 21:27:25 -05:00
|
|
|
|
nir: Drop unused name from nir_ssa_dest_init
Since 624e799cc34 ("nir: Drop nir_ssa_def::name and nir_register::name"), SSA
defs don't have names, making the name argument unused. Drop it from the
signature and fix the call sites. This was done with the help of the following
Coccinelle semantic patch:
@@
expression A, B, C, D, E;
@@
-nir_ssa_dest_init(A, B, C, D, E);
+nir_ssa_dest_init(A, B, C, D);
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Timur Kristóf <timur.kristof@gmail.com>
Reviewed-by: Emma Anholt <emma@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23078>
2023-05-17 09:08:22 -04:00
|
|
|
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32);
|
2015-12-21 21:27:25 -05:00
|
|
|
nir_builder_instr_insert(b, &tex->instr);
|
|
|
|
|
def_zw = &tex->dest.ssa;
|
|
|
|
|
|
|
|
|
|
/* def = vec4(def.xy, def.zw); */
|
|
|
|
|
def = nir_vec4(b,
|
|
|
|
|
nir_channel(b, def_xy, 0),
|
|
|
|
|
nir_channel(b, def_xy, 1),
|
|
|
|
|
nir_channel(b, def_zw, 0),
|
|
|
|
|
nir_channel(b, def_zw, 1));
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-03 00:13:38 -06:00
|
|
|
nir_ssa_def_rewrite_uses(&intr->dest.ssa, def);
|
2021-08-09 15:57:41 +02:00
|
|
|
return true;
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-09 15:57:41 +02:00
|
|
|
static bool
|
|
|
|
|
lower_texcoord(nir_builder *b, lower_drawpixels_state *state, nir_intrinsic_instr *intr)
|
2015-12-21 21:27:25 -05:00
|
|
|
{
|
2021-08-09 15:57:41 +02:00
|
|
|
b->cursor = nir_before_instr(&intr->instr);
|
2016-08-19 15:38:08 -07:00
|
|
|
|
2021-08-09 15:57:41 +02:00
|
|
|
nir_ssa_def *texcoord_const = get_texcoord_const(b, state);
|
2021-03-03 00:13:38 -06:00
|
|
|
nir_ssa_def_rewrite_uses(&intr->dest.ssa, texcoord_const);
|
2021-08-09 15:57:41 +02:00
|
|
|
return true;
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2021-08-09 15:57:41 +02:00
|
|
|
lower_drawpixels_instr(nir_builder *b, nir_instr *instr, void *cb_data)
|
2015-12-21 21:27:25 -05:00
|
|
|
{
|
2021-08-09 15:57:41 +02:00
|
|
|
lower_drawpixels_state *state = cb_data;
|
|
|
|
|
if (instr->type != nir_instr_type_intrinsic)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
|
|
|
|
|
|
|
|
|
|
switch (intr->intrinsic) {
|
|
|
|
|
case nir_intrinsic_load_deref: {
|
|
|
|
|
nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
|
|
|
|
|
nir_variable *var = nir_deref_instr_get_variable(deref);
|
|
|
|
|
|
|
|
|
|
if (var->data.location == VARYING_SLOT_COL0) {
|
|
|
|
|
/* gl_Color should not have array/struct derefs: */
|
|
|
|
|
assert(deref->deref_type == nir_deref_type_var);
|
|
|
|
|
return lower_color(b, state, intr);
|
|
|
|
|
} else if (var->data.location == VARYING_SLOT_TEX0) {
|
|
|
|
|
/* gl_TexCoord should not have array/struct derefs: */
|
|
|
|
|
assert(deref->deref_type == nir_deref_type_var);
|
|
|
|
|
return lower_texcoord(b, state, intr);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
2021-08-09 15:57:41 +02:00
|
|
|
break;
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-09 15:57:41 +02:00
|
|
|
case nir_intrinsic_load_color0:
|
|
|
|
|
return lower_color(b, state, intr);
|
2015-12-21 21:27:25 -05:00
|
|
|
|
2021-08-09 15:57:41 +02:00
|
|
|
case nir_intrinsic_load_interpolated_input:
|
|
|
|
|
case nir_intrinsic_load_input: {
|
|
|
|
|
if (nir_intrinsic_io_semantics(intr).location == VARYING_SLOT_TEX0)
|
|
|
|
|
return lower_texcoord(b, state, intr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
2021-08-09 15:57:41 +02:00
|
|
|
|
|
|
|
|
return false;
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nir_lower_drawpixels(nir_shader *shader,
|
|
|
|
|
const nir_lower_drawpixels_options *options)
|
|
|
|
|
{
|
|
|
|
|
lower_drawpixels_state state = {
|
|
|
|
|
.options = options,
|
|
|
|
|
.shader = shader,
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-14 19:52:38 -07:00
|
|
|
assert(shader->info.stage == MESA_SHADER_FRAGMENT);
|
2015-12-21 21:27:25 -05:00
|
|
|
|
2021-08-09 15:57:41 +02:00
|
|
|
nir_shader_instructions_pass(shader, lower_drawpixels_instr,
|
|
|
|
|
nir_metadata_block_index |
|
|
|
|
|
nir_metadata_dominance,
|
|
|
|
|
&state);
|
2015-12-21 21:27:25 -05:00
|
|
|
}
|