Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_nir_lower_txf_ms.c
4570 views
/*1* Copyright © 2015 Broadcom2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "vc4_qir.h"24#include "kernel/vc4_packet.h"25#include "compiler/nir/nir_builder.h"2627/** @file vc4_nir_lower_txf_ms.c28* Walks the NIR generated by TGSI-to-NIR to lower its nir_texop_txf_ms29* coordinates to do the math necessary and use a plain nir_texop_txf instead.30*31* MSAA textures are laid out as 32x32-aligned blocks of RGBA8888 or Z24S8.32* We can't load them through the normal sampler path because of the lack of33* linear support in the hardware. So, we treat MSAA textures as a giant UBO34* and do the math in the shader.35*/3637static nir_ssa_def *38vc4_nir_lower_txf_ms_instr(nir_builder *b, nir_instr *instr, void *data)39{40nir_tex_instr *txf_ms = nir_instr_as_tex(instr);41const struct vc4_compile *c = data;4243nir_tex_instr *txf = nir_tex_instr_create(c->s, 1);44txf->op = nir_texop_txf;45txf->texture_index = txf_ms->texture_index;46txf->coord_components = txf_ms->coord_components;47txf->is_shadow = txf_ms->is_shadow;48txf->is_new_style_shadow = txf_ms->is_new_style_shadow;4950nir_ssa_def *coord = NULL, *sample_index = NULL;51for (int i = 0; i < txf_ms->num_srcs; i++) {52assert(txf_ms->src[i].src.is_ssa);5354switch (txf_ms->src[i].src_type) {55case nir_tex_src_coord:56coord = txf_ms->src[i].src.ssa;57break;58case nir_tex_src_ms_index:59sample_index = txf_ms->src[i].src.ssa;60break;61default:62unreachable("Unknown txf_ms src\n");63}64}65assert(coord);66assert(sample_index);6768nir_ssa_def *x = nir_channel(b, coord, 0);69nir_ssa_def *y = nir_channel(b, coord, 1);7071uint32_t tile_w = 32;72uint32_t tile_h = 32;73uint32_t tile_w_shift = 5;74uint32_t tile_h_shift = 5;75uint32_t tile_size = (tile_h * tile_w *76VC4_MAX_SAMPLES * sizeof(uint32_t));77unsigned unit = txf_ms->texture_index;78uint32_t w = align(c->key->tex[unit].msaa_width, tile_w);79uint32_t w_tiles = w / tile_w;8081nir_ssa_def *x_tile = nir_ushr(b, x, nir_imm_int(b, tile_w_shift));82nir_ssa_def *y_tile = nir_ushr(b, y, nir_imm_int(b, tile_h_shift));83nir_ssa_def *tile_addr = nir_iadd(b,84nir_imul(b, x_tile,85nir_imm_int(b, tile_size)),86nir_imul(b, y_tile,87nir_imm_int(b, (w_tiles *88tile_size))));89nir_ssa_def *x_subspan = nir_iand(b, x,90nir_imm_int(b, (tile_w - 1) & ~1));91nir_ssa_def *y_subspan = nir_iand(b, y,92nir_imm_int(b, (tile_h - 1) & ~1));93nir_ssa_def *subspan_addr = nir_iadd(b,94nir_imul(b, x_subspan,95nir_imm_int(b, 2 * VC4_MAX_SAMPLES * sizeof(uint32_t))),96nir_imul(b, y_subspan,97nir_imm_int(b,98tile_w *99VC4_MAX_SAMPLES *100sizeof(uint32_t))));101102nir_ssa_def *pixel_addr = nir_ior(b,103nir_iand(b,104nir_ishl(b, x,105nir_imm_int(b, 2)),106nir_imm_int(b, (1 << 2))),107nir_iand(b,108nir_ishl(b, y,109nir_imm_int(b, 3)),110nir_imm_int(b, (1 << 3))));111112nir_ssa_def *sample_addr = nir_ishl(b, sample_index, nir_imm_int(b, 4));113114nir_ssa_def *addr = nir_iadd(b,115nir_ior(b, sample_addr, pixel_addr),116nir_iadd(b, subspan_addr, tile_addr));117118txf->src[0].src_type = nir_tex_src_coord;119txf->src[0].src = nir_src_for_ssa(nir_vec2(b, addr, nir_imm_int(b, 0)));120nir_ssa_dest_init(&txf->instr, &txf->dest, 4, 32, NULL);121nir_builder_instr_insert(b, &txf->instr);122123return &txf->dest.ssa;124}125126static bool127vc4_nir_lower_txf_ms_filter(const nir_instr *instr, const void *data)128{129return (instr->type == nir_instr_type_tex &&130nir_instr_as_tex(instr)->op == nir_texop_txf_ms);131}132133void134vc4_nir_lower_txf_ms(nir_shader *s, struct vc4_compile *c)135{136nir_shader_lower_instructions(s,137vc4_nir_lower_txf_ms_filter,138vc4_nir_lower_txf_ms_instr,139c);140}141142143