mirror of OpenBSD xenocara tree github.com/openbsd/xenocara
openbsd
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at jcs 157 lines 5.8 kB view raw
1/* 2 * Copyright © 2022 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#include "nir.h" 25#include "nir_builder.h" 26#include "nir_builtin_builder.h" 27 28typedef struct { 29 bool set_barycentrics; 30 nir_intrinsic_instr *found_baryc; 31} lower_point_smooth_state; 32 33static nir_intrinsic_instr * 34find_any_used_barycentrics(nir_function_impl *impl) 35{ 36 nir_foreach_block(block, impl) { 37 nir_foreach_instr(instr, block) { 38 if (instr->type == nir_instr_type_intrinsic) { 39 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); 40 if (intr->intrinsic == nir_intrinsic_load_barycentric_pixel || 41 intr->intrinsic == nir_intrinsic_load_barycentric_centroid || 42 intr->intrinsic == nir_intrinsic_load_barycentric_sample) 43 return intr; 44 } 45 } 46 } 47 48 return NULL; 49} 50 51/** 52 * This NIR lowers pass for point smoothing by modifying the alpha value of 53 * fragment outputs using the distance from the center of the point. 54 * Anti-aliased points get rounded with respect to their radius. 55 */ 56 57static bool 58lower_point_smooth(nir_builder *b, nir_intrinsic_instr *intr, void *state) 59{ 60 lower_point_smooth_state *s = (lower_point_smooth_state *)state; 61 62 if (intr->intrinsic != nir_intrinsic_store_output && 63 intr->intrinsic != nir_intrinsic_store_deref) 64 return false; 65 66 int out_src_idx; 67 if (intr->intrinsic == nir_intrinsic_store_output) { 68 int location = nir_intrinsic_io_semantics(intr).location; 69 if ((location != FRAG_RESULT_COLOR && location < FRAG_RESULT_DATA0) || 70 nir_intrinsic_src_type(intr) != nir_type_float32) 71 return false; 72 out_src_idx = 0; 73 } else { 74 nir_variable *var = nir_intrinsic_get_var(intr, 0); 75 if ((var->data.location != FRAG_RESULT_COLOR && 76 var->data.location < FRAG_RESULT_DATA0) || 77 glsl_get_base_type(var->type) != GLSL_TYPE_FLOAT) 78 return false; 79 out_src_idx = 1; 80 } 81 82 assert(intr->num_components == 4); 83 84 b->cursor = nir_before_instr(&intr->instr); 85 86 /* Determine the barycentric coordinates. */ 87 nir_def *baryc; 88 89 if (s->set_barycentrics) { 90 baryc = nir_load_barycentric_pixel(b, 32, 91 .interp_mode = INTERP_MODE_SMOOTH); 92 93 /* Since point interpolation mostly doesn't care about which barycentrics 94 * are used, use any that are used by the shader. This is an optimization 95 * for hw that is faster if only one set of barycentrics is used. 96 */ 97 if (s->found_baryc) { 98 nir_intrinsic_instr *baryc_intr = 99 nir_instr_as_intrinsic(baryc->parent_instr); 100 101 /* Overwrite the intrinsic we just created. */ 102 baryc_intr->intrinsic = s->found_baryc->intrinsic; 103 nir_intrinsic_set_interp_mode(baryc_intr, 104 nir_intrinsic_interp_mode(s->found_baryc)); 105 } 106 } else { 107 baryc = nir_undef(b, 2, 32); 108 } 109 110 nir_def *coord = nir_load_point_coord_maybe_flipped(b, baryc); 111 112 /* point_size = 1.0 / dFdx(gl_PointCoord.x); */ 113 nir_def *point_size = nir_frcp(b, nir_ddx(b, nir_channel(b, coord, 0))); 114 115 /* radius = point_size * 0.5 */ 116 nir_def *radius = nir_fmul_imm(b, point_size, 0.5); 117 118 /** 119 * Compute the distance of point from centre 120 * distance = √ (x - 0.5)^2 + (y - 0.5)^2 121 */ 122 nir_def *distance = nir_fast_distance(b, coord, 123 nir_imm_vec2(b, 0.5, 0.5)); 124 distance = nir_fmul(b, distance, point_size); 125 126 /* alpha = min(max(radius - distance, 0.0), 1.0) */ 127 nir_def *coverage = nir_fsat(b, nir_fsub(b, radius, distance)); 128 129 /* Discard fragments that are not covered by the point */ 130 nir_discard_if(b, nir_feq_imm(b, coverage, 0.0f)); 131 132 /* Write out the fragment color*vec4(1, 1, 1, coverage)*/ 133 nir_def *one = nir_imm_float(b, 1.0f); 134 nir_def *new_val = nir_fmul(b, nir_vec4(b, one, one, one, coverage), 135 intr->src[out_src_idx].ssa); 136 nir_src_rewrite(&intr->src[out_src_idx], new_val); 137 138 return true; 139} 140 141bool 142nir_lower_point_smooth(nir_shader *shader, bool set_barycentrics) 143{ 144 assert(shader->info.stage == MESA_SHADER_FRAGMENT); 145 nir_function_impl *impl = nir_shader_get_entrypoint(shader); 146 147 lower_point_smooth_state state = { 148 .set_barycentrics = set_barycentrics, 149 .found_baryc = set_barycentrics ? find_any_used_barycentrics(impl) : NULL, 150 }; 151 152 return nir_shader_intrinsics_pass(shader, lower_point_smooth, 153 nir_metadata_loop_analysis | 154 nir_metadata_block_index | 155 nir_metadata_dominance, 156 &state); 157}