Path: blob/21.2-virgl/src/gallium/auxiliary/nir/nir_draw_helpers.c
4561 views
/**************************************************************************1*2* Copyright 2019 Red Hat.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**************************************************************************/2425/*26* NIR lowering passes to handle the draw stages for27* - pstipple28* - aaline29* - aapoint.30*31* These are all ported from the equivalent TGSI transforms.32*/3334#include "nir.h"35#include "tgsi/tgsi_from_mesa.h"36#include "nir_builder.h"3738#include "nir_draw_helpers.h"3940typedef struct {41nir_builder b;42nir_shader *shader;43bool fs_pos_is_sysval;44nir_variable *stip_tex;45nir_ssa_def *fragcoord;46} lower_pstipple;4748static nir_ssa_def *49load_frag_coord(nir_builder *b)50{51nir_foreach_shader_in_variable(var, b->shader) {52if (var->data.location == VARYING_SLOT_POS)53return nir_load_var(b, var);54}5556nir_variable *pos = nir_variable_create(b->shader, nir_var_shader_in,57glsl_vec4_type(), NULL);58pos->data.location = VARYING_SLOT_POS;59pos->data.interpolation = INTERP_MODE_NOPERSPECTIVE;60pos->data.driver_location = b->shader->num_inputs++;61return nir_load_var(b, pos);62}6364static void65nir_lower_pstipple_block(nir_block *block,66lower_pstipple *state)67{68nir_builder *b = &state->b;69nir_ssa_def *texcoord;7071b->cursor = nir_before_block(block);7273nir_ssa_def *frag_coord = state->fs_pos_is_sysval ? nir_load_frag_coord(b) : load_frag_coord(b);7475texcoord = nir_fmul(b, nir_channels(b, frag_coord, 0x3),76nir_imm_vec2(b, 1.0/32.0, 1.0/32.0));7778nir_tex_instr *tex = nir_tex_instr_create(b->shader, 1);79tex->op = nir_texop_tex;80tex->sampler_dim = GLSL_SAMPLER_DIM_2D;81tex->coord_components = 2;82tex->dest_type = nir_type_float32;83tex->texture_index = state->stip_tex->data.binding;84tex->sampler_index = state->stip_tex->data.binding;85tex->src[0].src_type = nir_tex_src_coord;86tex->src[0].src = nir_src_for_ssa(texcoord);87nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);8889nir_builder_instr_insert(b, &tex->instr);9091nir_ssa_def *condition = nir_f2b32(b, nir_channel(b, &tex->dest.ssa, 3));92nir_discard_if(b, condition);93b->shader->info.fs.uses_discard = true;94}9596static void97nir_lower_pstipple_impl(nir_function_impl *impl,98lower_pstipple *state)99{100nir_builder *b = &state->b;101102nir_builder_init(b, impl);103104nir_block *start = nir_start_block(impl);105nir_lower_pstipple_block(start, state);106}107108void109nir_lower_pstipple_fs(struct nir_shader *shader,110unsigned *samplerUnitOut,111unsigned fixedUnit,112bool fs_pos_is_sysval)113{114lower_pstipple state = {115.shader = shader,116.fs_pos_is_sysval = fs_pos_is_sysval,117};118if (shader->info.stage != MESA_SHADER_FRAGMENT)119return;120121int binding = 0;122nir_foreach_uniform_variable(var, shader) {123if (glsl_type_is_sampler(var->type)) {124if (var->data.binding >= binding)125binding = var->data.binding + 1;126}127}128const struct glsl_type *sampler2D =129glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT);130131nir_variable *tex_var = nir_variable_create(shader, nir_var_uniform, sampler2D, "stipple_tex");132tex_var->data.binding = binding;133tex_var->data.explicit_binding = true;134tex_var->data.how_declared = nir_var_hidden;135136BITSET_SET(shader->info.textures_used, binding);137state.stip_tex = tex_var;138139nir_foreach_function(function, shader) {140if (function->impl) {141nir_lower_pstipple_impl(function->impl, &state);142}143}144*samplerUnitOut = binding;145}146147typedef struct {148nir_builder b;149nir_shader *shader;150nir_variable *line_width_input;151} lower_aaline;152153static void154nir_lower_aaline_block(nir_block *block,155lower_aaline *state)156{157nir_builder *b = &state->b;158nir_foreach_instr(instr, block) {159if (instr->type != nir_instr_type_intrinsic)160continue;161162nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);163if (intrin->intrinsic != nir_intrinsic_store_deref)164continue;165166nir_variable *var = nir_intrinsic_get_var(intrin, 0);167if (var->data.mode != nir_var_shader_out)168continue;169if (var->data.location < FRAG_RESULT_DATA0 && var->data.location != FRAG_RESULT_COLOR)170continue;171172nir_ssa_def *out_input = intrin->src[1].ssa;173b->cursor = nir_before_instr(instr);174nir_ssa_def *lw = nir_load_var(b, state->line_width_input);175nir_ssa_def *tmp = nir_fsat(b, nir_fadd(b, nir_channel(b, lw, 1),176nir_fneg(b, nir_fabs(b, nir_channel(b, lw, 0)))));177nir_ssa_def *tmp1 = nir_fsat(b, nir_fadd(b, nir_channel(b, lw, 3),178nir_fneg(b, nir_fabs(b, nir_channel(b, lw, 2)))));179180tmp = nir_fmul(b, tmp, tmp1);181tmp = nir_fmul(b, nir_channel(b, out_input, 3), tmp);182183nir_ssa_def *out = nir_vec4(b, nir_channel(b, out_input, 0),184nir_channel(b, out_input, 1),185nir_channel(b, out_input, 2),186tmp);187nir_instr_rewrite_src(instr, &intrin->src[1], nir_src_for_ssa(out));188}189190}191192static void193nir_lower_aaline_impl(nir_function_impl *impl,194lower_aaline *state)195{196nir_builder *b = &state->b;197198nir_builder_init(b, impl);199200nir_foreach_block(block, impl) {201nir_lower_aaline_block(block, state);202}203}204205void206nir_lower_aaline_fs(struct nir_shader *shader, int *varying)207{208lower_aaline state = {209.shader = shader,210};211if (shader->info.stage != MESA_SHADER_FRAGMENT)212return;213214int highest_location = -1, highest_drv_location = -1;215nir_foreach_shader_in_variable(var, shader) {216if ((int)var->data.location > highest_location)217highest_location = var->data.location;218if ((int)var->data.driver_location > highest_drv_location)219highest_drv_location = var->data.driver_location;220}221222nir_variable *line_width = nir_variable_create(shader, nir_var_shader_in,223glsl_vec4_type(), "aaline");224if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) {225line_width->data.location = VARYING_SLOT_VAR0;226line_width->data.driver_location = highest_drv_location + 1;227} else {228line_width->data.location = highest_location + 1;229line_width->data.driver_location = highest_drv_location + 1;230}231shader->num_inputs++;232*varying = tgsi_get_generic_gl_varying_index(line_width->data.location, true);233state.line_width_input = line_width;234235nir_foreach_function(function, shader) {236if (function->impl) {237nir_lower_aaline_impl(function->impl, &state);238}239}240}241242typedef struct {243nir_builder b;244nir_shader *shader;245nir_variable *input;246} lower_aapoint;247248static void249nir_lower_aapoint_block(nir_block *block,250lower_aapoint *state, nir_ssa_def *sel)251{252nir_builder *b = &state->b;253nir_foreach_instr(instr, block) {254if (instr->type != nir_instr_type_intrinsic)255continue;256257nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);258if (intrin->intrinsic != nir_intrinsic_store_deref)259continue;260261nir_variable *var = nir_intrinsic_get_var(intrin, 0);262if (var->data.mode != nir_var_shader_out)263continue;264if (var->data.location < FRAG_RESULT_DATA0 && var->data.location != FRAG_RESULT_COLOR)265continue;266267nir_ssa_def *out_input = intrin->src[1].ssa;268b->cursor = nir_before_instr(instr);269270nir_ssa_def *tmp = nir_fmul(b, nir_channel(b, out_input, 3), sel);271nir_ssa_def *out = nir_vec4(b, nir_channel(b, out_input, 0),272nir_channel(b, out_input, 1),273nir_channel(b, out_input, 2),274tmp);275nir_instr_rewrite_src(instr, &intrin->src[1], nir_src_for_ssa(out));276}277278}279280static void281nir_lower_aapoint_impl(nir_function_impl *impl,282lower_aapoint *state)283{284nir_builder *b = &state->b;285286nir_builder_init(b, impl);287288nir_block *block = nir_start_block(impl);289b->cursor = nir_before_block(block);290291nir_ssa_def *aainput = nir_load_var(b, state->input);292293nir_ssa_def *dist = nir_fadd(b, nir_fmul(b, nir_channel(b, aainput, 0), nir_channel(b, aainput, 0)),294nir_fmul(b, nir_channel(b, aainput, 1), nir_channel(b, aainput, 1)));295296nir_ssa_def *k = nir_channel(b, aainput, 2);297nir_ssa_def *chan_val_one = nir_channel(b, aainput, 3);298nir_ssa_def *comp = nir_flt32(b, chan_val_one, dist);299300nir_discard_if(b, comp);301b->shader->info.fs.uses_discard = true;302303/* compute coverage factor = (1-d)/(1-k) */304/* 1 - k */305nir_ssa_def *tmp = nir_fadd(b, chan_val_one, nir_fneg(b, k));306/* 1.0 / (1 - k) */307tmp = nir_frcp(b, tmp);308309/* 1 - d */310nir_ssa_def *tmp2 = nir_fadd(b, chan_val_one, nir_fneg(b, dist));311312/* (1 - d) / (1 - k) */313nir_ssa_def *coverage = nir_fmul(b, tmp, tmp2);314315/* if (k >= distance)316* sel = coverage;317* else318* sel = 1.0;319*/320nir_ssa_def *sel = nir_b32csel(b, nir_fge32(b, k, dist), coverage, chan_val_one);321322nir_foreach_block(block, impl) {323nir_lower_aapoint_block(block, state, sel);324}325}326327void328nir_lower_aapoint_fs(struct nir_shader *shader, int *varying)329{330lower_aapoint state = {331.shader = shader,332};333if (shader->info.stage != MESA_SHADER_FRAGMENT)334return;335336int highest_location = -1, highest_drv_location = -1;337nir_foreach_shader_in_variable(var, shader) {338if ((int)var->data.location > highest_location)339highest_location = var->data.location;340if ((int)var->data.driver_location > highest_drv_location)341highest_drv_location = var->data.driver_location;342}343344nir_variable *aapoint_input = nir_variable_create(shader, nir_var_shader_in,345glsl_vec4_type(), "aapoint");346if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) {347aapoint_input->data.location = VARYING_SLOT_VAR0;348} else {349aapoint_input->data.location = highest_location + 1;350}351aapoint_input->data.driver_location = highest_drv_location + 1;352353shader->num_inputs++;354*varying = tgsi_get_generic_gl_varying_index(aapoint_input->data.location, true);355state.input = aapoint_input;356357nir_foreach_function(function, shader) {358if (function->impl) {359nir_lower_aapoint_impl(function->impl, &state);360}361}362}363364365