Path: blob/21.2-virgl/src/gallium/drivers/d3d12/d3d12_nir_lower_texcmp.c
4570 views
/*1* Copyright © Microsoft Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*/2223#include "d3d12_nir_lower_texcmp.h"24#include "nir_builder.h"25#include "nir_builtin_builder.h"2627static bool28lower_sample_tex_compare_filter(const nir_instr *instr,29UNUSED const void *_options)30{31if (instr->type != nir_instr_type_tex)32return false;3334/* To be consistent we also want to lower tex when we lower anything,35* otherwise the differences in evaluating the shadow value might lead36* to artifacts. */37nir_tex_instr *tex = nir_instr_as_tex(instr);38if (tex->op != nir_texop_txb &&39tex->op != nir_texop_txl &&40tex->op != nir_texop_txd &&41tex->op != nir_texop_tex)42return false;4344return tex->is_shadow;45}4647static const struct glsl_type *48strip_shadow(const struct glsl_type *type)49{50const struct glsl_type *new_type =51glsl_sampler_type(52glsl_get_sampler_dim(type),53false, glsl_sampler_type_is_array(type),54GLSL_TYPE_FLOAT);55return new_type;56}575859static const struct glsl_type *60strip_shadow_with_array(const struct glsl_type *type)61{62if (glsl_type_is_array(type))63return glsl_array_type(strip_shadow(glsl_without_array(type)),64glsl_get_length(type), 0);65return strip_shadow(type);66}6768typedef struct {69unsigned n_states;70enum compare_func *compare_func;71dxil_texture_swizzle_state *tex_swizzles;72} sampler_state;7374static nir_ssa_def *75lower_sample_tex_compare_impl(nir_builder *b, nir_instr *instr,76void *options)7778{79nir_tex_instr *tex = nir_instr_as_tex(instr);8081sampler_state *state = (sampler_state *)options;82unsigned num_components = nir_tex_instr_result_size(tex);8384b->cursor = nir_after_instr(instr);85tex->is_shadow = false;8687int comp_index = nir_tex_instr_src_index(tex, nir_tex_src_comparator);8889nir_deref_instr *sampler_deref = NULL;90nir_variable *sampler = NULL;9192int sampler_index = nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref);93assert(sampler_index >= 0);9495sampler_deref = nir_instr_as_deref(tex->src[sampler_index].src.ssa->parent_instr);96sampler = nir_deref_instr_get_variable(sampler_deref);9798/* NIR expects a vec4 result from the above texture instructions */99nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);100101nir_ssa_def *tex_r = nir_channel(b, &tex->dest.ssa, 0);102nir_ssa_def *cmp = tex->src[comp_index].src.ssa;103104int proj_index = nir_tex_instr_src_index(tex, nir_tex_src_projector);105if (proj_index >= 0)106cmp = nir_fmul(b, cmp, nir_frcp(b, tex->src[proj_index].src.ssa));107108nir_ssa_def * result =109nir_compare_func(b,110sampler->data.binding < state->n_states ?111state->compare_func[sampler->data.binding] : COMPARE_FUNC_ALWAYS,112cmp, tex_r);113114result = nir_b2f32(b, result);115nir_ssa_def *one = nir_imm_float(b, 1.0);116nir_ssa_def *zero = nir_imm_float(b, 0.0);117118nir_ssa_def *lookup[6] = {result, NULL, NULL, NULL, zero, one};119nir_ssa_def *r[4] = {lookup[state->tex_swizzles[sampler->data.binding].swizzle_r],120lookup[state->tex_swizzles[sampler->data.binding].swizzle_g],121lookup[state->tex_swizzles[sampler->data.binding].swizzle_b],122lookup[state->tex_swizzles[sampler->data.binding].swizzle_a]123};124125result = nir_vec(b, r, num_components);126127sampler->type = strip_shadow_with_array(sampler->type);128sampler_deref->type = sampler->type;129130tex->is_shadow = false;131nir_tex_instr_remove_src(tex, comp_index);132133return result;134}135136bool137d3d12_lower_sample_tex_compare(nir_shader *s,138unsigned n_states,139enum compare_func *compare_func,140dxil_texture_swizzle_state *tex_swizzles)141{142sampler_state state = {n_states, compare_func, tex_swizzles};143144bool result =145nir_shader_lower_instructions(s,146lower_sample_tex_compare_filter,147lower_sample_tex_compare_impl,148&state);149return result;150}151152153