1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2009 VMware, Inc. 4bf215546Sopenharmony_ci * Copyright 2007 VMware, Inc. 5bf215546Sopenharmony_ci * All Rights Reserved. 6bf215546Sopenharmony_ci * 7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 9bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 10bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 11bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 12bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 13bf215546Sopenharmony_ci * the following conditions: 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 16bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 17bf215546Sopenharmony_ci * of the Software. 18bf215546Sopenharmony_ci * 19bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 23bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26bf215546Sopenharmony_ci * 27bf215546Sopenharmony_ci **************************************************************************/ 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci/** 30bf215546Sopenharmony_ci * @file 31bf215546Sopenharmony_ci * Code generate the whole fragment pipeline. 32bf215546Sopenharmony_ci * 33bf215546Sopenharmony_ci * The fragment pipeline consists of the following stages: 34bf215546Sopenharmony_ci * - early depth test 35bf215546Sopenharmony_ci * - fragment shader 36bf215546Sopenharmony_ci * - alpha test 37bf215546Sopenharmony_ci * - depth/stencil test 38bf215546Sopenharmony_ci * - blending 39bf215546Sopenharmony_ci * 40bf215546Sopenharmony_ci * This file has only the glue to assemble the fragment pipeline. The actual 41bf215546Sopenharmony_ci * plumbing of converting Gallium state into LLVM IR is done elsewhere, in the 42bf215546Sopenharmony_ci * lp_bld_*.[ch] files, and in a complete generic and reusable way. Here we 43bf215546Sopenharmony_ci * muster the LLVM JIT execution engine to create a function that follows an 44bf215546Sopenharmony_ci * established binary interface and that can be called from C directly. 45bf215546Sopenharmony_ci * 46bf215546Sopenharmony_ci * A big source of complexity here is that we often want to run different 47bf215546Sopenharmony_ci * stages with different precisions and data types and precisions. For example, 48bf215546Sopenharmony_ci * the fragment shader needs typically to be done in floats, but the 49bf215546Sopenharmony_ci * depth/stencil test and blending is better done in the type that most closely 50bf215546Sopenharmony_ci * matches the depth/stencil and color buffer respectively. 51bf215546Sopenharmony_ci * 52bf215546Sopenharmony_ci * Since the width of a SIMD vector register stays the same regardless of the 53bf215546Sopenharmony_ci * element type, different types imply different number of elements, so we must 54bf215546Sopenharmony_ci * code generate more instances of the stages with larger types to be able to 55bf215546Sopenharmony_ci * feed/consume the stages with smaller types. 56bf215546Sopenharmony_ci * 57bf215546Sopenharmony_ci * @author Jose Fonseca <jfonseca@vmware.com> 58bf215546Sopenharmony_ci */ 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ci#include <limits.h> 61bf215546Sopenharmony_ci#include "pipe/p_defines.h" 62bf215546Sopenharmony_ci#include "util/u_inlines.h" 63bf215546Sopenharmony_ci#include "util/u_memory.h" 64bf215546Sopenharmony_ci#include "util/u_pointer.h" 65bf215546Sopenharmony_ci#include "util/format/u_format.h" 66bf215546Sopenharmony_ci#include "util/u_dump.h" 67bf215546Sopenharmony_ci#include "util/u_string.h" 68bf215546Sopenharmony_ci#include "util/u_dual_blend.h" 69bf215546Sopenharmony_ci#include "util/u_upload_mgr.h" 70bf215546Sopenharmony_ci#include "util/os_time.h" 71bf215546Sopenharmony_ci#include "pipe/p_shader_tokens.h" 72bf215546Sopenharmony_ci#include "draw/draw_context.h" 73bf215546Sopenharmony_ci#include "tgsi/tgsi_dump.h" 74bf215546Sopenharmony_ci#include "tgsi/tgsi_scan.h" 75bf215546Sopenharmony_ci#include "tgsi/tgsi_parse.h" 76bf215546Sopenharmony_ci#include "gallivm/lp_bld_type.h" 77bf215546Sopenharmony_ci#include "gallivm/lp_bld_const.h" 78bf215546Sopenharmony_ci#include "gallivm/lp_bld_conv.h" 79bf215546Sopenharmony_ci#include "gallivm/lp_bld_init.h" 80bf215546Sopenharmony_ci#include "gallivm/lp_bld_intr.h" 81bf215546Sopenharmony_ci#include "gallivm/lp_bld_logic.h" 82bf215546Sopenharmony_ci#include "gallivm/lp_bld_tgsi.h" 83bf215546Sopenharmony_ci#include "gallivm/lp_bld_nir.h" 84bf215546Sopenharmony_ci#include "gallivm/lp_bld_swizzle.h" 85bf215546Sopenharmony_ci#include "gallivm/lp_bld_flow.h" 86bf215546Sopenharmony_ci#include "gallivm/lp_bld_debug.h" 87bf215546Sopenharmony_ci#include "gallivm/lp_bld_arit.h" 88bf215546Sopenharmony_ci#include "gallivm/lp_bld_bitarit.h" 89bf215546Sopenharmony_ci#include "gallivm/lp_bld_pack.h" 90bf215546Sopenharmony_ci#include "gallivm/lp_bld_format.h" 91bf215546Sopenharmony_ci#include "gallivm/lp_bld_quad.h" 92bf215546Sopenharmony_ci#include "gallivm/lp_bld_gather.h" 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci#include "lp_bld_alpha.h" 95bf215546Sopenharmony_ci#include "lp_bld_blend.h" 96bf215546Sopenharmony_ci#include "lp_bld_depth.h" 97bf215546Sopenharmony_ci#include "lp_bld_interp.h" 98bf215546Sopenharmony_ci#include "lp_context.h" 99bf215546Sopenharmony_ci#include "lp_debug.h" 100bf215546Sopenharmony_ci#include "lp_perf.h" 101bf215546Sopenharmony_ci#include "lp_setup.h" 102bf215546Sopenharmony_ci#include "lp_state.h" 103bf215546Sopenharmony_ci#include "lp_tex_sample.h" 104bf215546Sopenharmony_ci#include "lp_flush.h" 105bf215546Sopenharmony_ci#include "lp_state_fs.h" 106bf215546Sopenharmony_ci#include "lp_rast.h" 107bf215546Sopenharmony_ci#include "nir/nir_to_tgsi_info.h" 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci#include "lp_screen.h" 110bf215546Sopenharmony_ci#include "compiler/nir/nir_serialize.h" 111bf215546Sopenharmony_ci#include "util/mesa-sha1.h" 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci/** Fragment shader number (for debugging) */ 115bf215546Sopenharmony_cistatic unsigned fs_no = 0; 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_cistatic void 119bf215546Sopenharmony_ciload_unswizzled_block(struct gallivm_state *gallivm, 120bf215546Sopenharmony_ci LLVMValueRef base_ptr, 121bf215546Sopenharmony_ci LLVMValueRef stride, 122bf215546Sopenharmony_ci unsigned block_width, 123bf215546Sopenharmony_ci unsigned block_height, 124bf215546Sopenharmony_ci LLVMValueRef* dst, 125bf215546Sopenharmony_ci struct lp_type dst_type, 126bf215546Sopenharmony_ci unsigned dst_count, 127bf215546Sopenharmony_ci unsigned dst_alignment); 128bf215546Sopenharmony_ci/** 129bf215546Sopenharmony_ci * Checks if a format description is an arithmetic format 130bf215546Sopenharmony_ci * 131bf215546Sopenharmony_ci * A format which has irregular channel sizes such as R3_G3_B2 or R5_G6_B5. 132bf215546Sopenharmony_ci */ 133bf215546Sopenharmony_cistatic inline boolean 134bf215546Sopenharmony_ciis_arithmetic_format(const struct util_format_description *format_desc) 135bf215546Sopenharmony_ci{ 136bf215546Sopenharmony_ci boolean arith = false; 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci for (unsigned i = 0; i < format_desc->nr_channels; ++i) { 139bf215546Sopenharmony_ci arith |= format_desc->channel[i].size != format_desc->channel[0].size; 140bf215546Sopenharmony_ci arith |= (format_desc->channel[i].size % 8) != 0; 141bf215546Sopenharmony_ci } 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci return arith; 144bf215546Sopenharmony_ci} 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci 147bf215546Sopenharmony_ci/** 148bf215546Sopenharmony_ci * Checks if this format requires special handling due to required expansion 149bf215546Sopenharmony_ci * to floats for blending, and furthermore has "natural" packed AoS -> unpacked 150bf215546Sopenharmony_ci * SoA conversion. 151bf215546Sopenharmony_ci */ 152bf215546Sopenharmony_cistatic inline boolean 153bf215546Sopenharmony_ciformat_expands_to_float_soa(const struct util_format_description *format_desc) 154bf215546Sopenharmony_ci{ 155bf215546Sopenharmony_ci if (format_desc->format == PIPE_FORMAT_R11G11B10_FLOAT || 156bf215546Sopenharmony_ci format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) { 157bf215546Sopenharmony_ci return true; 158bf215546Sopenharmony_ci } 159bf215546Sopenharmony_ci return false; 160bf215546Sopenharmony_ci} 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci/** 164bf215546Sopenharmony_ci * Retrieves the type representing the memory layout for a format 165bf215546Sopenharmony_ci * 166bf215546Sopenharmony_ci * e.g. RGBA16F = 4x half-float and R3G3B2 = 1x byte 167bf215546Sopenharmony_ci */ 168bf215546Sopenharmony_cistatic inline void 169bf215546Sopenharmony_cilp_mem_type_from_format_desc(const struct util_format_description *format_desc, 170bf215546Sopenharmony_ci struct lp_type* type) 171bf215546Sopenharmony_ci{ 172bf215546Sopenharmony_ci unsigned i; 173bf215546Sopenharmony_ci unsigned chan; 174bf215546Sopenharmony_ci 175bf215546Sopenharmony_ci if (format_expands_to_float_soa(format_desc)) { 176bf215546Sopenharmony_ci /* just make this a uint with width of block */ 177bf215546Sopenharmony_ci type->floating = false; 178bf215546Sopenharmony_ci type->fixed = false; 179bf215546Sopenharmony_ci type->sign = false; 180bf215546Sopenharmony_ci type->norm = false; 181bf215546Sopenharmony_ci type->width = format_desc->block.bits; 182bf215546Sopenharmony_ci type->length = 1; 183bf215546Sopenharmony_ci return; 184bf215546Sopenharmony_ci } 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci for (i = 0; i < 4; i++) { 187bf215546Sopenharmony_ci if (format_desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) 188bf215546Sopenharmony_ci break; 189bf215546Sopenharmony_ci } 190bf215546Sopenharmony_ci chan = i; 191bf215546Sopenharmony_ci 192bf215546Sopenharmony_ci memset(type, 0, sizeof(struct lp_type)); 193bf215546Sopenharmony_ci type->floating = format_desc->channel[chan].type == UTIL_FORMAT_TYPE_FLOAT; 194bf215546Sopenharmony_ci type->fixed = format_desc->channel[chan].type == UTIL_FORMAT_TYPE_FIXED; 195bf215546Sopenharmony_ci type->sign = format_desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED; 196bf215546Sopenharmony_ci type->norm = format_desc->channel[chan].normalized; 197bf215546Sopenharmony_ci 198bf215546Sopenharmony_ci if (is_arithmetic_format(format_desc)) { 199bf215546Sopenharmony_ci type->width = 0; 200bf215546Sopenharmony_ci type->length = 1; 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci for (unsigned i = 0; i < format_desc->nr_channels; ++i) { 203bf215546Sopenharmony_ci type->width += format_desc->channel[i].size; 204bf215546Sopenharmony_ci } 205bf215546Sopenharmony_ci } else { 206bf215546Sopenharmony_ci type->width = format_desc->channel[chan].size; 207bf215546Sopenharmony_ci type->length = format_desc->nr_channels; 208bf215546Sopenharmony_ci } 209bf215546Sopenharmony_ci} 210bf215546Sopenharmony_ci 211bf215546Sopenharmony_ci/** 212bf215546Sopenharmony_ci * Expand the relevant bits of mask_input to a n*4-dword mask for the 213bf215546Sopenharmony_ci * n*four pixels in n 2x2 quads. This will set the n*four elements of the 214bf215546Sopenharmony_ci * quad mask vector to 0 or ~0. 215bf215546Sopenharmony_ci * Grouping is 01, 23 for 2 quad mode hence only 0 and 2 are valid 216bf215546Sopenharmony_ci * quad arguments with fs length 8. 217bf215546Sopenharmony_ci * 218bf215546Sopenharmony_ci * \param first_quad which quad(s) of the quad group to test, in [0,3] 219bf215546Sopenharmony_ci * \param mask_input bitwise mask for the whole 4x4 stamp 220bf215546Sopenharmony_ci */ 221bf215546Sopenharmony_cistatic LLVMValueRef 222bf215546Sopenharmony_cigenerate_quad_mask(struct gallivm_state *gallivm, 223bf215546Sopenharmony_ci struct lp_type fs_type, 224bf215546Sopenharmony_ci unsigned first_quad, 225bf215546Sopenharmony_ci unsigned sample, 226bf215546Sopenharmony_ci LLVMValueRef mask_input) /* int64 */ 227bf215546Sopenharmony_ci{ 228bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 229bf215546Sopenharmony_ci LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context); 230bf215546Sopenharmony_ci LLVMValueRef bits[16]; 231bf215546Sopenharmony_ci LLVMValueRef mask, bits_vec; 232bf215546Sopenharmony_ci 233bf215546Sopenharmony_ci /* 234bf215546Sopenharmony_ci * XXX: We'll need a different path for 16 x u8 235bf215546Sopenharmony_ci */ 236bf215546Sopenharmony_ci assert(fs_type.width == 32); 237bf215546Sopenharmony_ci assert(fs_type.length <= ARRAY_SIZE(bits)); 238bf215546Sopenharmony_ci struct lp_type mask_type = lp_int_type(fs_type); 239bf215546Sopenharmony_ci 240bf215546Sopenharmony_ci /* 241bf215546Sopenharmony_ci * mask_input >>= (quad * 4) 242bf215546Sopenharmony_ci */ 243bf215546Sopenharmony_ci int shift; 244bf215546Sopenharmony_ci switch (first_quad) { 245bf215546Sopenharmony_ci case 0: 246bf215546Sopenharmony_ci shift = 0; 247bf215546Sopenharmony_ci break; 248bf215546Sopenharmony_ci case 1: 249bf215546Sopenharmony_ci assert(fs_type.length == 4); 250bf215546Sopenharmony_ci shift = 2; 251bf215546Sopenharmony_ci break; 252bf215546Sopenharmony_ci case 2: 253bf215546Sopenharmony_ci shift = 8; 254bf215546Sopenharmony_ci break; 255bf215546Sopenharmony_ci case 3: 256bf215546Sopenharmony_ci assert(fs_type.length == 4); 257bf215546Sopenharmony_ci shift = 10; 258bf215546Sopenharmony_ci break; 259bf215546Sopenharmony_ci default: 260bf215546Sopenharmony_ci assert(0); 261bf215546Sopenharmony_ci shift = 0; 262bf215546Sopenharmony_ci } 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_ci mask_input = LLVMBuildLShr(builder, mask_input, 265bf215546Sopenharmony_ci lp_build_const_int64(gallivm, 16 * sample), ""); 266bf215546Sopenharmony_ci mask_input = LLVMBuildTrunc(builder, mask_input, i32t, ""); 267bf215546Sopenharmony_ci mask_input = LLVMBuildAnd(builder, mask_input, 268bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 0xffff), ""); 269bf215546Sopenharmony_ci mask_input = LLVMBuildLShr(builder, mask_input, 270bf215546Sopenharmony_ci LLVMConstInt(i32t, shift, 0), ""); 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_ci /* 273bf215546Sopenharmony_ci * mask = { mask_input & (1 << i), for i in [0,3] } 274bf215546Sopenharmony_ci */ 275bf215546Sopenharmony_ci mask = lp_build_broadcast(gallivm, 276bf215546Sopenharmony_ci lp_build_vec_type(gallivm, mask_type), 277bf215546Sopenharmony_ci mask_input); 278bf215546Sopenharmony_ci 279bf215546Sopenharmony_ci for (int i = 0; i < fs_type.length / 4; i++) { 280bf215546Sopenharmony_ci unsigned j = 2 * (i % 2) + (i / 2) * 8; 281bf215546Sopenharmony_ci bits[4*i + 0] = LLVMConstInt(i32t, 1ULL << (j + 0), 0); 282bf215546Sopenharmony_ci bits[4*i + 1] = LLVMConstInt(i32t, 1ULL << (j + 1), 0); 283bf215546Sopenharmony_ci bits[4*i + 2] = LLVMConstInt(i32t, 1ULL << (j + 4), 0); 284bf215546Sopenharmony_ci bits[4*i + 3] = LLVMConstInt(i32t, 1ULL << (j + 5), 0); 285bf215546Sopenharmony_ci } 286bf215546Sopenharmony_ci bits_vec = LLVMConstVector(bits, fs_type.length); 287bf215546Sopenharmony_ci mask = LLVMBuildAnd(builder, mask, bits_vec, ""); 288bf215546Sopenharmony_ci 289bf215546Sopenharmony_ci /* 290bf215546Sopenharmony_ci * mask = mask == bits ? ~0 : 0 291bf215546Sopenharmony_ci */ 292bf215546Sopenharmony_ci mask = lp_build_compare(gallivm, 293bf215546Sopenharmony_ci mask_type, PIPE_FUNC_EQUAL, 294bf215546Sopenharmony_ci mask, bits_vec); 295bf215546Sopenharmony_ci 296bf215546Sopenharmony_ci return mask; 297bf215546Sopenharmony_ci} 298bf215546Sopenharmony_ci 299bf215546Sopenharmony_ci 300bf215546Sopenharmony_ci#define EARLY_DEPTH_TEST 0x1 301bf215546Sopenharmony_ci#define LATE_DEPTH_TEST 0x2 302bf215546Sopenharmony_ci#define EARLY_DEPTH_WRITE 0x4 303bf215546Sopenharmony_ci#define LATE_DEPTH_WRITE 0x8 304bf215546Sopenharmony_ci#define EARLY_DEPTH_TEST_INFERRED 0x10 //only with EARLY_DEPTH_TEST 305bf215546Sopenharmony_ci 306bf215546Sopenharmony_ci 307bf215546Sopenharmony_cistatic int 308bf215546Sopenharmony_cifind_output_by_semantic(const struct tgsi_shader_info *info, 309bf215546Sopenharmony_ci enum tgsi_semantic semantic, 310bf215546Sopenharmony_ci unsigned index) 311bf215546Sopenharmony_ci{ 312bf215546Sopenharmony_ci for (int i = 0; i < info->num_outputs; i++) 313bf215546Sopenharmony_ci if (info->output_semantic_name[i] == semantic && 314bf215546Sopenharmony_ci info->output_semantic_index[i] == index) 315bf215546Sopenharmony_ci return i; 316bf215546Sopenharmony_ci 317bf215546Sopenharmony_ci return -1; 318bf215546Sopenharmony_ci} 319bf215546Sopenharmony_ci 320bf215546Sopenharmony_ci 321bf215546Sopenharmony_ci/** 322bf215546Sopenharmony_ci * Fetch the specified lp_jit_viewport structure for a given viewport_index. 323bf215546Sopenharmony_ci */ 324bf215546Sopenharmony_cistatic LLVMValueRef 325bf215546Sopenharmony_cilp_llvm_viewport(LLVMValueRef context_ptr, 326bf215546Sopenharmony_ci struct gallivm_state *gallivm, 327bf215546Sopenharmony_ci LLVMValueRef viewport_index) 328bf215546Sopenharmony_ci{ 329bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 330bf215546Sopenharmony_ci LLVMValueRef ptr; 331bf215546Sopenharmony_ci LLVMValueRef res; 332bf215546Sopenharmony_ci struct lp_type viewport_type = 333bf215546Sopenharmony_ci lp_type_float_vec(32, 32 * LP_JIT_VIEWPORT_NUM_FIELDS); 334bf215546Sopenharmony_ci 335bf215546Sopenharmony_ci ptr = lp_jit_context_viewports(gallivm, context_ptr); 336bf215546Sopenharmony_ci ptr = LLVMBuildPointerCast(builder, ptr, 337bf215546Sopenharmony_ci LLVMPointerType(lp_build_vec_type(gallivm, viewport_type), 0), ""); 338bf215546Sopenharmony_ci 339bf215546Sopenharmony_ci res = lp_build_pointer_get(builder, ptr, viewport_index); 340bf215546Sopenharmony_ci 341bf215546Sopenharmony_ci return res; 342bf215546Sopenharmony_ci} 343bf215546Sopenharmony_ci 344bf215546Sopenharmony_ci 345bf215546Sopenharmony_cistatic LLVMValueRef 346bf215546Sopenharmony_cilp_build_depth_clamp(struct gallivm_state *gallivm, 347bf215546Sopenharmony_ci LLVMBuilderRef builder, 348bf215546Sopenharmony_ci bool depth_clamp, 349bf215546Sopenharmony_ci bool restrict_depth, 350bf215546Sopenharmony_ci struct lp_type type, 351bf215546Sopenharmony_ci LLVMValueRef context_ptr, 352bf215546Sopenharmony_ci LLVMValueRef thread_data_ptr, 353bf215546Sopenharmony_ci LLVMValueRef z) 354bf215546Sopenharmony_ci{ 355bf215546Sopenharmony_ci LLVMValueRef viewport, min_depth, max_depth; 356bf215546Sopenharmony_ci LLVMValueRef viewport_index; 357bf215546Sopenharmony_ci struct lp_build_context f32_bld; 358bf215546Sopenharmony_ci 359bf215546Sopenharmony_ci assert(type.floating); 360bf215546Sopenharmony_ci lp_build_context_init(&f32_bld, gallivm, type); 361bf215546Sopenharmony_ci 362bf215546Sopenharmony_ci if (restrict_depth) 363bf215546Sopenharmony_ci z = lp_build_clamp(&f32_bld, z, f32_bld.zero, f32_bld.one); 364bf215546Sopenharmony_ci 365bf215546Sopenharmony_ci if (!depth_clamp) 366bf215546Sopenharmony_ci return z; 367bf215546Sopenharmony_ci 368bf215546Sopenharmony_ci /* 369bf215546Sopenharmony_ci * Assumes clamping of the viewport index will occur in setup/gs. Value 370bf215546Sopenharmony_ci * is passed through the rasterization stage via lp_rast_shader_inputs. 371bf215546Sopenharmony_ci * 372bf215546Sopenharmony_ci * See: draw_clamp_viewport_idx and lp_clamp_viewport_idx for clamping 373bf215546Sopenharmony_ci * semantics. 374bf215546Sopenharmony_ci */ 375bf215546Sopenharmony_ci viewport_index = lp_jit_thread_data_raster_state_viewport_index(gallivm, 376bf215546Sopenharmony_ci thread_data_ptr); 377bf215546Sopenharmony_ci 378bf215546Sopenharmony_ci /* 379bf215546Sopenharmony_ci * Load the min and max depth from the lp_jit_context.viewports 380bf215546Sopenharmony_ci * array of lp_jit_viewport structures. 381bf215546Sopenharmony_ci */ 382bf215546Sopenharmony_ci viewport = lp_llvm_viewport(context_ptr, gallivm, viewport_index); 383bf215546Sopenharmony_ci 384bf215546Sopenharmony_ci /* viewports[viewport_index].min_depth */ 385bf215546Sopenharmony_ci min_depth = LLVMBuildExtractElement(builder, viewport, 386bf215546Sopenharmony_ci lp_build_const_int32(gallivm, LP_JIT_VIEWPORT_MIN_DEPTH), ""); 387bf215546Sopenharmony_ci min_depth = lp_build_broadcast_scalar(&f32_bld, min_depth); 388bf215546Sopenharmony_ci 389bf215546Sopenharmony_ci /* viewports[viewport_index].max_depth */ 390bf215546Sopenharmony_ci max_depth = LLVMBuildExtractElement(builder, viewport, 391bf215546Sopenharmony_ci lp_build_const_int32(gallivm, LP_JIT_VIEWPORT_MAX_DEPTH), ""); 392bf215546Sopenharmony_ci max_depth = lp_build_broadcast_scalar(&f32_bld, max_depth); 393bf215546Sopenharmony_ci 394bf215546Sopenharmony_ci /* 395bf215546Sopenharmony_ci * Clamp to the min and max depth values for the given viewport. 396bf215546Sopenharmony_ci */ 397bf215546Sopenharmony_ci return lp_build_clamp(&f32_bld, z, min_depth, max_depth); 398bf215546Sopenharmony_ci} 399bf215546Sopenharmony_ci 400bf215546Sopenharmony_ci 401bf215546Sopenharmony_cistatic void 402bf215546Sopenharmony_cilp_build_sample_alpha_to_coverage(struct gallivm_state *gallivm, 403bf215546Sopenharmony_ci struct lp_type type, 404bf215546Sopenharmony_ci unsigned coverage_samples, 405bf215546Sopenharmony_ci LLVMValueRef num_loop, 406bf215546Sopenharmony_ci LLVMValueRef loop_counter, 407bf215546Sopenharmony_ci LLVMValueRef coverage_mask_store, 408bf215546Sopenharmony_ci LLVMValueRef alpha) 409bf215546Sopenharmony_ci{ 410bf215546Sopenharmony_ci struct lp_build_context bld; 411bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 412bf215546Sopenharmony_ci float step = 1.0 / coverage_samples; 413bf215546Sopenharmony_ci 414bf215546Sopenharmony_ci lp_build_context_init(&bld, gallivm, type); 415bf215546Sopenharmony_ci for (unsigned s = 0; s < coverage_samples; s++) { 416bf215546Sopenharmony_ci LLVMValueRef alpha_ref_value = lp_build_const_vec(gallivm, type, step * s); 417bf215546Sopenharmony_ci LLVMValueRef test = lp_build_cmp(&bld, PIPE_FUNC_GREATER, alpha, alpha_ref_value); 418bf215546Sopenharmony_ci 419bf215546Sopenharmony_ci LLVMValueRef s_mask_idx = LLVMBuildMul(builder, lp_build_const_int32(gallivm, s), num_loop, ""); 420bf215546Sopenharmony_ci s_mask_idx = LLVMBuildAdd(builder, s_mask_idx, loop_counter, ""); 421bf215546Sopenharmony_ci LLVMValueRef s_mask_ptr = LLVMBuildGEP(builder, coverage_mask_store, &s_mask_idx, 1, ""); 422bf215546Sopenharmony_ci LLVMValueRef s_mask = LLVMBuildLoad(builder, s_mask_ptr, ""); 423bf215546Sopenharmony_ci s_mask = LLVMBuildAnd(builder, s_mask, test, ""); 424bf215546Sopenharmony_ci LLVMBuildStore(builder, s_mask, s_mask_ptr); 425bf215546Sopenharmony_ci } 426bf215546Sopenharmony_ci}; 427bf215546Sopenharmony_ci 428bf215546Sopenharmony_ci 429bf215546Sopenharmony_cistruct lp_build_fs_llvm_iface { 430bf215546Sopenharmony_ci struct lp_build_fs_iface base; 431bf215546Sopenharmony_ci struct lp_build_interp_soa_context *interp; 432bf215546Sopenharmony_ci struct lp_build_for_loop_state *loop_state; 433bf215546Sopenharmony_ci LLVMValueRef mask_store; 434bf215546Sopenharmony_ci LLVMValueRef sample_id; 435bf215546Sopenharmony_ci LLVMValueRef color_ptr_ptr; 436bf215546Sopenharmony_ci LLVMValueRef color_stride_ptr; 437bf215546Sopenharmony_ci LLVMValueRef color_sample_stride_ptr; 438bf215546Sopenharmony_ci LLVMValueRef zs_base_ptr; 439bf215546Sopenharmony_ci LLVMValueRef zs_stride; 440bf215546Sopenharmony_ci LLVMValueRef zs_sample_stride; 441bf215546Sopenharmony_ci const struct lp_fragment_shader_variant_key *key; 442bf215546Sopenharmony_ci}; 443bf215546Sopenharmony_ci 444bf215546Sopenharmony_ci 445bf215546Sopenharmony_cistatic LLVMValueRef 446bf215546Sopenharmony_cifs_interp(const struct lp_build_fs_iface *iface, 447bf215546Sopenharmony_ci struct lp_build_context *bld, 448bf215546Sopenharmony_ci unsigned attrib, unsigned chan, 449bf215546Sopenharmony_ci bool centroid, bool sample, 450bf215546Sopenharmony_ci LLVMValueRef attrib_indir, 451bf215546Sopenharmony_ci LLVMValueRef offsets[2]) 452bf215546Sopenharmony_ci{ 453bf215546Sopenharmony_ci struct lp_build_fs_llvm_iface *fs_iface = (struct lp_build_fs_llvm_iface *)iface; 454bf215546Sopenharmony_ci struct lp_build_interp_soa_context *interp = fs_iface->interp; 455bf215546Sopenharmony_ci unsigned loc = TGSI_INTERPOLATE_LOC_CENTER; 456bf215546Sopenharmony_ci if (centroid) 457bf215546Sopenharmony_ci loc = TGSI_INTERPOLATE_LOC_CENTROID; 458bf215546Sopenharmony_ci if (sample) 459bf215546Sopenharmony_ci loc = TGSI_INTERPOLATE_LOC_SAMPLE; 460bf215546Sopenharmony_ci 461bf215546Sopenharmony_ci return lp_build_interp_soa(interp, bld->gallivm, fs_iface->loop_state->counter, 462bf215546Sopenharmony_ci fs_iface->mask_store, 463bf215546Sopenharmony_ci attrib, chan, loc, attrib_indir, offsets); 464bf215546Sopenharmony_ci} 465bf215546Sopenharmony_ci 466bf215546Sopenharmony_ci/* Convert depth-stencil format to a single component one, returning 467bf215546Sopenharmony_ci * PIPE_FORMAT_NONE if it doesn't contain the required component. */ 468bf215546Sopenharmony_cistatic enum pipe_format 469bf215546Sopenharmony_ciselect_zs_component_format(enum pipe_format format, 470bf215546Sopenharmony_ci bool fetch_stencil) 471bf215546Sopenharmony_ci{ 472bf215546Sopenharmony_ci const struct util_format_description* desc = util_format_description(format); 473bf215546Sopenharmony_ci if (fetch_stencil && !util_format_has_stencil(desc)) 474bf215546Sopenharmony_ci return PIPE_FORMAT_NONE; 475bf215546Sopenharmony_ci if (!fetch_stencil && !util_format_has_depth(desc)) 476bf215546Sopenharmony_ci return PIPE_FORMAT_NONE; 477bf215546Sopenharmony_ci 478bf215546Sopenharmony_ci switch (format) { 479bf215546Sopenharmony_ci case PIPE_FORMAT_Z24_UNORM_S8_UINT: 480bf215546Sopenharmony_ci return fetch_stencil ? PIPE_FORMAT_X24S8_UINT : PIPE_FORMAT_Z24X8_UNORM; 481bf215546Sopenharmony_ci case PIPE_FORMAT_S8_UINT_Z24_UNORM: 482bf215546Sopenharmony_ci return fetch_stencil ? PIPE_FORMAT_S8X24_UINT : PIPE_FORMAT_X8Z24_UNORM; 483bf215546Sopenharmony_ci case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT: 484bf215546Sopenharmony_ci return fetch_stencil ? PIPE_FORMAT_X32_S8X24_UINT : format; 485bf215546Sopenharmony_ci default: 486bf215546Sopenharmony_ci return format; 487bf215546Sopenharmony_ci } 488bf215546Sopenharmony_ci} 489bf215546Sopenharmony_ci 490bf215546Sopenharmony_cistatic void 491bf215546Sopenharmony_cifs_fb_fetch(const struct lp_build_fs_iface *iface, 492bf215546Sopenharmony_ci struct lp_build_context *bld, 493bf215546Sopenharmony_ci int location, 494bf215546Sopenharmony_ci LLVMValueRef result[4]) 495bf215546Sopenharmony_ci{ 496bf215546Sopenharmony_ci struct lp_build_fs_llvm_iface *fs_iface = (struct lp_build_fs_llvm_iface *)iface; 497bf215546Sopenharmony_ci struct gallivm_state *gallivm = bld->gallivm; 498bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 499bf215546Sopenharmony_ci const struct lp_fragment_shader_variant_key *key = fs_iface->key; 500bf215546Sopenharmony_ci 501bf215546Sopenharmony_ci LLVMValueRef buf_ptr; 502bf215546Sopenharmony_ci LLVMValueRef stride; 503bf215546Sopenharmony_ci enum pipe_format buf_format; 504bf215546Sopenharmony_ci 505bf215546Sopenharmony_ci const bool fetch_stencil = location == FRAG_RESULT_STENCIL; 506bf215546Sopenharmony_ci const bool fetch_zs = fetch_stencil || location == FRAG_RESULT_DEPTH; 507bf215546Sopenharmony_ci if (fetch_zs) { 508bf215546Sopenharmony_ci buf_ptr = fs_iface->zs_base_ptr; 509bf215546Sopenharmony_ci stride = fs_iface->zs_stride; 510bf215546Sopenharmony_ci buf_format = select_zs_component_format(key->zsbuf_format, fetch_stencil); 511bf215546Sopenharmony_ci } 512bf215546Sopenharmony_ci else { 513bf215546Sopenharmony_ci assert(location >= FRAG_RESULT_DATA0 && location <= FRAG_RESULT_DATA7); 514bf215546Sopenharmony_ci const int cbuf = location - FRAG_RESULT_DATA0; 515bf215546Sopenharmony_ci LLVMValueRef index = lp_build_const_int32(gallivm, cbuf); 516bf215546Sopenharmony_ci 517bf215546Sopenharmony_ci buf_ptr = LLVMBuildLoad(builder, LLVMBuildGEP(builder, fs_iface->color_ptr_ptr, &index, 1, ""), ""); 518bf215546Sopenharmony_ci stride = LLVMBuildLoad(builder, LLVMBuildGEP(builder, fs_iface->color_stride_ptr, &index, 1, ""), ""); 519bf215546Sopenharmony_ci buf_format = key->cbuf_format[cbuf]; 520bf215546Sopenharmony_ci } 521bf215546Sopenharmony_ci 522bf215546Sopenharmony_ci const struct util_format_description* out_format_desc = util_format_description(buf_format); 523bf215546Sopenharmony_ci if (out_format_desc->format == PIPE_FORMAT_NONE) { 524bf215546Sopenharmony_ci result[0] = result[1] = result[2] = result[3] = bld->undef; 525bf215546Sopenharmony_ci return; 526bf215546Sopenharmony_ci } 527bf215546Sopenharmony_ci 528bf215546Sopenharmony_ci unsigned block_size = bld->type.length; 529bf215546Sopenharmony_ci unsigned block_height = key->resource_1d ? 1 : 2; 530bf215546Sopenharmony_ci unsigned block_width = block_size / block_height; 531bf215546Sopenharmony_ci 532bf215546Sopenharmony_ci if (key->multisample) { 533bf215546Sopenharmony_ci LLVMValueRef sample_stride; 534bf215546Sopenharmony_ci 535bf215546Sopenharmony_ci if (fetch_zs) { 536bf215546Sopenharmony_ci sample_stride = fs_iface->zs_sample_stride; 537bf215546Sopenharmony_ci } 538bf215546Sopenharmony_ci else { 539bf215546Sopenharmony_ci LLVMValueRef index = lp_build_const_int32(gallivm, location - FRAG_RESULT_DATA0); 540bf215546Sopenharmony_ci sample_stride = LLVMBuildLoad(builder, 541bf215546Sopenharmony_ci LLVMBuildGEP(builder, fs_iface->color_sample_stride_ptr, 542bf215546Sopenharmony_ci &index, 1, ""), ""); 543bf215546Sopenharmony_ci } 544bf215546Sopenharmony_ci 545bf215546Sopenharmony_ci LLVMValueRef sample_offset = LLVMBuildMul(builder, sample_stride, fs_iface->sample_id, ""); 546bf215546Sopenharmony_ci buf_ptr = LLVMBuildGEP(builder, buf_ptr, &sample_offset, 1, ""); 547bf215546Sopenharmony_ci } 548bf215546Sopenharmony_ci 549bf215546Sopenharmony_ci /* fragment shader executes on 4x4 blocks. depending on vector width it can 550bf215546Sopenharmony_ci * execute 2 or 4 iterations. only move to the next row once the top row 551bf215546Sopenharmony_ci * has completed 8 wide 1 iteration, 4 wide 2 iterations */ 552bf215546Sopenharmony_ci LLVMValueRef x_offset = NULL, y_offset = NULL; 553bf215546Sopenharmony_ci if (!key->resource_1d) { 554bf215546Sopenharmony_ci LLVMValueRef counter = fs_iface->loop_state->counter; 555bf215546Sopenharmony_ci 556bf215546Sopenharmony_ci if (block_size == 4) { 557bf215546Sopenharmony_ci x_offset = LLVMBuildShl(builder, 558bf215546Sopenharmony_ci LLVMBuildAnd(builder, fs_iface->loop_state->counter, lp_build_const_int32(gallivm, 1), ""), 559bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 1), ""); 560bf215546Sopenharmony_ci counter = LLVMBuildLShr(builder, fs_iface->loop_state->counter, lp_build_const_int32(gallivm, 1), ""); 561bf215546Sopenharmony_ci } 562bf215546Sopenharmony_ci y_offset = LLVMBuildMul(builder, counter, lp_build_const_int32(gallivm, 2), ""); 563bf215546Sopenharmony_ci } 564bf215546Sopenharmony_ci 565bf215546Sopenharmony_ci LLVMValueRef offsets[4 * 4]; 566bf215546Sopenharmony_ci for (unsigned i = 0; i < block_size; i++) { 567bf215546Sopenharmony_ci unsigned x = i % block_width; 568bf215546Sopenharmony_ci unsigned y = i / block_width; 569bf215546Sopenharmony_ci 570bf215546Sopenharmony_ci if (block_size == 8) { 571bf215546Sopenharmony_ci /* remap the raw slots into the fragment shader execution mode. */ 572bf215546Sopenharmony_ci /* this math took me way too long to work out, I'm sure it's overkill. */ 573bf215546Sopenharmony_ci x = (i & 1) + ((i >> 2) << 1); 574bf215546Sopenharmony_ci if (!key->resource_1d) 575bf215546Sopenharmony_ci y = (i & 2) >> 1; 576bf215546Sopenharmony_ci } 577bf215546Sopenharmony_ci 578bf215546Sopenharmony_ci LLVMValueRef x_val; 579bf215546Sopenharmony_ci if (x_offset) { 580bf215546Sopenharmony_ci x_val = LLVMBuildAdd(builder, lp_build_const_int32(gallivm, x), x_offset, ""); 581bf215546Sopenharmony_ci x_val = LLVMBuildMul(builder, x_val, lp_build_const_int32(gallivm, out_format_desc->block.bits / 8), ""); 582bf215546Sopenharmony_ci } else { 583bf215546Sopenharmony_ci x_val = lp_build_const_int32(gallivm, x * (out_format_desc->block.bits / 8)); 584bf215546Sopenharmony_ci } 585bf215546Sopenharmony_ci 586bf215546Sopenharmony_ci LLVMValueRef y_val = lp_build_const_int32(gallivm, y); 587bf215546Sopenharmony_ci if (y_offset) 588bf215546Sopenharmony_ci y_val = LLVMBuildAdd(builder, y_val, y_offset, ""); 589bf215546Sopenharmony_ci y_val = LLVMBuildMul(builder, y_val, stride, ""); 590bf215546Sopenharmony_ci 591bf215546Sopenharmony_ci offsets[i] = LLVMBuildAdd(builder, x_val, y_val, ""); 592bf215546Sopenharmony_ci } 593bf215546Sopenharmony_ci LLVMValueRef offset = lp_build_gather_values(gallivm, offsets, block_size); 594bf215546Sopenharmony_ci 595bf215546Sopenharmony_ci struct lp_type texel_type = bld->type; 596bf215546Sopenharmony_ci if (out_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB && 597bf215546Sopenharmony_ci out_format_desc->channel[0].pure_integer) { 598bf215546Sopenharmony_ci if (out_format_desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) { 599bf215546Sopenharmony_ci texel_type = lp_type_int_vec(bld->type.width, bld->type.width * bld->type.length); 600bf215546Sopenharmony_ci } 601bf215546Sopenharmony_ci else if (out_format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED) { 602bf215546Sopenharmony_ci texel_type = lp_type_uint_vec(bld->type.width, bld->type.width * bld->type.length); 603bf215546Sopenharmony_ci } 604bf215546Sopenharmony_ci } else if (fetch_stencil) { 605bf215546Sopenharmony_ci texel_type = lp_type_uint_vec(bld->type.width, bld->type.width * bld->type.length); 606bf215546Sopenharmony_ci } 607bf215546Sopenharmony_ci 608bf215546Sopenharmony_ci lp_build_fetch_rgba_soa(gallivm, out_format_desc, texel_type, 609bf215546Sopenharmony_ci true, buf_ptr, offset, 610bf215546Sopenharmony_ci NULL, NULL, NULL, result); 611bf215546Sopenharmony_ci} 612bf215546Sopenharmony_ci 613bf215546Sopenharmony_ci 614bf215546Sopenharmony_ci/** 615bf215546Sopenharmony_ci * Generate the fragment shader, depth/stencil test, and alpha tests. 616bf215546Sopenharmony_ci */ 617bf215546Sopenharmony_cistatic void 618bf215546Sopenharmony_cigenerate_fs_loop(struct gallivm_state *gallivm, 619bf215546Sopenharmony_ci struct lp_fragment_shader *shader, 620bf215546Sopenharmony_ci const struct lp_fragment_shader_variant_key *key, 621bf215546Sopenharmony_ci LLVMBuilderRef builder, 622bf215546Sopenharmony_ci struct lp_type type, 623bf215546Sopenharmony_ci LLVMValueRef context_ptr, 624bf215546Sopenharmony_ci LLVMValueRef sample_pos_array, 625bf215546Sopenharmony_ci LLVMValueRef num_loop, 626bf215546Sopenharmony_ci struct lp_build_interp_soa_context *interp, 627bf215546Sopenharmony_ci const struct lp_build_sampler_soa *sampler, 628bf215546Sopenharmony_ci const struct lp_build_image_soa *image, 629bf215546Sopenharmony_ci LLVMValueRef mask_store, 630bf215546Sopenharmony_ci LLVMValueRef (*out_color)[4], 631bf215546Sopenharmony_ci LLVMValueRef depth_base_ptr, 632bf215546Sopenharmony_ci LLVMValueRef depth_stride, 633bf215546Sopenharmony_ci LLVMValueRef depth_sample_stride, 634bf215546Sopenharmony_ci LLVMValueRef color_ptr_ptr, 635bf215546Sopenharmony_ci LLVMValueRef color_stride_ptr, 636bf215546Sopenharmony_ci LLVMValueRef color_sample_stride_ptr, 637bf215546Sopenharmony_ci LLVMValueRef facing, 638bf215546Sopenharmony_ci LLVMValueRef thread_data_ptr) 639bf215546Sopenharmony_ci{ 640bf215546Sopenharmony_ci const struct tgsi_token *tokens = shader->base.tokens; 641bf215546Sopenharmony_ci struct lp_type int_type = lp_int_type(type); 642bf215546Sopenharmony_ci LLVMValueRef mask_ptr = NULL, mask_val = NULL; 643bf215546Sopenharmony_ci LLVMValueRef z; 644bf215546Sopenharmony_ci LLVMValueRef z_value, s_value; 645bf215546Sopenharmony_ci LLVMValueRef z_fb, s_fb; 646bf215546Sopenharmony_ci LLVMValueRef zs_samples = lp_build_const_int32(gallivm, key->zsbuf_nr_samples); 647bf215546Sopenharmony_ci LLVMValueRef z_out = NULL, s_out = NULL; 648bf215546Sopenharmony_ci struct lp_build_for_loop_state loop_state, sample_loop_state = {0}; 649bf215546Sopenharmony_ci struct lp_build_mask_context mask; 650bf215546Sopenharmony_ci /* 651bf215546Sopenharmony_ci * TODO: figure out if simple_shader optimization is really worthwile to 652bf215546Sopenharmony_ci * keep. Disabled because it may hide some real bugs in the (depth/stencil) 653bf215546Sopenharmony_ci * code since tests tend to take another codepath than real shaders. 654bf215546Sopenharmony_ci */ 655bf215546Sopenharmony_ci boolean simple_shader = (shader->info.base.file_count[TGSI_FILE_SAMPLER] == 0 && 656bf215546Sopenharmony_ci shader->info.base.num_inputs < 3 && 657bf215546Sopenharmony_ci shader->info.base.num_instructions < 8) && 0; 658bf215546Sopenharmony_ci const boolean dual_source_blend = key->blend.rt[0].blend_enable && 659bf215546Sopenharmony_ci util_blend_state_is_dual(&key->blend, 0); 660bf215546Sopenharmony_ci const bool post_depth_coverage = shader->info.base.properties[TGSI_PROPERTY_FS_POST_DEPTH_COVERAGE]; 661bf215546Sopenharmony_ci 662bf215546Sopenharmony_ci struct lp_bld_tgsi_system_values system_values; 663bf215546Sopenharmony_ci 664bf215546Sopenharmony_ci memset(&system_values, 0, sizeof(system_values)); 665bf215546Sopenharmony_ci 666bf215546Sopenharmony_ci /* truncate then sign extend. */ 667bf215546Sopenharmony_ci system_values.front_facing = 668bf215546Sopenharmony_ci LLVMBuildTrunc(gallivm->builder, facing, 669bf215546Sopenharmony_ci LLVMInt1TypeInContext(gallivm->context), ""); 670bf215546Sopenharmony_ci system_values.front_facing = 671bf215546Sopenharmony_ci LLVMBuildSExt(gallivm->builder, system_values.front_facing, 672bf215546Sopenharmony_ci LLVMInt32TypeInContext(gallivm->context), ""); 673bf215546Sopenharmony_ci system_values.view_index = 674bf215546Sopenharmony_ci lp_jit_thread_data_raster_state_view_index(gallivm, thread_data_ptr); 675bf215546Sopenharmony_ci 676bf215546Sopenharmony_ci unsigned depth_mode; 677bf215546Sopenharmony_ci const struct util_format_description *zs_format_desc = NULL; 678bf215546Sopenharmony_ci if (key->depth.enabled || 679bf215546Sopenharmony_ci key->stencil[0].enabled) { 680bf215546Sopenharmony_ci zs_format_desc = util_format_description(key->zsbuf_format); 681bf215546Sopenharmony_ci 682bf215546Sopenharmony_ci if (shader->info.base.properties[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL]) 683bf215546Sopenharmony_ci depth_mode = EARLY_DEPTH_TEST | EARLY_DEPTH_WRITE; 684bf215546Sopenharmony_ci else if (!shader->info.base.writes_z && !shader->info.base.writes_stencil && 685bf215546Sopenharmony_ci !shader->info.base.uses_fbfetch && !shader->info.base.writes_memory) { 686bf215546Sopenharmony_ci if (key->alpha.enabled || 687bf215546Sopenharmony_ci key->blend.alpha_to_coverage || 688bf215546Sopenharmony_ci shader->info.base.uses_kill || 689bf215546Sopenharmony_ci shader->info.base.writes_samplemask) { 690bf215546Sopenharmony_ci /* With alpha test and kill, can do the depth test early 691bf215546Sopenharmony_ci * and hopefully eliminate some quads. But need to do a 692bf215546Sopenharmony_ci * special deferred depth write once the final mask value 693bf215546Sopenharmony_ci * is known. This only works though if there's either no 694bf215546Sopenharmony_ci * stencil test or the stencil value isn't written. 695bf215546Sopenharmony_ci */ 696bf215546Sopenharmony_ci if (key->stencil[0].enabled && (key->stencil[0].writemask || 697bf215546Sopenharmony_ci (key->stencil[1].enabled && 698bf215546Sopenharmony_ci key->stencil[1].writemask))) 699bf215546Sopenharmony_ci depth_mode = LATE_DEPTH_TEST | LATE_DEPTH_WRITE; 700bf215546Sopenharmony_ci else 701bf215546Sopenharmony_ci depth_mode = EARLY_DEPTH_TEST | LATE_DEPTH_WRITE | EARLY_DEPTH_TEST_INFERRED; 702bf215546Sopenharmony_ci } 703bf215546Sopenharmony_ci else 704bf215546Sopenharmony_ci depth_mode = EARLY_DEPTH_TEST | EARLY_DEPTH_WRITE | EARLY_DEPTH_TEST_INFERRED; 705bf215546Sopenharmony_ci } 706bf215546Sopenharmony_ci else { 707bf215546Sopenharmony_ci depth_mode = LATE_DEPTH_TEST | LATE_DEPTH_WRITE; 708bf215546Sopenharmony_ci } 709bf215546Sopenharmony_ci 710bf215546Sopenharmony_ci if (!(key->depth.enabled && key->depth.writemask) && 711bf215546Sopenharmony_ci !(key->stencil[0].enabled && (key->stencil[0].writemask || 712bf215546Sopenharmony_ci (key->stencil[1].enabled && 713bf215546Sopenharmony_ci key->stencil[1].writemask)))) 714bf215546Sopenharmony_ci depth_mode &= ~(LATE_DEPTH_WRITE | EARLY_DEPTH_WRITE); 715bf215546Sopenharmony_ci } 716bf215546Sopenharmony_ci else { 717bf215546Sopenharmony_ci depth_mode = 0; 718bf215546Sopenharmony_ci } 719bf215546Sopenharmony_ci 720bf215546Sopenharmony_ci LLVMTypeRef vec_type = lp_build_vec_type(gallivm, type); 721bf215546Sopenharmony_ci LLVMTypeRef int_vec_type = lp_build_vec_type(gallivm, int_type); 722bf215546Sopenharmony_ci 723bf215546Sopenharmony_ci LLVMValueRef stencil_refs[2]; 724bf215546Sopenharmony_ci stencil_refs[0] = lp_jit_context_stencil_ref_front_value(gallivm, context_ptr); 725bf215546Sopenharmony_ci stencil_refs[1] = lp_jit_context_stencil_ref_back_value(gallivm, context_ptr); 726bf215546Sopenharmony_ci /* convert scalar stencil refs into vectors */ 727bf215546Sopenharmony_ci stencil_refs[0] = lp_build_broadcast(gallivm, int_vec_type, stencil_refs[0]); 728bf215546Sopenharmony_ci stencil_refs[1] = lp_build_broadcast(gallivm, int_vec_type, stencil_refs[1]); 729bf215546Sopenharmony_ci 730bf215546Sopenharmony_ci LLVMValueRef consts_ptr = lp_jit_context_constants(gallivm, context_ptr); 731bf215546Sopenharmony_ci LLVMValueRef num_consts_ptr = lp_jit_context_num_constants(gallivm, 732bf215546Sopenharmony_ci context_ptr); 733bf215546Sopenharmony_ci 734bf215546Sopenharmony_ci LLVMValueRef ssbo_ptr = lp_jit_context_ssbos(gallivm, context_ptr); 735bf215546Sopenharmony_ci LLVMValueRef num_ssbo_ptr = lp_jit_context_num_ssbos(gallivm, context_ptr); 736bf215546Sopenharmony_ci 737bf215546Sopenharmony_ci LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS]; 738bf215546Sopenharmony_ci memset(outputs, 0, sizeof outputs); 739bf215546Sopenharmony_ci 740bf215546Sopenharmony_ci /* Allocate color storage for each fragment sample */ 741bf215546Sopenharmony_ci LLVMValueRef color_store_size = num_loop; 742bf215546Sopenharmony_ci if (key->min_samples > 1) 743bf215546Sopenharmony_ci color_store_size = LLVMBuildMul(builder, num_loop, lp_build_const_int32(gallivm, key->min_samples), ""); 744bf215546Sopenharmony_ci 745bf215546Sopenharmony_ci for (unsigned cbuf = 0; cbuf < key->nr_cbufs; cbuf++) { 746bf215546Sopenharmony_ci for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 747bf215546Sopenharmony_ci out_color[cbuf][chan] = lp_build_array_alloca(gallivm, 748bf215546Sopenharmony_ci lp_build_vec_type(gallivm, 749bf215546Sopenharmony_ci type), 750bf215546Sopenharmony_ci color_store_size, "color"); 751bf215546Sopenharmony_ci } 752bf215546Sopenharmony_ci } 753bf215546Sopenharmony_ci if (dual_source_blend) { 754bf215546Sopenharmony_ci assert(key->nr_cbufs <= 1); 755bf215546Sopenharmony_ci for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 756bf215546Sopenharmony_ci out_color[1][chan] = lp_build_array_alloca(gallivm, 757bf215546Sopenharmony_ci lp_build_vec_type(gallivm, 758bf215546Sopenharmony_ci type), 759bf215546Sopenharmony_ci color_store_size, "color1"); 760bf215546Sopenharmony_ci } 761bf215546Sopenharmony_ci } 762bf215546Sopenharmony_ci if (shader->info.base.writes_z) { 763bf215546Sopenharmony_ci z_out = lp_build_array_alloca(gallivm, 764bf215546Sopenharmony_ci lp_build_vec_type(gallivm, type), 765bf215546Sopenharmony_ci color_store_size, "depth"); 766bf215546Sopenharmony_ci } 767bf215546Sopenharmony_ci 768bf215546Sopenharmony_ci if (shader->info.base.writes_stencil) { 769bf215546Sopenharmony_ci s_out = lp_build_array_alloca(gallivm, 770bf215546Sopenharmony_ci lp_build_vec_type(gallivm, type), 771bf215546Sopenharmony_ci color_store_size, "depth"); 772bf215546Sopenharmony_ci } 773bf215546Sopenharmony_ci 774bf215546Sopenharmony_ci lp_build_for_loop_begin(&loop_state, gallivm, 775bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 0), 776bf215546Sopenharmony_ci LLVMIntULT, 777bf215546Sopenharmony_ci num_loop, 778bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 1)); 779bf215546Sopenharmony_ci 780bf215546Sopenharmony_ci LLVMValueRef sample_mask_in; 781bf215546Sopenharmony_ci if (key->multisample) { 782bf215546Sopenharmony_ci sample_mask_in = lp_build_const_int_vec(gallivm, type, 0); 783bf215546Sopenharmony_ci /* create shader execution mask by combining all sample masks. */ 784bf215546Sopenharmony_ci for (unsigned s = 0; s < key->coverage_samples; s++) { 785bf215546Sopenharmony_ci LLVMValueRef s_mask_idx = LLVMBuildMul(builder, num_loop, lp_build_const_int32(gallivm, s), ""); 786bf215546Sopenharmony_ci s_mask_idx = LLVMBuildAdd(builder, s_mask_idx, loop_state.counter, ""); 787bf215546Sopenharmony_ci LLVMValueRef s_mask = lp_build_pointer_get(builder, mask_store, s_mask_idx); 788bf215546Sopenharmony_ci if (s == 0) 789bf215546Sopenharmony_ci mask_val = s_mask; 790bf215546Sopenharmony_ci else 791bf215546Sopenharmony_ci mask_val = LLVMBuildOr(builder, s_mask, mask_val, ""); 792bf215546Sopenharmony_ci 793bf215546Sopenharmony_ci LLVMValueRef mask_in = LLVMBuildAnd(builder, s_mask, lp_build_const_int_vec(gallivm, type, (1ll << s)), ""); 794bf215546Sopenharmony_ci sample_mask_in = LLVMBuildOr(builder, sample_mask_in, mask_in, ""); 795bf215546Sopenharmony_ci } 796bf215546Sopenharmony_ci } else { 797bf215546Sopenharmony_ci sample_mask_in = lp_build_const_int_vec(gallivm, type, 1); 798bf215546Sopenharmony_ci mask_ptr = LLVMBuildGEP(builder, mask_store, 799bf215546Sopenharmony_ci &loop_state.counter, 1, "mask_ptr"); 800bf215546Sopenharmony_ci mask_val = LLVMBuildLoad(builder, mask_ptr, ""); 801bf215546Sopenharmony_ci 802bf215546Sopenharmony_ci LLVMValueRef mask_in = LLVMBuildAnd(builder, mask_val, lp_build_const_int_vec(gallivm, type, 1), ""); 803bf215546Sopenharmony_ci sample_mask_in = LLVMBuildOr(builder, sample_mask_in, mask_in, ""); 804bf215546Sopenharmony_ci } 805bf215546Sopenharmony_ci 806bf215546Sopenharmony_ci /* 'mask' will control execution based on quad's pixel alive/killed state */ 807bf215546Sopenharmony_ci lp_build_mask_begin(&mask, gallivm, type, mask_val); 808bf215546Sopenharmony_ci 809bf215546Sopenharmony_ci if (!(depth_mode & EARLY_DEPTH_TEST) && !simple_shader) 810bf215546Sopenharmony_ci lp_build_mask_check(&mask); 811bf215546Sopenharmony_ci 812bf215546Sopenharmony_ci /* Create storage for recombining sample masks after early Z pass. */ 813bf215546Sopenharmony_ci LLVMValueRef s_mask_or = lp_build_alloca(gallivm, lp_build_int_vec_type(gallivm, type), "cov_mask_early_depth"); 814bf215546Sopenharmony_ci LLVMBuildStore(builder, LLVMConstNull(lp_build_int_vec_type(gallivm, type)), s_mask_or); 815bf215546Sopenharmony_ci 816bf215546Sopenharmony_ci /* Create storage for post depth sample mask */ 817bf215546Sopenharmony_ci LLVMValueRef post_depth_sample_mask_in = NULL; 818bf215546Sopenharmony_ci if (post_depth_coverage) 819bf215546Sopenharmony_ci post_depth_sample_mask_in = lp_build_alloca(gallivm, int_vec_type, "post_depth_sample_mask_in"); 820bf215546Sopenharmony_ci 821bf215546Sopenharmony_ci LLVMValueRef s_mask = NULL, s_mask_ptr = NULL; 822bf215546Sopenharmony_ci LLVMValueRef z_sample_value_store = NULL, s_sample_value_store = NULL; 823bf215546Sopenharmony_ci LLVMValueRef z_fb_store = NULL, s_fb_store = NULL; 824bf215546Sopenharmony_ci LLVMTypeRef z_type = NULL, z_fb_type = NULL; 825bf215546Sopenharmony_ci 826bf215546Sopenharmony_ci /* Run early depth once per sample */ 827bf215546Sopenharmony_ci if (key->multisample) { 828bf215546Sopenharmony_ci 829bf215546Sopenharmony_ci if (zs_format_desc) { 830bf215546Sopenharmony_ci struct lp_type zs_type = lp_depth_type(zs_format_desc, type.length); 831bf215546Sopenharmony_ci struct lp_type z_type = zs_type; 832bf215546Sopenharmony_ci struct lp_type s_type = zs_type; 833bf215546Sopenharmony_ci if (zs_format_desc->block.bits < type.width) 834bf215546Sopenharmony_ci z_type.width = type.width; 835bf215546Sopenharmony_ci if (zs_format_desc->block.bits == 8) 836bf215546Sopenharmony_ci s_type.width = type.width; 837bf215546Sopenharmony_ci 838bf215546Sopenharmony_ci else if (zs_format_desc->block.bits > 32) { 839bf215546Sopenharmony_ci z_type.width = z_type.width / 2; 840bf215546Sopenharmony_ci s_type.width = s_type.width / 2; 841bf215546Sopenharmony_ci s_type.floating = 0; 842bf215546Sopenharmony_ci } 843bf215546Sopenharmony_ci z_sample_value_store = lp_build_array_alloca(gallivm, lp_build_int_vec_type(gallivm, type), 844bf215546Sopenharmony_ci zs_samples, "z_sample_store"); 845bf215546Sopenharmony_ci s_sample_value_store = lp_build_array_alloca(gallivm, lp_build_int_vec_type(gallivm, type), 846bf215546Sopenharmony_ci zs_samples, "s_sample_store"); 847bf215546Sopenharmony_ci z_fb_store = lp_build_array_alloca(gallivm, lp_build_vec_type(gallivm, z_type), 848bf215546Sopenharmony_ci zs_samples, "z_fb_store"); 849bf215546Sopenharmony_ci s_fb_store = lp_build_array_alloca(gallivm, lp_build_vec_type(gallivm, s_type), 850bf215546Sopenharmony_ci zs_samples, "s_fb_store"); 851bf215546Sopenharmony_ci } 852bf215546Sopenharmony_ci lp_build_for_loop_begin(&sample_loop_state, gallivm, 853bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 0), 854bf215546Sopenharmony_ci LLVMIntULT, lp_build_const_int32(gallivm, key->coverage_samples), 855bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 1)); 856bf215546Sopenharmony_ci 857bf215546Sopenharmony_ci LLVMValueRef s_mask_idx = LLVMBuildMul(builder, sample_loop_state.counter, num_loop, ""); 858bf215546Sopenharmony_ci s_mask_idx = LLVMBuildAdd(builder, s_mask_idx, loop_state.counter, ""); 859bf215546Sopenharmony_ci s_mask_ptr = LLVMBuildGEP(builder, mask_store, &s_mask_idx, 1, ""); 860bf215546Sopenharmony_ci 861bf215546Sopenharmony_ci s_mask = LLVMBuildLoad(builder, s_mask_ptr, ""); 862bf215546Sopenharmony_ci s_mask = LLVMBuildAnd(builder, s_mask, mask_val, ""); 863bf215546Sopenharmony_ci } 864bf215546Sopenharmony_ci 865bf215546Sopenharmony_ci 866bf215546Sopenharmony_ci /* for multisample Z needs to be interpolated at sample points for testing. */ 867bf215546Sopenharmony_ci lp_build_interp_soa_update_pos_dyn(interp, gallivm, loop_state.counter, 868bf215546Sopenharmony_ci key->multisample 869bf215546Sopenharmony_ci ? sample_loop_state.counter : NULL); 870bf215546Sopenharmony_ci z = interp->pos[2]; 871bf215546Sopenharmony_ci 872bf215546Sopenharmony_ci LLVMValueRef depth_ptr = depth_base_ptr; 873bf215546Sopenharmony_ci if (key->multisample) { 874bf215546Sopenharmony_ci LLVMValueRef sample_offset = 875bf215546Sopenharmony_ci LLVMBuildMul(builder, sample_loop_state.counter, 876bf215546Sopenharmony_ci depth_sample_stride, ""); 877bf215546Sopenharmony_ci depth_ptr = LLVMBuildGEP(builder, depth_ptr, &sample_offset, 1, ""); 878bf215546Sopenharmony_ci } 879bf215546Sopenharmony_ci 880bf215546Sopenharmony_ci if (depth_mode & EARLY_DEPTH_TEST) { 881bf215546Sopenharmony_ci z = lp_build_depth_clamp(gallivm, builder, key->depth_clamp, 882bf215546Sopenharmony_ci key->restrict_depth_values, type, context_ptr, 883bf215546Sopenharmony_ci thread_data_ptr, z); 884bf215546Sopenharmony_ci 885bf215546Sopenharmony_ci lp_build_depth_stencil_load_swizzled(gallivm, type, 886bf215546Sopenharmony_ci zs_format_desc, key->resource_1d, 887bf215546Sopenharmony_ci depth_ptr, depth_stride, 888bf215546Sopenharmony_ci &z_fb, &s_fb, loop_state.counter); 889bf215546Sopenharmony_ci lp_build_depth_stencil_test(gallivm, 890bf215546Sopenharmony_ci &key->depth, 891bf215546Sopenharmony_ci key->stencil, 892bf215546Sopenharmony_ci type, 893bf215546Sopenharmony_ci zs_format_desc, 894bf215546Sopenharmony_ci key->multisample ? NULL : &mask, 895bf215546Sopenharmony_ci &s_mask, 896bf215546Sopenharmony_ci stencil_refs, 897bf215546Sopenharmony_ci z, z_fb, s_fb, 898bf215546Sopenharmony_ci facing, 899bf215546Sopenharmony_ci &z_value, &s_value, 900bf215546Sopenharmony_ci !simple_shader && !key->multisample, 901bf215546Sopenharmony_ci key->restrict_depth_values); 902bf215546Sopenharmony_ci 903bf215546Sopenharmony_ci if (depth_mode & EARLY_DEPTH_WRITE) { 904bf215546Sopenharmony_ci lp_build_depth_stencil_write_swizzled(gallivm, type, 905bf215546Sopenharmony_ci zs_format_desc, key->resource_1d, 906bf215546Sopenharmony_ci NULL, NULL, NULL, loop_state.counter, 907bf215546Sopenharmony_ci depth_ptr, depth_stride, 908bf215546Sopenharmony_ci z_value, s_value); 909bf215546Sopenharmony_ci } 910bf215546Sopenharmony_ci /* 911bf215546Sopenharmony_ci * Note mask check if stencil is enabled must be after ds write not after 912bf215546Sopenharmony_ci * stencil test otherwise new stencil values may not get written if all 913bf215546Sopenharmony_ci * fragments got killed by depth/stencil test. 914bf215546Sopenharmony_ci */ 915bf215546Sopenharmony_ci if (!simple_shader && key->stencil[0].enabled && !key->multisample) 916bf215546Sopenharmony_ci lp_build_mask_check(&mask); 917bf215546Sopenharmony_ci 918bf215546Sopenharmony_ci if (key->multisample) { 919bf215546Sopenharmony_ci z_fb_type = LLVMTypeOf(z_fb); 920bf215546Sopenharmony_ci z_type = LLVMTypeOf(z_value); 921bf215546Sopenharmony_ci lp_build_pointer_set(builder, z_sample_value_store, sample_loop_state.counter, LLVMBuildBitCast(builder, z_value, lp_build_int_vec_type(gallivm, type), "")); 922bf215546Sopenharmony_ci lp_build_pointer_set(builder, s_sample_value_store, sample_loop_state.counter, LLVMBuildBitCast(builder, s_value, lp_build_int_vec_type(gallivm, type), "")); 923bf215546Sopenharmony_ci lp_build_pointer_set(builder, z_fb_store, sample_loop_state.counter, z_fb); 924bf215546Sopenharmony_ci lp_build_pointer_set(builder, s_fb_store, sample_loop_state.counter, s_fb); 925bf215546Sopenharmony_ci } 926bf215546Sopenharmony_ci } 927bf215546Sopenharmony_ci 928bf215546Sopenharmony_ci if (key->multisample) { 929bf215546Sopenharmony_ci /* 930bf215546Sopenharmony_ci * Store the post-early Z coverage mask. 931bf215546Sopenharmony_ci * Recombine the resulting coverage masks post early Z into the fragment 932bf215546Sopenharmony_ci * shader execution mask. 933bf215546Sopenharmony_ci */ 934bf215546Sopenharmony_ci LLVMValueRef tmp_s_mask_or = LLVMBuildLoad(builder, s_mask_or, ""); 935bf215546Sopenharmony_ci tmp_s_mask_or = LLVMBuildOr(builder, tmp_s_mask_or, s_mask, ""); 936bf215546Sopenharmony_ci LLVMBuildStore(builder, tmp_s_mask_or, s_mask_or); 937bf215546Sopenharmony_ci 938bf215546Sopenharmony_ci if (post_depth_coverage) { 939bf215546Sopenharmony_ci LLVMValueRef mask_bit_idx = LLVMBuildShl(builder, lp_build_const_int32(gallivm, 1), sample_loop_state.counter, ""); 940bf215546Sopenharmony_ci LLVMValueRef post_depth_mask_in = LLVMBuildLoad(builder, post_depth_sample_mask_in, ""); 941bf215546Sopenharmony_ci mask_bit_idx = LLVMBuildAnd(builder, s_mask, lp_build_broadcast(gallivm, int_vec_type, mask_bit_idx), ""); 942bf215546Sopenharmony_ci post_depth_mask_in = LLVMBuildOr(builder, post_depth_mask_in, mask_bit_idx, ""); 943bf215546Sopenharmony_ci LLVMBuildStore(builder, post_depth_mask_in, post_depth_sample_mask_in); 944bf215546Sopenharmony_ci } 945bf215546Sopenharmony_ci 946bf215546Sopenharmony_ci LLVMBuildStore(builder, s_mask, s_mask_ptr); 947bf215546Sopenharmony_ci 948bf215546Sopenharmony_ci lp_build_for_loop_end(&sample_loop_state); 949bf215546Sopenharmony_ci 950bf215546Sopenharmony_ci /* recombined all the coverage masks in the shader exec mask. */ 951bf215546Sopenharmony_ci tmp_s_mask_or = LLVMBuildLoad(builder, s_mask_or, ""); 952bf215546Sopenharmony_ci lp_build_mask_update(&mask, tmp_s_mask_or); 953bf215546Sopenharmony_ci 954bf215546Sopenharmony_ci if (key->min_samples == 1) { 955bf215546Sopenharmony_ci /* for multisample Z needs to be re interpolated at pixel center */ 956bf215546Sopenharmony_ci lp_build_interp_soa_update_pos_dyn(interp, gallivm, loop_state.counter, NULL); 957bf215546Sopenharmony_ci z = interp->pos[2]; 958bf215546Sopenharmony_ci lp_build_mask_update(&mask, tmp_s_mask_or); 959bf215546Sopenharmony_ci } 960bf215546Sopenharmony_ci } else { 961bf215546Sopenharmony_ci if (post_depth_coverage) { 962bf215546Sopenharmony_ci LLVMValueRef post_depth_mask_in = LLVMBuildAnd(builder, lp_build_mask_value(&mask), lp_build_const_int_vec(gallivm, type, 1), ""); 963bf215546Sopenharmony_ci LLVMBuildStore(builder, post_depth_mask_in, post_depth_sample_mask_in); 964bf215546Sopenharmony_ci } 965bf215546Sopenharmony_ci } 966bf215546Sopenharmony_ci 967bf215546Sopenharmony_ci LLVMValueRef out_sample_mask_storage = NULL; 968bf215546Sopenharmony_ci if (shader->info.base.writes_samplemask) { 969bf215546Sopenharmony_ci out_sample_mask_storage = lp_build_alloca(gallivm, int_vec_type, "write_mask"); 970bf215546Sopenharmony_ci if (key->min_samples > 1) 971bf215546Sopenharmony_ci LLVMBuildStore(builder, LLVMConstNull(int_vec_type), out_sample_mask_storage); 972bf215546Sopenharmony_ci } 973bf215546Sopenharmony_ci 974bf215546Sopenharmony_ci if (post_depth_coverage) { 975bf215546Sopenharmony_ci system_values.sample_mask_in = LLVMBuildLoad(builder, post_depth_sample_mask_in, ""); 976bf215546Sopenharmony_ci } 977bf215546Sopenharmony_ci else 978bf215546Sopenharmony_ci system_values.sample_mask_in = sample_mask_in; 979bf215546Sopenharmony_ci if (key->multisample && key->min_samples > 1) { 980bf215546Sopenharmony_ci lp_build_for_loop_begin(&sample_loop_state, gallivm, 981bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 0), 982bf215546Sopenharmony_ci LLVMIntULT, 983bf215546Sopenharmony_ci lp_build_const_int32(gallivm, key->min_samples), 984bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 1)); 985bf215546Sopenharmony_ci 986bf215546Sopenharmony_ci LLVMValueRef s_mask_idx = LLVMBuildMul(builder, sample_loop_state.counter, num_loop, ""); 987bf215546Sopenharmony_ci s_mask_idx = LLVMBuildAdd(builder, s_mask_idx, loop_state.counter, ""); 988bf215546Sopenharmony_ci s_mask_ptr = LLVMBuildGEP(builder, mask_store, &s_mask_idx, 1, ""); 989bf215546Sopenharmony_ci s_mask = LLVMBuildLoad(builder, s_mask_ptr, ""); 990bf215546Sopenharmony_ci lp_build_mask_force(&mask, s_mask); 991bf215546Sopenharmony_ci lp_build_interp_soa_update_pos_dyn(interp, gallivm, loop_state.counter, sample_loop_state.counter); 992bf215546Sopenharmony_ci system_values.sample_id = sample_loop_state.counter; 993bf215546Sopenharmony_ci system_values.sample_mask_in = LLVMBuildAnd(builder, system_values.sample_mask_in, 994bf215546Sopenharmony_ci lp_build_broadcast(gallivm, int_vec_type, 995bf215546Sopenharmony_ci LLVMBuildShl(builder, lp_build_const_int32(gallivm, 1), sample_loop_state.counter, "")), ""); 996bf215546Sopenharmony_ci } else { 997bf215546Sopenharmony_ci system_values.sample_id = lp_build_const_int32(gallivm, 0); 998bf215546Sopenharmony_ci 999bf215546Sopenharmony_ci } 1000bf215546Sopenharmony_ci system_values.sample_pos = sample_pos_array; 1001bf215546Sopenharmony_ci 1002bf215546Sopenharmony_ci lp_build_interp_soa_update_inputs_dyn(interp, gallivm, loop_state.counter, mask_store, sample_loop_state.counter); 1003bf215546Sopenharmony_ci 1004bf215546Sopenharmony_ci struct lp_build_fs_llvm_iface fs_iface = { 1005bf215546Sopenharmony_ci .base.interp_fn = fs_interp, 1006bf215546Sopenharmony_ci .base.fb_fetch = fs_fb_fetch, 1007bf215546Sopenharmony_ci .interp = interp, 1008bf215546Sopenharmony_ci .loop_state = &loop_state, 1009bf215546Sopenharmony_ci .sample_id = system_values.sample_id, 1010bf215546Sopenharmony_ci .mask_store = mask_store, 1011bf215546Sopenharmony_ci .color_ptr_ptr = color_ptr_ptr, 1012bf215546Sopenharmony_ci .color_stride_ptr = color_stride_ptr, 1013bf215546Sopenharmony_ci .color_sample_stride_ptr = color_sample_stride_ptr, 1014bf215546Sopenharmony_ci .zs_base_ptr = depth_base_ptr, 1015bf215546Sopenharmony_ci .zs_stride = depth_stride, 1016bf215546Sopenharmony_ci .zs_sample_stride = depth_sample_stride, 1017bf215546Sopenharmony_ci .key = key, 1018bf215546Sopenharmony_ci }; 1019bf215546Sopenharmony_ci 1020bf215546Sopenharmony_ci struct lp_build_tgsi_params params; 1021bf215546Sopenharmony_ci memset(¶ms, 0, sizeof(params)); 1022bf215546Sopenharmony_ci 1023bf215546Sopenharmony_ci params.type = type; 1024bf215546Sopenharmony_ci params.mask = &mask; 1025bf215546Sopenharmony_ci params.fs_iface = &fs_iface.base; 1026bf215546Sopenharmony_ci params.consts_ptr = consts_ptr; 1027bf215546Sopenharmony_ci params.const_sizes_ptr = num_consts_ptr; 1028bf215546Sopenharmony_ci params.system_values = &system_values; 1029bf215546Sopenharmony_ci params.inputs = interp->inputs; 1030bf215546Sopenharmony_ci params.context_ptr = context_ptr; 1031bf215546Sopenharmony_ci params.thread_data_ptr = thread_data_ptr; 1032bf215546Sopenharmony_ci params.sampler = sampler; 1033bf215546Sopenharmony_ci params.info = &shader->info.base; 1034bf215546Sopenharmony_ci params.ssbo_ptr = ssbo_ptr; 1035bf215546Sopenharmony_ci params.ssbo_sizes_ptr = num_ssbo_ptr; 1036bf215546Sopenharmony_ci params.image = image; 1037bf215546Sopenharmony_ci params.aniso_filter_table = lp_jit_context_aniso_filter_table(gallivm, context_ptr); 1038bf215546Sopenharmony_ci 1039bf215546Sopenharmony_ci /* Build the actual shader */ 1040bf215546Sopenharmony_ci if (shader->base.type == PIPE_SHADER_IR_TGSI) 1041bf215546Sopenharmony_ci lp_build_tgsi_soa(gallivm, tokens, ¶ms, 1042bf215546Sopenharmony_ci outputs); 1043bf215546Sopenharmony_ci else 1044bf215546Sopenharmony_ci lp_build_nir_soa(gallivm, shader->base.ir.nir, ¶ms, 1045bf215546Sopenharmony_ci outputs); 1046bf215546Sopenharmony_ci 1047bf215546Sopenharmony_ci /* Alpha test */ 1048bf215546Sopenharmony_ci if (key->alpha.enabled) { 1049bf215546Sopenharmony_ci int color0 = find_output_by_semantic(&shader->info.base, 1050bf215546Sopenharmony_ci TGSI_SEMANTIC_COLOR, 1051bf215546Sopenharmony_ci 0); 1052bf215546Sopenharmony_ci 1053bf215546Sopenharmony_ci if (color0 != -1 && outputs[color0][3]) { 1054bf215546Sopenharmony_ci const struct util_format_description *cbuf_format_desc; 1055bf215546Sopenharmony_ci LLVMValueRef alpha = LLVMBuildLoad(builder, outputs[color0][3], "alpha"); 1056bf215546Sopenharmony_ci LLVMValueRef alpha_ref_value; 1057bf215546Sopenharmony_ci 1058bf215546Sopenharmony_ci alpha_ref_value = lp_jit_context_alpha_ref_value(gallivm, context_ptr); 1059bf215546Sopenharmony_ci alpha_ref_value = lp_build_broadcast(gallivm, vec_type, alpha_ref_value); 1060bf215546Sopenharmony_ci 1061bf215546Sopenharmony_ci cbuf_format_desc = util_format_description(key->cbuf_format[0]); 1062bf215546Sopenharmony_ci 1063bf215546Sopenharmony_ci lp_build_alpha_test(gallivm, key->alpha.func, type, cbuf_format_desc, 1064bf215546Sopenharmony_ci &mask, alpha, alpha_ref_value, 1065bf215546Sopenharmony_ci ((depth_mode & LATE_DEPTH_TEST) != 0) && !key->multisample); 1066bf215546Sopenharmony_ci } 1067bf215546Sopenharmony_ci } 1068bf215546Sopenharmony_ci 1069bf215546Sopenharmony_ci /* Emulate Alpha to Coverage with Alpha test */ 1070bf215546Sopenharmony_ci if (key->blend.alpha_to_coverage) { 1071bf215546Sopenharmony_ci int color0 = find_output_by_semantic(&shader->info.base, 1072bf215546Sopenharmony_ci TGSI_SEMANTIC_COLOR, 1073bf215546Sopenharmony_ci 0); 1074bf215546Sopenharmony_ci 1075bf215546Sopenharmony_ci if (color0 != -1 && outputs[color0][3]) { 1076bf215546Sopenharmony_ci LLVMValueRef alpha = LLVMBuildLoad(builder, outputs[color0][3], "alpha"); 1077bf215546Sopenharmony_ci 1078bf215546Sopenharmony_ci if (!key->multisample) { 1079bf215546Sopenharmony_ci lp_build_alpha_to_coverage(gallivm, type, 1080bf215546Sopenharmony_ci &mask, alpha, 1081bf215546Sopenharmony_ci (depth_mode & LATE_DEPTH_TEST) != 0); 1082bf215546Sopenharmony_ci } else { 1083bf215546Sopenharmony_ci lp_build_sample_alpha_to_coverage(gallivm, type, key->coverage_samples, num_loop, 1084bf215546Sopenharmony_ci loop_state.counter, 1085bf215546Sopenharmony_ci mask_store, alpha); 1086bf215546Sopenharmony_ci } 1087bf215546Sopenharmony_ci } 1088bf215546Sopenharmony_ci } 1089bf215546Sopenharmony_ci 1090bf215546Sopenharmony_ci if (key->blend.alpha_to_one) { 1091bf215546Sopenharmony_ci for (unsigned attrib = 0; attrib < shader->info.base.num_outputs; ++attrib) { 1092bf215546Sopenharmony_ci unsigned cbuf = shader->info.base.output_semantic_index[attrib]; 1093bf215546Sopenharmony_ci if ((shader->info.base.output_semantic_name[attrib] == TGSI_SEMANTIC_COLOR) && 1094bf215546Sopenharmony_ci ((cbuf < key->nr_cbufs) || (cbuf == 1 && dual_source_blend))) 1095bf215546Sopenharmony_ci if (outputs[cbuf][3]) { 1096bf215546Sopenharmony_ci LLVMBuildStore(builder, lp_build_const_vec(gallivm, type, 1.0), 1097bf215546Sopenharmony_ci outputs[cbuf][3]); 1098bf215546Sopenharmony_ci } 1099bf215546Sopenharmony_ci } 1100bf215546Sopenharmony_ci } 1101bf215546Sopenharmony_ci 1102bf215546Sopenharmony_ci if (shader->info.base.writes_samplemask) { 1103bf215546Sopenharmony_ci LLVMValueRef output_smask = NULL; 1104bf215546Sopenharmony_ci int smaski = find_output_by_semantic(&shader->info.base, 1105bf215546Sopenharmony_ci TGSI_SEMANTIC_SAMPLEMASK, 1106bf215546Sopenharmony_ci 0); 1107bf215546Sopenharmony_ci struct lp_build_context smask_bld; 1108bf215546Sopenharmony_ci lp_build_context_init(&smask_bld, gallivm, int_type); 1109bf215546Sopenharmony_ci 1110bf215546Sopenharmony_ci assert(smaski >= 0); 1111bf215546Sopenharmony_ci output_smask = LLVMBuildLoad(builder, outputs[smaski][0], "smask"); 1112bf215546Sopenharmony_ci output_smask = LLVMBuildBitCast(builder, output_smask, smask_bld.vec_type, ""); 1113bf215546Sopenharmony_ci if (!key->multisample && key->no_ms_sample_mask_out) { 1114bf215546Sopenharmony_ci output_smask = lp_build_and(&smask_bld, output_smask, smask_bld.one); 1115bf215546Sopenharmony_ci output_smask = lp_build_cmp(&smask_bld, PIPE_FUNC_NOTEQUAL, output_smask, smask_bld.zero); 1116bf215546Sopenharmony_ci lp_build_mask_update(&mask, output_smask); 1117bf215546Sopenharmony_ci } 1118bf215546Sopenharmony_ci 1119bf215546Sopenharmony_ci if (key->min_samples > 1) { 1120bf215546Sopenharmony_ci /* only the bit corresponding to this sample is to be used. */ 1121bf215546Sopenharmony_ci LLVMValueRef tmp_mask = LLVMBuildLoad(builder, out_sample_mask_storage, "tmp_mask"); 1122bf215546Sopenharmony_ci LLVMValueRef out_smask_idx = LLVMBuildShl(builder, lp_build_const_int32(gallivm, 1), sample_loop_state.counter, ""); 1123bf215546Sopenharmony_ci LLVMValueRef smask_bit = LLVMBuildAnd(builder, output_smask, lp_build_broadcast(gallivm, int_vec_type, out_smask_idx), ""); 1124bf215546Sopenharmony_ci output_smask = LLVMBuildOr(builder, tmp_mask, smask_bit, ""); 1125bf215546Sopenharmony_ci } 1126bf215546Sopenharmony_ci 1127bf215546Sopenharmony_ci LLVMBuildStore(builder, output_smask, out_sample_mask_storage); 1128bf215546Sopenharmony_ci } 1129bf215546Sopenharmony_ci 1130bf215546Sopenharmony_ci if (shader->info.base.writes_z) { 1131bf215546Sopenharmony_ci int pos0 = find_output_by_semantic(&shader->info.base, 1132bf215546Sopenharmony_ci TGSI_SEMANTIC_POSITION, 1133bf215546Sopenharmony_ci 0); 1134bf215546Sopenharmony_ci LLVMValueRef out = LLVMBuildLoad(builder, outputs[pos0][2], ""); 1135bf215546Sopenharmony_ci LLVMValueRef idx = loop_state.counter; 1136bf215546Sopenharmony_ci if (key->min_samples > 1) 1137bf215546Sopenharmony_ci idx = LLVMBuildAdd(builder, idx, 1138bf215546Sopenharmony_ci LLVMBuildMul(builder, sample_loop_state.counter, num_loop, ""), ""); 1139bf215546Sopenharmony_ci LLVMValueRef ptr = LLVMBuildGEP(builder, z_out, &idx, 1, ""); 1140bf215546Sopenharmony_ci LLVMBuildStore(builder, out, ptr); 1141bf215546Sopenharmony_ci } 1142bf215546Sopenharmony_ci 1143bf215546Sopenharmony_ci if (shader->info.base.writes_stencil) { 1144bf215546Sopenharmony_ci int sten_out = find_output_by_semantic(&shader->info.base, 1145bf215546Sopenharmony_ci TGSI_SEMANTIC_STENCIL, 1146bf215546Sopenharmony_ci 0); 1147bf215546Sopenharmony_ci LLVMValueRef out = LLVMBuildLoad(builder, outputs[sten_out][1], "output.s"); 1148bf215546Sopenharmony_ci LLVMValueRef idx = loop_state.counter; 1149bf215546Sopenharmony_ci if (key->min_samples > 1) 1150bf215546Sopenharmony_ci idx = LLVMBuildAdd(builder, idx, 1151bf215546Sopenharmony_ci LLVMBuildMul(builder, sample_loop_state.counter, num_loop, ""), ""); 1152bf215546Sopenharmony_ci LLVMValueRef ptr = LLVMBuildGEP(builder, s_out, &idx, 1, ""); 1153bf215546Sopenharmony_ci LLVMBuildStore(builder, out, ptr); 1154bf215546Sopenharmony_ci } 1155bf215546Sopenharmony_ci 1156bf215546Sopenharmony_ci bool has_cbuf0_write = false; 1157bf215546Sopenharmony_ci /* Color write - per fragment sample */ 1158bf215546Sopenharmony_ci for (unsigned attrib = 0; attrib < shader->info.base.num_outputs; ++attrib) { 1159bf215546Sopenharmony_ci unsigned cbuf = shader->info.base.output_semantic_index[attrib]; 1160bf215546Sopenharmony_ci if ((shader->info.base.output_semantic_name[attrib] 1161bf215546Sopenharmony_ci == TGSI_SEMANTIC_COLOR) && 1162bf215546Sopenharmony_ci ((cbuf < key->nr_cbufs) || (cbuf == 1 && dual_source_blend))) { 1163bf215546Sopenharmony_ci if (cbuf == 0 && 1164bf215546Sopenharmony_ci shader->info.base.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS]) { 1165bf215546Sopenharmony_ci /* XXX: there is an edge case with FB fetch where gl_FragColor and 1166bf215546Sopenharmony_ci * gl_LastFragData[0] are used together. This creates both 1167bf215546Sopenharmony_ci * FRAG_RESULT_COLOR and FRAG_RESULT_DATA* output variables. This 1168bf215546Sopenharmony_ci * loop then writes to cbuf 0 twice, owerwriting the correct value 1169bf215546Sopenharmony_ci * from gl_FragColor with some garbage. This case is excercised in 1170bf215546Sopenharmony_ci * one of deqp tests. A similar bug can happen if 1171bf215546Sopenharmony_ci * gl_SecondaryFragColorEXT and gl_LastFragData[1] are mixed in 1172bf215546Sopenharmony_ci * the same fashion... This workaround will break if 1173bf215546Sopenharmony_ci * gl_LastFragData[0] goes in outputs list before 1174bf215546Sopenharmony_ci * gl_FragColor. This doesn't seem to happen though. 1175bf215546Sopenharmony_ci */ 1176bf215546Sopenharmony_ci if (has_cbuf0_write) 1177bf215546Sopenharmony_ci continue; 1178bf215546Sopenharmony_ci has_cbuf0_write = true; 1179bf215546Sopenharmony_ci } 1180bf215546Sopenharmony_ci 1181bf215546Sopenharmony_ci for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 1182bf215546Sopenharmony_ci if (outputs[attrib][chan]) { 1183bf215546Sopenharmony_ci /* XXX: just initialize outputs to point at colors[] and 1184bf215546Sopenharmony_ci * skip this. 1185bf215546Sopenharmony_ci */ 1186bf215546Sopenharmony_ci LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], ""); 1187bf215546Sopenharmony_ci LLVMValueRef color_ptr; 1188bf215546Sopenharmony_ci LLVMValueRef color_idx = loop_state.counter; 1189bf215546Sopenharmony_ci if (key->min_samples > 1) 1190bf215546Sopenharmony_ci color_idx = LLVMBuildAdd(builder, color_idx, 1191bf215546Sopenharmony_ci LLVMBuildMul(builder, sample_loop_state.counter, num_loop, ""), ""); 1192bf215546Sopenharmony_ci color_ptr = LLVMBuildGEP(builder, out_color[cbuf][chan], 1193bf215546Sopenharmony_ci &color_idx, 1, ""); 1194bf215546Sopenharmony_ci lp_build_name(out, "color%u.%c", attrib, "rgba"[chan]); 1195bf215546Sopenharmony_ci LLVMBuildStore(builder, out, color_ptr); 1196bf215546Sopenharmony_ci } 1197bf215546Sopenharmony_ci } 1198bf215546Sopenharmony_ci } 1199bf215546Sopenharmony_ci } 1200bf215546Sopenharmony_ci 1201bf215546Sopenharmony_ci if (key->multisample && key->min_samples > 1) { 1202bf215546Sopenharmony_ci LLVMBuildStore(builder, lp_build_mask_value(&mask), s_mask_ptr); 1203bf215546Sopenharmony_ci lp_build_for_loop_end(&sample_loop_state); 1204bf215546Sopenharmony_ci } 1205bf215546Sopenharmony_ci 1206bf215546Sopenharmony_ci if (key->multisample) { 1207bf215546Sopenharmony_ci /* execute depth test for each sample */ 1208bf215546Sopenharmony_ci lp_build_for_loop_begin(&sample_loop_state, gallivm, 1209bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 0), 1210bf215546Sopenharmony_ci LLVMIntULT, lp_build_const_int32(gallivm, key->coverage_samples), 1211bf215546Sopenharmony_ci lp_build_const_int32(gallivm, 1)); 1212bf215546Sopenharmony_ci 1213bf215546Sopenharmony_ci /* load the per-sample coverage mask */ 1214bf215546Sopenharmony_ci LLVMValueRef s_mask_idx = LLVMBuildMul(builder, sample_loop_state.counter, num_loop, ""); 1215bf215546Sopenharmony_ci s_mask_idx = LLVMBuildAdd(builder, s_mask_idx, loop_state.counter, ""); 1216bf215546Sopenharmony_ci s_mask_ptr = LLVMBuildGEP(builder, mask_store, &s_mask_idx, 1, ""); 1217bf215546Sopenharmony_ci 1218bf215546Sopenharmony_ci /* combine the execution mask post fragment shader with the coverage mask. */ 1219bf215546Sopenharmony_ci s_mask = LLVMBuildLoad(builder, s_mask_ptr, ""); 1220bf215546Sopenharmony_ci if (key->min_samples == 1) 1221bf215546Sopenharmony_ci s_mask = LLVMBuildAnd(builder, s_mask, lp_build_mask_value(&mask), ""); 1222bf215546Sopenharmony_ci 1223bf215546Sopenharmony_ci /* if the shader writes sample mask use that, 1224bf215546Sopenharmony_ci * but only if this isn't genuine early-depth to avoid breaking occlusion query */ 1225bf215546Sopenharmony_ci if (shader->info.base.writes_samplemask && 1226bf215546Sopenharmony_ci (!(depth_mode & EARLY_DEPTH_TEST) || (depth_mode & (EARLY_DEPTH_TEST_INFERRED)))) { 1227bf215546Sopenharmony_ci LLVMValueRef out_smask_idx = LLVMBuildShl(builder, lp_build_const_int32(gallivm, 1), sample_loop_state.counter, ""); 1228bf215546Sopenharmony_ci out_smask_idx = lp_build_broadcast(gallivm, int_vec_type, out_smask_idx); 1229bf215546Sopenharmony_ci LLVMValueRef output_smask = LLVMBuildLoad(builder, out_sample_mask_storage, ""); 1230bf215546Sopenharmony_ci LLVMValueRef smask_bit = LLVMBuildAnd(builder, output_smask, out_smask_idx, ""); 1231bf215546Sopenharmony_ci LLVMValueRef cmp = LLVMBuildICmp(builder, LLVMIntNE, smask_bit, lp_build_const_int_vec(gallivm, int_type, 0), ""); 1232bf215546Sopenharmony_ci smask_bit = LLVMBuildSExt(builder, cmp, int_vec_type, ""); 1233bf215546Sopenharmony_ci 1234bf215546Sopenharmony_ci s_mask = LLVMBuildAnd(builder, s_mask, smask_bit, ""); 1235bf215546Sopenharmony_ci } 1236bf215546Sopenharmony_ci } 1237bf215546Sopenharmony_ci 1238bf215546Sopenharmony_ci depth_ptr = depth_base_ptr; 1239bf215546Sopenharmony_ci if (key->multisample) { 1240bf215546Sopenharmony_ci LLVMValueRef sample_offset = LLVMBuildMul(builder, sample_loop_state.counter, depth_sample_stride, ""); 1241bf215546Sopenharmony_ci depth_ptr = LLVMBuildGEP(builder, depth_ptr, &sample_offset, 1, ""); 1242bf215546Sopenharmony_ci } 1243bf215546Sopenharmony_ci 1244bf215546Sopenharmony_ci /* Late Z test */ 1245bf215546Sopenharmony_ci if (depth_mode & LATE_DEPTH_TEST) { 1246bf215546Sopenharmony_ci if (shader->info.base.writes_z) { 1247bf215546Sopenharmony_ci LLVMValueRef idx = loop_state.counter; 1248bf215546Sopenharmony_ci if (key->min_samples > 1) 1249bf215546Sopenharmony_ci idx = LLVMBuildAdd(builder, idx, 1250bf215546Sopenharmony_ci LLVMBuildMul(builder, sample_loop_state.counter, num_loop, ""), ""); 1251bf215546Sopenharmony_ci LLVMValueRef ptr = LLVMBuildGEP(builder, z_out, &idx, 1, ""); 1252bf215546Sopenharmony_ci z = LLVMBuildLoad(builder, ptr, "output.z"); 1253bf215546Sopenharmony_ci } else { 1254bf215546Sopenharmony_ci if (key->multisample) { 1255bf215546Sopenharmony_ci lp_build_interp_soa_update_pos_dyn(interp, gallivm, loop_state.counter, key->multisample ? sample_loop_state.counter : NULL); 1256bf215546Sopenharmony_ci z = interp->pos[2]; 1257bf215546Sopenharmony_ci } 1258bf215546Sopenharmony_ci } 1259bf215546Sopenharmony_ci 1260bf215546Sopenharmony_ci /* 1261bf215546Sopenharmony_ci * Clamp according to ARB_depth_clamp semantics. 1262bf215546Sopenharmony_ci */ 1263bf215546Sopenharmony_ci z = lp_build_depth_clamp(gallivm, builder, key->depth_clamp, 1264bf215546Sopenharmony_ci key->restrict_depth_values, type, context_ptr, 1265bf215546Sopenharmony_ci thread_data_ptr, z); 1266bf215546Sopenharmony_ci 1267bf215546Sopenharmony_ci if (shader->info.base.writes_stencil) { 1268bf215546Sopenharmony_ci LLVMValueRef idx = loop_state.counter; 1269bf215546Sopenharmony_ci if (key->min_samples > 1) 1270bf215546Sopenharmony_ci idx = LLVMBuildAdd(builder, idx, 1271bf215546Sopenharmony_ci LLVMBuildMul(builder, sample_loop_state.counter, num_loop, ""), ""); 1272bf215546Sopenharmony_ci LLVMValueRef ptr = LLVMBuildGEP(builder, s_out, &idx, 1, ""); 1273bf215546Sopenharmony_ci stencil_refs[0] = LLVMBuildLoad(builder, ptr, "output.s"); 1274bf215546Sopenharmony_ci /* there's only one value, and spec says to discard additional bits */ 1275bf215546Sopenharmony_ci LLVMValueRef s_max_mask = lp_build_const_int_vec(gallivm, int_type, 255); 1276bf215546Sopenharmony_ci stencil_refs[0] = LLVMBuildBitCast(builder, stencil_refs[0], int_vec_type, ""); 1277bf215546Sopenharmony_ci stencil_refs[0] = LLVMBuildAnd(builder, stencil_refs[0], s_max_mask, ""); 1278bf215546Sopenharmony_ci stencil_refs[1] = stencil_refs[0]; 1279bf215546Sopenharmony_ci } 1280bf215546Sopenharmony_ci 1281bf215546Sopenharmony_ci lp_build_depth_stencil_load_swizzled(gallivm, type, 1282bf215546Sopenharmony_ci zs_format_desc, key->resource_1d, 1283bf215546Sopenharmony_ci depth_ptr, depth_stride, 1284bf215546Sopenharmony_ci &z_fb, &s_fb, loop_state.counter); 1285bf215546Sopenharmony_ci 1286bf215546Sopenharmony_ci lp_build_depth_stencil_test(gallivm, 1287bf215546Sopenharmony_ci &key->depth, 1288bf215546Sopenharmony_ci key->stencil, 1289bf215546Sopenharmony_ci type, 1290bf215546Sopenharmony_ci zs_format_desc, 1291bf215546Sopenharmony_ci key->multisample ? NULL : &mask, 1292bf215546Sopenharmony_ci &s_mask, 1293bf215546Sopenharmony_ci stencil_refs, 1294bf215546Sopenharmony_ci z, z_fb, s_fb, 1295bf215546Sopenharmony_ci facing, 1296bf215546Sopenharmony_ci &z_value, &s_value, 1297bf215546Sopenharmony_ci !simple_shader, 1298bf215546Sopenharmony_ci key->restrict_depth_values); 1299bf215546Sopenharmony_ci /* Late Z write */ 1300bf215546Sopenharmony_ci if (depth_mode & LATE_DEPTH_WRITE) { 1301bf215546Sopenharmony_ci lp_build_depth_stencil_write_swizzled(gallivm, type, 1302bf215546Sopenharmony_ci zs_format_desc, key->resource_1d, 1303bf215546Sopenharmony_ci NULL, NULL, NULL, loop_state.counter, 1304bf215546Sopenharmony_ci depth_ptr, depth_stride, 1305bf215546Sopenharmony_ci z_value, s_value); 1306bf215546Sopenharmony_ci } 1307bf215546Sopenharmony_ci } 1308bf215546Sopenharmony_ci else if ((depth_mode & EARLY_DEPTH_TEST) && 1309bf215546Sopenharmony_ci (depth_mode & LATE_DEPTH_WRITE)) 1310bf215546Sopenharmony_ci { 1311bf215546Sopenharmony_ci /* Need to apply a reduced mask to the depth write. Reload the 1312bf215546Sopenharmony_ci * depth value, update from zs_value with the new mask value and 1313bf215546Sopenharmony_ci * write that out. 1314bf215546Sopenharmony_ci */ 1315bf215546Sopenharmony_ci if (key->multisample) { 1316bf215546Sopenharmony_ci z_value = LLVMBuildBitCast(builder, lp_build_pointer_get(builder, z_sample_value_store, sample_loop_state.counter), z_type, "");; 1317bf215546Sopenharmony_ci s_value = lp_build_pointer_get(builder, s_sample_value_store, sample_loop_state.counter); 1318bf215546Sopenharmony_ci z_fb = LLVMBuildBitCast(builder, lp_build_pointer_get(builder, z_fb_store, sample_loop_state.counter), z_fb_type, ""); 1319bf215546Sopenharmony_ci s_fb = lp_build_pointer_get(builder, s_fb_store, sample_loop_state.counter); 1320bf215546Sopenharmony_ci } 1321bf215546Sopenharmony_ci lp_build_depth_stencil_write_swizzled(gallivm, type, 1322bf215546Sopenharmony_ci zs_format_desc, key->resource_1d, 1323bf215546Sopenharmony_ci key->multisample ? s_mask : lp_build_mask_value(&mask), z_fb, s_fb, loop_state.counter, 1324bf215546Sopenharmony_ci depth_ptr, depth_stride, 1325bf215546Sopenharmony_ci z_value, s_value); 1326bf215546Sopenharmony_ci } 1327bf215546Sopenharmony_ci 1328bf215546Sopenharmony_ci if (key->occlusion_count) { 1329bf215546Sopenharmony_ci LLVMValueRef counter = lp_jit_thread_data_counter(gallivm, thread_data_ptr); 1330bf215546Sopenharmony_ci lp_build_name(counter, "counter"); 1331bf215546Sopenharmony_ci 1332bf215546Sopenharmony_ci lp_build_occlusion_count(gallivm, type, 1333bf215546Sopenharmony_ci key->multisample ? s_mask : lp_build_mask_value(&mask), counter); 1334bf215546Sopenharmony_ci } 1335bf215546Sopenharmony_ci 1336bf215546Sopenharmony_ci /* if this is genuine early-depth in the shader, write samplemask now 1337bf215546Sopenharmony_ci * after occlusion count has been updated 1338bf215546Sopenharmony_ci */ 1339bf215546Sopenharmony_ci if (key->multisample && shader->info.base.writes_samplemask && 1340bf215546Sopenharmony_ci (depth_mode & (EARLY_DEPTH_TEST_INFERRED | EARLY_DEPTH_TEST)) == EARLY_DEPTH_TEST) { 1341bf215546Sopenharmony_ci /* if the shader writes sample mask use that */ 1342bf215546Sopenharmony_ci LLVMValueRef out_smask_idx = LLVMBuildShl(builder, lp_build_const_int32(gallivm, 1), sample_loop_state.counter, ""); 1343bf215546Sopenharmony_ci out_smask_idx = lp_build_broadcast(gallivm, int_vec_type, out_smask_idx); 1344bf215546Sopenharmony_ci LLVMValueRef output_smask = LLVMBuildLoad(builder, out_sample_mask_storage, ""); 1345bf215546Sopenharmony_ci LLVMValueRef smask_bit = LLVMBuildAnd(builder, output_smask, out_smask_idx, ""); 1346bf215546Sopenharmony_ci LLVMValueRef cmp = LLVMBuildICmp(builder, LLVMIntNE, smask_bit, lp_build_const_int_vec(gallivm, int_type, 0), ""); 1347bf215546Sopenharmony_ci smask_bit = LLVMBuildSExt(builder, cmp, int_vec_type, ""); 1348bf215546Sopenharmony_ci 1349bf215546Sopenharmony_ci s_mask = LLVMBuildAnd(builder, s_mask, smask_bit, ""); 1350bf215546Sopenharmony_ci } 1351bf215546Sopenharmony_ci 1352bf215546Sopenharmony_ci 1353bf215546Sopenharmony_ci if (key->multisample) { 1354bf215546Sopenharmony_ci /* store the sample mask for this loop */ 1355bf215546Sopenharmony_ci LLVMBuildStore(builder, s_mask, s_mask_ptr); 1356bf215546Sopenharmony_ci lp_build_for_loop_end(&sample_loop_state); 1357bf215546Sopenharmony_ci } 1358bf215546Sopenharmony_ci 1359bf215546Sopenharmony_ci mask_val = lp_build_mask_end(&mask); 1360bf215546Sopenharmony_ci if (!key->multisample) 1361bf215546Sopenharmony_ci LLVMBuildStore(builder, mask_val, mask_ptr); 1362bf215546Sopenharmony_ci lp_build_for_loop_end(&loop_state); 1363bf215546Sopenharmony_ci} 1364bf215546Sopenharmony_ci 1365bf215546Sopenharmony_ci 1366bf215546Sopenharmony_ci/** 1367bf215546Sopenharmony_ci * This function will reorder pixels from the fragment shader SoA to memory layout AoS 1368bf215546Sopenharmony_ci * 1369bf215546Sopenharmony_ci * Fragment Shader outputs pixels in small 2x2 blocks 1370bf215546Sopenharmony_ci * e.g. (0, 0), (1, 0), (0, 1), (1, 1) ; (2, 0) ... 1371bf215546Sopenharmony_ci * 1372bf215546Sopenharmony_ci * However in memory pixels are stored in rows 1373bf215546Sopenharmony_ci * e.g. (0, 0), (1, 0), (2, 0), (3, 0) ; (0, 1) ... 1374bf215546Sopenharmony_ci * 1375bf215546Sopenharmony_ci * @param type fragment shader type (4x or 8x float) 1376bf215546Sopenharmony_ci * @param num_fs number of fs_src 1377bf215546Sopenharmony_ci * @param is_1d whether we're outputting to a 1d resource 1378bf215546Sopenharmony_ci * @param dst_channels number of output channels 1379bf215546Sopenharmony_ci * @param fs_src output from fragment shader 1380bf215546Sopenharmony_ci * @param dst pointer to store result 1381bf215546Sopenharmony_ci * @param pad_inline is channel padding inline or at end of row 1382bf215546Sopenharmony_ci * @return the number of dsts 1383bf215546Sopenharmony_ci */ 1384bf215546Sopenharmony_cistatic int 1385bf215546Sopenharmony_cigenerate_fs_twiddle(struct gallivm_state *gallivm, 1386bf215546Sopenharmony_ci struct lp_type type, 1387bf215546Sopenharmony_ci unsigned num_fs, 1388bf215546Sopenharmony_ci unsigned dst_channels, 1389bf215546Sopenharmony_ci LLVMValueRef fs_src[][4], 1390bf215546Sopenharmony_ci LLVMValueRef* dst, 1391bf215546Sopenharmony_ci bool pad_inline) 1392bf215546Sopenharmony_ci{ 1393bf215546Sopenharmony_ci LLVMValueRef src[16]; 1394bf215546Sopenharmony_ci 1395bf215546Sopenharmony_ci bool swizzle_pad; 1396bf215546Sopenharmony_ci bool twiddle; 1397bf215546Sopenharmony_ci bool split; 1398bf215546Sopenharmony_ci 1399bf215546Sopenharmony_ci unsigned pixels = type.length / 4; 1400bf215546Sopenharmony_ci unsigned reorder_group; 1401bf215546Sopenharmony_ci unsigned src_channels; 1402bf215546Sopenharmony_ci unsigned src_count; 1403bf215546Sopenharmony_ci unsigned i; 1404bf215546Sopenharmony_ci 1405bf215546Sopenharmony_ci src_channels = dst_channels < 3 ? dst_channels : 4; 1406bf215546Sopenharmony_ci src_count = num_fs * src_channels; 1407bf215546Sopenharmony_ci 1408bf215546Sopenharmony_ci assert(pixels == 2 || pixels == 1); 1409bf215546Sopenharmony_ci assert(num_fs * src_channels <= ARRAY_SIZE(src)); 1410bf215546Sopenharmony_ci 1411bf215546Sopenharmony_ci /* 1412bf215546Sopenharmony_ci * Transpose from SoA -> AoS 1413bf215546Sopenharmony_ci */ 1414bf215546Sopenharmony_ci for (i = 0; i < num_fs; ++i) { 1415bf215546Sopenharmony_ci lp_build_transpose_aos_n(gallivm, type, &fs_src[i][0], src_channels, &src[i * src_channels]); 1416bf215546Sopenharmony_ci } 1417bf215546Sopenharmony_ci 1418bf215546Sopenharmony_ci /* 1419bf215546Sopenharmony_ci * Pick transformation options 1420bf215546Sopenharmony_ci */ 1421bf215546Sopenharmony_ci swizzle_pad = false; 1422bf215546Sopenharmony_ci twiddle = false; 1423bf215546Sopenharmony_ci split = false; 1424bf215546Sopenharmony_ci reorder_group = 0; 1425bf215546Sopenharmony_ci 1426bf215546Sopenharmony_ci if (dst_channels == 1) { 1427bf215546Sopenharmony_ci twiddle = true; 1428bf215546Sopenharmony_ci 1429bf215546Sopenharmony_ci if (pixels == 2) { 1430bf215546Sopenharmony_ci split = true; 1431bf215546Sopenharmony_ci } 1432bf215546Sopenharmony_ci } else if (dst_channels == 2) { 1433bf215546Sopenharmony_ci if (pixels == 1) { 1434bf215546Sopenharmony_ci reorder_group = 1; 1435bf215546Sopenharmony_ci } 1436bf215546Sopenharmony_ci } else if (dst_channels > 2) { 1437bf215546Sopenharmony_ci if (pixels == 1) { 1438bf215546Sopenharmony_ci reorder_group = 2; 1439bf215546Sopenharmony_ci } else { 1440bf215546Sopenharmony_ci twiddle = true; 1441bf215546Sopenharmony_ci } 1442bf215546Sopenharmony_ci 1443bf215546Sopenharmony_ci if (!pad_inline && dst_channels == 3 && pixels > 1) { 1444bf215546Sopenharmony_ci swizzle_pad = true; 1445bf215546Sopenharmony_ci } 1446bf215546Sopenharmony_ci } 1447bf215546Sopenharmony_ci 1448bf215546Sopenharmony_ci /* 1449bf215546Sopenharmony_ci * Split the src in half 1450bf215546Sopenharmony_ci */ 1451bf215546Sopenharmony_ci if (split) { 1452bf215546Sopenharmony_ci for (i = num_fs; i > 0; --i) { 1453bf215546Sopenharmony_ci src[(i - 1)*2 + 1] = lp_build_extract_range(gallivm, src[i - 1], 4, 4); 1454bf215546Sopenharmony_ci src[(i - 1)*2 + 0] = lp_build_extract_range(gallivm, src[i - 1], 0, 4); 1455bf215546Sopenharmony_ci } 1456bf215546Sopenharmony_ci 1457bf215546Sopenharmony_ci src_count *= 2; 1458bf215546Sopenharmony_ci type.length = 4; 1459bf215546Sopenharmony_ci } 1460bf215546Sopenharmony_ci 1461bf215546Sopenharmony_ci /* 1462bf215546Sopenharmony_ci * Ensure pixels are in memory order 1463bf215546Sopenharmony_ci */ 1464bf215546Sopenharmony_ci if (reorder_group) { 1465bf215546Sopenharmony_ci /* Twiddle pixels by reordering the array, e.g.: 1466bf215546Sopenharmony_ci * 1467bf215546Sopenharmony_ci * src_count = 8 -> 0 2 1 3 4 6 5 7 1468bf215546Sopenharmony_ci * src_count = 16 -> 0 1 4 5 2 3 6 7 8 9 12 13 10 11 14 15 1469bf215546Sopenharmony_ci */ 1470bf215546Sopenharmony_ci const unsigned reorder_sw[] = { 0, 2, 1, 3 }; 1471bf215546Sopenharmony_ci 1472bf215546Sopenharmony_ci for (i = 0; i < src_count; ++i) { 1473bf215546Sopenharmony_ci unsigned group = i / reorder_group; 1474bf215546Sopenharmony_ci unsigned block = (group / 4) * 4 * reorder_group; 1475bf215546Sopenharmony_ci unsigned j = block + (reorder_sw[group % 4] * reorder_group) + (i % reorder_group); 1476bf215546Sopenharmony_ci dst[i] = src[j]; 1477bf215546Sopenharmony_ci } 1478bf215546Sopenharmony_ci } else if (twiddle) { 1479bf215546Sopenharmony_ci /* Twiddle pixels across elements of array */ 1480bf215546Sopenharmony_ci /* 1481bf215546Sopenharmony_ci * XXX: we should avoid this in some cases, but would need to tell 1482bf215546Sopenharmony_ci * lp_build_conv to reorder (or deal with it ourselves). 1483bf215546Sopenharmony_ci */ 1484bf215546Sopenharmony_ci lp_bld_quad_twiddle(gallivm, type, src, src_count, dst); 1485bf215546Sopenharmony_ci } else { 1486bf215546Sopenharmony_ci /* Do nothing */ 1487bf215546Sopenharmony_ci memcpy(dst, src, sizeof(LLVMValueRef) * src_count); 1488bf215546Sopenharmony_ci } 1489bf215546Sopenharmony_ci 1490bf215546Sopenharmony_ci /* 1491bf215546Sopenharmony_ci * Moves any padding between pixels to the end 1492bf215546Sopenharmony_ci * e.g. RGBXRGBX -> RGBRGBXX 1493bf215546Sopenharmony_ci */ 1494bf215546Sopenharmony_ci if (swizzle_pad) { 1495bf215546Sopenharmony_ci unsigned char swizzles[16]; 1496bf215546Sopenharmony_ci unsigned elems = pixels * dst_channels; 1497bf215546Sopenharmony_ci 1498bf215546Sopenharmony_ci for (i = 0; i < type.length; ++i) { 1499bf215546Sopenharmony_ci if (i < elems) 1500bf215546Sopenharmony_ci swizzles[i] = i % dst_channels + (i / dst_channels) * 4; 1501bf215546Sopenharmony_ci else 1502bf215546Sopenharmony_ci swizzles[i] = LP_BLD_SWIZZLE_DONTCARE; 1503bf215546Sopenharmony_ci } 1504bf215546Sopenharmony_ci 1505bf215546Sopenharmony_ci for (i = 0; i < src_count; ++i) { 1506bf215546Sopenharmony_ci dst[i] = lp_build_swizzle_aos_n(gallivm, dst[i], swizzles, type.length, type.length); 1507bf215546Sopenharmony_ci } 1508bf215546Sopenharmony_ci } 1509bf215546Sopenharmony_ci 1510bf215546Sopenharmony_ci return src_count; 1511bf215546Sopenharmony_ci} 1512bf215546Sopenharmony_ci 1513bf215546Sopenharmony_ci 1514bf215546Sopenharmony_ci/* 1515bf215546Sopenharmony_ci * Untwiddle and transpose, much like the above. 1516bf215546Sopenharmony_ci * However, this is after conversion, so we get packed vectors. 1517bf215546Sopenharmony_ci * At this time only handle 4x16i8 rgba / 2x16i8 rg / 1x16i8 r data, 1518bf215546Sopenharmony_ci * the vectors will look like: 1519bf215546Sopenharmony_ci * r0r1r4r5r2r3r6r7r8r9r12... (albeit color channels may 1520bf215546Sopenharmony_ci * be swizzled here). Extending to 16bit should be trivial. 1521bf215546Sopenharmony_ci * Should also be extended to handle twice wide vectors with AVX2... 1522bf215546Sopenharmony_ci */ 1523bf215546Sopenharmony_cistatic void 1524bf215546Sopenharmony_cifs_twiddle_transpose(struct gallivm_state *gallivm, 1525bf215546Sopenharmony_ci struct lp_type type, 1526bf215546Sopenharmony_ci LLVMValueRef *src, 1527bf215546Sopenharmony_ci unsigned src_count, 1528bf215546Sopenharmony_ci LLVMValueRef *dst) 1529bf215546Sopenharmony_ci{ 1530bf215546Sopenharmony_ci struct lp_type type64, type16, type32; 1531bf215546Sopenharmony_ci LLVMTypeRef type64_t, type8_t, type16_t, type32_t; 1532bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 1533bf215546Sopenharmony_ci LLVMValueRef tmp[4], shuf[8]; 1534bf215546Sopenharmony_ci for (unsigned j = 0; j < 2; j++) { 1535bf215546Sopenharmony_ci shuf[j*4 + 0] = lp_build_const_int32(gallivm, j*4 + 0); 1536bf215546Sopenharmony_ci shuf[j*4 + 1] = lp_build_const_int32(gallivm, j*4 + 2); 1537bf215546Sopenharmony_ci shuf[j*4 + 2] = lp_build_const_int32(gallivm, j*4 + 1); 1538bf215546Sopenharmony_ci shuf[j*4 + 3] = lp_build_const_int32(gallivm, j*4 + 3); 1539bf215546Sopenharmony_ci } 1540bf215546Sopenharmony_ci 1541bf215546Sopenharmony_ci assert(src_count == 4 || src_count == 2 || src_count == 1); 1542bf215546Sopenharmony_ci assert(type.width == 8); 1543bf215546Sopenharmony_ci assert(type.length == 16); 1544bf215546Sopenharmony_ci 1545bf215546Sopenharmony_ci type8_t = lp_build_vec_type(gallivm, type); 1546bf215546Sopenharmony_ci 1547bf215546Sopenharmony_ci type64 = type; 1548bf215546Sopenharmony_ci type64.length /= 8; 1549bf215546Sopenharmony_ci type64.width *= 8; 1550bf215546Sopenharmony_ci type64_t = lp_build_vec_type(gallivm, type64); 1551bf215546Sopenharmony_ci 1552bf215546Sopenharmony_ci type16 = type; 1553bf215546Sopenharmony_ci type16.length /= 2; 1554bf215546Sopenharmony_ci type16.width *= 2; 1555bf215546Sopenharmony_ci type16_t = lp_build_vec_type(gallivm, type16); 1556bf215546Sopenharmony_ci 1557bf215546Sopenharmony_ci type32 = type; 1558bf215546Sopenharmony_ci type32.length /= 4; 1559bf215546Sopenharmony_ci type32.width *= 4; 1560bf215546Sopenharmony_ci type32_t = lp_build_vec_type(gallivm, type32); 1561bf215546Sopenharmony_ci 1562bf215546Sopenharmony_ci lp_build_transpose_aos_n(gallivm, type, src, src_count, tmp); 1563bf215546Sopenharmony_ci 1564bf215546Sopenharmony_ci if (src_count == 1) { 1565bf215546Sopenharmony_ci /* transpose was no-op, just untwiddle */ 1566bf215546Sopenharmony_ci LLVMValueRef shuf_vec; 1567bf215546Sopenharmony_ci shuf_vec = LLVMConstVector(shuf, 8); 1568bf215546Sopenharmony_ci tmp[0] = LLVMBuildBitCast(builder, src[0], type16_t, ""); 1569bf215546Sopenharmony_ci tmp[0] = LLVMBuildShuffleVector(builder, tmp[0], tmp[0], shuf_vec, ""); 1570bf215546Sopenharmony_ci dst[0] = LLVMBuildBitCast(builder, tmp[0], type8_t, ""); 1571bf215546Sopenharmony_ci } else if (src_count == 2) { 1572bf215546Sopenharmony_ci LLVMValueRef shuf_vec; 1573bf215546Sopenharmony_ci shuf_vec = LLVMConstVector(shuf, 4); 1574bf215546Sopenharmony_ci 1575bf215546Sopenharmony_ci for (unsigned i = 0; i < 2; i++) { 1576bf215546Sopenharmony_ci tmp[i] = LLVMBuildBitCast(builder, tmp[i], type32_t, ""); 1577bf215546Sopenharmony_ci tmp[i] = LLVMBuildShuffleVector(builder, tmp[i], tmp[i], shuf_vec, ""); 1578bf215546Sopenharmony_ci dst[i] = LLVMBuildBitCast(builder, tmp[i], type8_t, ""); 1579bf215546Sopenharmony_ci } 1580bf215546Sopenharmony_ci } else { 1581bf215546Sopenharmony_ci for (unsigned j = 0; j < 2; j++) { 1582bf215546Sopenharmony_ci LLVMValueRef lo, hi, lo2, hi2; 1583bf215546Sopenharmony_ci /* 1584bf215546Sopenharmony_ci * Note that if we only really have 3 valid channels (rgb) 1585bf215546Sopenharmony_ci * and we don't need alpha we could substitute a undef here 1586bf215546Sopenharmony_ci * for the respective channel (causing llvm to drop conversion 1587bf215546Sopenharmony_ci * for alpha). 1588bf215546Sopenharmony_ci */ 1589bf215546Sopenharmony_ci /* we now have rgba0rgba1rgba4rgba5 etc, untwiddle */ 1590bf215546Sopenharmony_ci lo2 = LLVMBuildBitCast(builder, tmp[j*2], type64_t, ""); 1591bf215546Sopenharmony_ci hi2 = LLVMBuildBitCast(builder, tmp[j*2 + 1], type64_t, ""); 1592bf215546Sopenharmony_ci lo = lp_build_interleave2(gallivm, type64, lo2, hi2, 0); 1593bf215546Sopenharmony_ci hi = lp_build_interleave2(gallivm, type64, lo2, hi2, 1); 1594bf215546Sopenharmony_ci dst[j*2] = LLVMBuildBitCast(builder, lo, type8_t, ""); 1595bf215546Sopenharmony_ci dst[j*2 + 1] = LLVMBuildBitCast(builder, hi, type8_t, ""); 1596bf215546Sopenharmony_ci } 1597bf215546Sopenharmony_ci } 1598bf215546Sopenharmony_ci} 1599bf215546Sopenharmony_ci 1600bf215546Sopenharmony_ci 1601bf215546Sopenharmony_ci/** 1602bf215546Sopenharmony_ci * Load an unswizzled block of pixels from memory 1603bf215546Sopenharmony_ci */ 1604bf215546Sopenharmony_cistatic void 1605bf215546Sopenharmony_ciload_unswizzled_block(struct gallivm_state *gallivm, 1606bf215546Sopenharmony_ci LLVMValueRef base_ptr, 1607bf215546Sopenharmony_ci LLVMValueRef stride, 1608bf215546Sopenharmony_ci unsigned block_width, 1609bf215546Sopenharmony_ci unsigned block_height, 1610bf215546Sopenharmony_ci LLVMValueRef* dst, 1611bf215546Sopenharmony_ci struct lp_type dst_type, 1612bf215546Sopenharmony_ci unsigned dst_count, 1613bf215546Sopenharmony_ci unsigned dst_alignment) 1614bf215546Sopenharmony_ci{ 1615bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 1616bf215546Sopenharmony_ci const unsigned row_size = dst_count / block_height; 1617bf215546Sopenharmony_ci 1618bf215546Sopenharmony_ci /* Ensure block exactly fits into dst */ 1619bf215546Sopenharmony_ci assert((block_width * block_height) % dst_count == 0); 1620bf215546Sopenharmony_ci 1621bf215546Sopenharmony_ci for (unsigned i = 0; i < dst_count; ++i) { 1622bf215546Sopenharmony_ci unsigned x = i % row_size; 1623bf215546Sopenharmony_ci unsigned y = i / row_size; 1624bf215546Sopenharmony_ci 1625bf215546Sopenharmony_ci LLVMValueRef bx = lp_build_const_int32(gallivm, x * (dst_type.width / 8) * dst_type.length); 1626bf215546Sopenharmony_ci LLVMValueRef by = LLVMBuildMul(builder, lp_build_const_int32(gallivm, y), stride, ""); 1627bf215546Sopenharmony_ci 1628bf215546Sopenharmony_ci LLVMValueRef gep[2]; 1629bf215546Sopenharmony_ci LLVMValueRef dst_ptr; 1630bf215546Sopenharmony_ci 1631bf215546Sopenharmony_ci gep[0] = lp_build_const_int32(gallivm, 0); 1632bf215546Sopenharmony_ci gep[1] = LLVMBuildAdd(builder, bx, by, ""); 1633bf215546Sopenharmony_ci 1634bf215546Sopenharmony_ci dst_ptr = LLVMBuildGEP(builder, base_ptr, gep, 2, ""); 1635bf215546Sopenharmony_ci dst_ptr = LLVMBuildBitCast(builder, dst_ptr, 1636bf215546Sopenharmony_ci LLVMPointerType(lp_build_vec_type(gallivm, dst_type), 0), ""); 1637bf215546Sopenharmony_ci 1638bf215546Sopenharmony_ci dst[i] = LLVMBuildLoad(builder, dst_ptr, ""); 1639bf215546Sopenharmony_ci 1640bf215546Sopenharmony_ci LLVMSetAlignment(dst[i], dst_alignment); 1641bf215546Sopenharmony_ci } 1642bf215546Sopenharmony_ci} 1643bf215546Sopenharmony_ci 1644bf215546Sopenharmony_ci 1645bf215546Sopenharmony_ci/** 1646bf215546Sopenharmony_ci * Store an unswizzled block of pixels to memory 1647bf215546Sopenharmony_ci */ 1648bf215546Sopenharmony_cistatic void 1649bf215546Sopenharmony_cistore_unswizzled_block(struct gallivm_state *gallivm, 1650bf215546Sopenharmony_ci LLVMValueRef base_ptr, 1651bf215546Sopenharmony_ci LLVMValueRef stride, 1652bf215546Sopenharmony_ci unsigned block_width, 1653bf215546Sopenharmony_ci unsigned block_height, 1654bf215546Sopenharmony_ci LLVMValueRef* src, 1655bf215546Sopenharmony_ci struct lp_type src_type, 1656bf215546Sopenharmony_ci unsigned src_count, 1657bf215546Sopenharmony_ci unsigned src_alignment) 1658bf215546Sopenharmony_ci{ 1659bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 1660bf215546Sopenharmony_ci const unsigned row_size = src_count / block_height; 1661bf215546Sopenharmony_ci 1662bf215546Sopenharmony_ci /* Ensure src exactly fits into block */ 1663bf215546Sopenharmony_ci assert((block_width * block_height) % src_count == 0); 1664bf215546Sopenharmony_ci 1665bf215546Sopenharmony_ci for (unsigned i = 0; i < src_count; ++i) { 1666bf215546Sopenharmony_ci unsigned x = i % row_size; 1667bf215546Sopenharmony_ci unsigned y = i / row_size; 1668bf215546Sopenharmony_ci 1669bf215546Sopenharmony_ci LLVMValueRef bx = lp_build_const_int32(gallivm, x * (src_type.width / 8) * src_type.length); 1670bf215546Sopenharmony_ci LLVMValueRef by = LLVMBuildMul(builder, lp_build_const_int32(gallivm, y), stride, ""); 1671bf215546Sopenharmony_ci 1672bf215546Sopenharmony_ci LLVMValueRef gep[2]; 1673bf215546Sopenharmony_ci LLVMValueRef src_ptr; 1674bf215546Sopenharmony_ci 1675bf215546Sopenharmony_ci gep[0] = lp_build_const_int32(gallivm, 0); 1676bf215546Sopenharmony_ci gep[1] = LLVMBuildAdd(builder, bx, by, ""); 1677bf215546Sopenharmony_ci 1678bf215546Sopenharmony_ci src_ptr = LLVMBuildGEP(builder, base_ptr, gep, 2, ""); 1679bf215546Sopenharmony_ci src_ptr = LLVMBuildBitCast(builder, src_ptr, 1680bf215546Sopenharmony_ci LLVMPointerType(lp_build_vec_type(gallivm, src_type), 0), ""); 1681bf215546Sopenharmony_ci 1682bf215546Sopenharmony_ci src_ptr = LLVMBuildStore(builder, src[i], src_ptr); 1683bf215546Sopenharmony_ci 1684bf215546Sopenharmony_ci LLVMSetAlignment(src_ptr, src_alignment); 1685bf215546Sopenharmony_ci } 1686bf215546Sopenharmony_ci} 1687bf215546Sopenharmony_ci 1688bf215546Sopenharmony_ci 1689bf215546Sopenharmony_ci 1690bf215546Sopenharmony_ci/** 1691bf215546Sopenharmony_ci * Retrieves the type for a format which is usable in the blending code. 1692bf215546Sopenharmony_ci * 1693bf215546Sopenharmony_ci * e.g. RGBA16F = 4x float, R3G3B2 = 3x byte 1694bf215546Sopenharmony_ci */ 1695bf215546Sopenharmony_cistatic inline void 1696bf215546Sopenharmony_cilp_blend_type_from_format_desc(const struct util_format_description *format_desc, 1697bf215546Sopenharmony_ci struct lp_type* type) 1698bf215546Sopenharmony_ci{ 1699bf215546Sopenharmony_ci if (format_expands_to_float_soa(format_desc)) { 1700bf215546Sopenharmony_ci /* always use ordinary floats for blending */ 1701bf215546Sopenharmony_ci type->floating = true; 1702bf215546Sopenharmony_ci type->fixed = false; 1703bf215546Sopenharmony_ci type->sign = true; 1704bf215546Sopenharmony_ci type->norm = false; 1705bf215546Sopenharmony_ci type->width = 32; 1706bf215546Sopenharmony_ci type->length = 4; 1707bf215546Sopenharmony_ci return; 1708bf215546Sopenharmony_ci } 1709bf215546Sopenharmony_ci 1710bf215546Sopenharmony_ci unsigned i; 1711bf215546Sopenharmony_ci for (i = 0; i < 4; i++) 1712bf215546Sopenharmony_ci if (format_desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) 1713bf215546Sopenharmony_ci break; 1714bf215546Sopenharmony_ci const unsigned chan = i; 1715bf215546Sopenharmony_ci 1716bf215546Sopenharmony_ci memset(type, 0, sizeof(struct lp_type)); 1717bf215546Sopenharmony_ci type->floating = format_desc->channel[chan].type == UTIL_FORMAT_TYPE_FLOAT; 1718bf215546Sopenharmony_ci type->fixed = format_desc->channel[chan].type == UTIL_FORMAT_TYPE_FIXED; 1719bf215546Sopenharmony_ci type->sign = format_desc->channel[chan].type != UTIL_FORMAT_TYPE_UNSIGNED; 1720bf215546Sopenharmony_ci type->norm = format_desc->channel[chan].normalized; 1721bf215546Sopenharmony_ci type->width = format_desc->channel[chan].size; 1722bf215546Sopenharmony_ci type->length = format_desc->nr_channels; 1723bf215546Sopenharmony_ci 1724bf215546Sopenharmony_ci for (unsigned i = 1; i < format_desc->nr_channels; ++i) { 1725bf215546Sopenharmony_ci if (format_desc->channel[i].size > type->width) 1726bf215546Sopenharmony_ci type->width = format_desc->channel[i].size; 1727bf215546Sopenharmony_ci } 1728bf215546Sopenharmony_ci 1729bf215546Sopenharmony_ci if (type->floating) { 1730bf215546Sopenharmony_ci type->width = 32; 1731bf215546Sopenharmony_ci } else { 1732bf215546Sopenharmony_ci if (type->width <= 8) { 1733bf215546Sopenharmony_ci type->width = 8; 1734bf215546Sopenharmony_ci } else if (type->width <= 16) { 1735bf215546Sopenharmony_ci type->width = 16; 1736bf215546Sopenharmony_ci } else { 1737bf215546Sopenharmony_ci type->width = 32; 1738bf215546Sopenharmony_ci } 1739bf215546Sopenharmony_ci } 1740bf215546Sopenharmony_ci 1741bf215546Sopenharmony_ci if (is_arithmetic_format(format_desc) && type->length == 3) { 1742bf215546Sopenharmony_ci type->length = 4; 1743bf215546Sopenharmony_ci } 1744bf215546Sopenharmony_ci} 1745bf215546Sopenharmony_ci 1746bf215546Sopenharmony_ci 1747bf215546Sopenharmony_ci/** 1748bf215546Sopenharmony_ci * Scale a normalized value from src_bits to dst_bits. 1749bf215546Sopenharmony_ci * 1750bf215546Sopenharmony_ci * The exact calculation is 1751bf215546Sopenharmony_ci * 1752bf215546Sopenharmony_ci * dst = iround(src * dst_mask / src_mask) 1753bf215546Sopenharmony_ci * 1754bf215546Sopenharmony_ci * or with integer rounding 1755bf215546Sopenharmony_ci * 1756bf215546Sopenharmony_ci * dst = src * (2*dst_mask + sign(src)*src_mask) / (2*src_mask) 1757bf215546Sopenharmony_ci * 1758bf215546Sopenharmony_ci * where 1759bf215546Sopenharmony_ci * 1760bf215546Sopenharmony_ci * src_mask = (1 << src_bits) - 1 1761bf215546Sopenharmony_ci * dst_mask = (1 << dst_bits) - 1 1762bf215546Sopenharmony_ci * 1763bf215546Sopenharmony_ci * but we try to avoid division and multiplication through shifts. 1764bf215546Sopenharmony_ci */ 1765bf215546Sopenharmony_cistatic inline LLVMValueRef 1766bf215546Sopenharmony_ciscale_bits(struct gallivm_state *gallivm, 1767bf215546Sopenharmony_ci int src_bits, 1768bf215546Sopenharmony_ci int dst_bits, 1769bf215546Sopenharmony_ci LLVMValueRef src, 1770bf215546Sopenharmony_ci struct lp_type src_type) 1771bf215546Sopenharmony_ci{ 1772bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 1773bf215546Sopenharmony_ci LLVMValueRef result = src; 1774bf215546Sopenharmony_ci 1775bf215546Sopenharmony_ci if (dst_bits < src_bits) { 1776bf215546Sopenharmony_ci int delta_bits = src_bits - dst_bits; 1777bf215546Sopenharmony_ci 1778bf215546Sopenharmony_ci if (delta_bits <= dst_bits) { 1779bf215546Sopenharmony_ci 1780bf215546Sopenharmony_ci if (dst_bits == 4) { 1781bf215546Sopenharmony_ci struct lp_type flt_type = lp_type_float_vec(32, src_type.length * 32); 1782bf215546Sopenharmony_ci 1783bf215546Sopenharmony_ci result = lp_build_unsigned_norm_to_float(gallivm, src_bits, flt_type, src); 1784bf215546Sopenharmony_ci result = lp_build_clamped_float_to_unsigned_norm(gallivm, flt_type, dst_bits, result); 1785bf215546Sopenharmony_ci result = LLVMBuildTrunc(gallivm->builder, result, lp_build_int_vec_type(gallivm, src_type), ""); 1786bf215546Sopenharmony_ci return result; 1787bf215546Sopenharmony_ci } 1788bf215546Sopenharmony_ci 1789bf215546Sopenharmony_ci /* 1790bf215546Sopenharmony_ci * Approximate the rescaling with a single shift. 1791bf215546Sopenharmony_ci * 1792bf215546Sopenharmony_ci * This gives the wrong rounding. 1793bf215546Sopenharmony_ci */ 1794bf215546Sopenharmony_ci 1795bf215546Sopenharmony_ci result = LLVMBuildLShr(builder, 1796bf215546Sopenharmony_ci src, 1797bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, delta_bits), 1798bf215546Sopenharmony_ci ""); 1799bf215546Sopenharmony_ci 1800bf215546Sopenharmony_ci } else { 1801bf215546Sopenharmony_ci /* 1802bf215546Sopenharmony_ci * Try more accurate rescaling. 1803bf215546Sopenharmony_ci */ 1804bf215546Sopenharmony_ci 1805bf215546Sopenharmony_ci /* 1806bf215546Sopenharmony_ci * Drop the least significant bits to make space for the multiplication. 1807bf215546Sopenharmony_ci * 1808bf215546Sopenharmony_ci * XXX: A better approach would be to use a wider integer type as intermediate. But 1809bf215546Sopenharmony_ci * this is enough to convert alpha from 16bits -> 2 when rendering to 1810bf215546Sopenharmony_ci * PIPE_FORMAT_R10G10B10A2_UNORM. 1811bf215546Sopenharmony_ci */ 1812bf215546Sopenharmony_ci result = LLVMBuildLShr(builder, 1813bf215546Sopenharmony_ci src, 1814bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, dst_bits), 1815bf215546Sopenharmony_ci ""); 1816bf215546Sopenharmony_ci 1817bf215546Sopenharmony_ci 1818bf215546Sopenharmony_ci result = LLVMBuildMul(builder, 1819bf215546Sopenharmony_ci result, 1820bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, (1LL << dst_bits) - 1), 1821bf215546Sopenharmony_ci ""); 1822bf215546Sopenharmony_ci 1823bf215546Sopenharmony_ci /* 1824bf215546Sopenharmony_ci * Add a rounding term before the division. 1825bf215546Sopenharmony_ci * 1826bf215546Sopenharmony_ci * TODO: Handle signed integers too. 1827bf215546Sopenharmony_ci */ 1828bf215546Sopenharmony_ci if (!src_type.sign) { 1829bf215546Sopenharmony_ci result = LLVMBuildAdd(builder, 1830bf215546Sopenharmony_ci result, 1831bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, (1LL << (delta_bits - 1))), 1832bf215546Sopenharmony_ci ""); 1833bf215546Sopenharmony_ci } 1834bf215546Sopenharmony_ci 1835bf215546Sopenharmony_ci /* 1836bf215546Sopenharmony_ci * Approximate the division by src_mask with a src_bits shift. 1837bf215546Sopenharmony_ci * 1838bf215546Sopenharmony_ci * Given the src has already been shifted by dst_bits, all we need 1839bf215546Sopenharmony_ci * to do is to shift by the difference. 1840bf215546Sopenharmony_ci */ 1841bf215546Sopenharmony_ci 1842bf215546Sopenharmony_ci result = LLVMBuildLShr(builder, 1843bf215546Sopenharmony_ci result, 1844bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, delta_bits), 1845bf215546Sopenharmony_ci ""); 1846bf215546Sopenharmony_ci } 1847bf215546Sopenharmony_ci 1848bf215546Sopenharmony_ci } else if (dst_bits > src_bits) { 1849bf215546Sopenharmony_ci /* Scale up bits */ 1850bf215546Sopenharmony_ci int db = dst_bits - src_bits; 1851bf215546Sopenharmony_ci 1852bf215546Sopenharmony_ci /* Shift left by difference in bits */ 1853bf215546Sopenharmony_ci result = LLVMBuildShl(builder, 1854bf215546Sopenharmony_ci src, 1855bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, db), 1856bf215546Sopenharmony_ci ""); 1857bf215546Sopenharmony_ci 1858bf215546Sopenharmony_ci if (db <= src_bits) { 1859bf215546Sopenharmony_ci /* Enough bits in src to fill the remainder */ 1860bf215546Sopenharmony_ci LLVMValueRef lower = LLVMBuildLShr(builder, 1861bf215546Sopenharmony_ci src, 1862bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, src_bits - db), 1863bf215546Sopenharmony_ci ""); 1864bf215546Sopenharmony_ci 1865bf215546Sopenharmony_ci result = LLVMBuildOr(builder, result, lower, ""); 1866bf215546Sopenharmony_ci } else if (db > src_bits) { 1867bf215546Sopenharmony_ci /* Need to repeatedly copy src bits to fill remainder in dst */ 1868bf215546Sopenharmony_ci unsigned n; 1869bf215546Sopenharmony_ci 1870bf215546Sopenharmony_ci for (n = src_bits; n < dst_bits; n *= 2) { 1871bf215546Sopenharmony_ci LLVMValueRef shuv = lp_build_const_int_vec(gallivm, src_type, n); 1872bf215546Sopenharmony_ci 1873bf215546Sopenharmony_ci result = LLVMBuildOr(builder, 1874bf215546Sopenharmony_ci result, 1875bf215546Sopenharmony_ci LLVMBuildLShr(builder, result, shuv, ""), 1876bf215546Sopenharmony_ci ""); 1877bf215546Sopenharmony_ci } 1878bf215546Sopenharmony_ci } 1879bf215546Sopenharmony_ci } 1880bf215546Sopenharmony_ci 1881bf215546Sopenharmony_ci return result; 1882bf215546Sopenharmony_ci} 1883bf215546Sopenharmony_ci 1884bf215546Sopenharmony_ci/** 1885bf215546Sopenharmony_ci * If RT is a smallfloat (needing denorms) format 1886bf215546Sopenharmony_ci */ 1887bf215546Sopenharmony_cistatic inline int 1888bf215546Sopenharmony_cihave_smallfloat_format(struct lp_type dst_type, 1889bf215546Sopenharmony_ci enum pipe_format format) 1890bf215546Sopenharmony_ci{ 1891bf215546Sopenharmony_ci return ((dst_type.floating && dst_type.width != 32) || 1892bf215546Sopenharmony_ci /* due to format handling hacks this format doesn't have floating set 1893bf215546Sopenharmony_ci * here (and actually has width set to 32 too) so special case this. */ 1894bf215546Sopenharmony_ci (format == PIPE_FORMAT_R11G11B10_FLOAT)); 1895bf215546Sopenharmony_ci} 1896bf215546Sopenharmony_ci 1897bf215546Sopenharmony_ci 1898bf215546Sopenharmony_ci/** 1899bf215546Sopenharmony_ci * Convert from memory format to blending format 1900bf215546Sopenharmony_ci * 1901bf215546Sopenharmony_ci * e.g. GL_R3G3B2 is 1 byte in memory but 3 bytes for blending 1902bf215546Sopenharmony_ci */ 1903bf215546Sopenharmony_cistatic void 1904bf215546Sopenharmony_ciconvert_to_blend_type(struct gallivm_state *gallivm, 1905bf215546Sopenharmony_ci unsigned block_size, 1906bf215546Sopenharmony_ci const struct util_format_description *src_fmt, 1907bf215546Sopenharmony_ci struct lp_type src_type, 1908bf215546Sopenharmony_ci struct lp_type dst_type, 1909bf215546Sopenharmony_ci LLVMValueRef* src, // and dst 1910bf215546Sopenharmony_ci unsigned num_srcs) 1911bf215546Sopenharmony_ci{ 1912bf215546Sopenharmony_ci LLVMValueRef *dst = src; 1913bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 1914bf215546Sopenharmony_ci struct lp_type blend_type; 1915bf215546Sopenharmony_ci struct lp_type mem_type; 1916bf215546Sopenharmony_ci unsigned i, j; 1917bf215546Sopenharmony_ci unsigned pixels = block_size / num_srcs; 1918bf215546Sopenharmony_ci bool is_arith; 1919bf215546Sopenharmony_ci 1920bf215546Sopenharmony_ci /* 1921bf215546Sopenharmony_ci * full custom path for packed floats and srgb formats - none of the later 1922bf215546Sopenharmony_ci * functions would do anything useful, and given the lp_type representation they 1923bf215546Sopenharmony_ci * can't be fixed. Should really have some SoA blend path for these kind of 1924bf215546Sopenharmony_ci * formats rather than hacking them in here. 1925bf215546Sopenharmony_ci */ 1926bf215546Sopenharmony_ci if (format_expands_to_float_soa(src_fmt)) { 1927bf215546Sopenharmony_ci LLVMValueRef tmpsrc[4]; 1928bf215546Sopenharmony_ci /* 1929bf215546Sopenharmony_ci * This is pretty suboptimal for this case blending in SoA would be much 1930bf215546Sopenharmony_ci * better, since conversion gets us SoA values so need to convert back. 1931bf215546Sopenharmony_ci */ 1932bf215546Sopenharmony_ci assert(src_type.width == 32 || src_type.width == 16); 1933bf215546Sopenharmony_ci assert(dst_type.floating); 1934bf215546Sopenharmony_ci assert(dst_type.width == 32); 1935bf215546Sopenharmony_ci assert(dst_type.length % 4 == 0); 1936bf215546Sopenharmony_ci assert(num_srcs % 4 == 0); 1937bf215546Sopenharmony_ci 1938bf215546Sopenharmony_ci if (src_type.width == 16) { 1939bf215546Sopenharmony_ci /* expand 4x16bit values to 4x32bit */ 1940bf215546Sopenharmony_ci struct lp_type type32x4 = src_type; 1941bf215546Sopenharmony_ci LLVMTypeRef ltype32x4; 1942bf215546Sopenharmony_ci unsigned num_fetch = dst_type.length == 8 ? num_srcs / 2 : num_srcs / 4; 1943bf215546Sopenharmony_ci type32x4.width = 32; 1944bf215546Sopenharmony_ci ltype32x4 = lp_build_vec_type(gallivm, type32x4); 1945bf215546Sopenharmony_ci for (i = 0; i < num_fetch; i++) { 1946bf215546Sopenharmony_ci src[i] = LLVMBuildZExt(builder, src[i], ltype32x4, ""); 1947bf215546Sopenharmony_ci } 1948bf215546Sopenharmony_ci src_type.width = 32; 1949bf215546Sopenharmony_ci } 1950bf215546Sopenharmony_ci for (i = 0; i < 4; i++) { 1951bf215546Sopenharmony_ci tmpsrc[i] = src[i]; 1952bf215546Sopenharmony_ci } 1953bf215546Sopenharmony_ci for (i = 0; i < num_srcs / 4; i++) { 1954bf215546Sopenharmony_ci LLVMValueRef tmpsoa[4]; 1955bf215546Sopenharmony_ci LLVMValueRef tmps = tmpsrc[i]; 1956bf215546Sopenharmony_ci if (dst_type.length == 8) { 1957bf215546Sopenharmony_ci LLVMValueRef shuffles[8]; 1958bf215546Sopenharmony_ci unsigned j; 1959bf215546Sopenharmony_ci /* fetch was 4 values but need 8-wide output values */ 1960bf215546Sopenharmony_ci tmps = lp_build_concat(gallivm, &tmpsrc[i * 2], src_type, 2); 1961bf215546Sopenharmony_ci /* 1962bf215546Sopenharmony_ci * for 8-wide aos transpose would give us wrong order not matching 1963bf215546Sopenharmony_ci * incoming converted fs values and mask. ARGH. 1964bf215546Sopenharmony_ci */ 1965bf215546Sopenharmony_ci for (j = 0; j < 4; j++) { 1966bf215546Sopenharmony_ci shuffles[j] = lp_build_const_int32(gallivm, j * 2); 1967bf215546Sopenharmony_ci shuffles[j + 4] = lp_build_const_int32(gallivm, j * 2 + 1); 1968bf215546Sopenharmony_ci } 1969bf215546Sopenharmony_ci tmps = LLVMBuildShuffleVector(builder, tmps, tmps, 1970bf215546Sopenharmony_ci LLVMConstVector(shuffles, 8), ""); 1971bf215546Sopenharmony_ci } 1972bf215546Sopenharmony_ci if (src_fmt->format == PIPE_FORMAT_R11G11B10_FLOAT) { 1973bf215546Sopenharmony_ci lp_build_r11g11b10_to_float(gallivm, tmps, tmpsoa); 1974bf215546Sopenharmony_ci } 1975bf215546Sopenharmony_ci else { 1976bf215546Sopenharmony_ci lp_build_unpack_rgba_soa(gallivm, src_fmt, dst_type, tmps, tmpsoa); 1977bf215546Sopenharmony_ci } 1978bf215546Sopenharmony_ci lp_build_transpose_aos(gallivm, dst_type, tmpsoa, &src[i * 4]); 1979bf215546Sopenharmony_ci } 1980bf215546Sopenharmony_ci return; 1981bf215546Sopenharmony_ci } 1982bf215546Sopenharmony_ci 1983bf215546Sopenharmony_ci lp_mem_type_from_format_desc(src_fmt, &mem_type); 1984bf215546Sopenharmony_ci lp_blend_type_from_format_desc(src_fmt, &blend_type); 1985bf215546Sopenharmony_ci 1986bf215546Sopenharmony_ci /* Is the format arithmetic */ 1987bf215546Sopenharmony_ci is_arith = blend_type.length * blend_type.width != mem_type.width * mem_type.length; 1988bf215546Sopenharmony_ci is_arith &= !(mem_type.width == 16 && mem_type.floating); 1989bf215546Sopenharmony_ci 1990bf215546Sopenharmony_ci /* Pad if necessary */ 1991bf215546Sopenharmony_ci if (!is_arith && src_type.length < dst_type.length) { 1992bf215546Sopenharmony_ci for (i = 0; i < num_srcs; ++i) { 1993bf215546Sopenharmony_ci dst[i] = lp_build_pad_vector(gallivm, src[i], dst_type.length); 1994bf215546Sopenharmony_ci } 1995bf215546Sopenharmony_ci 1996bf215546Sopenharmony_ci src_type.length = dst_type.length; 1997bf215546Sopenharmony_ci } 1998bf215546Sopenharmony_ci 1999bf215546Sopenharmony_ci /* Special case for half-floats */ 2000bf215546Sopenharmony_ci if (mem_type.width == 16 && mem_type.floating) { 2001bf215546Sopenharmony_ci assert(blend_type.width == 32 && blend_type.floating); 2002bf215546Sopenharmony_ci lp_build_conv_auto(gallivm, src_type, &dst_type, dst, num_srcs, dst); 2003bf215546Sopenharmony_ci is_arith = false; 2004bf215546Sopenharmony_ci } 2005bf215546Sopenharmony_ci 2006bf215546Sopenharmony_ci if (!is_arith) { 2007bf215546Sopenharmony_ci return; 2008bf215546Sopenharmony_ci } 2009bf215546Sopenharmony_ci 2010bf215546Sopenharmony_ci src_type.width = blend_type.width * blend_type.length; 2011bf215546Sopenharmony_ci blend_type.length *= pixels; 2012bf215546Sopenharmony_ci src_type.length *= pixels / (src_type.length / mem_type.length); 2013bf215546Sopenharmony_ci 2014bf215546Sopenharmony_ci for (i = 0; i < num_srcs; ++i) { 2015bf215546Sopenharmony_ci LLVMValueRef chans; 2016bf215546Sopenharmony_ci LLVMValueRef res = NULL; 2017bf215546Sopenharmony_ci 2018bf215546Sopenharmony_ci dst[i] = LLVMBuildZExt(builder, src[i], lp_build_vec_type(gallivm, src_type), ""); 2019bf215546Sopenharmony_ci 2020bf215546Sopenharmony_ci for (j = 0; j < src_fmt->nr_channels; ++j) { 2021bf215546Sopenharmony_ci unsigned mask = 0; 2022bf215546Sopenharmony_ci unsigned sa = src_fmt->channel[j].shift; 2023bf215546Sopenharmony_ci#if UTIL_ARCH_LITTLE_ENDIAN 2024bf215546Sopenharmony_ci unsigned from_lsb = j; 2025bf215546Sopenharmony_ci#else 2026bf215546Sopenharmony_ci unsigned from_lsb = (blend_type.length / pixels) - j - 1; 2027bf215546Sopenharmony_ci#endif 2028bf215546Sopenharmony_ci 2029bf215546Sopenharmony_ci mask = (1 << src_fmt->channel[j].size) - 1; 2030bf215546Sopenharmony_ci 2031bf215546Sopenharmony_ci /* Extract bits from source */ 2032bf215546Sopenharmony_ci chans = LLVMBuildLShr(builder, 2033bf215546Sopenharmony_ci dst[i], 2034bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, sa), 2035bf215546Sopenharmony_ci ""); 2036bf215546Sopenharmony_ci 2037bf215546Sopenharmony_ci chans = LLVMBuildAnd(builder, 2038bf215546Sopenharmony_ci chans, 2039bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, mask), 2040bf215546Sopenharmony_ci ""); 2041bf215546Sopenharmony_ci 2042bf215546Sopenharmony_ci /* Scale bits */ 2043bf215546Sopenharmony_ci if (src_type.norm) { 2044bf215546Sopenharmony_ci chans = scale_bits(gallivm, src_fmt->channel[j].size, 2045bf215546Sopenharmony_ci blend_type.width, chans, src_type); 2046bf215546Sopenharmony_ci } 2047bf215546Sopenharmony_ci 2048bf215546Sopenharmony_ci /* Insert bits into correct position */ 2049bf215546Sopenharmony_ci chans = LLVMBuildShl(builder, 2050bf215546Sopenharmony_ci chans, 2051bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, from_lsb * blend_type.width), 2052bf215546Sopenharmony_ci ""); 2053bf215546Sopenharmony_ci 2054bf215546Sopenharmony_ci if (j == 0) { 2055bf215546Sopenharmony_ci res = chans; 2056bf215546Sopenharmony_ci } else { 2057bf215546Sopenharmony_ci res = LLVMBuildOr(builder, res, chans, ""); 2058bf215546Sopenharmony_ci } 2059bf215546Sopenharmony_ci } 2060bf215546Sopenharmony_ci 2061bf215546Sopenharmony_ci dst[i] = LLVMBuildBitCast(builder, res, lp_build_vec_type(gallivm, blend_type), ""); 2062bf215546Sopenharmony_ci } 2063bf215546Sopenharmony_ci} 2064bf215546Sopenharmony_ci 2065bf215546Sopenharmony_ci 2066bf215546Sopenharmony_ci/** 2067bf215546Sopenharmony_ci * Convert from blending format to memory format 2068bf215546Sopenharmony_ci * 2069bf215546Sopenharmony_ci * e.g. GL_R3G3B2 is 3 bytes for blending but 1 byte in memory 2070bf215546Sopenharmony_ci */ 2071bf215546Sopenharmony_cistatic void 2072bf215546Sopenharmony_ciconvert_from_blend_type(struct gallivm_state *gallivm, 2073bf215546Sopenharmony_ci unsigned block_size, 2074bf215546Sopenharmony_ci const struct util_format_description *src_fmt, 2075bf215546Sopenharmony_ci struct lp_type src_type, 2076bf215546Sopenharmony_ci struct lp_type dst_type, 2077bf215546Sopenharmony_ci LLVMValueRef* src, // and dst 2078bf215546Sopenharmony_ci unsigned num_srcs) 2079bf215546Sopenharmony_ci{ 2080bf215546Sopenharmony_ci LLVMValueRef* dst = src; 2081bf215546Sopenharmony_ci unsigned i, j, k; 2082bf215546Sopenharmony_ci struct lp_type mem_type; 2083bf215546Sopenharmony_ci struct lp_type blend_type; 2084bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 2085bf215546Sopenharmony_ci unsigned pixels = block_size / num_srcs; 2086bf215546Sopenharmony_ci bool is_arith; 2087bf215546Sopenharmony_ci 2088bf215546Sopenharmony_ci /* 2089bf215546Sopenharmony_ci * full custom path for packed floats and srgb formats - none of the later 2090bf215546Sopenharmony_ci * functions would do anything useful, and given the lp_type representation they 2091bf215546Sopenharmony_ci * can't be fixed. Should really have some SoA blend path for these kind of 2092bf215546Sopenharmony_ci * formats rather than hacking them in here. 2093bf215546Sopenharmony_ci */ 2094bf215546Sopenharmony_ci if (format_expands_to_float_soa(src_fmt)) { 2095bf215546Sopenharmony_ci /* 2096bf215546Sopenharmony_ci * This is pretty suboptimal for this case blending in SoA would be much 2097bf215546Sopenharmony_ci * better - we need to transpose the AoS values back to SoA values for 2098bf215546Sopenharmony_ci * conversion/packing. 2099bf215546Sopenharmony_ci */ 2100bf215546Sopenharmony_ci assert(src_type.floating); 2101bf215546Sopenharmony_ci assert(src_type.width == 32); 2102bf215546Sopenharmony_ci assert(src_type.length % 4 == 0); 2103bf215546Sopenharmony_ci assert(dst_type.width == 32 || dst_type.width == 16); 2104bf215546Sopenharmony_ci 2105bf215546Sopenharmony_ci for (i = 0; i < num_srcs / 4; i++) { 2106bf215546Sopenharmony_ci LLVMValueRef tmpsoa[4], tmpdst; 2107bf215546Sopenharmony_ci lp_build_transpose_aos(gallivm, src_type, &src[i * 4], tmpsoa); 2108bf215546Sopenharmony_ci /* really really need SoA here */ 2109bf215546Sopenharmony_ci 2110bf215546Sopenharmony_ci if (src_fmt->format == PIPE_FORMAT_R11G11B10_FLOAT) { 2111bf215546Sopenharmony_ci tmpdst = lp_build_float_to_r11g11b10(gallivm, tmpsoa); 2112bf215546Sopenharmony_ci } 2113bf215546Sopenharmony_ci else { 2114bf215546Sopenharmony_ci tmpdst = lp_build_float_to_srgb_packed(gallivm, src_fmt, 2115bf215546Sopenharmony_ci src_type, tmpsoa); 2116bf215546Sopenharmony_ci } 2117bf215546Sopenharmony_ci 2118bf215546Sopenharmony_ci if (src_type.length == 8) { 2119bf215546Sopenharmony_ci LLVMValueRef tmpaos, shuffles[8]; 2120bf215546Sopenharmony_ci unsigned j; 2121bf215546Sopenharmony_ci /* 2122bf215546Sopenharmony_ci * for 8-wide aos transpose has given us wrong order not matching 2123bf215546Sopenharmony_ci * output order. HMPF. Also need to split the output values manually. 2124bf215546Sopenharmony_ci */ 2125bf215546Sopenharmony_ci for (j = 0; j < 4; j++) { 2126bf215546Sopenharmony_ci shuffles[j * 2] = lp_build_const_int32(gallivm, j); 2127bf215546Sopenharmony_ci shuffles[j * 2 + 1] = lp_build_const_int32(gallivm, j + 4); 2128bf215546Sopenharmony_ci } 2129bf215546Sopenharmony_ci tmpaos = LLVMBuildShuffleVector(builder, tmpdst, tmpdst, 2130bf215546Sopenharmony_ci LLVMConstVector(shuffles, 8), ""); 2131bf215546Sopenharmony_ci src[i * 2] = lp_build_extract_range(gallivm, tmpaos, 0, 4); 2132bf215546Sopenharmony_ci src[i * 2 + 1] = lp_build_extract_range(gallivm, tmpaos, 4, 4); 2133bf215546Sopenharmony_ci } 2134bf215546Sopenharmony_ci else { 2135bf215546Sopenharmony_ci src[i] = tmpdst; 2136bf215546Sopenharmony_ci } 2137bf215546Sopenharmony_ci } 2138bf215546Sopenharmony_ci if (dst_type.width == 16) { 2139bf215546Sopenharmony_ci struct lp_type type16x8 = dst_type; 2140bf215546Sopenharmony_ci struct lp_type type32x4 = dst_type; 2141bf215546Sopenharmony_ci LLVMTypeRef ltype16x4, ltypei64, ltypei128; 2142bf215546Sopenharmony_ci unsigned num_fetch = src_type.length == 8 ? num_srcs / 2 : num_srcs / 4; 2143bf215546Sopenharmony_ci type16x8.length = 8; 2144bf215546Sopenharmony_ci type32x4.width = 32; 2145bf215546Sopenharmony_ci ltypei128 = LLVMIntTypeInContext(gallivm->context, 128); 2146bf215546Sopenharmony_ci ltypei64 = LLVMIntTypeInContext(gallivm->context, 64); 2147bf215546Sopenharmony_ci ltype16x4 = lp_build_vec_type(gallivm, dst_type); 2148bf215546Sopenharmony_ci /* We could do vector truncation but it doesn't generate very good code */ 2149bf215546Sopenharmony_ci for (i = 0; i < num_fetch; i++) { 2150bf215546Sopenharmony_ci src[i] = lp_build_pack2(gallivm, type32x4, type16x8, 2151bf215546Sopenharmony_ci src[i], lp_build_zero(gallivm, type32x4)); 2152bf215546Sopenharmony_ci src[i] = LLVMBuildBitCast(builder, src[i], ltypei128, ""); 2153bf215546Sopenharmony_ci src[i] = LLVMBuildTrunc(builder, src[i], ltypei64, ""); 2154bf215546Sopenharmony_ci src[i] = LLVMBuildBitCast(builder, src[i], ltype16x4, ""); 2155bf215546Sopenharmony_ci } 2156bf215546Sopenharmony_ci } 2157bf215546Sopenharmony_ci return; 2158bf215546Sopenharmony_ci } 2159bf215546Sopenharmony_ci 2160bf215546Sopenharmony_ci lp_mem_type_from_format_desc(src_fmt, &mem_type); 2161bf215546Sopenharmony_ci lp_blend_type_from_format_desc(src_fmt, &blend_type); 2162bf215546Sopenharmony_ci 2163bf215546Sopenharmony_ci is_arith = (blend_type.length * blend_type.width != mem_type.width * mem_type.length); 2164bf215546Sopenharmony_ci 2165bf215546Sopenharmony_ci /* Special case for half-floats */ 2166bf215546Sopenharmony_ci if (mem_type.width == 16 && mem_type.floating) { 2167bf215546Sopenharmony_ci int length = dst_type.length; 2168bf215546Sopenharmony_ci assert(blend_type.width == 32 && blend_type.floating); 2169bf215546Sopenharmony_ci 2170bf215546Sopenharmony_ci dst_type.length = src_type.length; 2171bf215546Sopenharmony_ci 2172bf215546Sopenharmony_ci lp_build_conv_auto(gallivm, src_type, &dst_type, dst, num_srcs, dst); 2173bf215546Sopenharmony_ci 2174bf215546Sopenharmony_ci dst_type.length = length; 2175bf215546Sopenharmony_ci is_arith = false; 2176bf215546Sopenharmony_ci } 2177bf215546Sopenharmony_ci 2178bf215546Sopenharmony_ci /* Remove any padding */ 2179bf215546Sopenharmony_ci if (!is_arith && (src_type.length % mem_type.length)) { 2180bf215546Sopenharmony_ci src_type.length -= (src_type.length % mem_type.length); 2181bf215546Sopenharmony_ci 2182bf215546Sopenharmony_ci for (i = 0; i < num_srcs; ++i) { 2183bf215546Sopenharmony_ci dst[i] = lp_build_extract_range(gallivm, dst[i], 0, src_type.length); 2184bf215546Sopenharmony_ci } 2185bf215546Sopenharmony_ci } 2186bf215546Sopenharmony_ci 2187bf215546Sopenharmony_ci /* No bit arithmetic to do */ 2188bf215546Sopenharmony_ci if (!is_arith) { 2189bf215546Sopenharmony_ci return; 2190bf215546Sopenharmony_ci } 2191bf215546Sopenharmony_ci 2192bf215546Sopenharmony_ci src_type.length = pixels; 2193bf215546Sopenharmony_ci src_type.width = blend_type.length * blend_type.width; 2194bf215546Sopenharmony_ci dst_type.length = pixels; 2195bf215546Sopenharmony_ci 2196bf215546Sopenharmony_ci for (i = 0; i < num_srcs; ++i) { 2197bf215546Sopenharmony_ci LLVMValueRef chans; 2198bf215546Sopenharmony_ci LLVMValueRef res = NULL; 2199bf215546Sopenharmony_ci 2200bf215546Sopenharmony_ci dst[i] = LLVMBuildBitCast(builder, src[i], lp_build_vec_type(gallivm, src_type), ""); 2201bf215546Sopenharmony_ci 2202bf215546Sopenharmony_ci for (j = 0; j < src_fmt->nr_channels; ++j) { 2203bf215546Sopenharmony_ci unsigned mask = 0; 2204bf215546Sopenharmony_ci unsigned sa = src_fmt->channel[j].shift; 2205bf215546Sopenharmony_ci unsigned sz_a = src_fmt->channel[j].size; 2206bf215546Sopenharmony_ci#if UTIL_ARCH_LITTLE_ENDIAN 2207bf215546Sopenharmony_ci unsigned from_lsb = j; 2208bf215546Sopenharmony_ci#else 2209bf215546Sopenharmony_ci unsigned from_lsb = blend_type.length - j - 1; 2210bf215546Sopenharmony_ci#endif 2211bf215546Sopenharmony_ci 2212bf215546Sopenharmony_ci assert(blend_type.width > src_fmt->channel[j].size); 2213bf215546Sopenharmony_ci 2214bf215546Sopenharmony_ci for (k = 0; k < blend_type.width; ++k) { 2215bf215546Sopenharmony_ci mask |= 1 << k; 2216bf215546Sopenharmony_ci } 2217bf215546Sopenharmony_ci 2218bf215546Sopenharmony_ci /* Extract bits */ 2219bf215546Sopenharmony_ci chans = LLVMBuildLShr(builder, 2220bf215546Sopenharmony_ci dst[i], 2221bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, 2222bf215546Sopenharmony_ci from_lsb * blend_type.width), 2223bf215546Sopenharmony_ci ""); 2224bf215546Sopenharmony_ci 2225bf215546Sopenharmony_ci chans = LLVMBuildAnd(builder, 2226bf215546Sopenharmony_ci chans, 2227bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, mask), 2228bf215546Sopenharmony_ci ""); 2229bf215546Sopenharmony_ci 2230bf215546Sopenharmony_ci /* Scale down bits */ 2231bf215546Sopenharmony_ci if (src_type.norm) { 2232bf215546Sopenharmony_ci chans = scale_bits(gallivm, blend_type.width, 2233bf215546Sopenharmony_ci src_fmt->channel[j].size, chans, src_type); 2234bf215546Sopenharmony_ci } else if (!src_type.floating && sz_a < blend_type.width) { 2235bf215546Sopenharmony_ci LLVMValueRef mask_val = lp_build_const_int_vec(gallivm, src_type, (1UL << sz_a) - 1); 2236bf215546Sopenharmony_ci LLVMValueRef mask = LLVMBuildICmp(builder, LLVMIntUGT, chans, mask_val, ""); 2237bf215546Sopenharmony_ci chans = LLVMBuildSelect(builder, mask, mask_val, chans, ""); 2238bf215546Sopenharmony_ci } 2239bf215546Sopenharmony_ci 2240bf215546Sopenharmony_ci /* Insert bits */ 2241bf215546Sopenharmony_ci chans = LLVMBuildShl(builder, 2242bf215546Sopenharmony_ci chans, 2243bf215546Sopenharmony_ci lp_build_const_int_vec(gallivm, src_type, sa), 2244bf215546Sopenharmony_ci ""); 2245bf215546Sopenharmony_ci 2246bf215546Sopenharmony_ci sa += src_fmt->channel[j].size; 2247bf215546Sopenharmony_ci 2248bf215546Sopenharmony_ci if (j == 0) { 2249bf215546Sopenharmony_ci res = chans; 2250bf215546Sopenharmony_ci } else { 2251bf215546Sopenharmony_ci res = LLVMBuildOr(builder, res, chans, ""); 2252bf215546Sopenharmony_ci } 2253bf215546Sopenharmony_ci } 2254bf215546Sopenharmony_ci 2255bf215546Sopenharmony_ci assert (dst_type.width != 24); 2256bf215546Sopenharmony_ci 2257bf215546Sopenharmony_ci dst[i] = LLVMBuildTrunc(builder, res, lp_build_vec_type(gallivm, dst_type), ""); 2258bf215546Sopenharmony_ci } 2259bf215546Sopenharmony_ci} 2260bf215546Sopenharmony_ci 2261bf215546Sopenharmony_ci 2262bf215546Sopenharmony_ci/** 2263bf215546Sopenharmony_ci * Convert alpha to same blend type as src 2264bf215546Sopenharmony_ci */ 2265bf215546Sopenharmony_cistatic void 2266bf215546Sopenharmony_ciconvert_alpha(struct gallivm_state *gallivm, 2267bf215546Sopenharmony_ci struct lp_type row_type, 2268bf215546Sopenharmony_ci struct lp_type alpha_type, 2269bf215546Sopenharmony_ci const unsigned block_size, 2270bf215546Sopenharmony_ci const unsigned block_height, 2271bf215546Sopenharmony_ci const unsigned src_count, 2272bf215546Sopenharmony_ci const unsigned dst_channels, 2273bf215546Sopenharmony_ci const bool pad_inline, 2274bf215546Sopenharmony_ci LLVMValueRef* src_alpha) 2275bf215546Sopenharmony_ci{ 2276bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 2277bf215546Sopenharmony_ci unsigned i, j; 2278bf215546Sopenharmony_ci unsigned length = row_type.length; 2279bf215546Sopenharmony_ci row_type.length = alpha_type.length; 2280bf215546Sopenharmony_ci 2281bf215546Sopenharmony_ci /* Twiddle the alpha to match pixels */ 2282bf215546Sopenharmony_ci lp_bld_quad_twiddle(gallivm, alpha_type, src_alpha, block_height, src_alpha); 2283bf215546Sopenharmony_ci 2284bf215546Sopenharmony_ci /* 2285bf215546Sopenharmony_ci * TODO this should use single lp_build_conv call for 2286bf215546Sopenharmony_ci * src_count == 1 && dst_channels == 1 case (dropping the concat below) 2287bf215546Sopenharmony_ci */ 2288bf215546Sopenharmony_ci for (i = 0; i < block_height; ++i) { 2289bf215546Sopenharmony_ci lp_build_conv(gallivm, alpha_type, row_type, &src_alpha[i], 1, &src_alpha[i], 1); 2290bf215546Sopenharmony_ci } 2291bf215546Sopenharmony_ci 2292bf215546Sopenharmony_ci alpha_type = row_type; 2293bf215546Sopenharmony_ci row_type.length = length; 2294bf215546Sopenharmony_ci 2295bf215546Sopenharmony_ci /* If only one channel we can only need the single alpha value per pixel */ 2296bf215546Sopenharmony_ci if (src_count == 1 && dst_channels == 1) { 2297bf215546Sopenharmony_ci 2298bf215546Sopenharmony_ci lp_build_concat_n(gallivm, alpha_type, src_alpha, block_height, src_alpha, src_count); 2299bf215546Sopenharmony_ci } else { 2300bf215546Sopenharmony_ci /* If there are more srcs than rows then we need to split alpha up */ 2301bf215546Sopenharmony_ci if (src_count > block_height) { 2302bf215546Sopenharmony_ci for (i = src_count; i > 0; --i) { 2303bf215546Sopenharmony_ci unsigned pixels = block_size / src_count; 2304bf215546Sopenharmony_ci unsigned idx = i - 1; 2305bf215546Sopenharmony_ci 2306bf215546Sopenharmony_ci src_alpha[idx] = lp_build_extract_range(gallivm, src_alpha[(idx * pixels) / 4], 2307bf215546Sopenharmony_ci (idx * pixels) % 4, pixels); 2308bf215546Sopenharmony_ci } 2309bf215546Sopenharmony_ci } 2310bf215546Sopenharmony_ci 2311bf215546Sopenharmony_ci /* If there is a src for each pixel broadcast the alpha across whole row */ 2312bf215546Sopenharmony_ci if (src_count == block_size) { 2313bf215546Sopenharmony_ci for (i = 0; i < src_count; ++i) { 2314bf215546Sopenharmony_ci src_alpha[i] = lp_build_broadcast(gallivm, 2315bf215546Sopenharmony_ci lp_build_vec_type(gallivm, row_type), src_alpha[i]); 2316bf215546Sopenharmony_ci } 2317bf215546Sopenharmony_ci } else { 2318bf215546Sopenharmony_ci unsigned pixels = block_size / src_count; 2319bf215546Sopenharmony_ci unsigned channels = pad_inline ? TGSI_NUM_CHANNELS : dst_channels; 2320bf215546Sopenharmony_ci unsigned alpha_span = 1; 2321bf215546Sopenharmony_ci LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH]; 2322bf215546Sopenharmony_ci 2323bf215546Sopenharmony_ci /* Check if we need 2 src_alphas for our shuffles */ 2324bf215546Sopenharmony_ci if (pixels > alpha_type.length) { 2325bf215546Sopenharmony_ci alpha_span = 2; 2326bf215546Sopenharmony_ci } 2327bf215546Sopenharmony_ci 2328bf215546Sopenharmony_ci /* Broadcast alpha across all channels, e.g. a1a2 to a1a1a1a1a2a2a2a2 */ 2329bf215546Sopenharmony_ci for (j = 0; j < row_type.length; ++j) { 2330bf215546Sopenharmony_ci if (j < pixels * channels) { 2331bf215546Sopenharmony_ci shuffles[j] = lp_build_const_int32(gallivm, j / channels); 2332bf215546Sopenharmony_ci } else { 2333bf215546Sopenharmony_ci shuffles[j] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); 2334bf215546Sopenharmony_ci } 2335bf215546Sopenharmony_ci } 2336bf215546Sopenharmony_ci 2337bf215546Sopenharmony_ci for (i = 0; i < src_count; ++i) { 2338bf215546Sopenharmony_ci unsigned idx1 = i, idx2 = i; 2339bf215546Sopenharmony_ci 2340bf215546Sopenharmony_ci if (alpha_span > 1){ 2341bf215546Sopenharmony_ci idx1 *= alpha_span; 2342bf215546Sopenharmony_ci idx2 = idx1 + 1; 2343bf215546Sopenharmony_ci } 2344bf215546Sopenharmony_ci 2345bf215546Sopenharmony_ci src_alpha[i] = LLVMBuildShuffleVector(builder, 2346bf215546Sopenharmony_ci src_alpha[idx1], 2347bf215546Sopenharmony_ci src_alpha[idx2], 2348bf215546Sopenharmony_ci LLVMConstVector(shuffles, row_type.length), 2349bf215546Sopenharmony_ci ""); 2350bf215546Sopenharmony_ci } 2351bf215546Sopenharmony_ci } 2352bf215546Sopenharmony_ci } 2353bf215546Sopenharmony_ci} 2354bf215546Sopenharmony_ci 2355bf215546Sopenharmony_ci 2356bf215546Sopenharmony_ci/** 2357bf215546Sopenharmony_ci * Generates the blend function for unswizzled colour buffers 2358bf215546Sopenharmony_ci * Also generates the read & write from colour buffer 2359bf215546Sopenharmony_ci */ 2360bf215546Sopenharmony_cistatic void 2361bf215546Sopenharmony_cigenerate_unswizzled_blend(struct gallivm_state *gallivm, 2362bf215546Sopenharmony_ci unsigned rt, 2363bf215546Sopenharmony_ci struct lp_fragment_shader_variant *variant, 2364bf215546Sopenharmony_ci enum pipe_format out_format, 2365bf215546Sopenharmony_ci unsigned int num_fs, 2366bf215546Sopenharmony_ci struct lp_type fs_type, 2367bf215546Sopenharmony_ci LLVMValueRef* fs_mask, 2368bf215546Sopenharmony_ci LLVMValueRef fs_out_color[PIPE_MAX_COLOR_BUFS][TGSI_NUM_CHANNELS][4], 2369bf215546Sopenharmony_ci LLVMValueRef context_ptr, 2370bf215546Sopenharmony_ci LLVMValueRef color_ptr, 2371bf215546Sopenharmony_ci LLVMValueRef stride, 2372bf215546Sopenharmony_ci unsigned partial_mask, 2373bf215546Sopenharmony_ci boolean do_branch) 2374bf215546Sopenharmony_ci{ 2375bf215546Sopenharmony_ci const unsigned alpha_channel = 3; 2376bf215546Sopenharmony_ci const unsigned block_width = LP_RASTER_BLOCK_SIZE; 2377bf215546Sopenharmony_ci const unsigned block_height = LP_RASTER_BLOCK_SIZE; 2378bf215546Sopenharmony_ci const unsigned block_size = block_width * block_height; 2379bf215546Sopenharmony_ci const unsigned lp_integer_vector_width = 128; 2380bf215546Sopenharmony_ci 2381bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 2382bf215546Sopenharmony_ci LLVMValueRef fs_src[4][TGSI_NUM_CHANNELS]; 2383bf215546Sopenharmony_ci LLVMValueRef fs_src1[4][TGSI_NUM_CHANNELS]; 2384bf215546Sopenharmony_ci LLVMValueRef src_alpha[4 * 4]; 2385bf215546Sopenharmony_ci LLVMValueRef src1_alpha[4 * 4] = { NULL }; 2386bf215546Sopenharmony_ci LLVMValueRef src_mask[4 * 4]; 2387bf215546Sopenharmony_ci LLVMValueRef src[4 * 4]; 2388bf215546Sopenharmony_ci LLVMValueRef src1[4 * 4]; 2389bf215546Sopenharmony_ci LLVMValueRef dst[4 * 4]; 2390bf215546Sopenharmony_ci LLVMValueRef blend_color; 2391bf215546Sopenharmony_ci LLVMValueRef blend_alpha; 2392bf215546Sopenharmony_ci LLVMValueRef i32_zero; 2393bf215546Sopenharmony_ci LLVMValueRef check_mask; 2394bf215546Sopenharmony_ci LLVMValueRef undef_src_val; 2395bf215546Sopenharmony_ci 2396bf215546Sopenharmony_ci struct lp_build_mask_context mask_ctx; 2397bf215546Sopenharmony_ci struct lp_type mask_type; 2398bf215546Sopenharmony_ci struct lp_type blend_type; 2399bf215546Sopenharmony_ci struct lp_type row_type; 2400bf215546Sopenharmony_ci struct lp_type dst_type; 2401bf215546Sopenharmony_ci struct lp_type ls_type; 2402bf215546Sopenharmony_ci 2403bf215546Sopenharmony_ci unsigned char swizzle[TGSI_NUM_CHANNELS]; 2404bf215546Sopenharmony_ci unsigned vector_width; 2405bf215546Sopenharmony_ci unsigned src_channels = TGSI_NUM_CHANNELS; 2406bf215546Sopenharmony_ci unsigned dst_channels; 2407bf215546Sopenharmony_ci unsigned dst_count; 2408bf215546Sopenharmony_ci unsigned src_count; 2409bf215546Sopenharmony_ci 2410bf215546Sopenharmony_ci const struct util_format_description* out_format_desc = util_format_description(out_format); 2411bf215546Sopenharmony_ci 2412bf215546Sopenharmony_ci unsigned dst_alignment; 2413bf215546Sopenharmony_ci 2414bf215546Sopenharmony_ci bool pad_inline = is_arithmetic_format(out_format_desc); 2415bf215546Sopenharmony_ci bool has_alpha = false; 2416bf215546Sopenharmony_ci const boolean dual_source_blend = variant->key.blend.rt[0].blend_enable && 2417bf215546Sopenharmony_ci util_blend_state_is_dual(&variant->key.blend, 0); 2418bf215546Sopenharmony_ci 2419bf215546Sopenharmony_ci const boolean is_1d = variant->key.resource_1d; 2420bf215546Sopenharmony_ci boolean twiddle_after_convert = FALSE; 2421bf215546Sopenharmony_ci unsigned num_fullblock_fs = is_1d ? 2 * num_fs : num_fs; 2422bf215546Sopenharmony_ci LLVMValueRef fpstate = 0; 2423bf215546Sopenharmony_ci 2424bf215546Sopenharmony_ci /* Get type from output format */ 2425bf215546Sopenharmony_ci lp_blend_type_from_format_desc(out_format_desc, &row_type); 2426bf215546Sopenharmony_ci lp_mem_type_from_format_desc(out_format_desc, &dst_type); 2427bf215546Sopenharmony_ci 2428bf215546Sopenharmony_ci /* 2429bf215546Sopenharmony_ci * Technically this code should go into lp_build_smallfloat_to_float 2430bf215546Sopenharmony_ci * and lp_build_float_to_smallfloat but due to the 2431bf215546Sopenharmony_ci * http://llvm.org/bugs/show_bug.cgi?id=6393 2432bf215546Sopenharmony_ci * llvm reorders the mxcsr intrinsics in a way that breaks the code. 2433bf215546Sopenharmony_ci * So the ordering is important here and there shouldn't be any 2434bf215546Sopenharmony_ci * llvm ir instrunctions in this function before 2435bf215546Sopenharmony_ci * this, otherwise half-float format conversions won't work 2436bf215546Sopenharmony_ci * (again due to llvm bug #6393). 2437bf215546Sopenharmony_ci */ 2438bf215546Sopenharmony_ci if (have_smallfloat_format(dst_type, out_format)) { 2439bf215546Sopenharmony_ci /* We need to make sure that denorms are ok for half float 2440bf215546Sopenharmony_ci conversions */ 2441bf215546Sopenharmony_ci fpstate = lp_build_fpstate_get(gallivm); 2442bf215546Sopenharmony_ci lp_build_fpstate_set_denorms_zero(gallivm, FALSE); 2443bf215546Sopenharmony_ci } 2444bf215546Sopenharmony_ci 2445bf215546Sopenharmony_ci mask_type = lp_int32_vec4_type(); 2446bf215546Sopenharmony_ci mask_type.length = fs_type.length; 2447bf215546Sopenharmony_ci 2448bf215546Sopenharmony_ci for (unsigned i = num_fs; i < num_fullblock_fs; i++) { 2449bf215546Sopenharmony_ci fs_mask[i] = lp_build_zero(gallivm, mask_type); 2450bf215546Sopenharmony_ci } 2451bf215546Sopenharmony_ci 2452bf215546Sopenharmony_ci /* Do not bother executing code when mask is empty.. */ 2453bf215546Sopenharmony_ci if (do_branch) { 2454bf215546Sopenharmony_ci check_mask = LLVMConstNull(lp_build_int_vec_type(gallivm, mask_type)); 2455bf215546Sopenharmony_ci 2456bf215546Sopenharmony_ci for (unsigned i = 0; i < num_fullblock_fs; ++i) { 2457bf215546Sopenharmony_ci check_mask = LLVMBuildOr(builder, check_mask, fs_mask[i], ""); 2458bf215546Sopenharmony_ci } 2459bf215546Sopenharmony_ci 2460bf215546Sopenharmony_ci lp_build_mask_begin(&mask_ctx, gallivm, mask_type, check_mask); 2461bf215546Sopenharmony_ci lp_build_mask_check(&mask_ctx); 2462bf215546Sopenharmony_ci } 2463bf215546Sopenharmony_ci 2464bf215546Sopenharmony_ci partial_mask |= !variant->opaque; 2465bf215546Sopenharmony_ci i32_zero = lp_build_const_int32(gallivm, 0); 2466bf215546Sopenharmony_ci 2467bf215546Sopenharmony_ci undef_src_val = lp_build_undef(gallivm, fs_type); 2468bf215546Sopenharmony_ci 2469bf215546Sopenharmony_ci row_type.length = fs_type.length; 2470bf215546Sopenharmony_ci vector_width = dst_type.floating ? lp_native_vector_width : lp_integer_vector_width; 2471bf215546Sopenharmony_ci 2472bf215546Sopenharmony_ci /* Compute correct swizzle and count channels */ 2473bf215546Sopenharmony_ci memset(swizzle, LP_BLD_SWIZZLE_DONTCARE, TGSI_NUM_CHANNELS); 2474bf215546Sopenharmony_ci dst_channels = 0; 2475bf215546Sopenharmony_ci 2476bf215546Sopenharmony_ci for (unsigned i = 0; i < TGSI_NUM_CHANNELS; ++i) { 2477bf215546Sopenharmony_ci /* Ensure channel is used */ 2478bf215546Sopenharmony_ci if (out_format_desc->swizzle[i] >= TGSI_NUM_CHANNELS) { 2479bf215546Sopenharmony_ci continue; 2480bf215546Sopenharmony_ci } 2481bf215546Sopenharmony_ci 2482bf215546Sopenharmony_ci /* Ensure not already written to (happens in case with GL_ALPHA) */ 2483bf215546Sopenharmony_ci if (swizzle[out_format_desc->swizzle[i]] < TGSI_NUM_CHANNELS) { 2484bf215546Sopenharmony_ci continue; 2485bf215546Sopenharmony_ci } 2486bf215546Sopenharmony_ci 2487bf215546Sopenharmony_ci /* Ensure we haven't already found all channels */ 2488bf215546Sopenharmony_ci if (dst_channels >= out_format_desc->nr_channels) { 2489bf215546Sopenharmony_ci continue; 2490bf215546Sopenharmony_ci } 2491bf215546Sopenharmony_ci 2492bf215546Sopenharmony_ci swizzle[out_format_desc->swizzle[i]] = i; 2493bf215546Sopenharmony_ci ++dst_channels; 2494bf215546Sopenharmony_ci 2495bf215546Sopenharmony_ci if (i == alpha_channel) { 2496bf215546Sopenharmony_ci has_alpha = true; 2497bf215546Sopenharmony_ci } 2498bf215546Sopenharmony_ci } 2499bf215546Sopenharmony_ci 2500bf215546Sopenharmony_ci if (format_expands_to_float_soa(out_format_desc)) { 2501bf215546Sopenharmony_ci /* 2502bf215546Sopenharmony_ci * the code above can't work for layout_other 2503bf215546Sopenharmony_ci * for srgb it would sort of work but we short-circuit swizzles, etc. 2504bf215546Sopenharmony_ci * as that is done as part of unpack / pack. 2505bf215546Sopenharmony_ci */ 2506bf215546Sopenharmony_ci dst_channels = 4; /* HACK: this is fake 4 really but need it due to transpose stuff later */ 2507bf215546Sopenharmony_ci has_alpha = true; 2508bf215546Sopenharmony_ci swizzle[0] = 0; 2509bf215546Sopenharmony_ci swizzle[1] = 1; 2510bf215546Sopenharmony_ci swizzle[2] = 2; 2511bf215546Sopenharmony_ci swizzle[3] = 3; 2512bf215546Sopenharmony_ci pad_inline = true; /* HACK: prevent rgbxrgbx->rgbrgbxx conversion later */ 2513bf215546Sopenharmony_ci } 2514bf215546Sopenharmony_ci 2515bf215546Sopenharmony_ci /* If 3 channels then pad to include alpha for 4 element transpose */ 2516bf215546Sopenharmony_ci if (dst_channels == 3) { 2517bf215546Sopenharmony_ci assert (!has_alpha); 2518bf215546Sopenharmony_ci for (unsigned i = 0; i < TGSI_NUM_CHANNELS; i++) { 2519bf215546Sopenharmony_ci if (swizzle[i] > TGSI_NUM_CHANNELS) 2520bf215546Sopenharmony_ci swizzle[i] = 3; 2521bf215546Sopenharmony_ci } 2522bf215546Sopenharmony_ci if (out_format_desc->nr_channels == 4) { 2523bf215546Sopenharmony_ci dst_channels = 4; 2524bf215546Sopenharmony_ci /* 2525bf215546Sopenharmony_ci * We use alpha from the color conversion, not separate one. 2526bf215546Sopenharmony_ci * We had to include it for transpose, hence it will get converted 2527bf215546Sopenharmony_ci * too (albeit when doing transpose after conversion, that would 2528bf215546Sopenharmony_ci * no longer be the case necessarily). 2529bf215546Sopenharmony_ci * (It works only with 4 channel dsts, e.g. rgbx formats, because 2530bf215546Sopenharmony_ci * otherwise we really have padding, not alpha, included.) 2531bf215546Sopenharmony_ci */ 2532bf215546Sopenharmony_ci has_alpha = true; 2533bf215546Sopenharmony_ci } 2534bf215546Sopenharmony_ci } 2535bf215546Sopenharmony_ci 2536bf215546Sopenharmony_ci /* 2537bf215546Sopenharmony_ci * Load shader output 2538bf215546Sopenharmony_ci */ 2539bf215546Sopenharmony_ci for (unsigned i = 0; i < num_fullblock_fs; ++i) { 2540bf215546Sopenharmony_ci /* Always load alpha for use in blending */ 2541bf215546Sopenharmony_ci LLVMValueRef alpha; 2542bf215546Sopenharmony_ci if (i < num_fs) { 2543bf215546Sopenharmony_ci alpha = LLVMBuildLoad(builder, fs_out_color[rt][alpha_channel][i], ""); 2544bf215546Sopenharmony_ci } 2545bf215546Sopenharmony_ci else { 2546bf215546Sopenharmony_ci alpha = undef_src_val; 2547bf215546Sopenharmony_ci } 2548bf215546Sopenharmony_ci 2549bf215546Sopenharmony_ci /* Load each channel */ 2550bf215546Sopenharmony_ci for (unsigned j = 0; j < dst_channels; ++j) { 2551bf215546Sopenharmony_ci assert(swizzle[j] < 4); 2552bf215546Sopenharmony_ci if (i < num_fs) { 2553bf215546Sopenharmony_ci fs_src[i][j] = LLVMBuildLoad(builder, fs_out_color[rt][swizzle[j]][i], ""); 2554bf215546Sopenharmony_ci } 2555bf215546Sopenharmony_ci else { 2556bf215546Sopenharmony_ci fs_src[i][j] = undef_src_val; 2557bf215546Sopenharmony_ci } 2558bf215546Sopenharmony_ci } 2559bf215546Sopenharmony_ci 2560bf215546Sopenharmony_ci /* If 3 channels then pad to include alpha for 4 element transpose */ 2561bf215546Sopenharmony_ci /* 2562bf215546Sopenharmony_ci * XXX If we include that here maybe could actually use it instead of 2563bf215546Sopenharmony_ci * separate alpha for blending? 2564bf215546Sopenharmony_ci * (Difficult though we actually convert pad channels, not alpha.) 2565bf215546Sopenharmony_ci */ 2566bf215546Sopenharmony_ci if (dst_channels == 3 && !has_alpha) { 2567bf215546Sopenharmony_ci fs_src[i][3] = alpha; 2568bf215546Sopenharmony_ci } 2569bf215546Sopenharmony_ci 2570bf215546Sopenharmony_ci /* We split the row_mask and row_alpha as we want 128bit interleave */ 2571bf215546Sopenharmony_ci if (fs_type.length == 8) { 2572bf215546Sopenharmony_ci src_mask[i*2 + 0] = lp_build_extract_range(gallivm, fs_mask[i], 2573bf215546Sopenharmony_ci 0, src_channels); 2574bf215546Sopenharmony_ci src_mask[i*2 + 1] = lp_build_extract_range(gallivm, fs_mask[i], 2575bf215546Sopenharmony_ci src_channels, src_channels); 2576bf215546Sopenharmony_ci 2577bf215546Sopenharmony_ci src_alpha[i*2 + 0] = lp_build_extract_range(gallivm, alpha, 0, src_channels); 2578bf215546Sopenharmony_ci src_alpha[i*2 + 1] = lp_build_extract_range(gallivm, alpha, 2579bf215546Sopenharmony_ci src_channels, src_channels); 2580bf215546Sopenharmony_ci } else { 2581bf215546Sopenharmony_ci src_mask[i] = fs_mask[i]; 2582bf215546Sopenharmony_ci src_alpha[i] = alpha; 2583bf215546Sopenharmony_ci } 2584bf215546Sopenharmony_ci } 2585bf215546Sopenharmony_ci if (dual_source_blend) { 2586bf215546Sopenharmony_ci /* same as above except different src/dst, skip masks and comments... */ 2587bf215546Sopenharmony_ci for (unsigned i = 0; i < num_fullblock_fs; ++i) { 2588bf215546Sopenharmony_ci LLVMValueRef alpha; 2589bf215546Sopenharmony_ci if (i < num_fs) { 2590bf215546Sopenharmony_ci alpha = LLVMBuildLoad(builder, fs_out_color[1][alpha_channel][i], ""); 2591bf215546Sopenharmony_ci } 2592bf215546Sopenharmony_ci else { 2593bf215546Sopenharmony_ci alpha = undef_src_val; 2594bf215546Sopenharmony_ci } 2595bf215546Sopenharmony_ci 2596bf215546Sopenharmony_ci for (unsigned j = 0; j < dst_channels; ++j) { 2597bf215546Sopenharmony_ci assert(swizzle[j] < 4); 2598bf215546Sopenharmony_ci if (i < num_fs) { 2599bf215546Sopenharmony_ci fs_src1[i][j] = LLVMBuildLoad(builder, fs_out_color[1][swizzle[j]][i], ""); 2600bf215546Sopenharmony_ci } 2601bf215546Sopenharmony_ci else { 2602bf215546Sopenharmony_ci fs_src1[i][j] = undef_src_val; 2603bf215546Sopenharmony_ci } 2604bf215546Sopenharmony_ci } 2605bf215546Sopenharmony_ci if (dst_channels == 3 && !has_alpha) { 2606bf215546Sopenharmony_ci fs_src1[i][3] = alpha; 2607bf215546Sopenharmony_ci } 2608bf215546Sopenharmony_ci if (fs_type.length == 8) { 2609bf215546Sopenharmony_ci src1_alpha[i*2 + 0] = lp_build_extract_range(gallivm, alpha, 0, src_channels); 2610bf215546Sopenharmony_ci src1_alpha[i*2 + 1] = lp_build_extract_range(gallivm, alpha, 2611bf215546Sopenharmony_ci src_channels, src_channels); 2612bf215546Sopenharmony_ci } else { 2613bf215546Sopenharmony_ci src1_alpha[i] = alpha; 2614bf215546Sopenharmony_ci } 2615bf215546Sopenharmony_ci } 2616bf215546Sopenharmony_ci } 2617bf215546Sopenharmony_ci 2618bf215546Sopenharmony_ci if (util_format_is_pure_integer(out_format)) { 2619bf215546Sopenharmony_ci /* 2620bf215546Sopenharmony_ci * In this case fs_type was really ints or uints disguised as floats, 2621bf215546Sopenharmony_ci * fix that up now. 2622bf215546Sopenharmony_ci */ 2623bf215546Sopenharmony_ci fs_type.floating = 0; 2624bf215546Sopenharmony_ci fs_type.sign = dst_type.sign; 2625bf215546Sopenharmony_ci for (unsigned i = 0; i < num_fullblock_fs; ++i) { 2626bf215546Sopenharmony_ci for (unsigned j = 0; j < dst_channels; ++j) { 2627bf215546Sopenharmony_ci fs_src[i][j] = LLVMBuildBitCast(builder, fs_src[i][j], 2628bf215546Sopenharmony_ci lp_build_vec_type(gallivm, fs_type), ""); 2629bf215546Sopenharmony_ci } 2630bf215546Sopenharmony_ci if (dst_channels == 3 && !has_alpha) { 2631bf215546Sopenharmony_ci fs_src[i][3] = LLVMBuildBitCast(builder, fs_src[i][3], 2632bf215546Sopenharmony_ci lp_build_vec_type(gallivm, fs_type), ""); 2633bf215546Sopenharmony_ci } 2634bf215546Sopenharmony_ci } 2635bf215546Sopenharmony_ci } 2636bf215546Sopenharmony_ci 2637bf215546Sopenharmony_ci /* 2638bf215546Sopenharmony_ci * We actually should generally do conversion first (for non-1d cases) 2639bf215546Sopenharmony_ci * when the blend format is 8 or 16 bits. The reason is obvious, 2640bf215546Sopenharmony_ci * there's 2 or 4 times less vectors to deal with for the interleave... 2641bf215546Sopenharmony_ci * Albeit for the AVX (not AVX2) case there's no benefit with 16 bit 2642bf215546Sopenharmony_ci * vectors (as it can do 32bit unpack with 256bit vectors, but 8/16bit 2643bf215546Sopenharmony_ci * unpack only with 128bit vectors). 2644bf215546Sopenharmony_ci * Note: for 16bit sizes really need matching pack conversion code 2645bf215546Sopenharmony_ci */ 2646bf215546Sopenharmony_ci if (!is_1d && dst_channels != 3 && dst_type.width == 8) { 2647bf215546Sopenharmony_ci twiddle_after_convert = TRUE; 2648bf215546Sopenharmony_ci } 2649bf215546Sopenharmony_ci 2650bf215546Sopenharmony_ci /* 2651bf215546Sopenharmony_ci * Pixel twiddle from fragment shader order to memory order 2652bf215546Sopenharmony_ci */ 2653bf215546Sopenharmony_ci if (!twiddle_after_convert) { 2654bf215546Sopenharmony_ci src_count = generate_fs_twiddle(gallivm, fs_type, num_fullblock_fs, 2655bf215546Sopenharmony_ci dst_channels, fs_src, src, pad_inline); 2656bf215546Sopenharmony_ci if (dual_source_blend) { 2657bf215546Sopenharmony_ci generate_fs_twiddle(gallivm, fs_type, num_fullblock_fs, dst_channels, 2658bf215546Sopenharmony_ci fs_src1, src1, pad_inline); 2659bf215546Sopenharmony_ci } 2660bf215546Sopenharmony_ci } else { 2661bf215546Sopenharmony_ci src_count = num_fullblock_fs * dst_channels; 2662bf215546Sopenharmony_ci /* 2663bf215546Sopenharmony_ci * We reorder things a bit here, so the cases for 4-wide and 8-wide 2664bf215546Sopenharmony_ci * (AVX) turn out the same later when untwiddling/transpose (albeit 2665bf215546Sopenharmony_ci * for true AVX2 path untwiddle needs to be different). 2666bf215546Sopenharmony_ci * For now just order by colors first (so we can use unpack later). 2667bf215546Sopenharmony_ci */ 2668bf215546Sopenharmony_ci for (unsigned j = 0; j < num_fullblock_fs; j++) { 2669bf215546Sopenharmony_ci for (unsigned i = 0; i < dst_channels; i++) { 2670bf215546Sopenharmony_ci src[i*num_fullblock_fs + j] = fs_src[j][i]; 2671bf215546Sopenharmony_ci if (dual_source_blend) { 2672bf215546Sopenharmony_ci src1[i*num_fullblock_fs + j] = fs_src1[j][i]; 2673bf215546Sopenharmony_ci } 2674bf215546Sopenharmony_ci } 2675bf215546Sopenharmony_ci } 2676bf215546Sopenharmony_ci } 2677bf215546Sopenharmony_ci 2678bf215546Sopenharmony_ci src_channels = dst_channels < 3 ? dst_channels : 4; 2679bf215546Sopenharmony_ci if (src_count != num_fullblock_fs * src_channels) { 2680bf215546Sopenharmony_ci unsigned ds = src_count / (num_fullblock_fs * src_channels); 2681bf215546Sopenharmony_ci row_type.length /= ds; 2682bf215546Sopenharmony_ci fs_type.length = row_type.length; 2683bf215546Sopenharmony_ci } 2684bf215546Sopenharmony_ci 2685bf215546Sopenharmony_ci blend_type = row_type; 2686bf215546Sopenharmony_ci mask_type.length = 4; 2687bf215546Sopenharmony_ci 2688bf215546Sopenharmony_ci /* Convert src to row_type */ 2689bf215546Sopenharmony_ci if (dual_source_blend) { 2690bf215546Sopenharmony_ci struct lp_type old_row_type = row_type; 2691bf215546Sopenharmony_ci lp_build_conv_auto(gallivm, fs_type, &row_type, src, src_count, src); 2692bf215546Sopenharmony_ci src_count = lp_build_conv_auto(gallivm, fs_type, &old_row_type, src1, src_count, src1); 2693bf215546Sopenharmony_ci } 2694bf215546Sopenharmony_ci else { 2695bf215546Sopenharmony_ci src_count = lp_build_conv_auto(gallivm, fs_type, &row_type, src, src_count, src); 2696bf215546Sopenharmony_ci } 2697bf215546Sopenharmony_ci 2698bf215546Sopenharmony_ci /* If the rows are not an SSE vector, combine them to become SSE size! */ 2699bf215546Sopenharmony_ci if ((row_type.width * row_type.length) % 128) { 2700bf215546Sopenharmony_ci unsigned bits = row_type.width * row_type.length; 2701bf215546Sopenharmony_ci unsigned combined; 2702bf215546Sopenharmony_ci 2703bf215546Sopenharmony_ci assert(src_count >= (vector_width / bits)); 2704bf215546Sopenharmony_ci 2705bf215546Sopenharmony_ci dst_count = src_count / (vector_width / bits); 2706bf215546Sopenharmony_ci 2707bf215546Sopenharmony_ci combined = lp_build_concat_n(gallivm, row_type, src, src_count, src, dst_count); 2708bf215546Sopenharmony_ci if (dual_source_blend) { 2709bf215546Sopenharmony_ci lp_build_concat_n(gallivm, row_type, src1, src_count, src1, dst_count); 2710bf215546Sopenharmony_ci } 2711bf215546Sopenharmony_ci 2712bf215546Sopenharmony_ci row_type.length *= combined; 2713bf215546Sopenharmony_ci src_count /= combined; 2714bf215546Sopenharmony_ci 2715bf215546Sopenharmony_ci bits = row_type.width * row_type.length; 2716bf215546Sopenharmony_ci assert(bits == 128 || bits == 256); 2717bf215546Sopenharmony_ci } 2718bf215546Sopenharmony_ci 2719bf215546Sopenharmony_ci if (twiddle_after_convert) { 2720bf215546Sopenharmony_ci fs_twiddle_transpose(gallivm, row_type, src, src_count, src); 2721bf215546Sopenharmony_ci if (dual_source_blend) { 2722bf215546Sopenharmony_ci fs_twiddle_transpose(gallivm, row_type, src1, src_count, src1); 2723bf215546Sopenharmony_ci } 2724bf215546Sopenharmony_ci } 2725bf215546Sopenharmony_ci 2726bf215546Sopenharmony_ci /* 2727bf215546Sopenharmony_ci * Blend Colour conversion 2728bf215546Sopenharmony_ci */ 2729bf215546Sopenharmony_ci blend_color = lp_jit_context_f_blend_color(gallivm, context_ptr); 2730bf215546Sopenharmony_ci blend_color = LLVMBuildPointerCast(builder, blend_color, 2731bf215546Sopenharmony_ci LLVMPointerType(lp_build_vec_type(gallivm, fs_type), 0), ""); 2732bf215546Sopenharmony_ci blend_color = LLVMBuildLoad(builder, LLVMBuildGEP(builder, blend_color, 2733bf215546Sopenharmony_ci &i32_zero, 1, ""), ""); 2734bf215546Sopenharmony_ci 2735bf215546Sopenharmony_ci /* Convert */ 2736bf215546Sopenharmony_ci lp_build_conv(gallivm, fs_type, blend_type, &blend_color, 1, &blend_color, 1); 2737bf215546Sopenharmony_ci 2738bf215546Sopenharmony_ci if (out_format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) { 2739bf215546Sopenharmony_ci /* 2740bf215546Sopenharmony_ci * since blending is done with floats, there was no conversion. 2741bf215546Sopenharmony_ci * However, the rules according to fixed point renderbuffers still 2742bf215546Sopenharmony_ci * apply, that is we must clamp inputs to 0.0/1.0. 2743bf215546Sopenharmony_ci * (This would apply to separate alpha conversion too but we currently 2744bf215546Sopenharmony_ci * force has_alpha to be true.) 2745bf215546Sopenharmony_ci * TODO: should skip this with "fake" blend, since post-blend conversion 2746bf215546Sopenharmony_ci * will clamp anyway. 2747bf215546Sopenharmony_ci * TODO: could also skip this if fragment color clamping is enabled. 2748bf215546Sopenharmony_ci * We don't support it natively so it gets baked into the shader 2749bf215546Sopenharmony_ci * however, so can't really tell here. 2750bf215546Sopenharmony_ci */ 2751bf215546Sopenharmony_ci struct lp_build_context f32_bld; 2752bf215546Sopenharmony_ci assert(row_type.floating); 2753bf215546Sopenharmony_ci lp_build_context_init(&f32_bld, gallivm, row_type); 2754bf215546Sopenharmony_ci for (unsigned i = 0; i < src_count; i++) { 2755bf215546Sopenharmony_ci src[i] = lp_build_clamp_zero_one_nanzero(&f32_bld, src[i]); 2756bf215546Sopenharmony_ci } 2757bf215546Sopenharmony_ci if (dual_source_blend) { 2758bf215546Sopenharmony_ci for (unsigned i = 0; i < src_count; i++) { 2759bf215546Sopenharmony_ci src1[i] = lp_build_clamp_zero_one_nanzero(&f32_bld, src1[i]); 2760bf215546Sopenharmony_ci } 2761bf215546Sopenharmony_ci } 2762bf215546Sopenharmony_ci /* probably can't be different than row_type but better safe than sorry... */ 2763bf215546Sopenharmony_ci lp_build_context_init(&f32_bld, gallivm, blend_type); 2764bf215546Sopenharmony_ci blend_color = lp_build_clamp(&f32_bld, blend_color, f32_bld.zero, f32_bld.one); 2765bf215546Sopenharmony_ci } 2766bf215546Sopenharmony_ci 2767bf215546Sopenharmony_ci /* Extract alpha */ 2768bf215546Sopenharmony_ci blend_alpha = lp_build_extract_broadcast(gallivm, blend_type, row_type, blend_color, lp_build_const_int32(gallivm, 3)); 2769bf215546Sopenharmony_ci 2770bf215546Sopenharmony_ci /* Swizzle to appropriate channels, e.g. from RGBA to BGRA BGRA */ 2771bf215546Sopenharmony_ci pad_inline &= (dst_channels * (block_size / src_count) * row_type.width) != vector_width; 2772bf215546Sopenharmony_ci if (pad_inline) { 2773bf215546Sopenharmony_ci /* Use all 4 channels e.g. from RGBA RGBA to RGxx RGxx */ 2774bf215546Sopenharmony_ci blend_color = lp_build_swizzle_aos_n(gallivm, blend_color, swizzle, TGSI_NUM_CHANNELS, row_type.length); 2775bf215546Sopenharmony_ci } else { 2776bf215546Sopenharmony_ci /* Only use dst_channels e.g. RGBA RGBA to RG RG xxxx */ 2777bf215546Sopenharmony_ci blend_color = lp_build_swizzle_aos_n(gallivm, blend_color, swizzle, dst_channels, row_type.length); 2778bf215546Sopenharmony_ci } 2779bf215546Sopenharmony_ci 2780bf215546Sopenharmony_ci /* 2781bf215546Sopenharmony_ci * Mask conversion 2782bf215546Sopenharmony_ci */ 2783bf215546Sopenharmony_ci lp_bld_quad_twiddle(gallivm, mask_type, &src_mask[0], block_height, &src_mask[0]); 2784bf215546Sopenharmony_ci 2785bf215546Sopenharmony_ci if (src_count < block_height) { 2786bf215546Sopenharmony_ci lp_build_concat_n(gallivm, mask_type, src_mask, 4, src_mask, src_count); 2787bf215546Sopenharmony_ci } else if (src_count > block_height) { 2788bf215546Sopenharmony_ci for (unsigned i = src_count; i > 0; --i) { 2789bf215546Sopenharmony_ci unsigned pixels = block_size / src_count; 2790bf215546Sopenharmony_ci unsigned idx = i - 1; 2791bf215546Sopenharmony_ci 2792bf215546Sopenharmony_ci src_mask[idx] = lp_build_extract_range(gallivm, src_mask[(idx * pixels) / 4], 2793bf215546Sopenharmony_ci (idx * pixels) % 4, pixels); 2794bf215546Sopenharmony_ci } 2795bf215546Sopenharmony_ci } 2796bf215546Sopenharmony_ci 2797bf215546Sopenharmony_ci assert(mask_type.width == 32); 2798bf215546Sopenharmony_ci 2799bf215546Sopenharmony_ci for (unsigned i = 0; i < src_count; ++i) { 2800bf215546Sopenharmony_ci unsigned pixels = block_size / src_count; 2801bf215546Sopenharmony_ci unsigned pixel_width = row_type.width * dst_channels; 2802bf215546Sopenharmony_ci 2803bf215546Sopenharmony_ci if (pixel_width == 24) { 2804bf215546Sopenharmony_ci mask_type.width = 8; 2805bf215546Sopenharmony_ci mask_type.length = vector_width / mask_type.width; 2806bf215546Sopenharmony_ci } else { 2807bf215546Sopenharmony_ci mask_type.length = pixels; 2808bf215546Sopenharmony_ci mask_type.width = row_type.width * dst_channels; 2809bf215546Sopenharmony_ci 2810bf215546Sopenharmony_ci /* 2811bf215546Sopenharmony_ci * If mask_type width is smaller than 32bit, this doesn't quite 2812bf215546Sopenharmony_ci * generate the most efficient code (could use some pack). 2813bf215546Sopenharmony_ci */ 2814bf215546Sopenharmony_ci src_mask[i] = LLVMBuildIntCast(builder, src_mask[i], 2815bf215546Sopenharmony_ci lp_build_int_vec_type(gallivm, mask_type), ""); 2816bf215546Sopenharmony_ci 2817bf215546Sopenharmony_ci mask_type.length *= dst_channels; 2818bf215546Sopenharmony_ci mask_type.width /= dst_channels; 2819bf215546Sopenharmony_ci } 2820bf215546Sopenharmony_ci 2821bf215546Sopenharmony_ci src_mask[i] = LLVMBuildBitCast(builder, src_mask[i], 2822bf215546Sopenharmony_ci lp_build_int_vec_type(gallivm, mask_type), ""); 2823bf215546Sopenharmony_ci src_mask[i] = lp_build_pad_vector(gallivm, src_mask[i], row_type.length); 2824bf215546Sopenharmony_ci } 2825bf215546Sopenharmony_ci 2826bf215546Sopenharmony_ci /* 2827bf215546Sopenharmony_ci * Alpha conversion 2828bf215546Sopenharmony_ci */ 2829bf215546Sopenharmony_ci if (!has_alpha) { 2830bf215546Sopenharmony_ci struct lp_type alpha_type = fs_type; 2831bf215546Sopenharmony_ci alpha_type.length = 4; 2832bf215546Sopenharmony_ci convert_alpha(gallivm, row_type, alpha_type, 2833bf215546Sopenharmony_ci block_size, block_height, 2834bf215546Sopenharmony_ci src_count, dst_channels, 2835bf215546Sopenharmony_ci pad_inline, src_alpha); 2836bf215546Sopenharmony_ci if (dual_source_blend) { 2837bf215546Sopenharmony_ci convert_alpha(gallivm, row_type, alpha_type, 2838bf215546Sopenharmony_ci block_size, block_height, 2839bf215546Sopenharmony_ci src_count, dst_channels, 2840bf215546Sopenharmony_ci pad_inline, src1_alpha); 2841bf215546Sopenharmony_ci } 2842bf215546Sopenharmony_ci } 2843bf215546Sopenharmony_ci 2844bf215546Sopenharmony_ci 2845bf215546Sopenharmony_ci /* 2846bf215546Sopenharmony_ci * Load dst from memory 2847bf215546Sopenharmony_ci */ 2848bf215546Sopenharmony_ci if (src_count < block_height) { 2849bf215546Sopenharmony_ci dst_count = block_height; 2850bf215546Sopenharmony_ci } else { 2851bf215546Sopenharmony_ci dst_count = src_count; 2852bf215546Sopenharmony_ci } 2853bf215546Sopenharmony_ci 2854bf215546Sopenharmony_ci dst_type.length *= block_size / dst_count; 2855bf215546Sopenharmony_ci 2856bf215546Sopenharmony_ci if (format_expands_to_float_soa(out_format_desc)) { 2857bf215546Sopenharmony_ci /* 2858bf215546Sopenharmony_ci * we need multiple values at once for the conversion, so can as well 2859bf215546Sopenharmony_ci * load them vectorized here too instead of concatenating later. 2860bf215546Sopenharmony_ci * (Still need concatenation later for 8-wide vectors). 2861bf215546Sopenharmony_ci */ 2862bf215546Sopenharmony_ci dst_count = block_height; 2863bf215546Sopenharmony_ci dst_type.length = block_width; 2864bf215546Sopenharmony_ci } 2865bf215546Sopenharmony_ci 2866bf215546Sopenharmony_ci /* 2867bf215546Sopenharmony_ci * Compute the alignment of the destination pointer in bytes 2868bf215546Sopenharmony_ci * We fetch 1-4 pixels, if the format has pot alignment then those fetches 2869bf215546Sopenharmony_ci * are always aligned by MIN2(16, fetch_width) except for buffers (not 2870bf215546Sopenharmony_ci * 1d tex but can't distinguish here) so need to stick with per-pixel 2871bf215546Sopenharmony_ci * alignment in this case. 2872bf215546Sopenharmony_ci */ 2873bf215546Sopenharmony_ci if (is_1d) { 2874bf215546Sopenharmony_ci dst_alignment = (out_format_desc->block.bits + 7)/(out_format_desc->block.width * 8); 2875bf215546Sopenharmony_ci } 2876bf215546Sopenharmony_ci else { 2877bf215546Sopenharmony_ci dst_alignment = dst_type.length * dst_type.width / 8; 2878bf215546Sopenharmony_ci } 2879bf215546Sopenharmony_ci /* Force power-of-two alignment by extracting only the least-significant-bit */ 2880bf215546Sopenharmony_ci dst_alignment = 1 << (ffs(dst_alignment) - 1); 2881bf215546Sopenharmony_ci /* 2882bf215546Sopenharmony_ci * Resource base and stride pointers are aligned to 16 bytes, so that's 2883bf215546Sopenharmony_ci * the maximum alignment we can guarantee 2884bf215546Sopenharmony_ci */ 2885bf215546Sopenharmony_ci dst_alignment = MIN2(16, dst_alignment); 2886bf215546Sopenharmony_ci 2887bf215546Sopenharmony_ci ls_type = dst_type; 2888bf215546Sopenharmony_ci 2889bf215546Sopenharmony_ci if (dst_count > src_count) { 2890bf215546Sopenharmony_ci if ((dst_type.width == 8 || dst_type.width == 16) && 2891bf215546Sopenharmony_ci util_is_power_of_two_or_zero(dst_type.length) && 2892bf215546Sopenharmony_ci dst_type.length * dst_type.width < 128) { 2893bf215546Sopenharmony_ci /* 2894bf215546Sopenharmony_ci * Never try to load values as 4xi8 which we will then 2895bf215546Sopenharmony_ci * concatenate to larger vectors. This gives llvm a real 2896bf215546Sopenharmony_ci * headache (the problem is the type legalizer (?) will 2897bf215546Sopenharmony_ci * try to load that as 4xi8 zext to 4xi32 to fill the vector, 2898bf215546Sopenharmony_ci * then the shuffles to concatenate are more or less impossible 2899bf215546Sopenharmony_ci * - llvm is easily capable of generating a sequence of 32 2900bf215546Sopenharmony_ci * pextrb/pinsrb instructions for that. Albeit it appears to 2901bf215546Sopenharmony_ci * be fixed in llvm 4.0. So, load and concatenate with 32bit 2902bf215546Sopenharmony_ci * width to avoid the trouble (16bit seems not as bad, llvm 2903bf215546Sopenharmony_ci * probably recognizes the load+shuffle as only one shuffle 2904bf215546Sopenharmony_ci * is necessary, but we can do just the same anyway). 2905bf215546Sopenharmony_ci */ 2906bf215546Sopenharmony_ci ls_type.length = dst_type.length * dst_type.width / 32; 2907bf215546Sopenharmony_ci ls_type.width = 32; 2908bf215546Sopenharmony_ci } 2909bf215546Sopenharmony_ci } 2910bf215546Sopenharmony_ci 2911bf215546Sopenharmony_ci if (is_1d) { 2912bf215546Sopenharmony_ci load_unswizzled_block(gallivm, color_ptr, stride, block_width, 1, 2913bf215546Sopenharmony_ci dst, ls_type, dst_count / 4, dst_alignment); 2914bf215546Sopenharmony_ci for (unsigned i = dst_count / 4; i < dst_count; i++) { 2915bf215546Sopenharmony_ci dst[i] = lp_build_undef(gallivm, ls_type); 2916bf215546Sopenharmony_ci } 2917bf215546Sopenharmony_ci 2918bf215546Sopenharmony_ci } 2919bf215546Sopenharmony_ci else { 2920bf215546Sopenharmony_ci load_unswizzled_block(gallivm, color_ptr, stride, block_width, block_height, 2921bf215546Sopenharmony_ci dst, ls_type, dst_count, dst_alignment); 2922bf215546Sopenharmony_ci } 2923bf215546Sopenharmony_ci 2924bf215546Sopenharmony_ci 2925bf215546Sopenharmony_ci /* 2926bf215546Sopenharmony_ci * Convert from dst/output format to src/blending format. 2927bf215546Sopenharmony_ci * 2928bf215546Sopenharmony_ci * This is necessary as we can only read 1 row from memory at a time, 2929bf215546Sopenharmony_ci * so the minimum dst_count will ever be at this point is 4. 2930bf215546Sopenharmony_ci * 2931bf215546Sopenharmony_ci * With, for example, R8 format you can have all 16 pixels in a 128 bit 2932bf215546Sopenharmony_ci * vector, this will take the 4 dsts and combine them into 1 src so we can 2933bf215546Sopenharmony_ci * perform blending on all 16 pixels in that single vector at once. 2934bf215546Sopenharmony_ci */ 2935bf215546Sopenharmony_ci if (dst_count > src_count) { 2936bf215546Sopenharmony_ci if (ls_type.length != dst_type.length && ls_type.length == 1) { 2937bf215546Sopenharmony_ci LLVMTypeRef elem_type = lp_build_elem_type(gallivm, ls_type); 2938bf215546Sopenharmony_ci LLVMTypeRef ls_vec_type = LLVMVectorType(elem_type, 1); 2939bf215546Sopenharmony_ci for (unsigned i = 0; i < dst_count; i++) { 2940bf215546Sopenharmony_ci dst[i] = LLVMBuildBitCast(builder, dst[i], ls_vec_type, ""); 2941bf215546Sopenharmony_ci } 2942bf215546Sopenharmony_ci } 2943bf215546Sopenharmony_ci 2944bf215546Sopenharmony_ci lp_build_concat_n(gallivm, ls_type, dst, 4, dst, src_count); 2945bf215546Sopenharmony_ci 2946bf215546Sopenharmony_ci if (ls_type.length != dst_type.length) { 2947bf215546Sopenharmony_ci struct lp_type tmp_type = dst_type; 2948bf215546Sopenharmony_ci tmp_type.length = dst_type.length * 4 / src_count; 2949bf215546Sopenharmony_ci for (unsigned i = 0; i < src_count; i++) { 2950bf215546Sopenharmony_ci dst[i] = LLVMBuildBitCast(builder, dst[i], 2951bf215546Sopenharmony_ci lp_build_vec_type(gallivm, tmp_type), ""); 2952bf215546Sopenharmony_ci } 2953bf215546Sopenharmony_ci } 2954bf215546Sopenharmony_ci } 2955bf215546Sopenharmony_ci 2956bf215546Sopenharmony_ci /* 2957bf215546Sopenharmony_ci * Blending 2958bf215546Sopenharmony_ci */ 2959bf215546Sopenharmony_ci /* XXX this is broken for RGB8 formats - 2960bf215546Sopenharmony_ci * they get expanded from 12 to 16 elements (to include alpha) 2961bf215546Sopenharmony_ci * by convert_to_blend_type then reduced to 15 instead of 12 2962bf215546Sopenharmony_ci * by convert_from_blend_type (a simple fix though breaks A8...). 2963bf215546Sopenharmony_ci * R16G16B16 also crashes differently however something going wrong 2964bf215546Sopenharmony_ci * inside llvm handling npot vector sizes seemingly. 2965bf215546Sopenharmony_ci * It seems some cleanup could be done here (like skipping conversion/blend 2966bf215546Sopenharmony_ci * when not needed). 2967bf215546Sopenharmony_ci */ 2968bf215546Sopenharmony_ci convert_to_blend_type(gallivm, block_size, out_format_desc, dst_type, 2969bf215546Sopenharmony_ci row_type, dst, src_count); 2970bf215546Sopenharmony_ci 2971bf215546Sopenharmony_ci /* 2972bf215546Sopenharmony_ci * FIXME: Really should get logic ops / masks out of generic blend / row 2973bf215546Sopenharmony_ci * format. Logic ops will definitely not work on the blend float format 2974bf215546Sopenharmony_ci * used for SRGB here and I think OpenGL expects this to work as expected 2975bf215546Sopenharmony_ci * (that is incoming values converted to srgb then logic op applied). 2976bf215546Sopenharmony_ci */ 2977bf215546Sopenharmony_ci for (unsigned i = 0; i < src_count; ++i) { 2978bf215546Sopenharmony_ci dst[i] = lp_build_blend_aos(gallivm, 2979bf215546Sopenharmony_ci &variant->key.blend, 2980bf215546Sopenharmony_ci out_format, 2981bf215546Sopenharmony_ci row_type, 2982bf215546Sopenharmony_ci rt, 2983bf215546Sopenharmony_ci src[i], 2984bf215546Sopenharmony_ci has_alpha ? NULL : src_alpha[i], 2985bf215546Sopenharmony_ci src1[i], 2986bf215546Sopenharmony_ci has_alpha ? NULL : src1_alpha[i], 2987bf215546Sopenharmony_ci dst[i], 2988bf215546Sopenharmony_ci partial_mask ? src_mask[i] : NULL, 2989bf215546Sopenharmony_ci blend_color, 2990bf215546Sopenharmony_ci has_alpha ? NULL : blend_alpha, 2991bf215546Sopenharmony_ci swizzle, 2992bf215546Sopenharmony_ci pad_inline ? 4 : dst_channels); 2993bf215546Sopenharmony_ci } 2994bf215546Sopenharmony_ci 2995bf215546Sopenharmony_ci convert_from_blend_type(gallivm, block_size, out_format_desc, 2996bf215546Sopenharmony_ci row_type, dst_type, dst, src_count); 2997bf215546Sopenharmony_ci 2998bf215546Sopenharmony_ci /* Split the blend rows back to memory rows */ 2999bf215546Sopenharmony_ci if (dst_count > src_count) { 3000bf215546Sopenharmony_ci row_type.length = dst_type.length * (dst_count / src_count); 3001bf215546Sopenharmony_ci 3002bf215546Sopenharmony_ci if (src_count == 1) { 3003bf215546Sopenharmony_ci dst[1] = lp_build_extract_range(gallivm, dst[0], row_type.length / 2, row_type.length / 2); 3004bf215546Sopenharmony_ci dst[0] = lp_build_extract_range(gallivm, dst[0], 0, row_type.length / 2); 3005bf215546Sopenharmony_ci 3006bf215546Sopenharmony_ci row_type.length /= 2; 3007bf215546Sopenharmony_ci src_count *= 2; 3008bf215546Sopenharmony_ci } 3009bf215546Sopenharmony_ci 3010bf215546Sopenharmony_ci dst[3] = lp_build_extract_range(gallivm, dst[1], row_type.length / 2, row_type.length / 2); 3011bf215546Sopenharmony_ci dst[2] = lp_build_extract_range(gallivm, dst[1], 0, row_type.length / 2); 3012bf215546Sopenharmony_ci dst[1] = lp_build_extract_range(gallivm, dst[0], row_type.length / 2, row_type.length / 2); 3013bf215546Sopenharmony_ci dst[0] = lp_build_extract_range(gallivm, dst[0], 0, row_type.length / 2); 3014bf215546Sopenharmony_ci 3015bf215546Sopenharmony_ci row_type.length /= 2; 3016bf215546Sopenharmony_ci src_count *= 2; 3017bf215546Sopenharmony_ci } 3018bf215546Sopenharmony_ci 3019bf215546Sopenharmony_ci /* 3020bf215546Sopenharmony_ci * Store blend result to memory 3021bf215546Sopenharmony_ci */ 3022bf215546Sopenharmony_ci if (is_1d) { 3023bf215546Sopenharmony_ci store_unswizzled_block(gallivm, color_ptr, stride, block_width, 1, 3024bf215546Sopenharmony_ci dst, dst_type, dst_count / 4, dst_alignment); 3025bf215546Sopenharmony_ci } 3026bf215546Sopenharmony_ci else { 3027bf215546Sopenharmony_ci store_unswizzled_block(gallivm, color_ptr, stride, block_width, block_height, 3028bf215546Sopenharmony_ci dst, dst_type, dst_count, dst_alignment); 3029bf215546Sopenharmony_ci } 3030bf215546Sopenharmony_ci 3031bf215546Sopenharmony_ci if (have_smallfloat_format(dst_type, out_format)) { 3032bf215546Sopenharmony_ci lp_build_fpstate_set(gallivm, fpstate); 3033bf215546Sopenharmony_ci } 3034bf215546Sopenharmony_ci 3035bf215546Sopenharmony_ci if (do_branch) { 3036bf215546Sopenharmony_ci lp_build_mask_end(&mask_ctx); 3037bf215546Sopenharmony_ci } 3038bf215546Sopenharmony_ci} 3039bf215546Sopenharmony_ci 3040bf215546Sopenharmony_ci 3041bf215546Sopenharmony_ci/** 3042bf215546Sopenharmony_ci * Generate the runtime callable function for the whole fragment pipeline. 3043bf215546Sopenharmony_ci * Note that the function which we generate operates on a block of 16 3044bf215546Sopenharmony_ci * pixels at at time. The block contains 2x2 quads. Each quad contains 3045bf215546Sopenharmony_ci * 2x2 pixels. 3046bf215546Sopenharmony_ci */ 3047bf215546Sopenharmony_cistatic void 3048bf215546Sopenharmony_cigenerate_fragment(struct llvmpipe_context *lp, 3049bf215546Sopenharmony_ci struct lp_fragment_shader *shader, 3050bf215546Sopenharmony_ci struct lp_fragment_shader_variant *variant, 3051bf215546Sopenharmony_ci unsigned partial_mask) 3052bf215546Sopenharmony_ci{ 3053bf215546Sopenharmony_ci assert(partial_mask == RAST_WHOLE || 3054bf215546Sopenharmony_ci partial_mask == RAST_EDGE_TEST); 3055bf215546Sopenharmony_ci 3056bf215546Sopenharmony_ci struct gallivm_state *gallivm = variant->gallivm; 3057bf215546Sopenharmony_ci struct lp_fragment_shader_variant_key *key = &variant->key; 3058bf215546Sopenharmony_ci struct lp_shader_input inputs[PIPE_MAX_SHADER_INPUTS]; 3059bf215546Sopenharmony_ci LLVMTypeRef fs_elem_type; 3060bf215546Sopenharmony_ci LLVMTypeRef blend_vec_type; 3061bf215546Sopenharmony_ci LLVMTypeRef arg_types[15]; 3062bf215546Sopenharmony_ci LLVMTypeRef func_type; 3063bf215546Sopenharmony_ci LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context); 3064bf215546Sopenharmony_ci LLVMTypeRef int8_type = LLVMInt8TypeInContext(gallivm->context); 3065bf215546Sopenharmony_ci LLVMValueRef context_ptr; 3066bf215546Sopenharmony_ci LLVMValueRef x; 3067bf215546Sopenharmony_ci LLVMValueRef y; 3068bf215546Sopenharmony_ci LLVMValueRef a0_ptr; 3069bf215546Sopenharmony_ci LLVMValueRef dadx_ptr; 3070bf215546Sopenharmony_ci LLVMValueRef dady_ptr; 3071bf215546Sopenharmony_ci LLVMValueRef color_ptr_ptr; 3072bf215546Sopenharmony_ci LLVMValueRef stride_ptr; 3073bf215546Sopenharmony_ci LLVMValueRef color_sample_stride_ptr; 3074bf215546Sopenharmony_ci LLVMValueRef depth_ptr; 3075bf215546Sopenharmony_ci LLVMValueRef depth_stride; 3076bf215546Sopenharmony_ci LLVMValueRef depth_sample_stride; 3077bf215546Sopenharmony_ci LLVMValueRef mask_input; 3078bf215546Sopenharmony_ci LLVMValueRef thread_data_ptr; 3079bf215546Sopenharmony_ci LLVMBasicBlockRef block; 3080bf215546Sopenharmony_ci LLVMBuilderRef builder; 3081bf215546Sopenharmony_ci struct lp_build_interp_soa_context interp; 3082bf215546Sopenharmony_ci LLVMValueRef fs_mask[(16 / 4) * LP_MAX_SAMPLES]; 3083bf215546Sopenharmony_ci LLVMValueRef fs_out_color[LP_MAX_SAMPLES][PIPE_MAX_COLOR_BUFS][TGSI_NUM_CHANNELS][16 / 4]; 3084bf215546Sopenharmony_ci LLVMValueRef function; 3085bf215546Sopenharmony_ci LLVMValueRef facing; 3086bf215546Sopenharmony_ci boolean cbuf0_write_all; 3087bf215546Sopenharmony_ci const boolean dual_source_blend = key->blend.rt[0].blend_enable && 3088bf215546Sopenharmony_ci util_blend_state_is_dual(&key->blend, 0); 3089bf215546Sopenharmony_ci 3090bf215546Sopenharmony_ci assert(lp_native_vector_width / 32 >= 4); 3091bf215546Sopenharmony_ci 3092bf215546Sopenharmony_ci /* Adjust color input interpolation according to flatshade state: 3093bf215546Sopenharmony_ci */ 3094bf215546Sopenharmony_ci memcpy(inputs, shader->inputs, shader->info.base.num_inputs * sizeof inputs[0]); 3095bf215546Sopenharmony_ci for (unsigned i = 0; i < shader->info.base.num_inputs; i++) { 3096bf215546Sopenharmony_ci if (inputs[i].interp == LP_INTERP_COLOR) { 3097bf215546Sopenharmony_ci if (key->flatshade) 3098bf215546Sopenharmony_ci inputs[i].interp = LP_INTERP_CONSTANT; 3099bf215546Sopenharmony_ci else 3100bf215546Sopenharmony_ci inputs[i].interp = LP_INTERP_PERSPECTIVE; 3101bf215546Sopenharmony_ci } 3102bf215546Sopenharmony_ci } 3103bf215546Sopenharmony_ci 3104bf215546Sopenharmony_ci /* check if writes to cbuf[0] are to be copied to all cbufs */ 3105bf215546Sopenharmony_ci cbuf0_write_all = 3106bf215546Sopenharmony_ci shader->info.base.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS]; 3107bf215546Sopenharmony_ci 3108bf215546Sopenharmony_ci /* TODO: actually pick these based on the fs and color buffer 3109bf215546Sopenharmony_ci * characteristics. */ 3110bf215546Sopenharmony_ci 3111bf215546Sopenharmony_ci struct lp_type fs_type; 3112bf215546Sopenharmony_ci memset(&fs_type, 0, sizeof fs_type); 3113bf215546Sopenharmony_ci fs_type.floating = TRUE; /* floating point values */ 3114bf215546Sopenharmony_ci fs_type.sign = TRUE; /* values are signed */ 3115bf215546Sopenharmony_ci fs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 3116bf215546Sopenharmony_ci fs_type.width = 32; /* 32-bit float */ 3117bf215546Sopenharmony_ci fs_type.length = MIN2(lp_native_vector_width / 32, 16); /* n*4 elements per vector */ 3118bf215546Sopenharmony_ci 3119bf215546Sopenharmony_ci struct lp_type blend_type; 3120bf215546Sopenharmony_ci memset(&blend_type, 0, sizeof blend_type); 3121bf215546Sopenharmony_ci blend_type.floating = FALSE; /* values are integers */ 3122bf215546Sopenharmony_ci blend_type.sign = FALSE; /* values are unsigned */ 3123bf215546Sopenharmony_ci blend_type.norm = TRUE; /* values are in [0,1] or [-1,1] */ 3124bf215546Sopenharmony_ci blend_type.width = 8; /* 8-bit ubyte values */ 3125bf215546Sopenharmony_ci blend_type.length = 16; /* 16 elements per vector */ 3126bf215546Sopenharmony_ci 3127bf215546Sopenharmony_ci /* 3128bf215546Sopenharmony_ci * Generate the function prototype. Any change here must be reflected in 3129bf215546Sopenharmony_ci * lp_jit.h's lp_jit_frag_func function pointer type, and vice-versa. 3130bf215546Sopenharmony_ci */ 3131bf215546Sopenharmony_ci 3132bf215546Sopenharmony_ci fs_elem_type = lp_build_elem_type(gallivm, fs_type); 3133bf215546Sopenharmony_ci 3134bf215546Sopenharmony_ci blend_vec_type = lp_build_vec_type(gallivm, blend_type); 3135bf215546Sopenharmony_ci 3136bf215546Sopenharmony_ci char func_name[64]; 3137bf215546Sopenharmony_ci snprintf(func_name, sizeof(func_name), "fs_variant_%s", 3138bf215546Sopenharmony_ci partial_mask ? "partial" : "whole"); 3139bf215546Sopenharmony_ci 3140bf215546Sopenharmony_ci arg_types[0] = variant->jit_context_ptr_type; /* context */ 3141bf215546Sopenharmony_ci arg_types[1] = int32_type; /* x */ 3142bf215546Sopenharmony_ci arg_types[2] = int32_type; /* y */ 3143bf215546Sopenharmony_ci arg_types[3] = int32_type; /* facing */ 3144bf215546Sopenharmony_ci arg_types[4] = LLVMPointerType(fs_elem_type, 0); /* a0 */ 3145bf215546Sopenharmony_ci arg_types[5] = LLVMPointerType(fs_elem_type, 0); /* dadx */ 3146bf215546Sopenharmony_ci arg_types[6] = LLVMPointerType(fs_elem_type, 0); /* dady */ 3147bf215546Sopenharmony_ci arg_types[7] = LLVMPointerType(LLVMPointerType(int8_type, 0), 0); /* color */ 3148bf215546Sopenharmony_ci arg_types[8] = LLVMPointerType(int8_type, 0); /* depth */ 3149bf215546Sopenharmony_ci arg_types[9] = LLVMInt64TypeInContext(gallivm->context); /* mask_input */ 3150bf215546Sopenharmony_ci arg_types[10] = variant->jit_thread_data_ptr_type; /* per thread data */ 3151bf215546Sopenharmony_ci arg_types[11] = LLVMPointerType(int32_type, 0); /* stride */ 3152bf215546Sopenharmony_ci arg_types[12] = int32_type; /* depth_stride */ 3153bf215546Sopenharmony_ci arg_types[13] = LLVMPointerType(int32_type, 0); /* color sample strides */ 3154bf215546Sopenharmony_ci arg_types[14] = int32_type; /* depth sample stride */ 3155bf215546Sopenharmony_ci 3156bf215546Sopenharmony_ci func_type = LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context), 3157bf215546Sopenharmony_ci arg_types, ARRAY_SIZE(arg_types), 0); 3158bf215546Sopenharmony_ci 3159bf215546Sopenharmony_ci function = LLVMAddFunction(gallivm->module, func_name, func_type); 3160bf215546Sopenharmony_ci LLVMSetFunctionCallConv(function, LLVMCCallConv); 3161bf215546Sopenharmony_ci 3162bf215546Sopenharmony_ci variant->function[partial_mask] = function; 3163bf215546Sopenharmony_ci 3164bf215546Sopenharmony_ci /* XXX: need to propagate noalias down into color param now we are 3165bf215546Sopenharmony_ci * passing a pointer-to-pointer? 3166bf215546Sopenharmony_ci */ 3167bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(arg_types); ++i) 3168bf215546Sopenharmony_ci if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 3169bf215546Sopenharmony_ci lp_add_function_attr(function, i + 1, LP_FUNC_ATTR_NOALIAS); 3170bf215546Sopenharmony_ci 3171bf215546Sopenharmony_ci if (variant->gallivm->cache->data_size) 3172bf215546Sopenharmony_ci return; 3173bf215546Sopenharmony_ci 3174bf215546Sopenharmony_ci context_ptr = LLVMGetParam(function, 0); 3175bf215546Sopenharmony_ci x = LLVMGetParam(function, 1); 3176bf215546Sopenharmony_ci y = LLVMGetParam(function, 2); 3177bf215546Sopenharmony_ci facing = LLVMGetParam(function, 3); 3178bf215546Sopenharmony_ci a0_ptr = LLVMGetParam(function, 4); 3179bf215546Sopenharmony_ci dadx_ptr = LLVMGetParam(function, 5); 3180bf215546Sopenharmony_ci dady_ptr = LLVMGetParam(function, 6); 3181bf215546Sopenharmony_ci color_ptr_ptr = LLVMGetParam(function, 7); 3182bf215546Sopenharmony_ci depth_ptr = LLVMGetParam(function, 8); 3183bf215546Sopenharmony_ci mask_input = LLVMGetParam(function, 9); 3184bf215546Sopenharmony_ci thread_data_ptr = LLVMGetParam(function, 10); 3185bf215546Sopenharmony_ci stride_ptr = LLVMGetParam(function, 11); 3186bf215546Sopenharmony_ci depth_stride = LLVMGetParam(function, 12); 3187bf215546Sopenharmony_ci color_sample_stride_ptr = LLVMGetParam(function, 13); 3188bf215546Sopenharmony_ci depth_sample_stride = LLVMGetParam(function, 14); 3189bf215546Sopenharmony_ci 3190bf215546Sopenharmony_ci lp_build_name(context_ptr, "context"); 3191bf215546Sopenharmony_ci lp_build_name(x, "x"); 3192bf215546Sopenharmony_ci lp_build_name(y, "y"); 3193bf215546Sopenharmony_ci lp_build_name(a0_ptr, "a0"); 3194bf215546Sopenharmony_ci lp_build_name(dadx_ptr, "dadx"); 3195bf215546Sopenharmony_ci lp_build_name(dady_ptr, "dady"); 3196bf215546Sopenharmony_ci lp_build_name(color_ptr_ptr, "color_ptr_ptr"); 3197bf215546Sopenharmony_ci lp_build_name(depth_ptr, "depth"); 3198bf215546Sopenharmony_ci lp_build_name(mask_input, "mask_input"); 3199bf215546Sopenharmony_ci lp_build_name(thread_data_ptr, "thread_data"); 3200bf215546Sopenharmony_ci lp_build_name(stride_ptr, "stride_ptr"); 3201bf215546Sopenharmony_ci lp_build_name(depth_stride, "depth_stride"); 3202bf215546Sopenharmony_ci lp_build_name(color_sample_stride_ptr, "color_sample_stride_ptr"); 3203bf215546Sopenharmony_ci lp_build_name(depth_sample_stride, "depth_sample_stride"); 3204bf215546Sopenharmony_ci 3205bf215546Sopenharmony_ci /* 3206bf215546Sopenharmony_ci * Function body 3207bf215546Sopenharmony_ci */ 3208bf215546Sopenharmony_ci 3209bf215546Sopenharmony_ci block = LLVMAppendBasicBlockInContext(gallivm->context, function, "entry"); 3210bf215546Sopenharmony_ci builder = gallivm->builder; 3211bf215546Sopenharmony_ci assert(builder); 3212bf215546Sopenharmony_ci LLVMPositionBuilderAtEnd(builder, block); 3213bf215546Sopenharmony_ci 3214bf215546Sopenharmony_ci /* 3215bf215546Sopenharmony_ci * Must not count ps invocations if there's a null shader. 3216bf215546Sopenharmony_ci * (It would be ok to count with null shader if there's d/s tests, 3217bf215546Sopenharmony_ci * but only if there's d/s buffers too, which is different 3218bf215546Sopenharmony_ci * to implicit rasterization disable which must not depend 3219bf215546Sopenharmony_ci * on the d/s buffers.) 3220bf215546Sopenharmony_ci * Could use popcount on mask, but pixel accuracy is not required. 3221bf215546Sopenharmony_ci * Could disable if there's no stats query, but maybe not worth it. 3222bf215546Sopenharmony_ci */ 3223bf215546Sopenharmony_ci if (shader->info.base.num_instructions > 1) { 3224bf215546Sopenharmony_ci LLVMValueRef invocs, val; 3225bf215546Sopenharmony_ci invocs = lp_jit_thread_data_invocations(gallivm, thread_data_ptr); 3226bf215546Sopenharmony_ci val = LLVMBuildLoad(builder, invocs, ""); 3227bf215546Sopenharmony_ci val = LLVMBuildAdd(builder, val, 3228bf215546Sopenharmony_ci LLVMConstInt(LLVMInt64TypeInContext(gallivm->context), 1, 0), 3229bf215546Sopenharmony_ci "invoc_count"); 3230bf215546Sopenharmony_ci LLVMBuildStore(builder, val, invocs); 3231bf215546Sopenharmony_ci } 3232bf215546Sopenharmony_ci 3233bf215546Sopenharmony_ci /* code generated texture sampling */ 3234bf215546Sopenharmony_ci struct lp_build_sampler_soa *sampler = 3235bf215546Sopenharmony_ci lp_llvm_sampler_soa_create(lp_fs_variant_key_samplers(key), 3236bf215546Sopenharmony_ci MAX2(key->nr_samplers, 3237bf215546Sopenharmony_ci key->nr_sampler_views)); 3238bf215546Sopenharmony_ci struct lp_build_image_soa *image = 3239bf215546Sopenharmony_ci lp_llvm_image_soa_create(lp_fs_variant_key_images(key), key->nr_images); 3240bf215546Sopenharmony_ci 3241bf215546Sopenharmony_ci unsigned num_fs = 16 / fs_type.length; /* number of loops per 4x4 stamp */ 3242bf215546Sopenharmony_ci /* for 1d resources only run "upper half" of stamp */ 3243bf215546Sopenharmony_ci if (key->resource_1d) 3244bf215546Sopenharmony_ci num_fs /= 2; 3245bf215546Sopenharmony_ci 3246bf215546Sopenharmony_ci { 3247bf215546Sopenharmony_ci LLVMValueRef num_loop = lp_build_const_int32(gallivm, num_fs); 3248bf215546Sopenharmony_ci LLVMTypeRef mask_type = lp_build_int_vec_type(gallivm, fs_type); 3249bf215546Sopenharmony_ci LLVMValueRef num_loop_samp = 3250bf215546Sopenharmony_ci lp_build_const_int32(gallivm, num_fs * key->coverage_samples); 3251bf215546Sopenharmony_ci LLVMValueRef mask_store = 3252bf215546Sopenharmony_ci lp_build_array_alloca(gallivm, mask_type, 3253bf215546Sopenharmony_ci num_loop_samp, "mask_store"); 3254bf215546Sopenharmony_ci LLVMTypeRef flt_type = LLVMFloatTypeInContext(gallivm->context); 3255bf215546Sopenharmony_ci LLVMValueRef glob_sample_pos = 3256bf215546Sopenharmony_ci LLVMAddGlobal(gallivm->module, 3257bf215546Sopenharmony_ci LLVMArrayType(flt_type, key->coverage_samples * 2), ""); 3258bf215546Sopenharmony_ci LLVMValueRef sample_pos_array; 3259bf215546Sopenharmony_ci 3260bf215546Sopenharmony_ci if (key->multisample && key->coverage_samples == 4) { 3261bf215546Sopenharmony_ci LLVMValueRef sample_pos_arr[8]; 3262bf215546Sopenharmony_ci for (unsigned i = 0; i < 4; i++) { 3263bf215546Sopenharmony_ci sample_pos_arr[i * 2] = LLVMConstReal(flt_type, lp_sample_pos_4x[i][0]); 3264bf215546Sopenharmony_ci sample_pos_arr[i * 2 + 1] = LLVMConstReal(flt_type, lp_sample_pos_4x[i][1]); 3265bf215546Sopenharmony_ci } 3266bf215546Sopenharmony_ci sample_pos_array = LLVMConstArray(LLVMFloatTypeInContext(gallivm->context), sample_pos_arr, 8); 3267bf215546Sopenharmony_ci } else { 3268bf215546Sopenharmony_ci LLVMValueRef sample_pos_arr[2]; 3269bf215546Sopenharmony_ci sample_pos_arr[0] = LLVMConstReal(flt_type, 0.5); 3270bf215546Sopenharmony_ci sample_pos_arr[1] = LLVMConstReal(flt_type, 0.5); 3271bf215546Sopenharmony_ci sample_pos_array = LLVMConstArray(LLVMFloatTypeInContext(gallivm->context), sample_pos_arr, 2); 3272bf215546Sopenharmony_ci } 3273bf215546Sopenharmony_ci LLVMSetInitializer(glob_sample_pos, sample_pos_array); 3274bf215546Sopenharmony_ci 3275bf215546Sopenharmony_ci LLVMValueRef color_store[PIPE_MAX_COLOR_BUFS][TGSI_NUM_CHANNELS]; 3276bf215546Sopenharmony_ci boolean pixel_center_integer = 3277bf215546Sopenharmony_ci shader->info.base.properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER]; 3278bf215546Sopenharmony_ci 3279bf215546Sopenharmony_ci /* 3280bf215546Sopenharmony_ci * The shader input interpolation info is not explicitely baked in the 3281bf215546Sopenharmony_ci * shader key, but everything it derives from (TGSI, and flatshade) is 3282bf215546Sopenharmony_ci * already included in the shader key. 3283bf215546Sopenharmony_ci */ 3284bf215546Sopenharmony_ci lp_build_interp_soa_init(&interp, 3285bf215546Sopenharmony_ci gallivm, 3286bf215546Sopenharmony_ci shader->info.base.num_inputs, 3287bf215546Sopenharmony_ci inputs, 3288bf215546Sopenharmony_ci pixel_center_integer, 3289bf215546Sopenharmony_ci key->coverage_samples, glob_sample_pos, 3290bf215546Sopenharmony_ci num_loop, 3291bf215546Sopenharmony_ci builder, fs_type, 3292bf215546Sopenharmony_ci a0_ptr, dadx_ptr, dady_ptr, 3293bf215546Sopenharmony_ci x, y); 3294bf215546Sopenharmony_ci 3295bf215546Sopenharmony_ci for (unsigned i = 0; i < num_fs; i++) { 3296bf215546Sopenharmony_ci if (key->multisample) { 3297bf215546Sopenharmony_ci LLVMValueRef smask_val = LLVMBuildLoad(builder, lp_jit_context_sample_mask(gallivm, context_ptr), ""); 3298bf215546Sopenharmony_ci 3299bf215546Sopenharmony_ci /* 3300bf215546Sopenharmony_ci * For multisampling, extract the per-sample mask from the 3301bf215546Sopenharmony_ci * incoming 64-bit mask, store to the per sample mask storage. Or 3302bf215546Sopenharmony_ci * all of them together to generate the fragment shader 3303bf215546Sopenharmony_ci * mask. (sample shading TODO). Take the incoming state coverage 3304bf215546Sopenharmony_ci * mask into account. 3305bf215546Sopenharmony_ci */ 3306bf215546Sopenharmony_ci for (unsigned s = 0; s < key->coverage_samples; s++) { 3307bf215546Sopenharmony_ci LLVMValueRef sindexi = lp_build_const_int32(gallivm, i + (s * num_fs)); 3308bf215546Sopenharmony_ci LLVMValueRef sample_mask_ptr = LLVMBuildGEP(builder, mask_store, 3309bf215546Sopenharmony_ci &sindexi, 1, "sample_mask_ptr"); 3310bf215546Sopenharmony_ci LLVMValueRef s_mask = generate_quad_mask(gallivm, fs_type, 3311bf215546Sopenharmony_ci i*fs_type.length/4, s, mask_input); 3312bf215546Sopenharmony_ci 3313bf215546Sopenharmony_ci LLVMValueRef smask_bit = LLVMBuildAnd(builder, smask_val, lp_build_const_int32(gallivm, (1 << s)), ""); 3314bf215546Sopenharmony_ci LLVMValueRef cmp = LLVMBuildICmp(builder, LLVMIntNE, smask_bit, lp_build_const_int32(gallivm, 0), ""); 3315bf215546Sopenharmony_ci smask_bit = LLVMBuildSExt(builder, cmp, int32_type, ""); 3316bf215546Sopenharmony_ci smask_bit = lp_build_broadcast(gallivm, mask_type, smask_bit); 3317bf215546Sopenharmony_ci 3318bf215546Sopenharmony_ci s_mask = LLVMBuildAnd(builder, s_mask, smask_bit, ""); 3319bf215546Sopenharmony_ci LLVMBuildStore(builder, s_mask, sample_mask_ptr); 3320bf215546Sopenharmony_ci } 3321bf215546Sopenharmony_ci } else { 3322bf215546Sopenharmony_ci LLVMValueRef mask; 3323bf215546Sopenharmony_ci LLVMValueRef indexi = lp_build_const_int32(gallivm, i); 3324bf215546Sopenharmony_ci LLVMValueRef mask_ptr = LLVMBuildGEP(builder, mask_store, 3325bf215546Sopenharmony_ci &indexi, 1, "mask_ptr"); 3326bf215546Sopenharmony_ci 3327bf215546Sopenharmony_ci if (partial_mask) { 3328bf215546Sopenharmony_ci mask = generate_quad_mask(gallivm, fs_type, 3329bf215546Sopenharmony_ci i*fs_type.length/4, 0, mask_input); 3330bf215546Sopenharmony_ci } 3331bf215546Sopenharmony_ci else { 3332bf215546Sopenharmony_ci mask = lp_build_const_int_vec(gallivm, fs_type, ~0); 3333bf215546Sopenharmony_ci } 3334bf215546Sopenharmony_ci LLVMBuildStore(builder, mask, mask_ptr); 3335bf215546Sopenharmony_ci } 3336bf215546Sopenharmony_ci } 3337bf215546Sopenharmony_ci 3338bf215546Sopenharmony_ci generate_fs_loop(gallivm, 3339bf215546Sopenharmony_ci shader, key, 3340bf215546Sopenharmony_ci builder, 3341bf215546Sopenharmony_ci fs_type, 3342bf215546Sopenharmony_ci context_ptr, 3343bf215546Sopenharmony_ci glob_sample_pos, 3344bf215546Sopenharmony_ci num_loop, 3345bf215546Sopenharmony_ci &interp, 3346bf215546Sopenharmony_ci sampler, 3347bf215546Sopenharmony_ci image, 3348bf215546Sopenharmony_ci mask_store, /* output */ 3349bf215546Sopenharmony_ci color_store, 3350bf215546Sopenharmony_ci depth_ptr, 3351bf215546Sopenharmony_ci depth_stride, 3352bf215546Sopenharmony_ci depth_sample_stride, 3353bf215546Sopenharmony_ci color_ptr_ptr, 3354bf215546Sopenharmony_ci stride_ptr, 3355bf215546Sopenharmony_ci color_sample_stride_ptr, 3356bf215546Sopenharmony_ci facing, 3357bf215546Sopenharmony_ci thread_data_ptr); 3358bf215546Sopenharmony_ci 3359bf215546Sopenharmony_ci for (unsigned i = 0; i < num_fs; i++) { 3360bf215546Sopenharmony_ci LLVMValueRef ptr; 3361bf215546Sopenharmony_ci for (unsigned s = 0; s < key->coverage_samples; s++) { 3362bf215546Sopenharmony_ci int idx = (i + (s * num_fs)); 3363bf215546Sopenharmony_ci LLVMValueRef sindexi = lp_build_const_int32(gallivm, idx); 3364bf215546Sopenharmony_ci ptr = LLVMBuildGEP(builder, mask_store, &sindexi, 1, ""); 3365bf215546Sopenharmony_ci 3366bf215546Sopenharmony_ci fs_mask[idx] = LLVMBuildLoad(builder, ptr, "smask"); 3367bf215546Sopenharmony_ci } 3368bf215546Sopenharmony_ci 3369bf215546Sopenharmony_ci for (unsigned s = 0; s < key->min_samples; s++) { 3370bf215546Sopenharmony_ci /* This is fucked up need to reorganize things */ 3371bf215546Sopenharmony_ci int idx = s * num_fs + i; 3372bf215546Sopenharmony_ci LLVMValueRef sindexi = lp_build_const_int32(gallivm, idx); 3373bf215546Sopenharmony_ci for (unsigned cbuf = 0; cbuf < key->nr_cbufs; cbuf++) { 3374bf215546Sopenharmony_ci for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 3375bf215546Sopenharmony_ci ptr = LLVMBuildGEP(builder, 3376bf215546Sopenharmony_ci color_store[cbuf * !cbuf0_write_all][chan], 3377bf215546Sopenharmony_ci &sindexi, 1, ""); 3378bf215546Sopenharmony_ci fs_out_color[s][cbuf][chan][i] = ptr; 3379bf215546Sopenharmony_ci } 3380bf215546Sopenharmony_ci } 3381bf215546Sopenharmony_ci if (dual_source_blend) { 3382bf215546Sopenharmony_ci /* only support one dual source blend target hence always use output 1 */ 3383bf215546Sopenharmony_ci for (unsigned chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 3384bf215546Sopenharmony_ci ptr = LLVMBuildGEP(builder, 3385bf215546Sopenharmony_ci color_store[1][chan], 3386bf215546Sopenharmony_ci &sindexi, 1, ""); 3387bf215546Sopenharmony_ci fs_out_color[s][1][chan][i] = ptr; 3388bf215546Sopenharmony_ci } 3389bf215546Sopenharmony_ci } 3390bf215546Sopenharmony_ci } 3391bf215546Sopenharmony_ci } 3392bf215546Sopenharmony_ci } 3393bf215546Sopenharmony_ci 3394bf215546Sopenharmony_ci sampler->destroy(sampler); 3395bf215546Sopenharmony_ci image->destroy(image); 3396bf215546Sopenharmony_ci 3397bf215546Sopenharmony_ci /* Loop over color outputs / color buffers to do blending */ 3398bf215546Sopenharmony_ci for (unsigned cbuf = 0; cbuf < key->nr_cbufs; cbuf++) { 3399bf215546Sopenharmony_ci if (key->cbuf_format[cbuf] != PIPE_FORMAT_NONE) { 3400bf215546Sopenharmony_ci LLVMValueRef color_ptr; 3401bf215546Sopenharmony_ci LLVMValueRef stride; 3402bf215546Sopenharmony_ci LLVMValueRef sample_stride = NULL; 3403bf215546Sopenharmony_ci LLVMValueRef index = lp_build_const_int32(gallivm, cbuf); 3404bf215546Sopenharmony_ci 3405bf215546Sopenharmony_ci boolean do_branch = ((key->depth.enabled 3406bf215546Sopenharmony_ci || key->stencil[0].enabled 3407bf215546Sopenharmony_ci || key->alpha.enabled) 3408bf215546Sopenharmony_ci && !shader->info.base.uses_kill); 3409bf215546Sopenharmony_ci 3410bf215546Sopenharmony_ci color_ptr = LLVMBuildLoad(builder, 3411bf215546Sopenharmony_ci LLVMBuildGEP(builder, color_ptr_ptr, 3412bf215546Sopenharmony_ci &index, 1, ""), 3413bf215546Sopenharmony_ci ""); 3414bf215546Sopenharmony_ci 3415bf215546Sopenharmony_ci stride = LLVMBuildLoad(builder, 3416bf215546Sopenharmony_ci LLVMBuildGEP(builder, stride_ptr, 3417bf215546Sopenharmony_ci &index, 1, ""), 3418bf215546Sopenharmony_ci ""); 3419bf215546Sopenharmony_ci 3420bf215546Sopenharmony_ci if (key->cbuf_nr_samples[cbuf] > 1) 3421bf215546Sopenharmony_ci sample_stride = LLVMBuildLoad(builder, 3422bf215546Sopenharmony_ci LLVMBuildGEP(builder, 3423bf215546Sopenharmony_ci color_sample_stride_ptr, 3424bf215546Sopenharmony_ci &index, 1, ""), ""); 3425bf215546Sopenharmony_ci 3426bf215546Sopenharmony_ci for (unsigned s = 0; s < key->cbuf_nr_samples[cbuf]; s++) { 3427bf215546Sopenharmony_ci unsigned mask_idx = num_fs * (key->multisample ? s : 0); 3428bf215546Sopenharmony_ci unsigned out_idx = key->min_samples == 1 ? 0 : s; 3429bf215546Sopenharmony_ci LLVMValueRef out_ptr = color_ptr;; 3430bf215546Sopenharmony_ci 3431bf215546Sopenharmony_ci if (sample_stride) { 3432bf215546Sopenharmony_ci LLVMValueRef sample_offset = 3433bf215546Sopenharmony_ci LLVMBuildMul(builder, sample_stride, 3434bf215546Sopenharmony_ci lp_build_const_int32(gallivm, s), ""); 3435bf215546Sopenharmony_ci out_ptr = LLVMBuildGEP(builder, out_ptr, &sample_offset, 1, ""); 3436bf215546Sopenharmony_ci } 3437bf215546Sopenharmony_ci out_ptr = LLVMBuildBitCast(builder, out_ptr, 3438bf215546Sopenharmony_ci LLVMPointerType(blend_vec_type, 0), ""); 3439bf215546Sopenharmony_ci 3440bf215546Sopenharmony_ci lp_build_name(out_ptr, "color_ptr%d", cbuf); 3441bf215546Sopenharmony_ci 3442bf215546Sopenharmony_ci generate_unswizzled_blend(gallivm, cbuf, variant, 3443bf215546Sopenharmony_ci key->cbuf_format[cbuf], 3444bf215546Sopenharmony_ci num_fs, fs_type, &fs_mask[mask_idx], 3445bf215546Sopenharmony_ci fs_out_color[out_idx], 3446bf215546Sopenharmony_ci context_ptr, out_ptr, stride, 3447bf215546Sopenharmony_ci partial_mask, do_branch); 3448bf215546Sopenharmony_ci } 3449bf215546Sopenharmony_ci } 3450bf215546Sopenharmony_ci } 3451bf215546Sopenharmony_ci 3452bf215546Sopenharmony_ci LLVMBuildRetVoid(builder); 3453bf215546Sopenharmony_ci 3454bf215546Sopenharmony_ci gallivm_verify_function(gallivm, function); 3455bf215546Sopenharmony_ci} 3456bf215546Sopenharmony_ci 3457bf215546Sopenharmony_ci 3458bf215546Sopenharmony_cistatic void 3459bf215546Sopenharmony_cidump_fs_variant_key(struct lp_fragment_shader_variant_key *key) 3460bf215546Sopenharmony_ci{ 3461bf215546Sopenharmony_ci debug_printf("fs variant %p:\n", (void *) key); 3462bf215546Sopenharmony_ci 3463bf215546Sopenharmony_ci if (key->flatshade) { 3464bf215546Sopenharmony_ci debug_printf("flatshade = 1\n"); 3465bf215546Sopenharmony_ci } 3466bf215546Sopenharmony_ci if (key->depth_clamp) 3467bf215546Sopenharmony_ci debug_printf("depth_clamp = 1\n"); 3468bf215546Sopenharmony_ci 3469bf215546Sopenharmony_ci if (key->restrict_depth_values) 3470bf215546Sopenharmony_ci debug_printf("restrict_depth_values = 1\n"); 3471bf215546Sopenharmony_ci 3472bf215546Sopenharmony_ci if (key->multisample) { 3473bf215546Sopenharmony_ci debug_printf("multisample = 1\n"); 3474bf215546Sopenharmony_ci debug_printf("coverage samples = %d\n", key->coverage_samples); 3475bf215546Sopenharmony_ci debug_printf("min samples = %d\n", key->min_samples); 3476bf215546Sopenharmony_ci } 3477bf215546Sopenharmony_ci for (unsigned i = 0; i < key->nr_cbufs; ++i) { 3478bf215546Sopenharmony_ci debug_printf("cbuf_format[%u] = %s\n", i, util_format_name(key->cbuf_format[i])); 3479bf215546Sopenharmony_ci debug_printf("cbuf nr_samples[%u] = %d\n", i, key->cbuf_nr_samples[i]); 3480bf215546Sopenharmony_ci } 3481bf215546Sopenharmony_ci if (key->depth.enabled || key->stencil[0].enabled) { 3482bf215546Sopenharmony_ci debug_printf("depth.format = %s\n", util_format_name(key->zsbuf_format)); 3483bf215546Sopenharmony_ci debug_printf("depth nr_samples = %d\n", key->zsbuf_nr_samples); 3484bf215546Sopenharmony_ci } 3485bf215546Sopenharmony_ci if (key->depth.enabled) { 3486bf215546Sopenharmony_ci debug_printf("depth.func = %s\n", util_str_func(key->depth.func, TRUE)); 3487bf215546Sopenharmony_ci debug_printf("depth.writemask = %u\n", key->depth.writemask); 3488bf215546Sopenharmony_ci } 3489bf215546Sopenharmony_ci 3490bf215546Sopenharmony_ci for (unsigned i = 0; i < 2; ++i) { 3491bf215546Sopenharmony_ci if (key->stencil[i].enabled) { 3492bf215546Sopenharmony_ci debug_printf("stencil[%u].func = %s\n", i, util_str_func(key->stencil[i].func, TRUE)); 3493bf215546Sopenharmony_ci debug_printf("stencil[%u].fail_op = %s\n", i, util_str_stencil_op(key->stencil[i].fail_op, TRUE)); 3494bf215546Sopenharmony_ci debug_printf("stencil[%u].zpass_op = %s\n", i, util_str_stencil_op(key->stencil[i].zpass_op, TRUE)); 3495bf215546Sopenharmony_ci debug_printf("stencil[%u].zfail_op = %s\n", i, util_str_stencil_op(key->stencil[i].zfail_op, TRUE)); 3496bf215546Sopenharmony_ci debug_printf("stencil[%u].valuemask = 0x%x\n", i, key->stencil[i].valuemask); 3497bf215546Sopenharmony_ci debug_printf("stencil[%u].writemask = 0x%x\n", i, key->stencil[i].writemask); 3498bf215546Sopenharmony_ci } 3499bf215546Sopenharmony_ci } 3500bf215546Sopenharmony_ci 3501bf215546Sopenharmony_ci if (key->alpha.enabled) { 3502bf215546Sopenharmony_ci debug_printf("alpha.func = %s\n", util_str_func(key->alpha.func, TRUE)); 3503bf215546Sopenharmony_ci } 3504bf215546Sopenharmony_ci 3505bf215546Sopenharmony_ci if (key->occlusion_count) { 3506bf215546Sopenharmony_ci debug_printf("occlusion_count = 1\n"); 3507bf215546Sopenharmony_ci } 3508bf215546Sopenharmony_ci 3509bf215546Sopenharmony_ci if (key->blend.logicop_enable) { 3510bf215546Sopenharmony_ci debug_printf("blend.logicop_func = %s\n", util_str_logicop(key->blend.logicop_func, TRUE)); 3511bf215546Sopenharmony_ci } 3512bf215546Sopenharmony_ci else if (key->blend.rt[0].blend_enable) { 3513bf215546Sopenharmony_ci debug_printf("blend.rgb_func = %s\n", util_str_blend_func (key->blend.rt[0].rgb_func, TRUE)); 3514bf215546Sopenharmony_ci debug_printf("blend.rgb_src_factor = %s\n", util_str_blend_factor(key->blend.rt[0].rgb_src_factor, TRUE)); 3515bf215546Sopenharmony_ci debug_printf("blend.rgb_dst_factor = %s\n", util_str_blend_factor(key->blend.rt[0].rgb_dst_factor, TRUE)); 3516bf215546Sopenharmony_ci debug_printf("blend.alpha_func = %s\n", util_str_blend_func (key->blend.rt[0].alpha_func, TRUE)); 3517bf215546Sopenharmony_ci debug_printf("blend.alpha_src_factor = %s\n", util_str_blend_factor(key->blend.rt[0].alpha_src_factor, TRUE)); 3518bf215546Sopenharmony_ci debug_printf("blend.alpha_dst_factor = %s\n", util_str_blend_factor(key->blend.rt[0].alpha_dst_factor, TRUE)); 3519bf215546Sopenharmony_ci } 3520bf215546Sopenharmony_ci debug_printf("blend.colormask = 0x%x\n", key->blend.rt[0].colormask); 3521bf215546Sopenharmony_ci if (key->blend.alpha_to_coverage) { 3522bf215546Sopenharmony_ci debug_printf("blend.alpha_to_coverage is enabled\n"); 3523bf215546Sopenharmony_ci } 3524bf215546Sopenharmony_ci for (unsigned i = 0; i < key->nr_samplers; ++i) { 3525bf215546Sopenharmony_ci const struct lp_sampler_static_state *samplers = lp_fs_variant_key_samplers(key); 3526bf215546Sopenharmony_ci const struct lp_static_sampler_state *sampler = &samplers[i].sampler_state; 3527bf215546Sopenharmony_ci debug_printf("sampler[%u] = \n", i); 3528bf215546Sopenharmony_ci debug_printf(" .wrap = %s %s %s\n", 3529bf215546Sopenharmony_ci util_str_tex_wrap(sampler->wrap_s, TRUE), 3530bf215546Sopenharmony_ci util_str_tex_wrap(sampler->wrap_t, TRUE), 3531bf215546Sopenharmony_ci util_str_tex_wrap(sampler->wrap_r, TRUE)); 3532bf215546Sopenharmony_ci debug_printf(" .min_img_filter = %s\n", 3533bf215546Sopenharmony_ci util_str_tex_filter(sampler->min_img_filter, TRUE)); 3534bf215546Sopenharmony_ci debug_printf(" .min_mip_filter = %s\n", 3535bf215546Sopenharmony_ci util_str_tex_mipfilter(sampler->min_mip_filter, TRUE)); 3536bf215546Sopenharmony_ci debug_printf(" .mag_img_filter = %s\n", 3537bf215546Sopenharmony_ci util_str_tex_filter(sampler->mag_img_filter, TRUE)); 3538bf215546Sopenharmony_ci if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) 3539bf215546Sopenharmony_ci debug_printf(" .compare_func = %s\n", util_str_func(sampler->compare_func, TRUE)); 3540bf215546Sopenharmony_ci debug_printf(" .normalized_coords = %u\n", sampler->normalized_coords); 3541bf215546Sopenharmony_ci debug_printf(" .min_max_lod_equal = %u\n", sampler->min_max_lod_equal); 3542bf215546Sopenharmony_ci debug_printf(" .lod_bias_non_zero = %u\n", sampler->lod_bias_non_zero); 3543bf215546Sopenharmony_ci debug_printf(" .apply_min_lod = %u\n", sampler->apply_min_lod); 3544bf215546Sopenharmony_ci debug_printf(" .apply_max_lod = %u\n", sampler->apply_max_lod); 3545bf215546Sopenharmony_ci debug_printf(" .reduction_mode = %u\n", sampler->reduction_mode); 3546bf215546Sopenharmony_ci debug_printf(" .aniso = %u\n", sampler->aniso); 3547bf215546Sopenharmony_ci } 3548bf215546Sopenharmony_ci for (unsigned i = 0; i < key->nr_sampler_views; ++i) { 3549bf215546Sopenharmony_ci const struct lp_sampler_static_state *samplers = lp_fs_variant_key_samplers(key); 3550bf215546Sopenharmony_ci const struct lp_static_texture_state *texture = &samplers[i].texture_state; 3551bf215546Sopenharmony_ci debug_printf("texture[%u] = \n", i); 3552bf215546Sopenharmony_ci debug_printf(" .format = %s\n", 3553bf215546Sopenharmony_ci util_format_name(texture->format)); 3554bf215546Sopenharmony_ci debug_printf(" .target = %s\n", 3555bf215546Sopenharmony_ci util_str_tex_target(texture->target, TRUE)); 3556bf215546Sopenharmony_ci debug_printf(" .level_zero_only = %u\n", 3557bf215546Sopenharmony_ci texture->level_zero_only); 3558bf215546Sopenharmony_ci debug_printf(" .pot = %u %u %u\n", 3559bf215546Sopenharmony_ci texture->pot_width, 3560bf215546Sopenharmony_ci texture->pot_height, 3561bf215546Sopenharmony_ci texture->pot_depth); 3562bf215546Sopenharmony_ci } 3563bf215546Sopenharmony_ci struct lp_image_static_state *images = lp_fs_variant_key_images(key); 3564bf215546Sopenharmony_ci for (unsigned i = 0; i < key->nr_images; ++i) { 3565bf215546Sopenharmony_ci const struct lp_static_texture_state *image = &images[i].image_state; 3566bf215546Sopenharmony_ci debug_printf("image[%u] = \n", i); 3567bf215546Sopenharmony_ci debug_printf(" .format = %s\n", 3568bf215546Sopenharmony_ci util_format_name(image->format)); 3569bf215546Sopenharmony_ci debug_printf(" .target = %s\n", 3570bf215546Sopenharmony_ci util_str_tex_target(image->target, TRUE)); 3571bf215546Sopenharmony_ci debug_printf(" .level_zero_only = %u\n", 3572bf215546Sopenharmony_ci image->level_zero_only); 3573bf215546Sopenharmony_ci debug_printf(" .pot = %u %u %u\n", 3574bf215546Sopenharmony_ci image->pot_width, 3575bf215546Sopenharmony_ci image->pot_height, 3576bf215546Sopenharmony_ci image->pot_depth); 3577bf215546Sopenharmony_ci } 3578bf215546Sopenharmony_ci} 3579bf215546Sopenharmony_ci 3580bf215546Sopenharmony_ci 3581bf215546Sopenharmony_ciconst char * 3582bf215546Sopenharmony_cilp_debug_fs_kind(enum lp_fs_kind kind) 3583bf215546Sopenharmony_ci{ 3584bf215546Sopenharmony_ci switch (kind) { 3585bf215546Sopenharmony_ci case LP_FS_KIND_GENERAL: 3586bf215546Sopenharmony_ci return "GENERAL"; 3587bf215546Sopenharmony_ci case LP_FS_KIND_BLIT_RGBA: 3588bf215546Sopenharmony_ci return "BLIT_RGBA"; 3589bf215546Sopenharmony_ci case LP_FS_KIND_BLIT_RGB1: 3590bf215546Sopenharmony_ci return "BLIT_RGB1"; 3591bf215546Sopenharmony_ci case LP_FS_KIND_AERO_MINIFICATION: 3592bf215546Sopenharmony_ci return "AERO_MINIFICATION"; 3593bf215546Sopenharmony_ci case LP_FS_KIND_LLVM_LINEAR: 3594bf215546Sopenharmony_ci return "LLVM_LINEAR"; 3595bf215546Sopenharmony_ci default: 3596bf215546Sopenharmony_ci return "unknown"; 3597bf215546Sopenharmony_ci } 3598bf215546Sopenharmony_ci} 3599bf215546Sopenharmony_ci 3600bf215546Sopenharmony_ci 3601bf215546Sopenharmony_civoid 3602bf215546Sopenharmony_cilp_debug_fs_variant(struct lp_fragment_shader_variant *variant) 3603bf215546Sopenharmony_ci{ 3604bf215546Sopenharmony_ci debug_printf("llvmpipe: Fragment shader #%u variant #%u:\n", 3605bf215546Sopenharmony_ci variant->shader->no, variant->no); 3606bf215546Sopenharmony_ci if (variant->shader->base.type == PIPE_SHADER_IR_TGSI) 3607bf215546Sopenharmony_ci tgsi_dump(variant->shader->base.tokens, 0); 3608bf215546Sopenharmony_ci else 3609bf215546Sopenharmony_ci nir_print_shader(variant->shader->base.ir.nir, stderr); 3610bf215546Sopenharmony_ci dump_fs_variant_key(&variant->key); 3611bf215546Sopenharmony_ci debug_printf("variant->opaque = %u\n", variant->opaque); 3612bf215546Sopenharmony_ci debug_printf("variant->potentially_opaque = %u\n", variant->potentially_opaque); 3613bf215546Sopenharmony_ci debug_printf("variant->blit = %u\n", variant->blit); 3614bf215546Sopenharmony_ci debug_printf("shader->kind = %s\n", lp_debug_fs_kind(variant->shader->kind)); 3615bf215546Sopenharmony_ci debug_printf("\n"); 3616bf215546Sopenharmony_ci} 3617bf215546Sopenharmony_ci 3618bf215546Sopenharmony_ci 3619bf215546Sopenharmony_cistatic void 3620bf215546Sopenharmony_cilp_fs_get_ir_cache_key(struct lp_fragment_shader_variant *variant, 3621bf215546Sopenharmony_ci unsigned char ir_sha1_cache_key[20]) 3622bf215546Sopenharmony_ci{ 3623bf215546Sopenharmony_ci struct blob blob = { 0 }; 3624bf215546Sopenharmony_ci unsigned ir_size; 3625bf215546Sopenharmony_ci void *ir_binary; 3626bf215546Sopenharmony_ci 3627bf215546Sopenharmony_ci blob_init(&blob); 3628bf215546Sopenharmony_ci nir_serialize(&blob, variant->shader->base.ir.nir, true); 3629bf215546Sopenharmony_ci ir_binary = blob.data; 3630bf215546Sopenharmony_ci ir_size = blob.size; 3631bf215546Sopenharmony_ci 3632bf215546Sopenharmony_ci struct mesa_sha1 ctx; 3633bf215546Sopenharmony_ci _mesa_sha1_init(&ctx); 3634bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, &variant->key, variant->shader->variant_key_size); 3635bf215546Sopenharmony_ci _mesa_sha1_update(&ctx, ir_binary, ir_size); 3636bf215546Sopenharmony_ci _mesa_sha1_final(&ctx, ir_sha1_cache_key); 3637bf215546Sopenharmony_ci 3638bf215546Sopenharmony_ci blob_finish(&blob); 3639bf215546Sopenharmony_ci} 3640bf215546Sopenharmony_ci 3641bf215546Sopenharmony_ci 3642bf215546Sopenharmony_ci/** 3643bf215546Sopenharmony_ci * Generate a new fragment shader variant from the shader code and 3644bf215546Sopenharmony_ci * other state indicated by the key. 3645bf215546Sopenharmony_ci */ 3646bf215546Sopenharmony_cistatic struct lp_fragment_shader_variant * 3647bf215546Sopenharmony_cigenerate_variant(struct llvmpipe_context *lp, 3648bf215546Sopenharmony_ci struct lp_fragment_shader *shader, 3649bf215546Sopenharmony_ci const struct lp_fragment_shader_variant_key *key) 3650bf215546Sopenharmony_ci{ 3651bf215546Sopenharmony_ci struct lp_fragment_shader_variant *variant = 3652bf215546Sopenharmony_ci MALLOC(sizeof *variant + shader->variant_key_size - sizeof variant->key); 3653bf215546Sopenharmony_ci if (!variant) 3654bf215546Sopenharmony_ci return NULL; 3655bf215546Sopenharmony_ci 3656bf215546Sopenharmony_ci memset(variant, 0, sizeof(*variant)); 3657bf215546Sopenharmony_ci 3658bf215546Sopenharmony_ci pipe_reference_init(&variant->reference, 1); 3659bf215546Sopenharmony_ci lp_fs_reference(lp, &variant->shader, shader); 3660bf215546Sopenharmony_ci 3661bf215546Sopenharmony_ci memcpy(&variant->key, key, shader->variant_key_size); 3662bf215546Sopenharmony_ci 3663bf215546Sopenharmony_ci struct llvmpipe_screen *screen = llvmpipe_screen(lp->pipe.screen); 3664bf215546Sopenharmony_ci struct lp_cached_code cached = { 0 }; 3665bf215546Sopenharmony_ci unsigned char ir_sha1_cache_key[20]; 3666bf215546Sopenharmony_ci bool needs_caching = false; 3667bf215546Sopenharmony_ci if (shader->base.ir.nir) { 3668bf215546Sopenharmony_ci lp_fs_get_ir_cache_key(variant, ir_sha1_cache_key); 3669bf215546Sopenharmony_ci 3670bf215546Sopenharmony_ci lp_disk_cache_find_shader(screen, &cached, ir_sha1_cache_key); 3671bf215546Sopenharmony_ci if (!cached.data_size) 3672bf215546Sopenharmony_ci needs_caching = true; 3673bf215546Sopenharmony_ci } 3674bf215546Sopenharmony_ci 3675bf215546Sopenharmony_ci char module_name[64]; 3676bf215546Sopenharmony_ci snprintf(module_name, sizeof(module_name), "fs%u_variant%u", 3677bf215546Sopenharmony_ci shader->no, shader->variants_created); 3678bf215546Sopenharmony_ci variant->gallivm = gallivm_create(module_name, lp->context, &cached); 3679bf215546Sopenharmony_ci if (!variant->gallivm) { 3680bf215546Sopenharmony_ci FREE(variant); 3681bf215546Sopenharmony_ci return NULL; 3682bf215546Sopenharmony_ci } 3683bf215546Sopenharmony_ci 3684bf215546Sopenharmony_ci variant->list_item_global.base = variant; 3685bf215546Sopenharmony_ci variant->list_item_local.base = variant; 3686bf215546Sopenharmony_ci variant->no = shader->variants_created++; 3687bf215546Sopenharmony_ci 3688bf215546Sopenharmony_ci /* 3689bf215546Sopenharmony_ci * Determine whether we are touching all channels in the color buffer. 3690bf215546Sopenharmony_ci */ 3691bf215546Sopenharmony_ci const struct util_format_description *cbuf0_format_desc = NULL; 3692bf215546Sopenharmony_ci boolean fullcolormask = FALSE; 3693bf215546Sopenharmony_ci if (key->nr_cbufs == 1) { 3694bf215546Sopenharmony_ci cbuf0_format_desc = util_format_description(key->cbuf_format[0]); 3695bf215546Sopenharmony_ci fullcolormask = util_format_colormask_full(cbuf0_format_desc, 3696bf215546Sopenharmony_ci key->blend.rt[0].colormask); 3697bf215546Sopenharmony_ci } 3698bf215546Sopenharmony_ci 3699bf215546Sopenharmony_ci /* The scissor is ignored here as only tiles inside the scissoring 3700bf215546Sopenharmony_ci * rectangle will refer to this */ 3701bf215546Sopenharmony_ci const boolean no_kill = 3702bf215546Sopenharmony_ci fullcolormask && 3703bf215546Sopenharmony_ci !key->stencil[0].enabled && 3704bf215546Sopenharmony_ci !key->alpha.enabled && 3705bf215546Sopenharmony_ci !key->multisample && 3706bf215546Sopenharmony_ci !key->blend.alpha_to_coverage && 3707bf215546Sopenharmony_ci !key->depth.enabled && 3708bf215546Sopenharmony_ci !shader->info.base.uses_kill && 3709bf215546Sopenharmony_ci !shader->info.base.writes_samplemask && 3710bf215546Sopenharmony_ci !shader->info.base.uses_fbfetch; 3711bf215546Sopenharmony_ci 3712bf215546Sopenharmony_ci variant->opaque = 3713bf215546Sopenharmony_ci no_kill && 3714bf215546Sopenharmony_ci !key->blend.logicop_enable && 3715bf215546Sopenharmony_ci !key->blend.rt[0].blend_enable 3716bf215546Sopenharmony_ci ? TRUE : FALSE; 3717bf215546Sopenharmony_ci 3718bf215546Sopenharmony_ci variant->potentially_opaque = 3719bf215546Sopenharmony_ci no_kill && 3720bf215546Sopenharmony_ci !key->blend.logicop_enable && 3721bf215546Sopenharmony_ci key->blend.rt[0].blend_enable && 3722bf215546Sopenharmony_ci key->blend.rt[0].rgb_func == PIPE_BLEND_ADD && 3723bf215546Sopenharmony_ci key->blend.rt[0].rgb_dst_factor == PIPE_BLENDFACTOR_INV_SRC_ALPHA && 3724bf215546Sopenharmony_ci key->blend.rt[0].alpha_func == key->blend.rt[0].rgb_func && 3725bf215546Sopenharmony_ci key->blend.rt[0].alpha_dst_factor == key->blend.rt[0].rgb_dst_factor && 3726bf215546Sopenharmony_ci shader->base.type == PIPE_SHADER_IR_TGSI && 3727bf215546Sopenharmony_ci /* 3728bf215546Sopenharmony_ci * FIXME: for NIR, all of the fields of info.xxx (except info.base) 3729bf215546Sopenharmony_ci * are zeros, hence shader analysis (here and elsewhere) using these 3730bf215546Sopenharmony_ci * bits cannot work and will silently fail (cbuf is the only pointer 3731bf215546Sopenharmony_ci * field, hence causing a crash). 3732bf215546Sopenharmony_ci */ 3733bf215546Sopenharmony_ci shader->info.cbuf[0][3].file != TGSI_FILE_NULL 3734bf215546Sopenharmony_ci ? TRUE : FALSE; 3735bf215546Sopenharmony_ci 3736bf215546Sopenharmony_ci /* We only care about opaque blits for now */ 3737bf215546Sopenharmony_ci if (variant->opaque && 3738bf215546Sopenharmony_ci (shader->kind == LP_FS_KIND_BLIT_RGBA || 3739bf215546Sopenharmony_ci shader->kind == LP_FS_KIND_BLIT_RGB1)) { 3740bf215546Sopenharmony_ci const struct lp_sampler_static_state *samp0 = 3741bf215546Sopenharmony_ci lp_fs_variant_key_sampler_idx(key, 0); 3742bf215546Sopenharmony_ci assert(samp0); 3743bf215546Sopenharmony_ci 3744bf215546Sopenharmony_ci const enum pipe_format texture_format = samp0->texture_state.format; 3745bf215546Sopenharmony_ci const enum pipe_texture_target target = samp0->texture_state.target; 3746bf215546Sopenharmony_ci const unsigned min_img_filter = samp0->sampler_state.min_img_filter; 3747bf215546Sopenharmony_ci const unsigned mag_img_filter = samp0->sampler_state.mag_img_filter; 3748bf215546Sopenharmony_ci 3749bf215546Sopenharmony_ci unsigned min_mip_filter; 3750bf215546Sopenharmony_ci if (samp0->texture_state.level_zero_only) { 3751bf215546Sopenharmony_ci min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 3752bf215546Sopenharmony_ci } else { 3753bf215546Sopenharmony_ci min_mip_filter = samp0->sampler_state.min_mip_filter; 3754bf215546Sopenharmony_ci } 3755bf215546Sopenharmony_ci 3756bf215546Sopenharmony_ci if (target == PIPE_TEXTURE_2D && 3757bf215546Sopenharmony_ci min_img_filter == PIPE_TEX_FILTER_NEAREST && 3758bf215546Sopenharmony_ci mag_img_filter == PIPE_TEX_FILTER_NEAREST && 3759bf215546Sopenharmony_ci min_mip_filter == PIPE_TEX_MIPFILTER_NONE && 3760bf215546Sopenharmony_ci ((texture_format && 3761bf215546Sopenharmony_ci util_is_format_compatible(util_format_description(texture_format), 3762bf215546Sopenharmony_ci cbuf0_format_desc)) || 3763bf215546Sopenharmony_ci (shader->kind == LP_FS_KIND_BLIT_RGB1 && 3764bf215546Sopenharmony_ci (texture_format == PIPE_FORMAT_B8G8R8A8_UNORM || 3765bf215546Sopenharmony_ci texture_format == PIPE_FORMAT_B8G8R8X8_UNORM) && 3766bf215546Sopenharmony_ci (key->cbuf_format[0] == PIPE_FORMAT_B8G8R8A8_UNORM || 3767bf215546Sopenharmony_ci key->cbuf_format[0] == PIPE_FORMAT_B8G8R8X8_UNORM)))) { 3768bf215546Sopenharmony_ci variant->blit = 1; 3769bf215546Sopenharmony_ci } 3770bf215546Sopenharmony_ci } 3771bf215546Sopenharmony_ci 3772bf215546Sopenharmony_ci /* Determine whether this shader + pipeline state is a candidate for 3773bf215546Sopenharmony_ci * the linear path. 3774bf215546Sopenharmony_ci */ 3775bf215546Sopenharmony_ci const boolean linear_pipeline = 3776bf215546Sopenharmony_ci !key->stencil[0].enabled && 3777bf215546Sopenharmony_ci !key->depth.enabled && 3778bf215546Sopenharmony_ci !shader->info.base.uses_kill && 3779bf215546Sopenharmony_ci !key->blend.logicop_enable && 3780bf215546Sopenharmony_ci (key->cbuf_format[0] == PIPE_FORMAT_B8G8R8A8_UNORM || 3781bf215546Sopenharmony_ci key->cbuf_format[0] == PIPE_FORMAT_B8G8R8X8_UNORM); 3782bf215546Sopenharmony_ci 3783bf215546Sopenharmony_ci memcpy(&variant->key, key, sizeof *key); 3784bf215546Sopenharmony_ci 3785bf215546Sopenharmony_ci if ((LP_DEBUG & DEBUG_FS) || (gallivm_debug & GALLIVM_DEBUG_IR)) { 3786bf215546Sopenharmony_ci lp_debug_fs_variant(variant); 3787bf215546Sopenharmony_ci } 3788bf215546Sopenharmony_ci 3789bf215546Sopenharmony_ci llvmpipe_fs_variant_fastpath(variant); 3790bf215546Sopenharmony_ci 3791bf215546Sopenharmony_ci lp_jit_init_types(variant); 3792bf215546Sopenharmony_ci 3793bf215546Sopenharmony_ci if (variant->jit_function[RAST_EDGE_TEST] == NULL) 3794bf215546Sopenharmony_ci generate_fragment(lp, shader, variant, RAST_EDGE_TEST); 3795bf215546Sopenharmony_ci 3796bf215546Sopenharmony_ci if (variant->jit_function[RAST_WHOLE] == NULL) { 3797bf215546Sopenharmony_ci if (variant->opaque) { 3798bf215546Sopenharmony_ci /* Specialized shader, which doesn't need to read the color buffer. */ 3799bf215546Sopenharmony_ci generate_fragment(lp, shader, variant, RAST_WHOLE); 3800bf215546Sopenharmony_ci } 3801bf215546Sopenharmony_ci } 3802bf215546Sopenharmony_ci 3803bf215546Sopenharmony_ci if (linear_pipeline) { 3804bf215546Sopenharmony_ci /* Currently keeping both the old fastpaths and new linear path 3805bf215546Sopenharmony_ci * active. The older code is still somewhat faster for the cases 3806bf215546Sopenharmony_ci * it covers. 3807bf215546Sopenharmony_ci * 3808bf215546Sopenharmony_ci * XXX: consider restricting this to aero-mode only. 3809bf215546Sopenharmony_ci */ 3810bf215546Sopenharmony_ci if (fullcolormask && 3811bf215546Sopenharmony_ci !key->alpha.enabled && 3812bf215546Sopenharmony_ci !key->blend.alpha_to_coverage) { 3813bf215546Sopenharmony_ci llvmpipe_fs_variant_linear_fastpath(variant); 3814bf215546Sopenharmony_ci } 3815bf215546Sopenharmony_ci 3816bf215546Sopenharmony_ci /* If the original fastpath doesn't cover this variant, try the new 3817bf215546Sopenharmony_ci * code: 3818bf215546Sopenharmony_ci */ 3819bf215546Sopenharmony_ci if (variant->jit_linear == NULL) { 3820bf215546Sopenharmony_ci if (shader->kind == LP_FS_KIND_BLIT_RGBA || 3821bf215546Sopenharmony_ci shader->kind == LP_FS_KIND_BLIT_RGB1 || 3822bf215546Sopenharmony_ci shader->kind == LP_FS_KIND_LLVM_LINEAR) { 3823bf215546Sopenharmony_ci llvmpipe_fs_variant_linear_llvm(lp, shader, variant); 3824bf215546Sopenharmony_ci } 3825bf215546Sopenharmony_ci } 3826bf215546Sopenharmony_ci } else { 3827bf215546Sopenharmony_ci if (LP_DEBUG & DEBUG_LINEAR) { 3828bf215546Sopenharmony_ci lp_debug_fs_variant(variant); 3829bf215546Sopenharmony_ci debug_printf(" ----> no linear path for this variant\n"); 3830bf215546Sopenharmony_ci } 3831bf215546Sopenharmony_ci } 3832bf215546Sopenharmony_ci 3833bf215546Sopenharmony_ci /* 3834bf215546Sopenharmony_ci * Compile everything 3835bf215546Sopenharmony_ci */ 3836bf215546Sopenharmony_ci 3837bf215546Sopenharmony_ci gallivm_compile_module(variant->gallivm); 3838bf215546Sopenharmony_ci 3839bf215546Sopenharmony_ci variant->nr_instrs += lp_build_count_ir_module(variant->gallivm->module); 3840bf215546Sopenharmony_ci 3841bf215546Sopenharmony_ci if (variant->function[RAST_EDGE_TEST]) { 3842bf215546Sopenharmony_ci variant->jit_function[RAST_EDGE_TEST] = (lp_jit_frag_func) 3843bf215546Sopenharmony_ci gallivm_jit_function(variant->gallivm, 3844bf215546Sopenharmony_ci variant->function[RAST_EDGE_TEST]); 3845bf215546Sopenharmony_ci } 3846bf215546Sopenharmony_ci 3847bf215546Sopenharmony_ci if (variant->function[RAST_WHOLE]) { 3848bf215546Sopenharmony_ci variant->jit_function[RAST_WHOLE] = (lp_jit_frag_func) 3849bf215546Sopenharmony_ci gallivm_jit_function(variant->gallivm, 3850bf215546Sopenharmony_ci variant->function[RAST_WHOLE]); 3851bf215546Sopenharmony_ci } else if (!variant->jit_function[RAST_WHOLE]) { 3852bf215546Sopenharmony_ci variant->jit_function[RAST_WHOLE] = (lp_jit_frag_func) 3853bf215546Sopenharmony_ci variant->jit_function[RAST_EDGE_TEST]; 3854bf215546Sopenharmony_ci } 3855bf215546Sopenharmony_ci 3856bf215546Sopenharmony_ci if (linear_pipeline) { 3857bf215546Sopenharmony_ci if (variant->linear_function) { 3858bf215546Sopenharmony_ci variant->jit_linear_llvm = (lp_jit_linear_llvm_func) 3859bf215546Sopenharmony_ci gallivm_jit_function(variant->gallivm, variant->linear_function); 3860bf215546Sopenharmony_ci } 3861bf215546Sopenharmony_ci 3862bf215546Sopenharmony_ci /* 3863bf215546Sopenharmony_ci * This must be done after LLVM compilation, as it will call the JIT'ed 3864bf215546Sopenharmony_ci * code to determine active inputs. 3865bf215546Sopenharmony_ci */ 3866bf215546Sopenharmony_ci lp_linear_check_variant(variant); 3867bf215546Sopenharmony_ci } 3868bf215546Sopenharmony_ci 3869bf215546Sopenharmony_ci if (needs_caching) { 3870bf215546Sopenharmony_ci lp_disk_cache_insert_shader(screen, &cached, ir_sha1_cache_key); 3871bf215546Sopenharmony_ci } 3872bf215546Sopenharmony_ci 3873bf215546Sopenharmony_ci gallivm_free_ir(variant->gallivm); 3874bf215546Sopenharmony_ci 3875bf215546Sopenharmony_ci return variant; 3876bf215546Sopenharmony_ci} 3877bf215546Sopenharmony_ci 3878bf215546Sopenharmony_ci 3879bf215546Sopenharmony_cistatic void * 3880bf215546Sopenharmony_cillvmpipe_create_fs_state(struct pipe_context *pipe, 3881bf215546Sopenharmony_ci const struct pipe_shader_state *templ) 3882bf215546Sopenharmony_ci{ 3883bf215546Sopenharmony_ci struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 3884bf215546Sopenharmony_ci 3885bf215546Sopenharmony_ci struct lp_fragment_shader *shader = CALLOC_STRUCT(lp_fragment_shader); 3886bf215546Sopenharmony_ci if (!shader) 3887bf215546Sopenharmony_ci return NULL; 3888bf215546Sopenharmony_ci 3889bf215546Sopenharmony_ci pipe_reference_init(&shader->reference, 1); 3890bf215546Sopenharmony_ci shader->no = fs_no++; 3891bf215546Sopenharmony_ci list_inithead(&shader->variants.list); 3892bf215546Sopenharmony_ci 3893bf215546Sopenharmony_ci shader->base.type = templ->type; 3894bf215546Sopenharmony_ci if (templ->type == PIPE_SHADER_IR_TGSI) { 3895bf215546Sopenharmony_ci /* get/save the summary info for this shader */ 3896bf215546Sopenharmony_ci lp_build_tgsi_info(templ->tokens, &shader->info); 3897bf215546Sopenharmony_ci 3898bf215546Sopenharmony_ci /* we need to keep a local copy of the tokens */ 3899bf215546Sopenharmony_ci shader->base.tokens = tgsi_dup_tokens(templ->tokens); 3900bf215546Sopenharmony_ci } else { 3901bf215546Sopenharmony_ci shader->base.ir.nir = templ->ir.nir; 3902bf215546Sopenharmony_ci nir_tgsi_scan_shader(templ->ir.nir, &shader->info.base, true); 3903bf215546Sopenharmony_ci } 3904bf215546Sopenharmony_ci 3905bf215546Sopenharmony_ci shader->draw_data = draw_create_fragment_shader(llvmpipe->draw, templ); 3906bf215546Sopenharmony_ci if (shader->draw_data == NULL) { 3907bf215546Sopenharmony_ci FREE((void *) shader->base.tokens); 3908bf215546Sopenharmony_ci FREE(shader); 3909bf215546Sopenharmony_ci return NULL; 3910bf215546Sopenharmony_ci } 3911bf215546Sopenharmony_ci 3912bf215546Sopenharmony_ci const int nr_samplers = shader->info.base.file_max[TGSI_FILE_SAMPLER] + 1; 3913bf215546Sopenharmony_ci const int nr_sampler_views = 3914bf215546Sopenharmony_ci shader->info.base.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; 3915bf215546Sopenharmony_ci const int nr_images = shader->info.base.file_max[TGSI_FILE_IMAGE] + 1; 3916bf215546Sopenharmony_ci 3917bf215546Sopenharmony_ci shader->variant_key_size = lp_fs_variant_key_size(MAX2(nr_samplers, 3918bf215546Sopenharmony_ci nr_sampler_views), 3919bf215546Sopenharmony_ci nr_images); 3920bf215546Sopenharmony_ci 3921bf215546Sopenharmony_ci for (int i = 0; i < shader->info.base.num_inputs; i++) { 3922bf215546Sopenharmony_ci shader->inputs[i].usage_mask = shader->info.base.input_usage_mask[i]; 3923bf215546Sopenharmony_ci shader->inputs[i].location = shader->info.base.input_interpolate_loc[i]; 3924bf215546Sopenharmony_ci 3925bf215546Sopenharmony_ci switch (shader->info.base.input_interpolate[i]) { 3926bf215546Sopenharmony_ci case TGSI_INTERPOLATE_CONSTANT: 3927bf215546Sopenharmony_ci shader->inputs[i].interp = LP_INTERP_CONSTANT; 3928bf215546Sopenharmony_ci break; 3929bf215546Sopenharmony_ci case TGSI_INTERPOLATE_LINEAR: 3930bf215546Sopenharmony_ci shader->inputs[i].interp = LP_INTERP_LINEAR; 3931bf215546Sopenharmony_ci break; 3932bf215546Sopenharmony_ci case TGSI_INTERPOLATE_PERSPECTIVE: 3933bf215546Sopenharmony_ci shader->inputs[i].interp = LP_INTERP_PERSPECTIVE; 3934bf215546Sopenharmony_ci break; 3935bf215546Sopenharmony_ci case TGSI_INTERPOLATE_COLOR: 3936bf215546Sopenharmony_ci shader->inputs[i].interp = LP_INTERP_COLOR; 3937bf215546Sopenharmony_ci break; 3938bf215546Sopenharmony_ci default: 3939bf215546Sopenharmony_ci assert(0); 3940bf215546Sopenharmony_ci break; 3941bf215546Sopenharmony_ci } 3942bf215546Sopenharmony_ci 3943bf215546Sopenharmony_ci switch (shader->info.base.input_semantic_name[i]) { 3944bf215546Sopenharmony_ci case TGSI_SEMANTIC_FACE: 3945bf215546Sopenharmony_ci shader->inputs[i].interp = LP_INTERP_FACING; 3946bf215546Sopenharmony_ci break; 3947bf215546Sopenharmony_ci case TGSI_SEMANTIC_POSITION: 3948bf215546Sopenharmony_ci /* Position was already emitted above 3949bf215546Sopenharmony_ci */ 3950bf215546Sopenharmony_ci shader->inputs[i].interp = LP_INTERP_POSITION; 3951bf215546Sopenharmony_ci shader->inputs[i].src_index = 0; 3952bf215546Sopenharmony_ci continue; 3953bf215546Sopenharmony_ci } 3954bf215546Sopenharmony_ci 3955bf215546Sopenharmony_ci /* XXX this is a completely pointless index map... */ 3956bf215546Sopenharmony_ci shader->inputs[i].src_index = i+1; 3957bf215546Sopenharmony_ci } 3958bf215546Sopenharmony_ci 3959bf215546Sopenharmony_ci if (LP_DEBUG & DEBUG_TGSI && templ->type == PIPE_SHADER_IR_TGSI) { 3960bf215546Sopenharmony_ci debug_printf("llvmpipe: Create fragment shader #%u %p:\n", 3961bf215546Sopenharmony_ci shader->no, (void *) shader); 3962bf215546Sopenharmony_ci tgsi_dump(templ->tokens, 0); 3963bf215546Sopenharmony_ci debug_printf("usage masks:\n"); 3964bf215546Sopenharmony_ci for (unsigned attrib = 0; attrib < shader->info.base.num_inputs; ++attrib) { 3965bf215546Sopenharmony_ci unsigned usage_mask = shader->info.base.input_usage_mask[attrib]; 3966bf215546Sopenharmony_ci debug_printf(" IN[%u].%s%s%s%s\n", 3967bf215546Sopenharmony_ci attrib, 3968bf215546Sopenharmony_ci usage_mask & TGSI_WRITEMASK_X ? "x" : "", 3969bf215546Sopenharmony_ci usage_mask & TGSI_WRITEMASK_Y ? "y" : "", 3970bf215546Sopenharmony_ci usage_mask & TGSI_WRITEMASK_Z ? "z" : "", 3971bf215546Sopenharmony_ci usage_mask & TGSI_WRITEMASK_W ? "w" : ""); 3972bf215546Sopenharmony_ci } 3973bf215546Sopenharmony_ci debug_printf("\n"); 3974bf215546Sopenharmony_ci } 3975bf215546Sopenharmony_ci 3976bf215546Sopenharmony_ci /* This will put a derived copy of the tokens into shader->base.tokens */ 3977bf215546Sopenharmony_ci if (templ->type == PIPE_SHADER_IR_TGSI) 3978bf215546Sopenharmony_ci llvmpipe_fs_analyse(shader, templ->tokens); 3979bf215546Sopenharmony_ci else 3980bf215546Sopenharmony_ci llvmpipe_fs_analyse_nir(shader); 3981bf215546Sopenharmony_ci 3982bf215546Sopenharmony_ci return shader; 3983bf215546Sopenharmony_ci} 3984bf215546Sopenharmony_ci 3985bf215546Sopenharmony_ci 3986bf215546Sopenharmony_cistatic void 3987bf215546Sopenharmony_cillvmpipe_bind_fs_state(struct pipe_context *pipe, void *fs) 3988bf215546Sopenharmony_ci{ 3989bf215546Sopenharmony_ci struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 3990bf215546Sopenharmony_ci struct lp_fragment_shader *lp_fs = (struct lp_fragment_shader *)fs; 3991bf215546Sopenharmony_ci if (llvmpipe->fs == lp_fs) 3992bf215546Sopenharmony_ci return; 3993bf215546Sopenharmony_ci 3994bf215546Sopenharmony_ci draw_bind_fragment_shader(llvmpipe->draw, 3995bf215546Sopenharmony_ci (lp_fs ? lp_fs->draw_data : NULL)); 3996bf215546Sopenharmony_ci 3997bf215546Sopenharmony_ci lp_fs_reference(llvmpipe, &llvmpipe->fs, lp_fs); 3998bf215546Sopenharmony_ci 3999bf215546Sopenharmony_ci /* invalidate the setup link, NEW_FS will make it update */ 4000bf215546Sopenharmony_ci lp_setup_set_fs_variant(llvmpipe->setup, NULL); 4001bf215546Sopenharmony_ci llvmpipe->dirty |= LP_NEW_FS; 4002bf215546Sopenharmony_ci} 4003bf215546Sopenharmony_ci 4004bf215546Sopenharmony_ci 4005bf215546Sopenharmony_ci/** 4006bf215546Sopenharmony_ci * Remove shader variant from two lists: the shader's variant list 4007bf215546Sopenharmony_ci * and the context's variant list. 4008bf215546Sopenharmony_ci */ 4009bf215546Sopenharmony_cistatic void 4010bf215546Sopenharmony_cillvmpipe_remove_shader_variant(struct llvmpipe_context *lp, 4011bf215546Sopenharmony_ci struct lp_fragment_shader_variant *variant) 4012bf215546Sopenharmony_ci{ 4013bf215546Sopenharmony_ci if ((LP_DEBUG & DEBUG_FS) || (gallivm_debug & GALLIVM_DEBUG_IR)) { 4014bf215546Sopenharmony_ci debug_printf("llvmpipe: del fs #%u var %u v created %u v cached %u " 4015bf215546Sopenharmony_ci "v total cached %u inst %u total inst %u\n", 4016bf215546Sopenharmony_ci variant->shader->no, variant->no, 4017bf215546Sopenharmony_ci variant->shader->variants_created, 4018bf215546Sopenharmony_ci variant->shader->variants_cached, 4019bf215546Sopenharmony_ci lp->nr_fs_variants, variant->nr_instrs, lp->nr_fs_instrs); 4020bf215546Sopenharmony_ci } 4021bf215546Sopenharmony_ci 4022bf215546Sopenharmony_ci /* remove from shader's list */ 4023bf215546Sopenharmony_ci list_del(&variant->list_item_local.list); 4024bf215546Sopenharmony_ci variant->shader->variants_cached--; 4025bf215546Sopenharmony_ci 4026bf215546Sopenharmony_ci /* remove from context's list */ 4027bf215546Sopenharmony_ci list_del(&variant->list_item_global.list); 4028bf215546Sopenharmony_ci lp->nr_fs_variants--; 4029bf215546Sopenharmony_ci lp->nr_fs_instrs -= variant->nr_instrs; 4030bf215546Sopenharmony_ci} 4031bf215546Sopenharmony_ci 4032bf215546Sopenharmony_ci 4033bf215546Sopenharmony_civoid 4034bf215546Sopenharmony_cillvmpipe_destroy_shader_variant(struct llvmpipe_context *lp, 4035bf215546Sopenharmony_ci struct lp_fragment_shader_variant *variant) 4036bf215546Sopenharmony_ci{ 4037bf215546Sopenharmony_ci gallivm_destroy(variant->gallivm); 4038bf215546Sopenharmony_ci lp_fs_reference(lp, &variant->shader, NULL); 4039bf215546Sopenharmony_ci FREE(variant); 4040bf215546Sopenharmony_ci} 4041bf215546Sopenharmony_ci 4042bf215546Sopenharmony_ci 4043bf215546Sopenharmony_civoid 4044bf215546Sopenharmony_cillvmpipe_destroy_fs(struct llvmpipe_context *llvmpipe, 4045bf215546Sopenharmony_ci struct lp_fragment_shader *shader) 4046bf215546Sopenharmony_ci{ 4047bf215546Sopenharmony_ci /* Delete draw module's data */ 4048bf215546Sopenharmony_ci draw_delete_fragment_shader(llvmpipe->draw, shader->draw_data); 4049bf215546Sopenharmony_ci 4050bf215546Sopenharmony_ci if (shader->base.ir.nir) 4051bf215546Sopenharmony_ci ralloc_free(shader->base.ir.nir); 4052bf215546Sopenharmony_ci assert(shader->variants_cached == 0); 4053bf215546Sopenharmony_ci FREE((void *) shader->base.tokens); 4054bf215546Sopenharmony_ci FREE(shader); 4055bf215546Sopenharmony_ci} 4056bf215546Sopenharmony_ci 4057bf215546Sopenharmony_ci 4058bf215546Sopenharmony_cistatic void 4059bf215546Sopenharmony_cillvmpipe_delete_fs_state(struct pipe_context *pipe, void *fs) 4060bf215546Sopenharmony_ci{ 4061bf215546Sopenharmony_ci struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 4062bf215546Sopenharmony_ci struct lp_fragment_shader *shader = fs; 4063bf215546Sopenharmony_ci struct lp_fs_variant_list_item *li, *next; 4064bf215546Sopenharmony_ci 4065bf215546Sopenharmony_ci /* Delete all the variants */ 4066bf215546Sopenharmony_ci LIST_FOR_EACH_ENTRY_SAFE(li, next, &shader->variants.list, list) { 4067bf215546Sopenharmony_ci struct lp_fragment_shader_variant *variant; 4068bf215546Sopenharmony_ci variant = li->base; 4069bf215546Sopenharmony_ci llvmpipe_remove_shader_variant(llvmpipe, li->base); 4070bf215546Sopenharmony_ci lp_fs_variant_reference(llvmpipe, &variant, NULL); 4071bf215546Sopenharmony_ci } 4072bf215546Sopenharmony_ci 4073bf215546Sopenharmony_ci lp_fs_reference(llvmpipe, &shader, NULL); 4074bf215546Sopenharmony_ci} 4075bf215546Sopenharmony_ci 4076bf215546Sopenharmony_ci 4077bf215546Sopenharmony_cistatic void 4078bf215546Sopenharmony_cillvmpipe_set_constant_buffer(struct pipe_context *pipe, 4079bf215546Sopenharmony_ci enum pipe_shader_type shader, uint index, 4080bf215546Sopenharmony_ci bool take_ownership, 4081bf215546Sopenharmony_ci const struct pipe_constant_buffer *cb) 4082bf215546Sopenharmony_ci{ 4083bf215546Sopenharmony_ci struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 4084bf215546Sopenharmony_ci struct pipe_constant_buffer *constants = &llvmpipe->constants[shader][index]; 4085bf215546Sopenharmony_ci 4086bf215546Sopenharmony_ci assert(shader < PIPE_SHADER_TYPES); 4087bf215546Sopenharmony_ci assert(index < ARRAY_SIZE(llvmpipe->constants[shader])); 4088bf215546Sopenharmony_ci 4089bf215546Sopenharmony_ci /* note: reference counting */ 4090bf215546Sopenharmony_ci util_copy_constant_buffer(&llvmpipe->constants[shader][index], cb, 4091bf215546Sopenharmony_ci take_ownership); 4092bf215546Sopenharmony_ci 4093bf215546Sopenharmony_ci /* user_buffer is only valid until the next set_constant_buffer (at most, 4094bf215546Sopenharmony_ci * possibly until shader deletion), so we need to upload it now to make sure 4095bf215546Sopenharmony_ci * it doesn't get updated/freed out from under us. 4096bf215546Sopenharmony_ci */ 4097bf215546Sopenharmony_ci if (constants->user_buffer) { 4098bf215546Sopenharmony_ci u_upload_data(llvmpipe->pipe.const_uploader, 0, constants->buffer_size, 4099bf215546Sopenharmony_ci 16, constants->user_buffer, &constants->buffer_offset, 4100bf215546Sopenharmony_ci &constants->buffer); 4101bf215546Sopenharmony_ci } 4102bf215546Sopenharmony_ci if (constants->buffer) { 4103bf215546Sopenharmony_ci if (!(constants->buffer->bind & PIPE_BIND_CONSTANT_BUFFER)) { 4104bf215546Sopenharmony_ci debug_printf("Illegal set constant without bind flag\n"); 4105bf215546Sopenharmony_ci constants->buffer->bind |= PIPE_BIND_CONSTANT_BUFFER; 4106bf215546Sopenharmony_ci } 4107bf215546Sopenharmony_ci } 4108bf215546Sopenharmony_ci 4109bf215546Sopenharmony_ci if (shader == PIPE_SHADER_VERTEX || 4110bf215546Sopenharmony_ci shader == PIPE_SHADER_GEOMETRY || 4111bf215546Sopenharmony_ci shader == PIPE_SHADER_TESS_CTRL || 4112bf215546Sopenharmony_ci shader == PIPE_SHADER_TESS_EVAL) { 4113bf215546Sopenharmony_ci /* Pass the constants to the 'draw' module */ 4114bf215546Sopenharmony_ci const unsigned size = cb ? cb->buffer_size : 0; 4115bf215546Sopenharmony_ci 4116bf215546Sopenharmony_ci const ubyte *data = NULL; 4117bf215546Sopenharmony_ci if (constants->buffer) { 4118bf215546Sopenharmony_ci data = (ubyte *) llvmpipe_resource_data(constants->buffer) 4119bf215546Sopenharmony_ci + constants->buffer_offset; 4120bf215546Sopenharmony_ci } 4121bf215546Sopenharmony_ci 4122bf215546Sopenharmony_ci draw_set_mapped_constant_buffer(llvmpipe->draw, shader, 4123bf215546Sopenharmony_ci index, data, size); 4124bf215546Sopenharmony_ci } else if (shader == PIPE_SHADER_COMPUTE) { 4125bf215546Sopenharmony_ci llvmpipe->cs_dirty |= LP_CSNEW_CONSTANTS; 4126bf215546Sopenharmony_ci } else { 4127bf215546Sopenharmony_ci llvmpipe->dirty |= LP_NEW_FS_CONSTANTS; 4128bf215546Sopenharmony_ci } 4129bf215546Sopenharmony_ci} 4130bf215546Sopenharmony_ci 4131bf215546Sopenharmony_ci 4132bf215546Sopenharmony_cistatic void 4133bf215546Sopenharmony_cillvmpipe_set_shader_buffers(struct pipe_context *pipe, 4134bf215546Sopenharmony_ci enum pipe_shader_type shader, unsigned start_slot, 4135bf215546Sopenharmony_ci unsigned count, 4136bf215546Sopenharmony_ci const struct pipe_shader_buffer *buffers, 4137bf215546Sopenharmony_ci unsigned writable_bitmask) 4138bf215546Sopenharmony_ci{ 4139bf215546Sopenharmony_ci struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 4140bf215546Sopenharmony_ci 4141bf215546Sopenharmony_ci unsigned i, idx; 4142bf215546Sopenharmony_ci for (i = start_slot, idx = 0; i < start_slot + count; i++, idx++) { 4143bf215546Sopenharmony_ci const struct pipe_shader_buffer *buffer = buffers ? &buffers[idx] : NULL; 4144bf215546Sopenharmony_ci 4145bf215546Sopenharmony_ci util_copy_shader_buffer(&llvmpipe->ssbos[shader][i], buffer); 4146bf215546Sopenharmony_ci 4147bf215546Sopenharmony_ci if (buffer && buffer->buffer) { 4148bf215546Sopenharmony_ci boolean read_only = !(writable_bitmask & (1 << idx)); 4149bf215546Sopenharmony_ci llvmpipe_flush_resource(pipe, buffer->buffer, 0, read_only, false, 4150bf215546Sopenharmony_ci false, "buffer"); 4151bf215546Sopenharmony_ci } 4152bf215546Sopenharmony_ci 4153bf215546Sopenharmony_ci if (shader == PIPE_SHADER_VERTEX || 4154bf215546Sopenharmony_ci shader == PIPE_SHADER_GEOMETRY || 4155bf215546Sopenharmony_ci shader == PIPE_SHADER_TESS_CTRL || 4156bf215546Sopenharmony_ci shader == PIPE_SHADER_TESS_EVAL) { 4157bf215546Sopenharmony_ci const unsigned size = buffer ? buffer->buffer_size : 0; 4158bf215546Sopenharmony_ci const ubyte *data = NULL; 4159bf215546Sopenharmony_ci if (buffer && buffer->buffer) 4160bf215546Sopenharmony_ci data = (ubyte *) llvmpipe_resource_data(buffer->buffer); 4161bf215546Sopenharmony_ci if (data) 4162bf215546Sopenharmony_ci data += buffer->buffer_offset; 4163bf215546Sopenharmony_ci draw_set_mapped_shader_buffer(llvmpipe->draw, shader, 4164bf215546Sopenharmony_ci i, data, size); 4165bf215546Sopenharmony_ci } else if (shader == PIPE_SHADER_COMPUTE) { 4166bf215546Sopenharmony_ci llvmpipe->cs_dirty |= LP_CSNEW_SSBOS; 4167bf215546Sopenharmony_ci } else if (shader == PIPE_SHADER_FRAGMENT) { 4168bf215546Sopenharmony_ci llvmpipe->fs_ssbo_write_mask &= ~(((1 << count) - 1) << start_slot); 4169bf215546Sopenharmony_ci llvmpipe->fs_ssbo_write_mask |= writable_bitmask << start_slot; 4170bf215546Sopenharmony_ci llvmpipe->dirty |= LP_NEW_FS_SSBOS; 4171bf215546Sopenharmony_ci } 4172bf215546Sopenharmony_ci } 4173bf215546Sopenharmony_ci} 4174bf215546Sopenharmony_ci 4175bf215546Sopenharmony_ci 4176bf215546Sopenharmony_cistatic void 4177bf215546Sopenharmony_cillvmpipe_set_shader_images(struct pipe_context *pipe, 4178bf215546Sopenharmony_ci enum pipe_shader_type shader, unsigned start_slot, 4179bf215546Sopenharmony_ci unsigned count, unsigned unbind_num_trailing_slots, 4180bf215546Sopenharmony_ci const struct pipe_image_view *images) 4181bf215546Sopenharmony_ci{ 4182bf215546Sopenharmony_ci struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 4183bf215546Sopenharmony_ci unsigned i, idx; 4184bf215546Sopenharmony_ci 4185bf215546Sopenharmony_ci draw_flush(llvmpipe->draw); 4186bf215546Sopenharmony_ci for (i = start_slot, idx = 0; i < start_slot + count; i++, idx++) { 4187bf215546Sopenharmony_ci const struct pipe_image_view *image = images ? &images[idx] : NULL; 4188bf215546Sopenharmony_ci 4189bf215546Sopenharmony_ci util_copy_image_view(&llvmpipe->images[shader][i], image); 4190bf215546Sopenharmony_ci 4191bf215546Sopenharmony_ci if (image && image->resource) { 4192bf215546Sopenharmony_ci bool read_only = !(image->access & PIPE_IMAGE_ACCESS_WRITE); 4193bf215546Sopenharmony_ci llvmpipe_flush_resource(pipe, image->resource, 0, read_only, false, 4194bf215546Sopenharmony_ci false, "image"); 4195bf215546Sopenharmony_ci } 4196bf215546Sopenharmony_ci } 4197bf215546Sopenharmony_ci 4198bf215546Sopenharmony_ci llvmpipe->num_images[shader] = start_slot + count; 4199bf215546Sopenharmony_ci if (shader == PIPE_SHADER_VERTEX || 4200bf215546Sopenharmony_ci shader == PIPE_SHADER_GEOMETRY || 4201bf215546Sopenharmony_ci shader == PIPE_SHADER_TESS_CTRL || 4202bf215546Sopenharmony_ci shader == PIPE_SHADER_TESS_EVAL) { 4203bf215546Sopenharmony_ci draw_set_images(llvmpipe->draw, 4204bf215546Sopenharmony_ci shader, 4205bf215546Sopenharmony_ci llvmpipe->images[shader], 4206bf215546Sopenharmony_ci start_slot + count); 4207bf215546Sopenharmony_ci } else if (shader == PIPE_SHADER_COMPUTE) { 4208bf215546Sopenharmony_ci llvmpipe->cs_dirty |= LP_CSNEW_IMAGES; 4209bf215546Sopenharmony_ci } else { 4210bf215546Sopenharmony_ci llvmpipe->dirty |= LP_NEW_FS_IMAGES; 4211bf215546Sopenharmony_ci } 4212bf215546Sopenharmony_ci 4213bf215546Sopenharmony_ci if (unbind_num_trailing_slots) { 4214bf215546Sopenharmony_ci llvmpipe_set_shader_images(pipe, shader, start_slot + count, 4215bf215546Sopenharmony_ci unbind_num_trailing_slots, 0, NULL); 4216bf215546Sopenharmony_ci } 4217bf215546Sopenharmony_ci} 4218bf215546Sopenharmony_ci 4219bf215546Sopenharmony_ci 4220bf215546Sopenharmony_ci/** 4221bf215546Sopenharmony_ci * Return the blend factor equivalent to a destination alpha of one. 4222bf215546Sopenharmony_ci */ 4223bf215546Sopenharmony_cistatic inline enum pipe_blendfactor 4224bf215546Sopenharmony_ciforce_dst_alpha_one(enum pipe_blendfactor factor, boolean clamped_zero) 4225bf215546Sopenharmony_ci{ 4226bf215546Sopenharmony_ci switch (factor) { 4227bf215546Sopenharmony_ci case PIPE_BLENDFACTOR_DST_ALPHA: 4228bf215546Sopenharmony_ci return PIPE_BLENDFACTOR_ONE; 4229bf215546Sopenharmony_ci case PIPE_BLENDFACTOR_INV_DST_ALPHA: 4230bf215546Sopenharmony_ci return PIPE_BLENDFACTOR_ZERO; 4231bf215546Sopenharmony_ci case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE: 4232bf215546Sopenharmony_ci if (clamped_zero) 4233bf215546Sopenharmony_ci return PIPE_BLENDFACTOR_ZERO; 4234bf215546Sopenharmony_ci else 4235bf215546Sopenharmony_ci return PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE; 4236bf215546Sopenharmony_ci default: 4237bf215546Sopenharmony_ci return factor; 4238bf215546Sopenharmony_ci } 4239bf215546Sopenharmony_ci} 4240bf215546Sopenharmony_ci 4241bf215546Sopenharmony_ci 4242bf215546Sopenharmony_ci/** 4243bf215546Sopenharmony_ci * We need to generate several variants of the fragment pipeline to match 4244bf215546Sopenharmony_ci * all the combinations of the contributing state atoms. 4245bf215546Sopenharmony_ci * 4246bf215546Sopenharmony_ci * TODO: there is actually no reason to tie this to context state -- the 4247bf215546Sopenharmony_ci * generated code could be cached globally in the screen. 4248bf215546Sopenharmony_ci */ 4249bf215546Sopenharmony_cistatic struct lp_fragment_shader_variant_key * 4250bf215546Sopenharmony_cimake_variant_key(struct llvmpipe_context *lp, 4251bf215546Sopenharmony_ci struct lp_fragment_shader *shader, 4252bf215546Sopenharmony_ci char *store) 4253bf215546Sopenharmony_ci{ 4254bf215546Sopenharmony_ci struct lp_fragment_shader_variant_key *key = 4255bf215546Sopenharmony_ci (struct lp_fragment_shader_variant_key *)store; 4256bf215546Sopenharmony_ci 4257bf215546Sopenharmony_ci memset(key, 0, sizeof(*key)); 4258bf215546Sopenharmony_ci 4259bf215546Sopenharmony_ci if (lp->framebuffer.zsbuf) { 4260bf215546Sopenharmony_ci const enum pipe_format zsbuf_format = lp->framebuffer.zsbuf->format; 4261bf215546Sopenharmony_ci const struct util_format_description *zsbuf_desc = 4262bf215546Sopenharmony_ci util_format_description(zsbuf_format); 4263bf215546Sopenharmony_ci 4264bf215546Sopenharmony_ci if (lp->depth_stencil->depth_enabled && 4265bf215546Sopenharmony_ci util_format_has_depth(zsbuf_desc)) { 4266bf215546Sopenharmony_ci key->zsbuf_format = zsbuf_format; 4267bf215546Sopenharmony_ci key->depth.enabled = lp->depth_stencil->depth_enabled; 4268bf215546Sopenharmony_ci key->depth.writemask = lp->depth_stencil->depth_writemask; 4269bf215546Sopenharmony_ci key->depth.func = lp->depth_stencil->depth_func; 4270bf215546Sopenharmony_ci } 4271bf215546Sopenharmony_ci if (lp->depth_stencil->stencil[0].enabled && 4272bf215546Sopenharmony_ci util_format_has_stencil(zsbuf_desc)) { 4273bf215546Sopenharmony_ci key->zsbuf_format = zsbuf_format; 4274bf215546Sopenharmony_ci memcpy(&key->stencil, &lp->depth_stencil->stencil, 4275bf215546Sopenharmony_ci sizeof key->stencil); 4276bf215546Sopenharmony_ci } 4277bf215546Sopenharmony_ci if (llvmpipe_resource_is_1d(lp->framebuffer.zsbuf->texture)) { 4278bf215546Sopenharmony_ci key->resource_1d = TRUE; 4279bf215546Sopenharmony_ci } 4280bf215546Sopenharmony_ci key->zsbuf_nr_samples = 4281bf215546Sopenharmony_ci util_res_sample_count(lp->framebuffer.zsbuf->texture); 4282bf215546Sopenharmony_ci 4283bf215546Sopenharmony_ci /* 4284bf215546Sopenharmony_ci * Restrict depth values if the API is clamped (GL, VK with ext) 4285bf215546Sopenharmony_ci * for non float Z buffer 4286bf215546Sopenharmony_ci */ 4287bf215546Sopenharmony_ci key->restrict_depth_values = 4288bf215546Sopenharmony_ci !(lp->rasterizer->unclamped_fragment_depth_values && 4289bf215546Sopenharmony_ci util_format_get_depth_only(zsbuf_format) == PIPE_FORMAT_Z32_FLOAT); 4290bf215546Sopenharmony_ci } 4291bf215546Sopenharmony_ci 4292bf215546Sopenharmony_ci /* 4293bf215546Sopenharmony_ci * Propagate the depth clamp setting from the rasterizer state. 4294bf215546Sopenharmony_ci */ 4295bf215546Sopenharmony_ci key->depth_clamp = lp->rasterizer->depth_clamp; 4296bf215546Sopenharmony_ci 4297bf215546Sopenharmony_ci /* alpha test only applies if render buffer 0 is non-integer 4298bf215546Sopenharmony_ci * (or does not exist) 4299bf215546Sopenharmony_ci */ 4300bf215546Sopenharmony_ci if (!lp->framebuffer.nr_cbufs || 4301bf215546Sopenharmony_ci !lp->framebuffer.cbufs[0] || 4302bf215546Sopenharmony_ci !util_format_is_pure_integer(lp->framebuffer.cbufs[0]->format)) { 4303bf215546Sopenharmony_ci key->alpha.enabled = lp->depth_stencil->alpha_enabled; 4304bf215546Sopenharmony_ci } 4305bf215546Sopenharmony_ci if (key->alpha.enabled) { 4306bf215546Sopenharmony_ci key->alpha.func = lp->depth_stencil->alpha_func; 4307bf215546Sopenharmony_ci /* alpha.ref_value is passed in jit_context */ 4308bf215546Sopenharmony_ci } 4309bf215546Sopenharmony_ci 4310bf215546Sopenharmony_ci key->flatshade = lp->rasterizer->flatshade; 4311bf215546Sopenharmony_ci key->multisample = lp->rasterizer->multisample; 4312bf215546Sopenharmony_ci key->no_ms_sample_mask_out = lp->rasterizer->no_ms_sample_mask_out; 4313bf215546Sopenharmony_ci if (lp->active_occlusion_queries && !lp->queries_disabled) { 4314bf215546Sopenharmony_ci key->occlusion_count = TRUE; 4315bf215546Sopenharmony_ci } 4316bf215546Sopenharmony_ci 4317bf215546Sopenharmony_ci memcpy(&key->blend, lp->blend, sizeof key->blend); 4318bf215546Sopenharmony_ci 4319bf215546Sopenharmony_ci key->coverage_samples = 1; 4320bf215546Sopenharmony_ci key->min_samples = 1; 4321bf215546Sopenharmony_ci if (key->multisample) { 4322bf215546Sopenharmony_ci key->coverage_samples = 4323bf215546Sopenharmony_ci util_framebuffer_get_num_samples(&lp->framebuffer); 4324bf215546Sopenharmony_ci /* Per EXT_shader_framebuffer_fetch spec: 4325bf215546Sopenharmony_ci * 4326bf215546Sopenharmony_ci * "1. How is framebuffer data treated during multisample rendering? 4327bf215546Sopenharmony_ci * 4328bf215546Sopenharmony_ci * RESOLVED: Reading the value of gl_LastFragData produces a different 4329bf215546Sopenharmony_ci * result for each sample. This implies that all or part of the shader be 4330bf215546Sopenharmony_ci * run once for each sample, but has no additional implications on fragment 4331bf215546Sopenharmony_ci * shader input variables which may still be interpolated per pixel by the 4332bf215546Sopenharmony_ci * implementation." 4333bf215546Sopenharmony_ci * 4334bf215546Sopenharmony_ci * ARM_shader_framebuffer_fetch_depth_stencil spec further says: 4335bf215546Sopenharmony_ci * 4336bf215546Sopenharmony_ci * "(1) When multisampling is enabled, does the shader run per sample? 4337bf215546Sopenharmony_ci * 4338bf215546Sopenharmony_ci * RESOLVED. 4339bf215546Sopenharmony_ci * 4340bf215546Sopenharmony_ci * This behavior is inherited from either EXT_shader_framebuffer_fetch or 4341bf215546Sopenharmony_ci * ARM_shader_framebuffer_fetch as described in the interactions section. 4342bf215546Sopenharmony_ci * If neither extension is supported, the shader runs once per fragment." 4343bf215546Sopenharmony_ci * 4344bf215546Sopenharmony_ci * Therefore we should always enable per-sample shading when FB fetch is used. 4345bf215546Sopenharmony_ci */ 4346bf215546Sopenharmony_ci if (lp->min_samples > 1 || shader->info.base.uses_fbfetch) 4347bf215546Sopenharmony_ci key->min_samples = key->coverage_samples; 4348bf215546Sopenharmony_ci } 4349bf215546Sopenharmony_ci key->nr_cbufs = lp->framebuffer.nr_cbufs; 4350bf215546Sopenharmony_ci 4351bf215546Sopenharmony_ci if (!key->blend.independent_blend_enable) { 4352bf215546Sopenharmony_ci // we always need independent blend otherwise the fixups below won't work 4353bf215546Sopenharmony_ci for (unsigned i = 1; i < key->nr_cbufs; i++) { 4354bf215546Sopenharmony_ci memcpy(&key->blend.rt[i], &key->blend.rt[0], 4355bf215546Sopenharmony_ci sizeof(key->blend.rt[0])); 4356bf215546Sopenharmony_ci } 4357bf215546Sopenharmony_ci key->blend.independent_blend_enable = 1; 4358bf215546Sopenharmony_ci } 4359bf215546Sopenharmony_ci 4360bf215546Sopenharmony_ci for (unsigned i = 0; i < lp->framebuffer.nr_cbufs; i++) { 4361bf215546Sopenharmony_ci struct pipe_rt_blend_state *blend_rt = &key->blend.rt[i]; 4362bf215546Sopenharmony_ci 4363bf215546Sopenharmony_ci if (lp->framebuffer.cbufs[i]) { 4364bf215546Sopenharmony_ci const enum pipe_format format = lp->framebuffer.cbufs[i]->format; 4365bf215546Sopenharmony_ci 4366bf215546Sopenharmony_ci key->cbuf_format[i] = format; 4367bf215546Sopenharmony_ci key->cbuf_nr_samples[i] = 4368bf215546Sopenharmony_ci util_res_sample_count(lp->framebuffer.cbufs[i]->texture); 4369bf215546Sopenharmony_ci 4370bf215546Sopenharmony_ci /* 4371bf215546Sopenharmony_ci * Figure out if this is a 1d resource. Note that OpenGL allows crazy 4372bf215546Sopenharmony_ci * mixing of 2d textures with height 1 and 1d textures, so make sure 4373bf215546Sopenharmony_ci * we pick 1d if any cbuf or zsbuf is 1d. 4374bf215546Sopenharmony_ci */ 4375bf215546Sopenharmony_ci if (llvmpipe_resource_is_1d(lp->framebuffer.cbufs[i]->texture)) { 4376bf215546Sopenharmony_ci key->resource_1d = TRUE; 4377bf215546Sopenharmony_ci } 4378bf215546Sopenharmony_ci 4379bf215546Sopenharmony_ci const struct util_format_description *format_desc = 4380bf215546Sopenharmony_ci util_format_description(format); 4381bf215546Sopenharmony_ci assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB || 4382bf215546Sopenharmony_ci format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB); 4383bf215546Sopenharmony_ci 4384bf215546Sopenharmony_ci /* 4385bf215546Sopenharmony_ci * Mask out color channels not present in the color buffer. 4386bf215546Sopenharmony_ci */ 4387bf215546Sopenharmony_ci blend_rt->colormask &= util_format_colormask(format_desc); 4388bf215546Sopenharmony_ci 4389bf215546Sopenharmony_ci /* 4390bf215546Sopenharmony_ci * Disable blend for integer formats. 4391bf215546Sopenharmony_ci */ 4392bf215546Sopenharmony_ci if (util_format_is_pure_integer(format)) { 4393bf215546Sopenharmony_ci blend_rt->blend_enable = 0; 4394bf215546Sopenharmony_ci } 4395bf215546Sopenharmony_ci 4396bf215546Sopenharmony_ci /* 4397bf215546Sopenharmony_ci * Our swizzled render tiles always have an alpha channel, but the 4398bf215546Sopenharmony_ci * linear render target format often does not, so force here the dst 4399bf215546Sopenharmony_ci * alpha to be one. 4400bf215546Sopenharmony_ci * 4401bf215546Sopenharmony_ci * This is not a mere optimization. Wrong results will be produced if 4402bf215546Sopenharmony_ci * the dst alpha is used, the dst format does not have alpha, and the 4403bf215546Sopenharmony_ci * previous rendering was not flushed from the swizzled to linear 4404bf215546Sopenharmony_ci * buffer. For example, NonPowTwo DCT. 4405bf215546Sopenharmony_ci * 4406bf215546Sopenharmony_ci * TODO: This should be generalized to all channels for better 4407bf215546Sopenharmony_ci * performance, but only alpha causes correctness issues. 4408bf215546Sopenharmony_ci * 4409bf215546Sopenharmony_ci * Also, force rgb/alpha func/factors match, to make AoS blending 4410bf215546Sopenharmony_ci * easier. 4411bf215546Sopenharmony_ci */ 4412bf215546Sopenharmony_ci if (format_desc->swizzle[3] > PIPE_SWIZZLE_W || 4413bf215546Sopenharmony_ci format_desc->swizzle[3] == format_desc->swizzle[0]) { 4414bf215546Sopenharmony_ci // Doesn't cover mixed snorm/unorm but can't render to them anyway 4415bf215546Sopenharmony_ci boolean clamped_zero = !util_format_is_float(format) && 4416bf215546Sopenharmony_ci !util_format_is_snorm(format); 4417bf215546Sopenharmony_ci blend_rt->rgb_src_factor = 4418bf215546Sopenharmony_ci force_dst_alpha_one(blend_rt->rgb_src_factor, clamped_zero); 4419bf215546Sopenharmony_ci blend_rt->rgb_dst_factor = 4420bf215546Sopenharmony_ci force_dst_alpha_one(blend_rt->rgb_dst_factor, clamped_zero); 4421bf215546Sopenharmony_ci blend_rt->alpha_func = blend_rt->rgb_func; 4422bf215546Sopenharmony_ci blend_rt->alpha_src_factor = blend_rt->rgb_src_factor; 4423bf215546Sopenharmony_ci blend_rt->alpha_dst_factor = blend_rt->rgb_dst_factor; 4424bf215546Sopenharmony_ci } 4425bf215546Sopenharmony_ci } 4426bf215546Sopenharmony_ci else { 4427bf215546Sopenharmony_ci /* no color buffer for this fragment output */ 4428bf215546Sopenharmony_ci key->cbuf_format[i] = PIPE_FORMAT_NONE; 4429bf215546Sopenharmony_ci key->cbuf_nr_samples[i] = 0; 4430bf215546Sopenharmony_ci blend_rt->colormask = 0x0; 4431bf215546Sopenharmony_ci blend_rt->blend_enable = 0; 4432bf215546Sopenharmony_ci } 4433bf215546Sopenharmony_ci } 4434bf215546Sopenharmony_ci 4435bf215546Sopenharmony_ci /* This value will be the same for all the variants of a given shader: 4436bf215546Sopenharmony_ci */ 4437bf215546Sopenharmony_ci key->nr_samplers = shader->info.base.file_max[TGSI_FILE_SAMPLER] + 1; 4438bf215546Sopenharmony_ci 4439bf215546Sopenharmony_ci if (shader->info.base.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) { 4440bf215546Sopenharmony_ci key->nr_sampler_views = 4441bf215546Sopenharmony_ci shader->info.base.file_max[TGSI_FILE_SAMPLER_VIEW] + 1; 4442bf215546Sopenharmony_ci } 4443bf215546Sopenharmony_ci 4444bf215546Sopenharmony_ci struct lp_sampler_static_state *fs_sampler = 4445bf215546Sopenharmony_ci lp_fs_variant_key_samplers(key); 4446bf215546Sopenharmony_ci 4447bf215546Sopenharmony_ci memset(fs_sampler, 0, 4448bf215546Sopenharmony_ci MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *fs_sampler); 4449bf215546Sopenharmony_ci 4450bf215546Sopenharmony_ci for (unsigned i = 0; i < key->nr_samplers; ++i) { 4451bf215546Sopenharmony_ci if (shader->info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) { 4452bf215546Sopenharmony_ci lp_sampler_static_sampler_state(&fs_sampler[i].sampler_state, 4453bf215546Sopenharmony_ci lp->samplers[PIPE_SHADER_FRAGMENT][i]); 4454bf215546Sopenharmony_ci } 4455bf215546Sopenharmony_ci } 4456bf215546Sopenharmony_ci 4457bf215546Sopenharmony_ci /* 4458bf215546Sopenharmony_ci * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes 4459bf215546Sopenharmony_ci * are dx10-style? Can't really have mixed opcodes, at least not 4460bf215546Sopenharmony_ci * if we want to skip the holes here (without rescanning tgsi). 4461bf215546Sopenharmony_ci */ 4462bf215546Sopenharmony_ci if (shader->info.base.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) { 4463bf215546Sopenharmony_ci for (unsigned i = 0; i < key->nr_sampler_views; ++i) { 4464bf215546Sopenharmony_ci /* 4465bf215546Sopenharmony_ci * Note sview may exceed what's representable by file_mask. 4466bf215546Sopenharmony_ci * This will still work, the only downside is that not actually 4467bf215546Sopenharmony_ci * used views may be included in the shader key. 4468bf215546Sopenharmony_ci */ 4469bf215546Sopenharmony_ci if ((shader->info.base.file_mask[TGSI_FILE_SAMPLER_VIEW] 4470bf215546Sopenharmony_ci & (1u << (i & 31))) || i > 31) { 4471bf215546Sopenharmony_ci lp_sampler_static_texture_state(&fs_sampler[i].texture_state, 4472bf215546Sopenharmony_ci lp->sampler_views[PIPE_SHADER_FRAGMENT][i]); 4473bf215546Sopenharmony_ci } 4474bf215546Sopenharmony_ci } 4475bf215546Sopenharmony_ci } 4476bf215546Sopenharmony_ci else { 4477bf215546Sopenharmony_ci key->nr_sampler_views = key->nr_samplers; 4478bf215546Sopenharmony_ci for (unsigned i = 0; i < key->nr_sampler_views; ++i) { 4479bf215546Sopenharmony_ci if ((shader->info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) || i > 31) { 4480bf215546Sopenharmony_ci lp_sampler_static_texture_state(&fs_sampler[i].texture_state, 4481bf215546Sopenharmony_ci lp->sampler_views[PIPE_SHADER_FRAGMENT][i]); 4482bf215546Sopenharmony_ci } 4483bf215546Sopenharmony_ci } 4484bf215546Sopenharmony_ci } 4485bf215546Sopenharmony_ci 4486bf215546Sopenharmony_ci struct lp_image_static_state *lp_image = lp_fs_variant_key_images(key); 4487bf215546Sopenharmony_ci key->nr_images = shader->info.base.file_max[TGSI_FILE_IMAGE] + 1; 4488bf215546Sopenharmony_ci 4489bf215546Sopenharmony_ci if (key->nr_images) 4490bf215546Sopenharmony_ci memset(lp_image, 0, 4491bf215546Sopenharmony_ci key->nr_images * sizeof *lp_image); 4492bf215546Sopenharmony_ci for (unsigned i = 0; i < key->nr_images; ++i) { 4493bf215546Sopenharmony_ci if ((shader->info.base.file_mask[TGSI_FILE_IMAGE] & (1 << i)) || i > 31) { 4494bf215546Sopenharmony_ci lp_sampler_static_texture_state_image(&lp_image[i].image_state, 4495bf215546Sopenharmony_ci &lp->images[PIPE_SHADER_FRAGMENT][i]); 4496bf215546Sopenharmony_ci } 4497bf215546Sopenharmony_ci } 4498bf215546Sopenharmony_ci 4499bf215546Sopenharmony_ci if (shader->kind == LP_FS_KIND_AERO_MINIFICATION) { 4500bf215546Sopenharmony_ci struct lp_sampler_static_state *samp0 = 4501bf215546Sopenharmony_ci lp_fs_variant_key_sampler_idx(key, 0); 4502bf215546Sopenharmony_ci assert(samp0); 4503bf215546Sopenharmony_ci samp0->sampler_state.min_img_filter = PIPE_TEX_FILTER_NEAREST; 4504bf215546Sopenharmony_ci samp0->sampler_state.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 4505bf215546Sopenharmony_ci } 4506bf215546Sopenharmony_ci 4507bf215546Sopenharmony_ci return key; 4508bf215546Sopenharmony_ci} 4509bf215546Sopenharmony_ci 4510bf215546Sopenharmony_ci 4511bf215546Sopenharmony_ci/** 4512bf215546Sopenharmony_ci * Update fragment shader state. This is called just prior to drawing 4513bf215546Sopenharmony_ci * something when some fragment-related state has changed. 4514bf215546Sopenharmony_ci */ 4515bf215546Sopenharmony_civoid 4516bf215546Sopenharmony_cillvmpipe_update_fs(struct llvmpipe_context *lp) 4517bf215546Sopenharmony_ci{ 4518bf215546Sopenharmony_ci struct lp_fragment_shader *shader = lp->fs; 4519bf215546Sopenharmony_ci 4520bf215546Sopenharmony_ci char store[LP_FS_MAX_VARIANT_KEY_SIZE]; 4521bf215546Sopenharmony_ci const struct lp_fragment_shader_variant_key *key = 4522bf215546Sopenharmony_ci make_variant_key(lp, shader, store); 4523bf215546Sopenharmony_ci 4524bf215546Sopenharmony_ci struct lp_fragment_shader_variant *variant = NULL; 4525bf215546Sopenharmony_ci struct lp_fs_variant_list_item *li; 4526bf215546Sopenharmony_ci /* Search the variants for one which matches the key */ 4527bf215546Sopenharmony_ci LIST_FOR_EACH_ENTRY(li, &shader->variants.list, list) { 4528bf215546Sopenharmony_ci if (memcmp(&li->base->key, key, shader->variant_key_size) == 0) { 4529bf215546Sopenharmony_ci variant = li->base; 4530bf215546Sopenharmony_ci break; 4531bf215546Sopenharmony_ci } 4532bf215546Sopenharmony_ci } 4533bf215546Sopenharmony_ci 4534bf215546Sopenharmony_ci if (variant) { 4535bf215546Sopenharmony_ci /* Move this variant to the head of the list to implement LRU 4536bf215546Sopenharmony_ci * deletion of shader's when we have too many. 4537bf215546Sopenharmony_ci */ 4538bf215546Sopenharmony_ci list_move_to(&variant->list_item_global.list, &lp->fs_variants_list.list); 4539bf215546Sopenharmony_ci } 4540bf215546Sopenharmony_ci else { 4541bf215546Sopenharmony_ci /* variant not found, create it now */ 4542bf215546Sopenharmony_ci 4543bf215546Sopenharmony_ci if (LP_DEBUG & DEBUG_FS) { 4544bf215546Sopenharmony_ci debug_printf("%u variants,\t%u instrs,\t%u instrs/variant\n", 4545bf215546Sopenharmony_ci lp->nr_fs_variants, 4546bf215546Sopenharmony_ci lp->nr_fs_instrs, 4547bf215546Sopenharmony_ci lp->nr_fs_variants ? lp->nr_fs_instrs / lp->nr_fs_variants : 0); 4548bf215546Sopenharmony_ci } 4549bf215546Sopenharmony_ci 4550bf215546Sopenharmony_ci /* First, check if we've exceeded the max number of shader variants. 4551bf215546Sopenharmony_ci * If so, free 6.25% of them (the least recently used ones). 4552bf215546Sopenharmony_ci */ 4553bf215546Sopenharmony_ci const unsigned variants_to_cull = 4554bf215546Sopenharmony_ci lp->nr_fs_variants >= LP_MAX_SHADER_VARIANTS 4555bf215546Sopenharmony_ci ? LP_MAX_SHADER_VARIANTS / 16 : 0; 4556bf215546Sopenharmony_ci 4557bf215546Sopenharmony_ci if (variants_to_cull || 4558bf215546Sopenharmony_ci lp->nr_fs_instrs >= LP_MAX_SHADER_INSTRUCTIONS) { 4559bf215546Sopenharmony_ci if (gallivm_debug & GALLIVM_DEBUG_PERF) { 4560bf215546Sopenharmony_ci debug_printf("Evicting FS: %u fs variants,\t%u total variants," 4561bf215546Sopenharmony_ci "\t%u instrs,\t%u instrs/variant\n", 4562bf215546Sopenharmony_ci shader->variants_cached, 4563bf215546Sopenharmony_ci lp->nr_fs_variants, lp->nr_fs_instrs, 4564bf215546Sopenharmony_ci lp->nr_fs_instrs / lp->nr_fs_variants); 4565bf215546Sopenharmony_ci } 4566bf215546Sopenharmony_ci 4567bf215546Sopenharmony_ci /* 4568bf215546Sopenharmony_ci * We need to re-check lp->nr_fs_variants because an arbitrarliy large 4569bf215546Sopenharmony_ci * number of shader variants (potentially all of them) could be 4570bf215546Sopenharmony_ci * pending for destruction on flush. 4571bf215546Sopenharmony_ci */ 4572bf215546Sopenharmony_ci 4573bf215546Sopenharmony_ci for (unsigned i = 0; 4574bf215546Sopenharmony_ci i < variants_to_cull || 4575bf215546Sopenharmony_ci lp->nr_fs_instrs >= LP_MAX_SHADER_INSTRUCTIONS; 4576bf215546Sopenharmony_ci i++) { 4577bf215546Sopenharmony_ci struct lp_fs_variant_list_item *item; 4578bf215546Sopenharmony_ci if (list_is_empty(&lp->fs_variants_list.list)) { 4579bf215546Sopenharmony_ci break; 4580bf215546Sopenharmony_ci } 4581bf215546Sopenharmony_ci item = list_last_entry(&lp->fs_variants_list.list, 4582bf215546Sopenharmony_ci struct lp_fs_variant_list_item, list); 4583bf215546Sopenharmony_ci assert(item); 4584bf215546Sopenharmony_ci assert(item->base); 4585bf215546Sopenharmony_ci llvmpipe_remove_shader_variant(lp, item->base); 4586bf215546Sopenharmony_ci struct lp_fragment_shader_variant *variant = item->base; 4587bf215546Sopenharmony_ci lp_fs_variant_reference(lp, &variant, NULL); 4588bf215546Sopenharmony_ci } 4589bf215546Sopenharmony_ci } 4590bf215546Sopenharmony_ci 4591bf215546Sopenharmony_ci /* 4592bf215546Sopenharmony_ci * Generate the new variant. 4593bf215546Sopenharmony_ci */ 4594bf215546Sopenharmony_ci int64_t t0 = os_time_get(); 4595bf215546Sopenharmony_ci variant = generate_variant(lp, shader, key); 4596bf215546Sopenharmony_ci int64_t t1 = os_time_get(); 4597bf215546Sopenharmony_ci int64_t dt = t1 - t0; 4598bf215546Sopenharmony_ci LP_COUNT_ADD(llvm_compile_time, dt); 4599bf215546Sopenharmony_ci LP_COUNT_ADD(nr_llvm_compiles, 2); /* emit vs. omit in/out test */ 4600bf215546Sopenharmony_ci 4601bf215546Sopenharmony_ci /* Put the new variant into the list */ 4602bf215546Sopenharmony_ci if (variant) { 4603bf215546Sopenharmony_ci list_add(&variant->list_item_local.list, &shader->variants.list); 4604bf215546Sopenharmony_ci list_add(&variant->list_item_global.list, &lp->fs_variants_list.list); 4605bf215546Sopenharmony_ci lp->nr_fs_variants++; 4606bf215546Sopenharmony_ci lp->nr_fs_instrs += variant->nr_instrs; 4607bf215546Sopenharmony_ci shader->variants_cached++; 4608bf215546Sopenharmony_ci } 4609bf215546Sopenharmony_ci } 4610bf215546Sopenharmony_ci 4611bf215546Sopenharmony_ci /* Bind this variant */ 4612bf215546Sopenharmony_ci lp_setup_set_fs_variant(lp->setup, variant); 4613bf215546Sopenharmony_ci} 4614bf215546Sopenharmony_ci 4615bf215546Sopenharmony_ci 4616bf215546Sopenharmony_civoid 4617bf215546Sopenharmony_cillvmpipe_init_fs_funcs(struct llvmpipe_context *llvmpipe) 4618bf215546Sopenharmony_ci{ 4619bf215546Sopenharmony_ci llvmpipe->pipe.create_fs_state = llvmpipe_create_fs_state; 4620bf215546Sopenharmony_ci llvmpipe->pipe.bind_fs_state = llvmpipe_bind_fs_state; 4621bf215546Sopenharmony_ci llvmpipe->pipe.delete_fs_state = llvmpipe_delete_fs_state; 4622bf215546Sopenharmony_ci llvmpipe->pipe.set_constant_buffer = llvmpipe_set_constant_buffer; 4623bf215546Sopenharmony_ci llvmpipe->pipe.set_shader_buffers = llvmpipe_set_shader_buffers; 4624bf215546Sopenharmony_ci llvmpipe->pipe.set_shader_images = llvmpipe_set_shader_images; 4625bf215546Sopenharmony_ci} 4626