From: Dave Airlie Date: Thu, 5 Sep 2019 05:47:39 +0000 (+1000) Subject: gallium: add nir lowering passes for the draw pipe stages. (v2) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3eb27cfccddaca7bb46bc42e0c1832b3c1085542;p=mesa.git gallium: add nir lowering passes for the draw pipe stages. (v2) This transforms the NIR shaders like the TGSI transforms worked. v2: fix some nir info requirements, use 32-bit bools Acked-by: Roland Scheidegger --- diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources index 4ee99b8f063..438cb600953 100644 --- a/src/gallium/auxiliary/Makefile.sources +++ b/src/gallium/auxiliary/Makefile.sources @@ -319,7 +319,9 @@ NIR_SOURCES := \ nir/tgsi_to_nir.c \ nir/tgsi_to_nir.h \ nir/nir_to_tgsi_info.c \ - nir/nir_to_tgsi_info.h + nir/nir_to_tgsi_info.h \ + nir/nir_draw_helpers.c \ + nir/nir_draw_helpers.h VL_SOURCES := \ vl/vl_bicubic_filter.c \ diff --git a/src/gallium/auxiliary/draw/draw_pipe_aaline.c b/src/gallium/auxiliary/draw/draw_pipe_aaline.c index e84fb5d6157..872b29e9c8d 100644 --- a/src/gallium/auxiliary/draw/draw_pipe_aaline.c +++ b/src/gallium/auxiliary/draw/draw_pipe_aaline.c @@ -48,6 +48,8 @@ #include "draw_private.h" #include "draw_pipe.h" +#include "nir.h" +#include "nir/nir_draw_helpers.h" /** Approx number of new tokens for instructions in aa_transform_inst() */ #define NUM_NEW_TOKENS 53 @@ -318,6 +320,30 @@ fail: return FALSE; } +static boolean +generate_aaline_fs_nir(struct aaline_stage *aaline) +{ +#ifdef LLVM_AVAILABLE + struct pipe_context *pipe = aaline->stage.draw->pipe; + const struct pipe_shader_state *orig_fs = &aaline->fs->state; + struct pipe_shader_state aaline_fs; + + aaline_fs = *orig_fs; /* copy to init */ + aaline_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir); + if (!aaline_fs.ir.nir) + return FALSE; + + nir_lower_aaline_fs(aaline_fs.ir.nir, &aaline->fs->generic_attrib); + aaline->fs->aaline_fs = aaline->driver_create_fs_state(pipe, &aaline_fs); + if (aaline->fs->aaline_fs == NULL) + goto fail; + + return TRUE; + +fail: +#endif + return FALSE; +} /** * When we're about to draw our first AA line in a batch, this function is @@ -329,8 +355,14 @@ bind_aaline_fragment_shader(struct aaline_stage *aaline) struct draw_context *draw = aaline->stage.draw; struct pipe_context *pipe = draw->pipe; - if (!aaline->fs->aaline_fs && !generate_aaline_fs(aaline)) - return FALSE; + if (!aaline->fs->aaline_fs) { + if (aaline->fs->state.type == PIPE_SHADER_IR_NIR) { + if (!generate_aaline_fs_nir(aaline)) + return FALSE; + } else + if (!generate_aaline_fs(aaline)) + return FALSE; + } draw->suspend_flushing = TRUE; aaline->driver_bind_fs_state(pipe, aaline->fs->aaline_fs); @@ -618,7 +650,13 @@ aaline_create_fs_state(struct pipe_context *pipe, if (!aafs) return NULL; - aafs->state.tokens = tgsi_dup_tokens(fs->tokens); + aafs->state.type = fs->type; + if (fs->type == PIPE_SHADER_IR_TGSI) + aafs->state.tokens = tgsi_dup_tokens(fs->tokens); +#ifdef LLVM_AVAILABLE + else + aafs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir); +#endif /* pass-through */ aafs->driver_fs = aaline->driver_create_fs_state(pipe, fs); @@ -662,7 +700,10 @@ aaline_delete_fs_state(struct pipe_context *pipe, void *fs) aaline->driver_delete_fs_state(pipe, aafs->aaline_fs); } - FREE((void*)aafs->state.tokens); + if (aafs->state.type == PIPE_SHADER_IR_TGSI) + FREE((void*)aafs->state.tokens); + else + ralloc_free(aafs->state.ir.nir); FREE(aafs); } @@ -681,9 +722,12 @@ draw_aaline_prepare_outputs(struct draw_context *draw, return; /* allocate the extra post-transformed vertex attribute */ - aaline->coord_slot = draw_alloc_extra_vertex_attrib(draw, - TGSI_SEMANTIC_GENERIC, - aaline->fs->generic_attrib); + if (aaline->fs->aaline_fs) + aaline->coord_slot = draw_alloc_extra_vertex_attrib(draw, + TGSI_SEMANTIC_GENERIC, + aaline->fs->generic_attrib); + else + aaline->coord_slot = -1; } /** diff --git a/src/gallium/auxiliary/draw/draw_pipe_aapoint.c b/src/gallium/auxiliary/draw/draw_pipe_aapoint.c index dc22039b127..797e0d2d9c2 100644 --- a/src/gallium/auxiliary/draw/draw_pipe_aapoint.c +++ b/src/gallium/auxiliary/draw/draw_pipe_aapoint.c @@ -52,6 +52,8 @@ #include "draw_vs.h" #include "draw_pipe.h" +#include "nir.h" +#include "nir/nir_draw_helpers.h" /** Approx number of new tokens for instructions in aa_transform_inst() */ #define NUM_NEW_TOKENS 200 @@ -364,6 +366,8 @@ generate_aapoint_fs(struct aapoint_stage *aapoint) struct pipe_context *pipe = aapoint->stage.draw->pipe; aapoint_fs = *orig_fs; /* copy to init */ + + assert(aapoint_fs.type == PIPE_SHADER_IR_TGSI); aapoint_fs.tokens = tgsi_alloc_tokens(newLen); if (aapoint_fs.tokens == NULL) return FALSE; @@ -404,6 +408,30 @@ fail: return FALSE; } +static boolean +generate_aapoint_fs_nir(struct aapoint_stage *aapoint) +{ +#ifdef LLVM_AVAILABLE + struct pipe_context *pipe = aapoint->stage.draw->pipe; + const struct pipe_shader_state *orig_fs = &aapoint->fs->state; + struct pipe_shader_state aapoint_fs; + + aapoint_fs = *orig_fs; /* copy to init */ + aapoint_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir); + if (!aapoint_fs.ir.nir) + return FALSE; + + nir_lower_aapoint_fs(aapoint_fs.ir.nir, &aapoint->fs->generic_attrib); + aapoint->fs->aapoint_fs = aapoint->driver_create_fs_state(pipe, &aapoint_fs); + if (aapoint->fs->aapoint_fs == NULL) + goto fail; + + return TRUE; + +fail: +#endif + return FALSE; +} /** * When we're about to draw our first AA point in a batch, this function is @@ -415,9 +443,13 @@ bind_aapoint_fragment_shader(struct aapoint_stage *aapoint) struct draw_context *draw = aapoint->stage.draw; struct pipe_context *pipe = draw->pipe; - if (!aapoint->fs->aapoint_fs && - !generate_aapoint_fs(aapoint)) - return FALSE; + if (!aapoint->fs->aapoint_fs) { + if (aapoint->fs->state.type == PIPE_SHADER_IR_NIR) { + if (!generate_aapoint_fs_nir(aapoint)) + return FALSE; + } else if (!generate_aapoint_fs(aapoint)) + return FALSE; + } draw->suspend_flushing = TRUE; aapoint->driver_bind_fs_state(pipe, aapoint->fs->aapoint_fs); @@ -637,11 +669,14 @@ draw_aapoint_prepare_outputs(struct draw_context *draw, if (!rast->point_smooth) return; - /* allocate the extra post-transformed vertex attribute */ - aapoint->tex_slot = draw_alloc_extra_vertex_attrib(draw, - TGSI_SEMANTIC_GENERIC, - aapoint->fs->generic_attrib); - assert(aapoint->tex_slot > 0); /* output[0] is vertex pos */ + if (aapoint->fs->aapoint_fs) { + /* allocate the extra post-transformed vertex attribute */ + aapoint->tex_slot = draw_alloc_extra_vertex_attrib(draw, + TGSI_SEMANTIC_GENERIC, + aapoint->fs->generic_attrib); + assert(aapoint->tex_slot > 0); /* output[0] is vertex pos */ + } else + aapoint->tex_slot = -1; /* find psize slot in post-transform vertex */ aapoint->psize_slot = -1; @@ -710,8 +745,13 @@ aapoint_create_fs_state(struct pipe_context *pipe, if (!aafs) return NULL; - aafs->state.tokens = tgsi_dup_tokens(fs->tokens); - + aafs->state.type = fs->type; + if (fs->type == PIPE_SHADER_IR_TGSI) + aafs->state.tokens = tgsi_dup_tokens(fs->tokens); +#ifdef LLVM_AVAILABLE + else + aafs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir); +#endif /* pass-through */ aafs->driver_fs = aapoint->driver_create_fs_state(pipe, fs); @@ -744,7 +784,10 @@ aapoint_delete_fs_state(struct pipe_context *pipe, void *fs) if (aafs->aapoint_fs) aapoint->driver_delete_fs_state(pipe, aafs->aapoint_fs); - FREE((void*)aafs->state.tokens); + if (aafs->state.type == PIPE_SHADER_IR_TGSI) + FREE((void*)aafs->state.tokens); + else + ralloc_free(aafs->state.ir.nir); FREE(aafs); } diff --git a/src/gallium/auxiliary/draw/draw_pipe_pstipple.c b/src/gallium/auxiliary/draw/draw_pipe_pstipple.c index bd6637a6730..bd74cb42ada 100644 --- a/src/gallium/auxiliary/draw/draw_pipe_pstipple.c +++ b/src/gallium/auxiliary/draw/draw_pipe_pstipple.c @@ -51,6 +51,8 @@ #include "draw_context.h" #include "draw_pipe.h" +#include "nir.h" +#include "nir/nir_draw_helpers.h" /** Approx number of new tokens for instructions in pstip_transform_inst() */ #define NUM_NEW_TOKENS 53 @@ -133,12 +135,20 @@ generate_pstip_fs(struct pstip_stage *pstip) TGSI_FILE_SYSTEM_VALUE : TGSI_FILE_INPUT; pstip_fs = *orig_fs; /* copy to init */ - pstip_fs.tokens = util_pstipple_create_fragment_shader(orig_fs->tokens, - &pstip->fs->sampler_unit, - 0, - wincoord_file); - if (pstip_fs.tokens == NULL) - return FALSE; + if (orig_fs->type == PIPE_SHADER_IR_TGSI) { + pstip_fs.tokens = util_pstipple_create_fragment_shader(orig_fs->tokens, + &pstip->fs->sampler_unit, + 0, + wincoord_file); + if (pstip_fs.tokens == NULL) + return FALSE; + } else { +#ifdef LLVM_AVAILABLE + pstip_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir); + nir_lower_pstipple_fs(pstip_fs.ir.nir, + &pstip->fs->sampler_unit, 0, wincoord_file == TGSI_FILE_SYSTEM_VALUE); +#endif + } assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS); @@ -334,7 +344,11 @@ pstip_create_fs_state(struct pipe_context *pipe, struct pstip_fragment_shader *pstipfs = CALLOC_STRUCT(pstip_fragment_shader); if (pstipfs) { - pstipfs->state.tokens = tgsi_dup_tokens(fs->tokens); + pstipfs->state.type = fs->type; + if (fs->type == PIPE_SHADER_IR_TGSI) + pstipfs->state.tokens = tgsi_dup_tokens(fs->tokens); + else + pstipfs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir); /* pass-through */ pstipfs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs); @@ -368,7 +382,10 @@ pstip_delete_fs_state(struct pipe_context *pipe, void *fs) if (pstipfs->pstip_fs) pstip->driver_delete_fs_state(pstip->pipe, pstipfs->pstip_fs); - FREE((void*)pstipfs->state.tokens); + if (pstipfs->state.type == PIPE_SHADER_IR_TGSI) + FREE((void*)pstipfs->state.tokens); + else + ralloc_free(pstipfs->state.ir.nir); FREE(pstipfs); } diff --git a/src/gallium/auxiliary/meson.build b/src/gallium/auxiliary/meson.build index ebc5e5449e0..f96b32e9d75 100644 --- a/src/gallium/auxiliary/meson.build +++ b/src/gallium/auxiliary/meson.build @@ -336,6 +336,8 @@ files_libgallium = files( 'util/u_viewport.h', 'nir/tgsi_to_nir.c', 'nir/tgsi_to_nir.h', + 'nir/nir_draw_helpers.c', + 'nir/nir_draw_helpers.h', ) if dep_libdrm.found() diff --git a/src/gallium/auxiliary/nir/nir_draw_helpers.c b/src/gallium/auxiliary/nir/nir_draw_helpers.c new file mode 100644 index 00000000000..036ce84938e --- /dev/null +++ b/src/gallium/auxiliary/nir/nir_draw_helpers.c @@ -0,0 +1,372 @@ +/************************************************************************** + * + * Copyright 2019 Red Hat. + * All Rights Reserved. + * + * 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 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. + * + **************************************************************************/ + +/* + * NIR lowering passes to handle the draw stages for + * - pstipple + * - aaline + * - aapoint. + * + * These are all ported from the equivalent TGSI transforms. + */ + +#include "nir.h" +#include "tgsi/tgsi_from_mesa.h" +#include "nir_builder.h" + +#include "nir_draw_helpers.h" + +typedef struct { + nir_builder b; + nir_shader *shader; + bool fs_pos_is_sysval; + nir_variable *stip_tex; + nir_ssa_def *fragcoord; +} lower_pstipple; + +static nir_ssa_def * +load_frag_coord(nir_builder *b) +{ + int max_driver_loc = -1; + nir_foreach_variable(var, &b->shader->inputs) { + if (var->data.location == VARYING_SLOT_POS) + return nir_load_var(b, var); + if (max_driver_loc < (int)var->data.driver_location) + max_driver_loc = var->data.driver_location; + } + + nir_variable *pos = nir_variable_create(b->shader, nir_var_shader_in, + glsl_vec4_type(), NULL); + pos->data.location = VARYING_SLOT_POS; + pos->data.interpolation = INTERP_MODE_NOPERSPECTIVE; + pos->data.driver_location = max_driver_loc + 1; + b->shader->num_inputs++; + return nir_load_var(b, pos); +} + +static void +nir_lower_pstipple_block(nir_block *block, + lower_pstipple *state) +{ + nir_builder *b = &state->b; + nir_ssa_def *texcoord; + + b->cursor = nir_before_block(block); + + nir_ssa_def *div32 = nir_imm_vec2(b, 1.0/32.0, 1.0/32.0); + + nir_ssa_def *frag_coord = state->fs_pos_is_sysval ? nir_load_frag_coord(b) : load_frag_coord(b); + + texcoord = nir_fmul(b, frag_coord, div32); + + nir_tex_instr *tex = nir_tex_instr_create(b->shader, 1); + tex->op = nir_texop_tex; + tex->sampler_dim = GLSL_SAMPLER_DIM_2D; + tex->coord_components = 2; + tex->dest_type = nir_type_float; + tex->texture_index = state->stip_tex->data.binding; + tex->sampler_index = state->stip_tex->data.binding; + tex->src[0].src_type = nir_tex_src_coord; + tex->src[0].src = nir_src_for_ssa(texcoord); + nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL); + + nir_builder_instr_insert(b, &tex->instr); + + nir_ssa_def *condition = nir_f2b32(b, nir_channel(b, &tex->dest.ssa, 3)); + nir_intrinsic_instr *discard = nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard_if); + discard->src[0] = nir_src_for_ssa(condition); + nir_builder_instr_insert(b, &discard->instr); + b->shader->info.fs.uses_discard = true; +} + +static void +nir_lower_pstipple_impl(nir_function_impl *impl, + lower_pstipple *state) +{ + nir_builder *b = &state->b; + + nir_builder_init(b, impl); + + nir_block *start = nir_start_block(impl); + nir_lower_pstipple_block(start, state); +} + +void +nir_lower_pstipple_fs(struct nir_shader *shader, + unsigned *samplerUnitOut, + unsigned fixedUnit, + bool fs_pos_is_sysval) +{ + lower_pstipple state = { + .shader = shader, + .fs_pos_is_sysval = fs_pos_is_sysval, + }; + if (shader->info.stage != MESA_SHADER_FRAGMENT) + return; + + int binding = 0; + nir_foreach_variable(var, &shader->uniforms) { + if (glsl_type_is_sampler(var->type)) { + if (var->data.binding >= binding) + binding = var->data.binding + 1; + } + } + const struct glsl_type *sampler2D = + glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false, GLSL_TYPE_FLOAT); + + nir_variable *tex_var = nir_variable_create(shader, nir_var_uniform, sampler2D, "stipple_tex"); + tex_var->data.binding = binding; + tex_var->data.explicit_binding = true; + tex_var->data.how_declared = nir_var_hidden; + + shader->info.textures_used |= (1 << binding); + state.stip_tex = tex_var; + + nir_foreach_function(function, shader) { + if (function->impl) { + nir_lower_pstipple_impl(function->impl, &state); + } + } + *samplerUnitOut = binding; +} + +typedef struct { + nir_builder b; + nir_shader *shader; + nir_variable *line_width_input; +} lower_aaline; + +static void +nir_lower_aaline_block(nir_block *block, + lower_aaline *state) +{ + nir_builder *b = &state->b; + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + + nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); + if (intrin->intrinsic != nir_intrinsic_store_deref) + continue; + + nir_variable *var = nir_intrinsic_get_var(intrin, 0); + if (var->data.mode != nir_var_shader_out) + continue; + if (var->data.location != FRAG_RESULT_COLOR) + continue; + + nir_ssa_def *out_input = intrin->src[1].ssa; + b->cursor = nir_before_instr(instr); + nir_ssa_def *lw = nir_load_var(b, state->line_width_input); + nir_ssa_def *tmp = nir_fsat(b, nir_fadd(b, nir_channel(b, lw, 1), + nir_fneg(b, nir_fabs(b, nir_channel(b, lw, 0))))); + nir_ssa_def *tmp1 = nir_fsat(b, nir_fadd(b, nir_channel(b, lw, 3), + nir_fneg(b, nir_fabs(b, nir_channel(b, lw, 2))))); + + tmp = nir_fmul(b, tmp, tmp1); + tmp = nir_fmul(b, nir_channel(b, out_input, 3), tmp); + + nir_ssa_def *out = nir_vec4(b, nir_channel(b, out_input, 0), + nir_channel(b, out_input, 1), + nir_channel(b, out_input, 2), + tmp); + nir_instr_rewrite_src(instr, &intrin->src[1], nir_src_for_ssa(out)); + } + +} + +static void +nir_lower_aaline_impl(nir_function_impl *impl, + lower_aaline *state) +{ + nir_builder *b = &state->b; + + nir_builder_init(b, impl); + + nir_foreach_block(block, impl) { + nir_lower_aaline_block(block, state); + } +} + +void +nir_lower_aaline_fs(struct nir_shader *shader, int *varying) +{ + lower_aaline state = { + .shader = shader, + }; + if (shader->info.stage != MESA_SHADER_FRAGMENT) + return; + + int highest_location = -1, highest_drv_location = -1; + nir_foreach_variable(var, &shader->inputs) { + if ((int)var->data.location > highest_location) + highest_location = var->data.location; + if ((int)var->data.driver_location > highest_drv_location) + highest_drv_location = var->data.driver_location; + } + + nir_variable *line_width = nir_variable_create(shader, nir_var_shader_in, + glsl_vec4_type(), "aaline"); + if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) { + line_width->data.location = VARYING_SLOT_VAR0; + line_width->data.driver_location = highest_drv_location + 1; + } else { + line_width->data.location = highest_location + 1; + line_width->data.driver_location = highest_drv_location + 1; + } + shader->num_inputs++; + *varying = tgsi_get_generic_gl_varying_index(line_width->data.location, false); + state.line_width_input = line_width; + + nir_foreach_function(function, shader) { + if (function->impl) { + nir_lower_aaline_impl(function->impl, &state); + } + } +} + +typedef struct { + nir_builder b; + nir_shader *shader; + nir_variable *input; +} lower_aapoint; + +static void +nir_lower_aapoint_block(nir_block *block, + lower_aapoint *state, nir_ssa_def *sel) +{ + nir_builder *b = &state->b; + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + + nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr); + if (intrin->intrinsic != nir_intrinsic_store_deref) + continue; + + nir_variable *var = nir_intrinsic_get_var(intrin, 0); + if (var->data.mode != nir_var_shader_out) + continue; + if (var->data.location != FRAG_RESULT_COLOR) + continue; + + nir_ssa_def *out_input = intrin->src[1].ssa; + b->cursor = nir_before_instr(instr); + + nir_ssa_def *tmp = nir_fmul(b, nir_channel(b, out_input, 3), sel); + nir_ssa_def *out = nir_vec4(b, nir_channel(b, out_input, 0), + nir_channel(b, out_input, 1), + nir_channel(b, out_input, 2), + tmp); + nir_instr_rewrite_src(instr, &intrin->src[1], nir_src_for_ssa(out)); + } + +} + +static void +nir_lower_aapoint_impl(nir_function_impl *impl, + lower_aapoint *state) +{ + nir_builder *b = &state->b; + + nir_builder_init(b, impl); + + nir_block *block = nir_start_block(impl); + b->cursor = nir_before_block(block); + + nir_ssa_def *aainput = nir_load_var(b, state->input); + + nir_ssa_def *dist = nir_fadd(b, nir_fmul(b, nir_channel(b, aainput, 0), nir_channel(b, aainput, 0)), + nir_fmul(b, nir_channel(b, aainput, 1), nir_channel(b, aainput, 1))); + + nir_ssa_def *k = nir_channel(b, aainput, 2); + nir_ssa_def *chan_val_one = nir_channel(b, aainput, 3); + nir_ssa_def *comp = nir_flt32(b, chan_val_one, dist); + + nir_intrinsic_instr *discard = nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard_if); + discard->src[0] = nir_src_for_ssa(comp); + nir_builder_instr_insert(b, &discard->instr); + b->shader->info.fs.uses_discard = true; + + /* compute coverage factor = (1-d)/(1-k) */ + /* 1 - k */ + nir_ssa_def *tmp = nir_fadd(b, chan_val_one, nir_fneg(b, k)); + /* 1.0 / (1 - k) */ + tmp = nir_frcp(b, tmp); + + /* 1 - d */ + nir_ssa_def *tmp2 = nir_fadd(b, chan_val_one, nir_fneg(b, dist)); + + /* (1 - d) / (1 - k) */ + nir_ssa_def *coverage = nir_fmul(b, tmp, tmp2); + + /* if (k >= distance) + * sel = coverage; + * else + * sel = 1.0; + */ + nir_ssa_def *sel = nir_b32csel(b, nir_fge32(b, k, dist), coverage, chan_val_one); + + nir_foreach_block(block, impl) { + nir_lower_aapoint_block(block, state, sel); + } +} + +void +nir_lower_aapoint_fs(struct nir_shader *shader, int *varying) +{ + lower_aapoint state = { + .shader = shader, + }; + if (shader->info.stage != MESA_SHADER_FRAGMENT) + return; + + int highest_location = -1, highest_drv_location = -1; + nir_foreach_variable(var, &shader->inputs) { + if ((int)var->data.location > highest_location) + highest_location = var->data.location; + if ((int)var->data.driver_location > highest_drv_location) + highest_drv_location = var->data.driver_location; + } + + nir_variable *aapoint_input = nir_variable_create(shader, nir_var_shader_in, + glsl_vec4_type(), "aapoint"); + if (highest_location == -1 || highest_location < VARYING_SLOT_VAR0) { + aapoint_input->data.location = VARYING_SLOT_VAR0; + } else { + aapoint_input->data.location = highest_location + 1; + } + aapoint_input->data.driver_location = highest_drv_location + 1; + + shader->num_inputs++; + *varying = tgsi_get_generic_gl_varying_index(aapoint_input->data.location, false); + state.input = aapoint_input; + + nir_foreach_function(function, shader) { + if (function->impl) { + nir_lower_aapoint_impl(function->impl, &state); + } + } +} diff --git a/src/gallium/auxiliary/nir/nir_draw_helpers.h b/src/gallium/auxiliary/nir/nir_draw_helpers.h new file mode 100644 index 00000000000..08d163b978d --- /dev/null +++ b/src/gallium/auxiliary/nir/nir_draw_helpers.h @@ -0,0 +1,40 @@ +/************************************************************************** + * + * Copyright 2019 Red Hat. + * All Rights Reserved. + * + * 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 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. + * + **************************************************************************/ + +#ifndef NIR_DRAW_HELPERS_H +#define NIR_DRAW_HELPERS_H +struct nir_shader; +void +nir_lower_pstipple_fs(struct nir_shader *shader, + unsigned *samplerUnitOut, + unsigned fixedUnit, + bool fs_pos_is_sysval); + +void +nir_lower_aaline_fs(struct nir_shader *shader, int *varying); + +void +nir_lower_aapoint_fs(struct nir_shader *shader, int *varying); +#endif