Path: blob/21.2-virgl/src/intel/compiler/brw_fs_visitor.cpp
4550 views
/*1* Copyright © 2010 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* 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/** @file brw_fs_visitor.cpp24*25* This file supports generating the FS LIR from the GLSL IR. The LIR26* makes it easier to do backend-specific optimizations than doing so27* in the GLSL IR or in the native code.28*/29#include "brw_fs.h"30#include "compiler/glsl_types.h"3132using namespace brw;3334/* Sample from the MCS surface attached to this multisample texture. */35fs_reg36fs_visitor::emit_mcs_fetch(const fs_reg &coordinate, unsigned components,37const fs_reg &texture,38const fs_reg &texture_handle)39{40const fs_reg dest = vgrf(glsl_type::uvec4_type);4142fs_reg srcs[TEX_LOGICAL_NUM_SRCS];43srcs[TEX_LOGICAL_SRC_COORDINATE] = coordinate;44srcs[TEX_LOGICAL_SRC_SURFACE] = texture;45srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(0);46srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE] = texture_handle;47srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(components);48srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(0);4950fs_inst *inst = bld.emit(SHADER_OPCODE_TXF_MCS_LOGICAL, dest, srcs,51ARRAY_SIZE(srcs));5253/* We only care about one or two regs of response, but the sampler always54* writes 4/8.55*/56inst->size_written = 4 * dest.component_size(inst->exec_size);5758return dest;59}6061/**62* Apply workarounds for Gfx6 gather with UINT/SINT63*/64void65fs_visitor::emit_gfx6_gather_wa(uint8_t wa, fs_reg dst)66{67if (!wa)68return;6970int width = (wa & WA_8BIT) ? 8 : 16;7172for (int i = 0; i < 4; i++) {73fs_reg dst_f = retype(dst, BRW_REGISTER_TYPE_F);74/* Convert from UNORM to UINT */75bld.MUL(dst_f, dst_f, brw_imm_f((1 << width) - 1));76bld.MOV(dst, dst_f);7778if (wa & WA_SIGN) {79/* Reinterpret the UINT value as a signed INT value by80* shifting the sign bit into place, then shifting back81* preserving sign.82*/83bld.SHL(dst, dst, brw_imm_d(32 - width));84bld.ASR(dst, dst, brw_imm_d(32 - width));85}8687dst = offset(dst, bld, 1);88}89}9091/** Emits a dummy fragment shader consisting of magenta for bringup purposes. */92void93fs_visitor::emit_dummy_fs()94{95int reg_width = dispatch_width / 8;9697/* Everyone's favorite color. */98const float color[4] = { 1.0, 0.0, 1.0, 0.0 };99for (int i = 0; i < 4; i++) {100bld.MOV(fs_reg(MRF, 2 + i * reg_width, BRW_REGISTER_TYPE_F),101brw_imm_f(color[i]));102}103104fs_inst *write;105write = bld.emit(FS_OPCODE_FB_WRITE);106write->eot = true;107write->last_rt = true;108if (devinfo->ver >= 6) {109write->base_mrf = 2;110write->mlen = 4 * reg_width;111} else {112write->header_size = 2;113write->base_mrf = 0;114write->mlen = 2 + 4 * reg_width;115}116117/* Tell the SF we don't have any inputs. Gfx4-5 require at least one118* varying to avoid GPU hangs, so set that.119*/120struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(this->prog_data);121wm_prog_data->num_varying_inputs = devinfo->ver < 6 ? 1 : 0;122memset(wm_prog_data->urb_setup, -1,123sizeof(wm_prog_data->urb_setup[0]) * VARYING_SLOT_MAX);124brw_compute_urb_setup_index(wm_prog_data);125126/* We don't have any uniforms. */127stage_prog_data->nr_params = 0;128stage_prog_data->nr_pull_params = 0;129stage_prog_data->curb_read_length = 0;130stage_prog_data->dispatch_grf_start_reg = 2;131wm_prog_data->dispatch_grf_start_reg_16 = 2;132wm_prog_data->dispatch_grf_start_reg_32 = 2;133grf_used = 1; /* Gfx4-5 don't allow zero GRF blocks */134135calculate_cfg();136}137138/* The register location here is relative to the start of the URB139* data. It will get adjusted to be a real location before140* generate_code() time.141*/142fs_reg143fs_visitor::interp_reg(int location, int channel)144{145assert(stage == MESA_SHADER_FRAGMENT);146struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);147int regnr = prog_data->urb_setup[location] * 4 + channel;148assert(prog_data->urb_setup[location] != -1);149150return fs_reg(ATTR, regnr, BRW_REGISTER_TYPE_F);151}152153/** Emits the interpolation for the varying inputs. */154void155fs_visitor::emit_interpolation_setup_gfx4()156{157struct brw_reg g1_uw = retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW);158159fs_builder abld = bld.annotate("compute pixel centers");160this->pixel_x = vgrf(glsl_type::uint_type);161this->pixel_y = vgrf(glsl_type::uint_type);162this->pixel_x.type = BRW_REGISTER_TYPE_UW;163this->pixel_y.type = BRW_REGISTER_TYPE_UW;164abld.ADD(this->pixel_x,165fs_reg(stride(suboffset(g1_uw, 4), 2, 4, 0)),166fs_reg(brw_imm_v(0x10101010)));167abld.ADD(this->pixel_y,168fs_reg(stride(suboffset(g1_uw, 5), 2, 4, 0)),169fs_reg(brw_imm_v(0x11001100)));170171abld = bld.annotate("compute pixel deltas from v0");172173this->delta_xy[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL] =174vgrf(glsl_type::vec2_type);175const fs_reg &delta_xy = this->delta_xy[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL];176const fs_reg xstart(negate(brw_vec1_grf(1, 0)));177const fs_reg ystart(negate(brw_vec1_grf(1, 1)));178179if (devinfo->has_pln) {180for (unsigned i = 0; i < dispatch_width / 8; i++) {181abld.quarter(i).ADD(quarter(offset(delta_xy, abld, 0), i),182quarter(this->pixel_x, i), xstart);183abld.quarter(i).ADD(quarter(offset(delta_xy, abld, 1), i),184quarter(this->pixel_y, i), ystart);185}186} else {187abld.ADD(offset(delta_xy, abld, 0), this->pixel_x, xstart);188abld.ADD(offset(delta_xy, abld, 1), this->pixel_y, ystart);189}190191this->pixel_z = fetch_payload_reg(bld, payload.source_depth_reg);192193/* The SF program automatically handles doing the perspective correction or194* not based on wm_prog_data::interp_mode[] so we can use the same pixel195* offsets for both perspective and non-perspective.196*/197this->delta_xy[BRW_BARYCENTRIC_NONPERSPECTIVE_PIXEL] =198this->delta_xy[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL];199200abld = bld.annotate("compute pos.w and 1/pos.w");201/* Compute wpos.w. It's always in our setup, since it's needed to202* interpolate the other attributes.203*/204this->wpos_w = vgrf(glsl_type::float_type);205abld.emit(FS_OPCODE_LINTERP, wpos_w, delta_xy,206component(interp_reg(VARYING_SLOT_POS, 3), 0));207/* Compute the pixel 1/W value from wpos.w. */208this->pixel_w = vgrf(glsl_type::float_type);209abld.emit(SHADER_OPCODE_RCP, this->pixel_w, wpos_w);210}211212static unsigned213brw_rnd_mode_from_nir(unsigned mode, unsigned *mask)214{215unsigned brw_mode = 0;216*mask = 0;217218if ((FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16 |219FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32 |220FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64) &221mode) {222brw_mode |= BRW_RND_MODE_RTZ << BRW_CR0_RND_MODE_SHIFT;223*mask |= BRW_CR0_RND_MODE_MASK;224}225if ((FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16 |226FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32 |227FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64) &228mode) {229brw_mode |= BRW_RND_MODE_RTNE << BRW_CR0_RND_MODE_SHIFT;230*mask |= BRW_CR0_RND_MODE_MASK;231}232if (mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP16) {233brw_mode |= BRW_CR0_FP16_DENORM_PRESERVE;234*mask |= BRW_CR0_FP16_DENORM_PRESERVE;235}236if (mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP32) {237brw_mode |= BRW_CR0_FP32_DENORM_PRESERVE;238*mask |= BRW_CR0_FP32_DENORM_PRESERVE;239}240if (mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP64) {241brw_mode |= BRW_CR0_FP64_DENORM_PRESERVE;242*mask |= BRW_CR0_FP64_DENORM_PRESERVE;243}244if (mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16)245*mask |= BRW_CR0_FP16_DENORM_PRESERVE;246if (mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32)247*mask |= BRW_CR0_FP32_DENORM_PRESERVE;248if (mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64)249*mask |= BRW_CR0_FP64_DENORM_PRESERVE;250if (mode == FLOAT_CONTROLS_DEFAULT_FLOAT_CONTROL_MODE)251*mask |= BRW_CR0_FP_MODE_MASK;252253if (*mask != 0)254assert((*mask & brw_mode) == brw_mode);255256return brw_mode;257}258259void260fs_visitor::emit_shader_float_controls_execution_mode()261{262unsigned execution_mode = this->nir->info.float_controls_execution_mode;263if (execution_mode == FLOAT_CONTROLS_DEFAULT_FLOAT_CONTROL_MODE)264return;265266fs_builder abld = bld.annotate("shader floats control execution mode");267unsigned mask, mode = brw_rnd_mode_from_nir(execution_mode, &mask);268269if (mask == 0)270return;271272abld.emit(SHADER_OPCODE_FLOAT_CONTROL_MODE, bld.null_reg_ud(),273brw_imm_d(mode), brw_imm_d(mask));274}275276/** Emits the interpolation for the varying inputs. */277void278fs_visitor::emit_interpolation_setup_gfx6()279{280fs_builder abld = bld.annotate("compute pixel centers");281282this->pixel_x = vgrf(glsl_type::float_type);283this->pixel_y = vgrf(glsl_type::float_type);284285struct brw_wm_prog_data *wm_prog_data = brw_wm_prog_data(prog_data);286287fs_reg int_pixel_offset_x, int_pixel_offset_y; /* Used on Gen12HP+ */288fs_reg int_pixel_offset_xy; /* Used on Gen8+ */289fs_reg half_int_pixel_offset_x, half_int_pixel_offset_y;290if (!wm_prog_data->per_coarse_pixel_dispatch) {291/* The thread payload only delivers subspan locations (ss0, ss1,292* ss2, ...). Since subspans covers 2x2 pixels blocks, we need to293* generate 4 pixel coordinates out of each subspan location. We do this294* by replicating a subspan coordinate 4 times and adding an offset of 1295* in each direction from the initial top left (tl) location to generate296* top right (tr = +1 in x), bottom left (bl = +1 in y) and bottom right297* (br = +1 in x, +1 in y).298*299* The locations we build look like this in SIMD8 :300*301* ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br302*303* The value 0x11001010 is a vector of 8 half byte vector. It adds304* following to generate the 4 pixels coordinates out of the subspan0:305*306* 0x307* 1 : ss0.y + 1 -> ss0.br.y308* 1 : ss0.y + 1 -> ss0.bl.y309* 0 : ss0.y + 0 -> ss0.tr.y310* 0 : ss0.y + 0 -> ss0.tl.y311* 1 : ss0.x + 1 -> ss0.br.x312* 0 : ss0.x + 0 -> ss0.bl.x313* 1 : ss0.x + 1 -> ss0.tr.x314* 0 : ss0.x + 0 -> ss0.tl.x315*316* By doing a SIMD16 add in a SIMD8 shader, we can generate the 8 pixels317* coordinates out of 2 subspans coordinates in a single ADD instruction318* (twice the operation above).319*/320int_pixel_offset_xy = fs_reg(brw_imm_v(0x11001010));321half_int_pixel_offset_x = fs_reg(brw_imm_uw(0));322half_int_pixel_offset_y = fs_reg(brw_imm_uw(0));323/* On Gfx12.5, because of regioning restrictions, the interpolation code324* is slightly different and works off X & Y only inputs. The ordering325* of the half bytes here is a bit odd, with each subspan replicated326* twice and every other element is discarded :327*328* ss0.tl ss0.tl ss0.tr ss0.tr ss0.bl ss0.bl ss0.br ss0.br329* X offset: 0 0 1 0 0 0 1 0330* Y offset: 0 0 0 0 1 0 1 0331*/332int_pixel_offset_x = fs_reg(brw_imm_v(0x01000100));333int_pixel_offset_y = fs_reg(brw_imm_v(0x01010000));334} else {335/* In coarse pixel dispatch we have to do the same ADD instruction that336* we do in normal per pixel dispatch, except this time we're not adding337* 1 in each direction, but instead the coarse pixel size.338*339* The coarse pixel size is delivered as 2 u8 in r1.0340*/341struct brw_reg r1_0 = retype(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE, 1, 0), BRW_REGISTER_TYPE_UB);342343const fs_builder dbld =344abld.exec_all().group(MIN2(16, dispatch_width) * 2, 0);345346if (devinfo->verx10 >= 125) {347/* To build the array of half bytes we do and AND operation with the348* right mask in X.349*/350int_pixel_offset_x = dbld.vgrf(BRW_REGISTER_TYPE_UW);351dbld.AND(int_pixel_offset_x, byte_offset(r1_0, 0), brw_imm_v(0x0f000f00));352353/* And the right mask in Y. */354int_pixel_offset_y = dbld.vgrf(BRW_REGISTER_TYPE_UW);355dbld.AND(int_pixel_offset_y, byte_offset(r1_0, 1), brw_imm_v(0x0f0f0000));356} else {357/* To build the array of half bytes we do and AND operation with the358* right mask in X.359*/360int_pixel_offset_x = dbld.vgrf(BRW_REGISTER_TYPE_UW);361dbld.AND(int_pixel_offset_x, byte_offset(r1_0, 0), brw_imm_v(0x0000f0f0));362363/* And the right mask in Y. */364int_pixel_offset_y = dbld.vgrf(BRW_REGISTER_TYPE_UW);365dbld.AND(int_pixel_offset_y, byte_offset(r1_0, 1), brw_imm_v(0xff000000));366367/* Finally OR the 2 registers. */368int_pixel_offset_xy = dbld.vgrf(BRW_REGISTER_TYPE_UW);369dbld.OR(int_pixel_offset_xy, int_pixel_offset_x, int_pixel_offset_y);370}371372/* Also compute the half pixel size used to center pixels. */373half_int_pixel_offset_x = bld.vgrf(BRW_REGISTER_TYPE_UW);374half_int_pixel_offset_y = bld.vgrf(BRW_REGISTER_TYPE_UW);375376bld.SHR(half_int_pixel_offset_x, suboffset(r1_0, 0), brw_imm_ud(1));377bld.SHR(half_int_pixel_offset_y, suboffset(r1_0, 1), brw_imm_ud(1));378}379380for (unsigned i = 0; i < DIV_ROUND_UP(dispatch_width, 16); i++) {381const fs_builder hbld = abld.group(MIN2(16, dispatch_width), i);382struct brw_reg gi_uw = retype(brw_vec1_grf(1 + i, 0), BRW_REGISTER_TYPE_UW);383384if (devinfo->verx10 >= 125) {385const fs_builder dbld =386abld.exec_all().group(hbld.dispatch_width() * 2, 0);387const fs_reg int_pixel_x = dbld.vgrf(BRW_REGISTER_TYPE_UW);388const fs_reg int_pixel_y = dbld.vgrf(BRW_REGISTER_TYPE_UW);389390dbld.ADD(int_pixel_x,391fs_reg(stride(suboffset(gi_uw, 4), 2, 8, 0)),392int_pixel_offset_x);393dbld.ADD(int_pixel_y,394fs_reg(stride(suboffset(gi_uw, 5), 2, 8, 0)),395int_pixel_offset_y);396397if (wm_prog_data->per_coarse_pixel_dispatch) {398dbld.ADD(int_pixel_x, int_pixel_x,399horiz_stride(half_int_pixel_offset_x, 0));400dbld.ADD(int_pixel_y, int_pixel_y,401horiz_stride(half_int_pixel_offset_y, 0));402}403404hbld.MOV(offset(pixel_x, hbld, i), horiz_stride(int_pixel_x, 2));405hbld.MOV(offset(pixel_y, hbld, i), horiz_stride(int_pixel_y, 2));406407} else if (devinfo->ver >= 8 || dispatch_width == 8) {408/* The "Register Region Restrictions" page says for BDW (and newer,409* presumably):410*411* "When destination spans two registers, the source may be one or412* two registers. The destination elements must be evenly split413* between the two registers."414*415* Thus we can do a single add(16) in SIMD8 or an add(32) in SIMD16416* to compute our pixel centers.417*/418const fs_builder dbld =419abld.exec_all().group(hbld.dispatch_width() * 2, 0);420fs_reg int_pixel_xy = dbld.vgrf(BRW_REGISTER_TYPE_UW);421422dbld.ADD(int_pixel_xy,423fs_reg(stride(suboffset(gi_uw, 4), 1, 4, 0)),424int_pixel_offset_xy);425426hbld.emit(FS_OPCODE_PIXEL_X, offset(pixel_x, hbld, i), int_pixel_xy,427horiz_stride(half_int_pixel_offset_x, 0));428hbld.emit(FS_OPCODE_PIXEL_Y, offset(pixel_y, hbld, i), int_pixel_xy,429horiz_stride(half_int_pixel_offset_y, 0));430} else {431/* The "Register Region Restrictions" page says for SNB, IVB, HSW:432*433* "When destination spans two registers, the source MUST span434* two registers."435*436* Since the GRF source of the ADD will only read a single register,437* we must do two separate ADDs in SIMD16.438*/439const fs_reg int_pixel_x = hbld.vgrf(BRW_REGISTER_TYPE_UW);440const fs_reg int_pixel_y = hbld.vgrf(BRW_REGISTER_TYPE_UW);441442hbld.ADD(int_pixel_x,443fs_reg(stride(suboffset(gi_uw, 4), 2, 4, 0)),444fs_reg(brw_imm_v(0x10101010)));445hbld.ADD(int_pixel_y,446fs_reg(stride(suboffset(gi_uw, 5), 2, 4, 0)),447fs_reg(brw_imm_v(0x11001100)));448449/* As of gfx6, we can no longer mix float and int sources. We have450* to turn the integer pixel centers into floats for their actual451* use.452*/453hbld.MOV(offset(pixel_x, hbld, i), int_pixel_x);454hbld.MOV(offset(pixel_y, hbld, i), int_pixel_y);455}456}457458abld = bld.annotate("compute pos.z");459if (wm_prog_data->uses_depth_w_coefficients) {460assert(!wm_prog_data->uses_src_depth);461/* In coarse pixel mode, the HW doesn't interpolate Z coordinate462* properly. In the same way we have to add the coarse pixel size to463* pixels locations, here we recompute the Z value with 2 coefficients464* in X & Y axis.465*/466fs_reg coef_payload = fetch_payload_reg(abld, payload.depth_w_coef_reg, BRW_REGISTER_TYPE_F);467const fs_reg x_start = brw_vec1_grf(coef_payload.nr, 2);468const fs_reg y_start = brw_vec1_grf(coef_payload.nr, 6);469const fs_reg z_cx = brw_vec1_grf(coef_payload.nr, 1);470const fs_reg z_cy = brw_vec1_grf(coef_payload.nr, 0);471const fs_reg z_c0 = brw_vec1_grf(coef_payload.nr, 3);472473const fs_reg float_pixel_x = abld.vgrf(BRW_REGISTER_TYPE_F);474const fs_reg float_pixel_y = abld.vgrf(BRW_REGISTER_TYPE_F);475476abld.ADD(float_pixel_x, this->pixel_x, negate(x_start));477abld.ADD(float_pixel_y, this->pixel_y, negate(y_start));478479/* r1.0 - 0:7 ActualCoarsePixelShadingSize.X */480const fs_reg u8_cps_width = fs_reg(retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UB));481/* r1.0 - 15:8 ActualCoarsePixelShadingSize.Y */482const fs_reg u8_cps_height = byte_offset(u8_cps_width, 1);483const fs_reg u32_cps_width = abld.vgrf(BRW_REGISTER_TYPE_UD);484const fs_reg u32_cps_height = abld.vgrf(BRW_REGISTER_TYPE_UD);485abld.MOV(u32_cps_width, u8_cps_width);486abld.MOV(u32_cps_height, u8_cps_height);487488const fs_reg f_cps_width = abld.vgrf(BRW_REGISTER_TYPE_F);489const fs_reg f_cps_height = abld.vgrf(BRW_REGISTER_TYPE_F);490abld.MOV(f_cps_width, u32_cps_width);491abld.MOV(f_cps_height, u32_cps_height);492493/* Center in the middle of the coarse pixel. */494abld.MAD(float_pixel_x, float_pixel_x, brw_imm_f(0.5f), f_cps_width);495abld.MAD(float_pixel_y, float_pixel_y, brw_imm_f(0.5f), f_cps_height);496497this->pixel_z = abld.vgrf(BRW_REGISTER_TYPE_F);498abld.MAD(this->pixel_z, z_c0, z_cx, float_pixel_x);499abld.MAD(this->pixel_z, this->pixel_z, z_cy, float_pixel_y);500}501502if (wm_prog_data->uses_src_depth) {503assert(!wm_prog_data->uses_depth_w_coefficients);504this->pixel_z = fetch_payload_reg(bld, payload.source_depth_reg);505}506507if (wm_prog_data->uses_src_w) {508abld = bld.annotate("compute pos.w");509this->pixel_w = fetch_payload_reg(abld, payload.source_w_reg);510this->wpos_w = vgrf(glsl_type::float_type);511abld.emit(SHADER_OPCODE_RCP, this->wpos_w, this->pixel_w);512}513514for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {515this->delta_xy[i] = fetch_barycentric_reg(516bld, payload.barycentric_coord_reg[i]);517}518519uint32_t centroid_modes = wm_prog_data->barycentric_interp_modes &520(1 << BRW_BARYCENTRIC_PERSPECTIVE_CENTROID |5211 << BRW_BARYCENTRIC_NONPERSPECTIVE_CENTROID);522523if (devinfo->needs_unlit_centroid_workaround && centroid_modes) {524/* Get the pixel/sample mask into f0 so that we know which525* pixels are lit. Then, for each channel that is unlit,526* replace the centroid data with non-centroid data.527*/528for (unsigned i = 0; i < DIV_ROUND_UP(dispatch_width, 16); i++) {529bld.exec_all().group(1, 0)530.MOV(retype(brw_flag_reg(0, i), BRW_REGISTER_TYPE_UW),531retype(brw_vec1_grf(1 + i, 7), BRW_REGISTER_TYPE_UW));532}533534for (int i = 0; i < BRW_BARYCENTRIC_MODE_COUNT; ++i) {535if (!(centroid_modes & (1 << i)))536continue;537538const fs_reg centroid_delta_xy = delta_xy[i];539const fs_reg &pixel_delta_xy = delta_xy[i - 1];540541delta_xy[i] = bld.vgrf(BRW_REGISTER_TYPE_F, 2);542543for (unsigned c = 0; c < 2; c++) {544for (unsigned q = 0; q < dispatch_width / 8; q++) {545set_predicate(BRW_PREDICATE_NORMAL,546bld.quarter(q).SEL(547quarter(offset(delta_xy[i], bld, c), q),548quarter(offset(centroid_delta_xy, bld, c), q),549quarter(offset(pixel_delta_xy, bld, c), q)));550}551}552}553}554}555556static enum brw_conditional_mod557cond_for_alpha_func(GLenum func)558{559switch(func) {560case GL_GREATER:561return BRW_CONDITIONAL_G;562case GL_GEQUAL:563return BRW_CONDITIONAL_GE;564case GL_LESS:565return BRW_CONDITIONAL_L;566case GL_LEQUAL:567return BRW_CONDITIONAL_LE;568case GL_EQUAL:569return BRW_CONDITIONAL_EQ;570case GL_NOTEQUAL:571return BRW_CONDITIONAL_NEQ;572default:573unreachable("Not reached");574}575}576577/**578* Alpha test support for when we compile it into the shader instead579* of using the normal fixed-function alpha test.580*/581void582fs_visitor::emit_alpha_test()583{584assert(stage == MESA_SHADER_FRAGMENT);585brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;586const fs_builder abld = bld.annotate("Alpha test");587588fs_inst *cmp;589if (key->alpha_test_func == GL_ALWAYS)590return;591592if (key->alpha_test_func == GL_NEVER) {593/* f0.1 = 0 */594fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),595BRW_REGISTER_TYPE_UW));596cmp = abld.CMP(bld.null_reg_f(), some_reg, some_reg,597BRW_CONDITIONAL_NEQ);598} else {599/* RT0 alpha */600fs_reg color = offset(outputs[0], bld, 3);601602/* f0.1 &= func(color, ref) */603cmp = abld.CMP(bld.null_reg_f(), color, brw_imm_f(key->alpha_test_ref),604cond_for_alpha_func(key->alpha_test_func));605}606cmp->predicate = BRW_PREDICATE_NORMAL;607cmp->flag_subreg = 1;608}609610fs_inst *611fs_visitor::emit_single_fb_write(const fs_builder &bld,612fs_reg color0, fs_reg color1,613fs_reg src0_alpha, unsigned components)614{615assert(stage == MESA_SHADER_FRAGMENT);616struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);617618/* Hand over gl_FragDepth or the payload depth. */619const fs_reg dst_depth = fetch_payload_reg(bld, payload.dest_depth_reg);620fs_reg src_depth, src_stencil;621622if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_DEPTH)) {623src_depth = frag_depth;624} else if (source_depth_to_render_target) {625/* If we got here, we're in one of those strange Gen4-5 cases where626* we're forced to pass the source depth, unmodified, to the FB write.627* In this case, we don't want to use pixel_z because we may not have628* set up interpolation. It's also perfectly safe because it only629* happens on old hardware (no coarse interpolation) and this is630* explicitly the pass-through case.631*/632assert(devinfo->ver <= 5);633src_depth = fetch_payload_reg(bld, payload.source_depth_reg);634}635636if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL))637src_stencil = frag_stencil;638639const fs_reg sources[] = {640color0, color1, src0_alpha, src_depth, dst_depth, src_stencil,641(prog_data->uses_omask ? sample_mask : fs_reg()),642brw_imm_ud(components)643};644assert(ARRAY_SIZE(sources) - 1 == FB_WRITE_LOGICAL_SRC_COMPONENTS);645fs_inst *write = bld.emit(FS_OPCODE_FB_WRITE_LOGICAL, fs_reg(),646sources, ARRAY_SIZE(sources));647648if (prog_data->uses_kill) {649write->predicate = BRW_PREDICATE_NORMAL;650write->flag_subreg = sample_mask_flag_subreg(this);651}652653return write;654}655656void657fs_visitor::emit_fb_writes()658{659assert(stage == MESA_SHADER_FRAGMENT);660struct brw_wm_prog_data *prog_data = brw_wm_prog_data(this->prog_data);661brw_wm_prog_key *key = (brw_wm_prog_key*) this->key;662663fs_inst *inst = NULL;664665if (source_depth_to_render_target && devinfo->ver == 6) {666/* For outputting oDepth on gfx6, SIMD8 writes have to be used. This667* would require SIMD8 moves of each half to message regs, e.g. by using668* the SIMD lowering pass. Unfortunately this is more difficult than it669* sounds because the SIMD8 single-source message lacks channel selects670* for the second and third subspans.671*/672limit_dispatch_width(8, "Depth writes unsupported in SIMD16+ mode.\n");673}674675if (nir->info.outputs_written & BITFIELD64_BIT(FRAG_RESULT_STENCIL)) {676/* From the 'Render Target Write message' section of the docs:677* "Output Stencil is not supported with SIMD16 Render Target Write678* Messages."679*/680limit_dispatch_width(8, "gl_FragStencilRefARB unsupported "681"in SIMD16+ mode.\n");682}683684/* ANV doesn't know about sample mask output during the wm key creation685* so we compute if we need replicate alpha and emit alpha to coverage686* workaround here.687*/688const bool replicate_alpha = key->alpha_test_replicate_alpha ||689(key->nr_color_regions > 1 && key->alpha_to_coverage &&690(sample_mask.file == BAD_FILE || devinfo->ver == 6));691692for (int target = 0; target < key->nr_color_regions; target++) {693/* Skip over outputs that weren't written. */694if (this->outputs[target].file == BAD_FILE)695continue;696697const fs_builder abld = bld.annotate(698ralloc_asprintf(this->mem_ctx, "FB write target %d", target));699700fs_reg src0_alpha;701if (devinfo->ver >= 6 && replicate_alpha && target != 0)702src0_alpha = offset(outputs[0], bld, 3);703704inst = emit_single_fb_write(abld, this->outputs[target],705this->dual_src_output, src0_alpha, 4);706inst->target = target;707}708709prog_data->dual_src_blend = (this->dual_src_output.file != BAD_FILE &&710this->outputs[0].file != BAD_FILE);711assert(!prog_data->dual_src_blend || key->nr_color_regions == 1);712713if (inst == NULL) {714/* Even if there's no color buffers enabled, we still need to send715* alpha out the pipeline to our null renderbuffer to support716* alpha-testing, alpha-to-coverage, and so on.717*/718/* FINISHME: Factor out this frequently recurring pattern into a719* helper function.720*/721const fs_reg srcs[] = { reg_undef, reg_undef,722reg_undef, offset(this->outputs[0], bld, 3) };723const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 4);724bld.LOAD_PAYLOAD(tmp, srcs, 4, 0);725726inst = emit_single_fb_write(bld, tmp, reg_undef, reg_undef, 4);727inst->target = 0;728}729730inst->last_rt = true;731inst->eot = true;732733if (devinfo->ver >= 11 && devinfo->ver <= 12 &&734prog_data->dual_src_blend) {735/* The dual-source RT write messages fail to release the thread736* dependency on ICL and TGL with SIMD32 dispatch, leading to hangs.737*738* XXX - Emit an extra single-source NULL RT-write marked LastRT in739* order to release the thread dependency without disabling740* SIMD32.741*742* The dual-source RT write messages may lead to hangs with SIMD16743* dispatch on ICL due some unknown reasons, see744* https://gitlab.freedesktop.org/mesa/mesa/-/issues/2183745*/746limit_dispatch_width(8, "Dual source blending unsupported "747"in SIMD16 and SIMD32 modes.\n");748}749}750751void752fs_visitor::emit_urb_writes(const fs_reg &gs_vertex_count)753{754int slot, urb_offset, length;755int starting_urb_offset = 0;756const struct brw_vue_prog_data *vue_prog_data =757brw_vue_prog_data(this->prog_data);758const struct brw_vs_prog_key *vs_key =759(const struct brw_vs_prog_key *) this->key;760const GLbitfield64 psiz_mask =761VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT | VARYING_BIT_PSIZ;762const struct brw_vue_map *vue_map = &vue_prog_data->vue_map;763bool flush;764fs_reg sources[8];765fs_reg urb_handle;766767if (stage == MESA_SHADER_TESS_EVAL)768urb_handle = fs_reg(retype(brw_vec8_grf(4, 0), BRW_REGISTER_TYPE_UD));769else770urb_handle = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));771772opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;773int header_size = 1;774fs_reg per_slot_offsets;775776if (stage == MESA_SHADER_GEOMETRY) {777const struct brw_gs_prog_data *gs_prog_data =778brw_gs_prog_data(this->prog_data);779780/* We need to increment the Global Offset to skip over the control data781* header and the extra "Vertex Count" field (1 HWord) at the beginning782* of the VUE. We're counting in OWords, so the units are doubled.783*/784starting_urb_offset = 2 * gs_prog_data->control_data_header_size_hwords;785if (gs_prog_data->static_vertex_count == -1)786starting_urb_offset += 2;787788/* We also need to use per-slot offsets. The per-slot offset is the789* Vertex Count. SIMD8 mode processes 8 different primitives at a790* time; each may output a different number of vertices.791*/792opcode = SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT;793header_size++;794795/* The URB offset is in 128-bit units, so we need to multiply by 2 */796const int output_vertex_size_owords =797gs_prog_data->output_vertex_size_hwords * 2;798799if (gs_vertex_count.file == IMM) {800per_slot_offsets = brw_imm_ud(output_vertex_size_owords *801gs_vertex_count.ud);802} else {803per_slot_offsets = vgrf(glsl_type::uint_type);804bld.MUL(per_slot_offsets, gs_vertex_count,805brw_imm_ud(output_vertex_size_owords));806}807}808809length = 0;810urb_offset = starting_urb_offset;811flush = false;812813/* SSO shaders can have VUE slots allocated which are never actually814* written to, so ignore them when looking for the last (written) slot.815*/816int last_slot = vue_map->num_slots - 1;817while (last_slot > 0 &&818(vue_map->slot_to_varying[last_slot] == BRW_VARYING_SLOT_PAD ||819outputs[vue_map->slot_to_varying[last_slot]].file == BAD_FILE)) {820last_slot--;821}822823bool urb_written = false;824for (slot = 0; slot < vue_map->num_slots; slot++) {825int varying = vue_map->slot_to_varying[slot];826switch (varying) {827case VARYING_SLOT_PSIZ: {828/* The point size varying slot is the vue header and is always in the829* vue map. But often none of the special varyings that live there830* are written and in that case we can skip writing to the vue831* header, provided the corresponding state properly clamps the832* values further down the pipeline. */833if ((vue_map->slots_valid & psiz_mask) == 0) {834assert(length == 0);835urb_offset++;836break;837}838839fs_reg zero(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);840bld.MOV(zero, brw_imm_ud(0u));841842sources[length++] = zero;843if (vue_map->slots_valid & VARYING_BIT_LAYER)844sources[length++] = this->outputs[VARYING_SLOT_LAYER];845else846sources[length++] = zero;847848if (vue_map->slots_valid & VARYING_BIT_VIEWPORT)849sources[length++] = this->outputs[VARYING_SLOT_VIEWPORT];850else851sources[length++] = zero;852853if (vue_map->slots_valid & VARYING_BIT_PSIZ)854sources[length++] = this->outputs[VARYING_SLOT_PSIZ];855else856sources[length++] = zero;857break;858}859case BRW_VARYING_SLOT_NDC:860case VARYING_SLOT_EDGE:861unreachable("unexpected scalar vs output");862break;863864default:865/* gl_Position is always in the vue map, but isn't always written by866* the shader. Other varyings (clip distances) get added to the vue867* map but don't always get written. In those cases, the868* corresponding this->output[] slot will be invalid we and can skip869* the urb write for the varying. If we've already queued up a vue870* slot for writing we flush a mlen 5 urb write, otherwise we just871* advance the urb_offset.872*/873if (varying == BRW_VARYING_SLOT_PAD ||874this->outputs[varying].file == BAD_FILE) {875if (length > 0)876flush = true;877else878urb_offset++;879break;880}881882if (stage == MESA_SHADER_VERTEX && vs_key->clamp_vertex_color &&883(varying == VARYING_SLOT_COL0 ||884varying == VARYING_SLOT_COL1 ||885varying == VARYING_SLOT_BFC0 ||886varying == VARYING_SLOT_BFC1)) {887/* We need to clamp these guys, so do a saturating MOV into a888* temp register and use that for the payload.889*/890for (int i = 0; i < 4; i++) {891fs_reg reg = fs_reg(VGRF, alloc.allocate(1), outputs[varying].type);892fs_reg src = offset(this->outputs[varying], bld, i);893set_saturate(true, bld.MOV(reg, src));894sources[length++] = reg;895}896} else {897int slot_offset = 0;898899/* When using Primitive Replication, there may be multiple slots900* assigned to POS.901*/902if (varying == VARYING_SLOT_POS)903slot_offset = slot - vue_map->varying_to_slot[VARYING_SLOT_POS];904905for (unsigned i = 0; i < 4; i++) {906sources[length++] = offset(this->outputs[varying], bld,907i + (slot_offset * 4));908}909}910break;911}912913const fs_builder abld = bld.annotate("URB write");914915/* If we've queued up 8 registers of payload (2 VUE slots), if this is916* the last slot or if we need to flush (see BAD_FILE varying case917* above), emit a URB write send now to flush out the data.918*/919if (length == 8 || (length > 0 && slot == last_slot))920flush = true;921if (flush) {922fs_reg *payload_sources =923ralloc_array(mem_ctx, fs_reg, length + header_size);924fs_reg payload = fs_reg(VGRF, alloc.allocate(length + header_size),925BRW_REGISTER_TYPE_F);926payload_sources[0] = urb_handle;927928if (opcode == SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT)929payload_sources[1] = per_slot_offsets;930931memcpy(&payload_sources[header_size], sources,932length * sizeof sources[0]);933934abld.LOAD_PAYLOAD(payload, payload_sources, length + header_size,935header_size);936937fs_inst *inst = abld.emit(opcode, reg_undef, payload);938939/* For ICL WA 1805992985 one needs additional write in the end. */940if (devinfo->ver == 11 && stage == MESA_SHADER_TESS_EVAL)941inst->eot = false;942else943inst->eot = slot == last_slot && stage != MESA_SHADER_GEOMETRY;944945inst->mlen = length + header_size;946inst->offset = urb_offset;947urb_offset = starting_urb_offset + slot + 1;948length = 0;949flush = false;950urb_written = true;951}952}953954/* If we don't have any valid slots to write, just do a minimal urb write955* send to terminate the shader. This includes 1 slot of undefined data,956* because it's invalid to write 0 data:957*958* From the Broadwell PRM, Volume 7: 3D Media GPGPU, Shared Functions -959* Unified Return Buffer (URB) > URB_SIMD8_Write and URB_SIMD8_Read >960* Write Data Payload:961*962* "The write data payload can be between 1 and 8 message phases long."963*/964if (!urb_written) {965/* For GS, just turn EmitVertex() into a no-op. We don't want it to966* end the thread, and emit_gs_thread_end() already emits a SEND with967* EOT at the end of the program for us.968*/969if (stage == MESA_SHADER_GEOMETRY)970return;971972fs_reg payload = fs_reg(VGRF, alloc.allocate(2), BRW_REGISTER_TYPE_UD);973bld.exec_all().MOV(payload, urb_handle);974975fs_inst *inst = bld.emit(SHADER_OPCODE_URB_WRITE_SIMD8, reg_undef, payload);976inst->eot = true;977inst->mlen = 2;978inst->offset = 1;979return;980}981982/* ICL WA 1805992985:983*984* ICLLP GPU hangs on one of tessellation vkcts tests with DS not done. The985* send cycle, which is a urb write with an eot must be 4 phases long and986* all 8 lanes must valid.987*/988if (devinfo->ver == 11 && stage == MESA_SHADER_TESS_EVAL) {989fs_reg payload = fs_reg(VGRF, alloc.allocate(6), BRW_REGISTER_TYPE_UD);990991/* Workaround requires all 8 channels (lanes) to be valid. This is992* understood to mean they all need to be alive. First trick is to find993* a live channel and copy its urb handle for all the other channels to994* make sure all handles are valid.995*/996bld.exec_all().MOV(payload, bld.emit_uniformize(urb_handle));997998/* Second trick is to use masked URB write where one can tell the HW to999* actually write data only for selected channels even though all are1000* active.1001* Third trick is to take advantage of the must-be-zero (MBZ) area in1002* the very beginning of the URB.1003*1004* One masks data to be written only for the first channel and uses1005* offset zero explicitly to land data to the MBZ area avoiding trashing1006* any other part of the URB.1007*1008* Since the WA says that the write needs to be 4 phases long one uses1009* 4 slots data. All are explicitly zeros in order to to keep the MBZ1010* area written as zeros.1011*/1012bld.exec_all().MOV(offset(payload, bld, 1), brw_imm_ud(0x10000u));1013bld.exec_all().MOV(offset(payload, bld, 2), brw_imm_ud(0u));1014bld.exec_all().MOV(offset(payload, bld, 3), brw_imm_ud(0u));1015bld.exec_all().MOV(offset(payload, bld, 4), brw_imm_ud(0u));1016bld.exec_all().MOV(offset(payload, bld, 5), brw_imm_ud(0u));10171018fs_inst *inst = bld.exec_all().emit(SHADER_OPCODE_URB_WRITE_SIMD8_MASKED,1019reg_undef, payload);1020inst->eot = true;1021inst->mlen = 6;1022inst->offset = 0;1023}1024}10251026void1027fs_visitor::emit_cs_terminate()1028{1029assert(devinfo->ver >= 7);10301031/* We can't directly send from g0, since sends with EOT have to use1032* g112-127. So, copy it to a virtual register, The register allocator will1033* make sure it uses the appropriate register range.1034*/1035struct brw_reg g0 = retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD);1036fs_reg payload = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);1037bld.group(8, 0).exec_all().MOV(payload, g0);10381039/* Send a message to the thread spawner to terminate the thread. */1040fs_inst *inst = bld.exec_all()1041.emit(CS_OPCODE_CS_TERMINATE, reg_undef, payload);1042inst->eot = true;1043}10441045void1046fs_visitor::emit_barrier()1047{1048uint32_t barrier_id_mask;1049switch (devinfo->ver) {1050case 7:1051case 8:1052barrier_id_mask = 0x0f000000u; break;1053case 9:1054barrier_id_mask = 0x8f000000u; break;1055case 11:1056case 12:1057barrier_id_mask = 0x7f000000u; break;1058default:1059unreachable("barrier is only available on gen >= 7");1060}10611062/* We are getting the barrier ID from the compute shader header */1063assert(stage == MESA_SHADER_COMPUTE || stage == MESA_SHADER_KERNEL);10641065fs_reg payload = fs_reg(VGRF, alloc.allocate(1), BRW_REGISTER_TYPE_UD);10661067/* Clear the message payload */1068bld.exec_all().group(8, 0).MOV(payload, brw_imm_ud(0u));10691070/* Copy the barrier id from r0.2 to the message payload reg.2 */1071fs_reg r0_2 = fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD));1072bld.exec_all().group(1, 0).AND(component(payload, 2), r0_2,1073brw_imm_ud(barrier_id_mask));10741075/* Emit a gateway "barrier" message using the payload we set up, followed1076* by a wait instruction.1077*/1078bld.exec_all().emit(SHADER_OPCODE_BARRIER, reg_undef, payload);1079}10801081fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,1082void *mem_ctx,1083const brw_base_prog_key *key,1084struct brw_stage_prog_data *prog_data,1085const nir_shader *shader,1086unsigned dispatch_width,1087int shader_time_index,1088bool debug_enabled)1089: backend_shader(compiler, log_data, mem_ctx, shader, prog_data,1090debug_enabled),1091key(key), gs_compile(NULL), prog_data(prog_data),1092live_analysis(this), regpressure_analysis(this),1093performance_analysis(this),1094dispatch_width(dispatch_width),1095shader_time_index(shader_time_index),1096bld(fs_builder(this, dispatch_width).at_end())1097{1098init();1099}11001101fs_visitor::fs_visitor(const struct brw_compiler *compiler, void *log_data,1102void *mem_ctx,1103struct brw_gs_compile *c,1104struct brw_gs_prog_data *prog_data,1105const nir_shader *shader,1106int shader_time_index,1107bool debug_enabled)1108: backend_shader(compiler, log_data, mem_ctx, shader,1109&prog_data->base.base, debug_enabled),1110key(&c->key.base), gs_compile(c),1111prog_data(&prog_data->base.base),1112live_analysis(this), regpressure_analysis(this),1113performance_analysis(this),1114dispatch_width(8),1115shader_time_index(shader_time_index),1116bld(fs_builder(this, dispatch_width).at_end())1117{1118init();1119}112011211122void1123fs_visitor::init()1124{1125if (key)1126this->key_tex = &key->tex;1127else1128this->key_tex = NULL;11291130this->max_dispatch_width = 32;1131this->prog_data = this->stage_prog_data;11321133this->failed = false;1134this->fail_msg = NULL;11351136this->nir_locals = NULL;1137this->nir_ssa_values = NULL;1138this->nir_system_values = NULL;11391140memset(&this->payload, 0, sizeof(this->payload));1141this->source_depth_to_render_target = false;1142this->runtime_check_aads_emit = false;1143this->first_non_payload_grf = 0;1144this->max_grf = devinfo->ver >= 7 ? GFX7_MRF_HACK_START : BRW_MAX_GRF;11451146this->uniforms = 0;1147this->last_scratch = 0;1148this->pull_constant_loc = NULL;1149this->push_constant_loc = NULL;11501151this->shader_stats.scheduler_mode = NULL;1152this->shader_stats.promoted_constants = 0,11531154this->grf_used = 0;1155this->spilled_any_registers = false;1156}11571158fs_visitor::~fs_visitor()1159{1160}116111621163