Path: blob/21.2-virgl/src/gallium/drivers/swr/swr_shader.cpp
4570 views
/****************************************************************************1* Copyright (C) 2015 Intel Corporation. All Rights Reserved.2*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 <llvm/Config/llvm-config.h>2425#if LLVM_VERSION_MAJOR < 726// llvm redefines DEBUG27#pragma push_macro("DEBUG")28#undef DEBUG29#endif3031#include "JitManager.h"32#include "llvm-c/Core.h"33#include "llvm/Support/CBindingWrapping.h"34#include "llvm/IR/LegacyPassManager.h"3536#if LLVM_VERSION_MAJOR < 737#pragma pop_macro("DEBUG")38#endif3940#include "state.h"41#include "gen_state_llvm.h"42#include "builder.h"43#include "functionpasses/passes.h"4445#include "tgsi/tgsi_strings.h"46#include "util/format/u_format.h"47#include "util/u_prim.h"48#include "gallivm/lp_bld_init.h"49#include "gallivm/lp_bld_flow.h"50#include "gallivm/lp_bld_struct.h"51#include "gallivm/lp_bld_tgsi.h"52#include "gallivm/lp_bld_const.h"53#include "gallivm/lp_bld_printf.h"54#include "gallivm/lp_bld_logic.h"5556#include "swr_context.h"57#include "gen_surf_state_llvm.h"58#include "gen_swr_context_llvm.h"59#include "swr_resource.h"60#include "swr_state.h"61#include "swr_screen.h"626364/////////////////////////////////////////////////////////////////////////6566#include <stdio.h>67#include <inttypes.h>6869#include "util/u_debug.h"70#include "util/u_memory.h"71#include "util/u_string.h"7273#include "gallivm/lp_bld_type.h"7475#if defined(DEBUG) && defined(SWR_VERBOSE_SHADER)76constexpr bool verbose_shader = true;77constexpr bool verbose_tcs_shader_in = true;78constexpr bool verbose_tcs_shader_out = true;79constexpr bool verbose_tcs_shader_loop = true;80constexpr bool verbose_vs_shader = true;81#else82constexpr bool verbose_shader = false;83constexpr bool verbose_tcs_shader_in = false;84constexpr bool verbose_tcs_shader_out = false;85constexpr bool verbose_tcs_shader_loop = false;86constexpr bool verbose_vs_shader = false;87#endif8889using namespace SwrJit;9091static unsigned92locate_linkage(ubyte name, ubyte index, struct tgsi_shader_info *info);9394bool operator==(const swr_jit_fs_key &lhs, const swr_jit_fs_key &rhs)95{96return !memcmp(&lhs, &rhs, sizeof(lhs));97}9899bool operator==(const swr_jit_vs_key &lhs, const swr_jit_vs_key &rhs)100{101return !memcmp(&lhs, &rhs, sizeof(lhs));102}103104bool operator==(const swr_jit_fetch_key &lhs, const swr_jit_fetch_key &rhs)105{106return !memcmp(&lhs, &rhs, sizeof(lhs));107}108109bool operator==(const swr_jit_gs_key &lhs, const swr_jit_gs_key &rhs)110{111return !memcmp(&lhs, &rhs, sizeof(lhs));112}113114bool operator==(const swr_jit_tcs_key &lhs, const swr_jit_tcs_key &rhs)115{116return !memcmp(&lhs, &rhs, sizeof(lhs));117}118119bool operator==(const swr_jit_tes_key &lhs, const swr_jit_tes_key &rhs)120{121return !memcmp(&lhs, &rhs, sizeof(lhs));122}123124125static void126swr_generate_sampler_key(const struct lp_tgsi_info &info,127struct swr_context *ctx,128enum pipe_shader_type shader_type,129struct swr_jit_sampler_key &key)130{131key.nr_samplers = info.base.file_max[TGSI_FILE_SAMPLER] + 1;132133for (unsigned i = 0; i < key.nr_samplers; i++) {134if (info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) {135lp_sampler_static_sampler_state(136&key.sampler[i].sampler_state,137ctx->samplers[shader_type][i]);138}139}140141/*142* XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes143* are dx10-style? Can't really have mixed opcodes, at least not144* if we want to skip the holes here (without rescanning tgsi).145*/146if (info.base.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {147key.nr_sampler_views =148info.base.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;149for (unsigned i = 0; i < key.nr_sampler_views; i++) {150if (info.base.file_mask[TGSI_FILE_SAMPLER_VIEW] & (1u << (i & 31))) {151const struct pipe_sampler_view *view =152ctx->sampler_views[shader_type][i];153lp_sampler_static_texture_state(154&key.sampler[i].texture_state, view);155if (view) {156struct swr_resource *swr_res = swr_resource(view->texture);157const struct util_format_description *desc =158util_format_description(view->format);159if (swr_res->has_depth && swr_res->has_stencil &&160!util_format_has_depth(desc))161key.sampler[i].texture_state.format = PIPE_FORMAT_S8_UINT;162}163}164}165} else {166key.nr_sampler_views = key.nr_samplers;167for (unsigned i = 0; i < key.nr_sampler_views; i++) {168if (info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) {169const struct pipe_sampler_view *view =170ctx->sampler_views[shader_type][i];171lp_sampler_static_texture_state(172&key.sampler[i].texture_state, view);173if (view) {174struct swr_resource *swr_res = swr_resource(view->texture);175const struct util_format_description *desc =176util_format_description(view->format);177if (swr_res->has_depth && swr_res->has_stencil &&178!util_format_has_depth(desc))179key.sampler[i].texture_state.format = PIPE_FORMAT_S8_UINT;180}181}182}183}184}185186void187swr_generate_fs_key(struct swr_jit_fs_key &key,188struct swr_context *ctx,189swr_fragment_shader *swr_fs)190{191memset((void*)&key, 0, sizeof(key));192193key.nr_cbufs = ctx->framebuffer.nr_cbufs;194key.light_twoside = ctx->rasterizer->light_twoside;195key.sprite_coord_enable = ctx->rasterizer->sprite_coord_enable;196197struct tgsi_shader_info *pPrevShader;198if (ctx->gs)199pPrevShader = &ctx->gs->info.base;200else if (ctx->tes)201pPrevShader = &ctx->tes->info.base;202else203pPrevShader = &ctx->vs->info.base;204205memcpy(&key.vs_output_semantic_name,206&pPrevShader->output_semantic_name,207sizeof(key.vs_output_semantic_name));208memcpy(&key.vs_output_semantic_idx,209&pPrevShader->output_semantic_index,210sizeof(key.vs_output_semantic_idx));211212swr_generate_sampler_key(swr_fs->info, ctx, PIPE_SHADER_FRAGMENT, key);213214key.poly_stipple_enable = ctx->rasterizer->poly_stipple_enable &&215ctx->poly_stipple.prim_is_poly;216}217218void219swr_generate_vs_key(struct swr_jit_vs_key &key,220struct swr_context *ctx,221swr_vertex_shader *swr_vs)222{223memset((void*)&key, 0, sizeof(key));224225key.clip_plane_mask =226swr_vs->info.base.clipdist_writemask ?227swr_vs->info.base.clipdist_writemask & ctx->rasterizer->clip_plane_enable :228ctx->rasterizer->clip_plane_enable;229230swr_generate_sampler_key(swr_vs->info, ctx, PIPE_SHADER_VERTEX, key);231}232233void234swr_generate_fetch_key(struct swr_jit_fetch_key &key,235struct swr_vertex_element_state *velems)236{237memset((void*)&key, 0, sizeof(key));238239key.fsState = velems->fsState;240}241242void243swr_generate_gs_key(struct swr_jit_gs_key &key,244struct swr_context *ctx,245swr_geometry_shader *swr_gs)246{247memset((void*)&key, 0, sizeof(key));248249struct tgsi_shader_info *pPrevShader = nullptr;250251if (ctx->tes) {252pPrevShader = &ctx->tes->info.base;253} else {254pPrevShader = &ctx->vs->info.base;255}256257memcpy(&key.vs_output_semantic_name,258&pPrevShader->output_semantic_name,259sizeof(key.vs_output_semantic_name));260memcpy(&key.vs_output_semantic_idx,261&pPrevShader->output_semantic_index,262sizeof(key.vs_output_semantic_idx));263264swr_generate_sampler_key(swr_gs->info, ctx, PIPE_SHADER_GEOMETRY, key);265}266267void268swr_generate_tcs_key(struct swr_jit_tcs_key &key,269struct swr_context *ctx,270swr_tess_control_shader *swr_tcs)271{272memset((void*)&key, 0, sizeof(key));273274struct tgsi_shader_info *pPrevShader = &ctx->vs->info.base;275276memcpy(&key.vs_output_semantic_name,277&pPrevShader->output_semantic_name,278sizeof(key.vs_output_semantic_name));279memcpy(&key.vs_output_semantic_idx,280&pPrevShader->output_semantic_index,281sizeof(key.vs_output_semantic_idx));282283key.clip_plane_mask =284swr_tcs->info.base.clipdist_writemask ?285swr_tcs->info.base.clipdist_writemask & ctx->rasterizer->clip_plane_enable :286ctx->rasterizer->clip_plane_enable;287288swr_generate_sampler_key(swr_tcs->info, ctx, PIPE_SHADER_TESS_CTRL, key);289}290291void292swr_generate_tes_key(struct swr_jit_tes_key &key,293struct swr_context *ctx,294swr_tess_evaluation_shader *swr_tes)295{296memset((void*)&key, 0, sizeof(key));297298struct tgsi_shader_info *pPrevShader = nullptr;299300if (ctx->tcs) {301pPrevShader = &ctx->tcs->info.base;302}303else {304pPrevShader = &ctx->vs->info.base;305}306307SWR_ASSERT(pPrevShader != nullptr, "TES: No TCS or VS defined");308309memcpy(&key.prev_output_semantic_name,310&pPrevShader->output_semantic_name,311sizeof(key.prev_output_semantic_name));312memcpy(&key.prev_output_semantic_idx,313&pPrevShader->output_semantic_index,314sizeof(key.prev_output_semantic_idx));315316key.clip_plane_mask =317swr_tes->info.base.clipdist_writemask ?318swr_tes->info.base.clipdist_writemask & ctx->rasterizer->clip_plane_enable :319ctx->rasterizer->clip_plane_enable;320321swr_generate_sampler_key(swr_tes->info, ctx, PIPE_SHADER_TESS_EVAL, key);322}323324struct BuilderSWR : public Builder {325BuilderSWR(JitManager *pJitMgr, const char *pName)326: Builder(pJitMgr)327{328pJitMgr->SetupNewModule();329gallivm = gallivm_create(pName, wrap(&JM()->mContext), NULL);330pJitMgr->mpCurrentModule = unwrap(gallivm->module);331}332333~BuilderSWR() {334gallivm_free_ir(gallivm);335}336337void WriteVS(Value *pVal, Value *pVsContext, Value *pVtxOutput,338unsigned slot, unsigned channel);339340struct gallivm_state *gallivm;341PFN_VERTEX_FUNC CompileVS(struct swr_context *ctx, swr_jit_vs_key &key);342PFN_PIXEL_KERNEL CompileFS(struct swr_context *ctx, swr_jit_fs_key &key);343PFN_GS_FUNC CompileGS(struct swr_context *ctx, swr_jit_gs_key &key);344PFN_TCS_FUNC CompileTCS(struct swr_context *ctx, swr_jit_tcs_key &key);345PFN_TES_FUNC CompileTES(struct swr_context *ctx, swr_jit_tes_key &key);346347// GS-specific emit functions348LLVMValueRef349swr_gs_llvm_fetch_input(const struct lp_build_gs_iface *gs_iface,350struct lp_build_context * bld,351boolean is_vindex_indirect,352LLVMValueRef vertex_index,353boolean is_aindex_indirect,354LLVMValueRef attrib_index,355LLVMValueRef swizzle_index);356void357swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface *gs_base,358struct lp_build_context * bld,359LLVMValueRef (*outputs)[4],360LLVMValueRef emitted_vertices_vec,361LLVMValueRef stream_id);362363void364swr_gs_llvm_end_primitive(const struct lp_build_gs_iface *gs_base,365struct lp_build_context * bld,366LLVMValueRef total_emitted_vertices_vec_ptr,367LLVMValueRef verts_per_prim_vec,368LLVMValueRef emitted_prims_vec,369LLVMValueRef mask_vec);370371void372swr_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,373LLVMValueRef total_emitted_vertices_vec,374LLVMValueRef emitted_prims_vec, unsigned stream);375376// TCS-specific emit functions377void swr_tcs_llvm_emit_prologue(struct lp_build_tgsi_soa_context* bld);378void swr_tcs_llvm_emit_epilogue(struct lp_build_tgsi_soa_context* bld);379380LLVMValueRef381swr_tcs_llvm_fetch_input(const struct lp_build_tcs_iface *tcs_iface,382struct lp_build_tgsi_context * bld_base,383boolean is_vindex_indirect,384LLVMValueRef vertex_index,385boolean is_aindex_indirect,386LLVMValueRef attrib_index,387LLVMValueRef swizzle_index);388389LLVMValueRef390swr_tcs_llvm_fetch_output(const struct lp_build_tcs_iface *tcs_iface,391struct lp_build_tgsi_context * bld_base,392boolean is_vindex_indirect,393LLVMValueRef vertex_index,394boolean is_aindex_indirect,395LLVMValueRef attrib_index,396LLVMValueRef swizzle_index,397uint32_t name);398399void400swr_tcs_llvm_store_output(const struct lp_build_tcs_iface *tcs_iface,401struct lp_build_tgsi_context * bld_base,402unsigned name,403boolean is_vindex_indirect,404LLVMValueRef vertex_index,405boolean is_aindex_indirect,406LLVMValueRef attrib_index,407LLVMValueRef swizzle_index,408LLVMValueRef value,409LLVMValueRef mask_vec);410411// Barrier implementation (available only in TCS)412void413swr_tcs_llvm_emit_barrier(const struct lp_build_tcs_iface *tcs_iface,414struct lp_build_tgsi_context *bld_base);415416// TES-specific emit functions417LLVMValueRef418swr_tes_llvm_fetch_vtx_input(const struct lp_build_tes_iface *tes_iface,419struct lp_build_tgsi_context * bld_base,420boolean is_vindex_indirect,421LLVMValueRef vertex_index,422boolean is_aindex_indirect,423LLVMValueRef attrib_index,424LLVMValueRef swizzle_index);425426LLVMValueRef427swr_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface *tes_iface,428struct lp_build_tgsi_context * bld_base,429boolean is_aindex_indirect,430LLVMValueRef attrib_index,431LLVMValueRef swizzle_index);432};433434struct swr_gs_llvm_iface {435struct lp_build_gs_iface base;436struct tgsi_shader_info *info;437438BuilderSWR *pBuilder;439440Value *pGsCtx;441SWR_GS_STATE *pGsState;442uint32_t num_outputs;443uint32_t num_verts_per_prim;444445Value *pVtxAttribMap;446};447448struct swr_tcs_llvm_iface {449struct lp_build_tcs_iface base;450struct tgsi_shader_info *info;451452BuilderSWR *pBuilder;453454Value *pTcsCtx;455SWR_TS_STATE *pTsState;456457uint32_t output_vertices;458459LLVMValueRef loop_var;460461Value *pVtxAttribMap;462Value *pVtxOutputAttribMap;463Value *pPatchOutputAttribMap;464};465466struct swr_tes_llvm_iface {467struct lp_build_tes_iface base;468struct tgsi_shader_info *info;469470BuilderSWR *pBuilder;471472Value *pTesCtx;473SWR_TS_STATE *pTsState;474475uint32_t num_outputs;476477Value *pVtxAttribMap;478Value *pPatchAttribMap;479};480481// trampoline functions so we can use the builder llvm construction methods482static LLVMValueRef483swr_gs_llvm_fetch_input(const struct lp_build_gs_iface *gs_iface,484struct lp_build_context * bld,485boolean is_vindex_indirect,486LLVMValueRef vertex_index,487boolean is_aindex_indirect,488LLVMValueRef attrib_index,489LLVMValueRef swizzle_index)490{491swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_iface;492493return iface->pBuilder->swr_gs_llvm_fetch_input(gs_iface, bld,494is_vindex_indirect,495vertex_index,496is_aindex_indirect,497attrib_index,498swizzle_index);499}500501static void502swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface *gs_base,503struct lp_build_context * bld,504LLVMValueRef (*outputs)[4],505LLVMValueRef emitted_vertices_vec,506LLVMValueRef mask_vec,507LLVMValueRef stream_id)508{509swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;510511iface->pBuilder->swr_gs_llvm_emit_vertex(gs_base, bld,512outputs,513emitted_vertices_vec,514stream_id);515}516517static void518swr_gs_llvm_end_primitive(const struct lp_build_gs_iface *gs_base,519struct lp_build_context * bld,520LLVMValueRef total_emitted_vertices_vec_ptr,521LLVMValueRef verts_per_prim_vec,522LLVMValueRef emitted_prims_vec,523LLVMValueRef mask_vec, unsigned stream_id)524{525swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;526527iface->pBuilder->swr_gs_llvm_end_primitive(gs_base, bld,528total_emitted_vertices_vec_ptr,529verts_per_prim_vec,530emitted_prims_vec,531mask_vec);532}533534static void535swr_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,536LLVMValueRef total_emitted_vertices_vec,537LLVMValueRef emitted_prims_vec, unsigned stream)538{539swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;540541iface->pBuilder->swr_gs_llvm_epilogue(gs_base,542total_emitted_vertices_vec,543emitted_prims_vec, stream);544}545546static LLVMValueRef547swr_tcs_llvm_fetch_input(const struct lp_build_tcs_iface *tcs_iface,548struct lp_build_context * bld,549boolean is_vindex_indirect,550LLVMValueRef vertex_index,551boolean is_aindex_indirect,552LLVMValueRef attrib_index,553boolean is_sindex_indirect,554LLVMValueRef swizzle_index)555{556swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;557struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;558559return iface->pBuilder->swr_tcs_llvm_fetch_input(tcs_iface, bld_base,560is_vindex_indirect,561vertex_index,562is_aindex_indirect,563attrib_index,564swizzle_index);565}566567static LLVMValueRef568swr_tcs_llvm_fetch_output(const struct lp_build_tcs_iface *tcs_iface,569struct lp_build_context * bld,570boolean is_vindex_indirect,571LLVMValueRef vertex_index,572boolean is_aindex_indirect,573LLVMValueRef attrib_index,574boolean is_sindex_indirect,575LLVMValueRef swizzle_index,576uint32_t name)577{578swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;579struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;580581return iface->pBuilder->swr_tcs_llvm_fetch_output(tcs_iface, bld_base,582is_vindex_indirect,583vertex_index,584is_aindex_indirect,585attrib_index,586swizzle_index,587name);588}589590591static void592swr_tcs_llvm_emit_prologue(struct lp_build_context* bld)593{594lp_build_tgsi_soa_context* bld_base = (lp_build_tgsi_soa_context*)bld;595swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld_base->tcs_iface;596iface->pBuilder->swr_tcs_llvm_emit_prologue(bld_base);597}598599static void600swr_tcs_llvm_emit_epilogue(struct lp_build_context* bld)601{602lp_build_tgsi_soa_context* bld_base = (lp_build_tgsi_soa_context*)bld;603swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld_base->tcs_iface;604iface->pBuilder->swr_tcs_llvm_emit_epilogue(bld_base);605}606607static608void swr_tcs_llvm_store_output(const struct lp_build_tcs_iface *tcs_iface,609struct lp_build_context * bld,610unsigned name,611boolean is_vindex_indirect,612LLVMValueRef vertex_index,613boolean is_aindex_indirect,614LLVMValueRef attrib_index,615boolean is_sindex_indirect,616LLVMValueRef swizzle_index,617LLVMValueRef value,618LLVMValueRef mask_vec)619{620swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;621struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;622623iface->pBuilder->swr_tcs_llvm_store_output(tcs_iface,624bld_base,625name,626is_vindex_indirect,627vertex_index,628is_aindex_indirect,629attrib_index,630swizzle_index,631value,632mask_vec);633}634635636static637void swr_tcs_llvm_emit_barrier(struct lp_build_context *bld)638{639lp_build_tgsi_soa_context* bld_base = (lp_build_tgsi_soa_context*)bld;640swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld_base->tcs_iface;641642iface->pBuilder->swr_tcs_llvm_emit_barrier(bld_base->tcs_iface, &bld_base->bld_base);643}644645646static LLVMValueRef647swr_tes_llvm_fetch_vtx_input(const struct lp_build_tes_iface *tes_iface,648struct lp_build_context * bld,649boolean is_vindex_indirect,650LLVMValueRef vertex_index,651boolean is_aindex_indirect,652LLVMValueRef attrib_index,653boolean is_sindex_indirect,654LLVMValueRef swizzle_index)655{656swr_tes_llvm_iface *iface = (swr_tes_llvm_iface*)tes_iface;657struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;658659return iface->pBuilder->swr_tes_llvm_fetch_vtx_input(tes_iface, bld_base,660is_vindex_indirect,661vertex_index,662is_aindex_indirect,663attrib_index,664swizzle_index);665}666667static LLVMValueRef668swr_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface *tes_iface,669struct lp_build_context * bld,670boolean is_aindex_indirect,671LLVMValueRef attrib_index,672LLVMValueRef swizzle_index)673{674swr_tes_llvm_iface *iface = (swr_tes_llvm_iface*)tes_iface;675struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;676677return iface->pBuilder->swr_tes_llvm_fetch_patch_input(tes_iface, bld_base,678is_aindex_indirect,679attrib_index,680swizzle_index);681}682683LLVMValueRef684BuilderSWR::swr_gs_llvm_fetch_input(const struct lp_build_gs_iface *gs_iface,685struct lp_build_context * bld,686boolean is_vindex_indirect,687LLVMValueRef vertex_index,688boolean is_aindex_indirect,689LLVMValueRef attrib_index,690LLVMValueRef swizzle_index)691{692swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_iface;693Value *vert_index = unwrap(vertex_index);694Value *attr_index = unwrap(attrib_index);695696IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));697698if (is_vindex_indirect || is_aindex_indirect) {699int i;700Value *res = unwrap(bld->zero);701struct lp_type type = bld->type;702703for (i = 0; i < type.length; i++) {704Value *vert_chan_index = vert_index;705Value *attr_chan_index = attr_index;706707if (is_vindex_indirect) {708vert_chan_index = VEXTRACT(vert_index, C(i));709}710if (is_aindex_indirect) {711attr_chan_index = VEXTRACT(attr_index, C(i));712}713714Value *attrib =715LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_chan_index}));716717Value *pVertex = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pVerts});718Value *pInputVertStride = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_inputVertStride});719720Value *pVector = ADD(MUL(vert_chan_index, pInputVertStride), attrib);721Value *pInput = LOAD(GEP(pVertex, {pVector, unwrap(swizzle_index)}));722723Value *value = VEXTRACT(pInput, C(i));724res = VINSERT(res, value, C(i));725}726727return wrap(res);728} else {729Value *attrib = LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_index}));730731Value *pVertex = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pVerts});732Value *pInputVertStride = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_inputVertStride});733734Value *pVector = ADD(MUL(vert_index, pInputVertStride), attrib);735736Value *pInput = LOAD(GEP(pVertex, {pVector, unwrap(swizzle_index)}));737738return wrap(pInput);739}740}741742// GS output stream layout743#define VERTEX_COUNT_SIZE 32744#define CONTROL_HEADER_SIZE (8*32)745746void747BuilderSWR::swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface *gs_base,748struct lp_build_context * bld,749LLVMValueRef (*outputs)[4],750LLVMValueRef emitted_vertices_vec,751LLVMValueRef stream_id)752{753swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;754755IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));756const uint32_t headerSize = VERTEX_COUNT_SIZE + CONTROL_HEADER_SIZE;757const uint32_t attribSize = 4 * sizeof(float);758const uint32_t vertSize = attribSize * SWR_VTX_NUM_SLOTS;759Value *pVertexOffset = MUL(unwrap(emitted_vertices_vec), VIMMED1(vertSize));760761Value *vMask = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_mask});762Value *vMask1 = TRUNC(vMask, getVectorType(mInt1Ty, mVWidth));763764Value *pStack = STACKSAVE();765Value *pTmpPtr = ALLOCA(mFP32Ty, C(4)); // used for dummy write for lane masking766767for (uint32_t attrib = 0; attrib < iface->num_outputs; ++attrib) {768uint32_t attribSlot = attrib;769uint32_t sgvChannel = 0;770if (iface->info->output_semantic_name[attrib] == TGSI_SEMANTIC_PSIZE) {771attribSlot = VERTEX_SGV_SLOT;772sgvChannel = VERTEX_SGV_POINT_SIZE_COMP;773} else if (iface->info->output_semantic_name[attrib] == TGSI_SEMANTIC_LAYER) {774attribSlot = VERTEX_SGV_SLOT;775sgvChannel = VERTEX_SGV_RTAI_COMP;776} else if (iface->info->output_semantic_name[attrib] == TGSI_SEMANTIC_VIEWPORT_INDEX) {777attribSlot = VERTEX_SGV_SLOT;778sgvChannel = VERTEX_SGV_VAI_COMP;779} else if (iface->info->output_semantic_name[attrib] == TGSI_SEMANTIC_POSITION) {780attribSlot = VERTEX_POSITION_SLOT;781} else {782attribSlot = VERTEX_ATTRIB_START_SLOT + attrib;783if (iface->info->writes_position) {784attribSlot--;785}786}787788Value *pOutputOffset = ADD(pVertexOffset, VIMMED1(headerSize + attribSize * attribSlot)); // + sgvChannel ?789790for (uint32_t lane = 0; lane < mVWidth; ++lane) {791Value *pLaneOffset = VEXTRACT(pOutputOffset, C(lane));792Value *pStream = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});793Value *pStreamOffset = GEP(pStream, pLaneOffset);794pStreamOffset = BITCAST(pStreamOffset, mFP32PtrTy);795796Value *pLaneMask = VEXTRACT(vMask1, C(lane));797pStreamOffset = SELECT(pLaneMask, pStreamOffset, pTmpPtr);798799for (uint32_t channel = 0; channel < 4; ++channel) {800Value *vData;801802if (attribSlot == VERTEX_SGV_SLOT)803vData = LOAD(unwrap(outputs[attrib][0]));804else805vData = LOAD(unwrap(outputs[attrib][channel]));806807if (attribSlot != VERTEX_SGV_SLOT ||808sgvChannel == channel) {809vData = VEXTRACT(vData, C(lane));810STORE(vData, pStreamOffset);811}812pStreamOffset = GEP(pStreamOffset, C(1));813}814}815}816817/* When the output type is not points, the geometry shader may not818* output data to multiple streams. So early exit here.819*/820if(iface->pGsState->outputTopology != TOP_POINT_LIST) {821STACKRESTORE(pStack);822return;823}824825// Info about stream id for each vertex826// is coded in 2 bits (4 vert per byte "box"):827// ----------------- ----------------- ----828// |d|d|c|c|b|b|a|a| |h|h|g|g|f|f|e|e| |...829// ----------------- ----------------- ----830831// Calculate where need to put stream id for current vert832// in 1 byte "box".833Value *pShiftControl = MUL(unwrap(emitted_vertices_vec), VIMMED1(2));834835// Calculate in which box put stream id for current vert.836Value *pOffsetControl = LSHR(unwrap(emitted_vertices_vec), VIMMED1(2));837838// Skip count header839Value *pStreamIdOffset = ADD(pOffsetControl, VIMMED1(VERTEX_COUNT_SIZE));840841for (uint32_t lane = 0; lane < mVWidth; ++lane) {842Value *pShift = TRUNC(VEXTRACT(pShiftControl, C(lane)), mInt8Ty);843Value *pStream = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});844845Value *pStreamOffset = GEP(pStream, VEXTRACT(pStreamIdOffset, C(lane)));846847// Just make sure that not overflow max - stream id = (0,1,2,3)848Value *vVal = TRUNC(AND(VEXTRACT(unwrap(stream_id), C(0)), C(0x3)), mInt8Ty);849850// Shift it to correct position in byte "box"851vVal = SHL(vVal, pShift);852853// Info about other vertices can be already stored854// so we need to read and add bits from current vert info.855Value *storedValue = LOAD(pStreamOffset);856vVal = OR(storedValue, vVal);857STORE(vVal, pStreamOffset);858}859860STACKRESTORE(pStack);861}862863void864BuilderSWR::swr_gs_llvm_end_primitive(const struct lp_build_gs_iface *gs_base,865struct lp_build_context * bld,866LLVMValueRef total_emitted_vertices_vec,867LLVMValueRef verts_per_prim_vec,868LLVMValueRef emitted_prims_vec,869LLVMValueRef mask_vec)870{871swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;872873/* When the output type is points, the geometry shader may output data874* to multiple streams, and end_primitive has no effect. Info about875* stream id for vertices is stored into the same place in memory where876* end primitive info is stored so early exit in this case.877*/878if (iface->pGsState->outputTopology == TOP_POINT_LIST) {879return;880}881882IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));883884Value *vMask = LOAD(iface->pGsCtx, { 0, SWR_GS_CONTEXT_mask });885Value *vMask1 = TRUNC(vMask, getVectorType(mInt1Ty, 8));886887uint32_t vertsPerPrim = iface->num_verts_per_prim;888889Value *vCount =890ADD(MUL(unwrap(emitted_prims_vec), VIMMED1(vertsPerPrim)),891unwrap(verts_per_prim_vec));892893vCount = unwrap(total_emitted_vertices_vec);894895Value *mask = unwrap(mask_vec);896Value *cmpMask = VMASK(ICMP_NE(unwrap(verts_per_prim_vec), VIMMED1(0)));897mask = AND(mask, cmpMask);898vMask1 = TRUNC(mask, getVectorType(mInt1Ty, 8));899900vCount = SUB(vCount, VIMMED1(1));901Value *vOffset = ADD(UDIV(vCount, VIMMED1(8)), VIMMED1(VERTEX_COUNT_SIZE));902Value *vValue = SHL(VIMMED1(1), UREM(vCount, VIMMED1(8)));903904vValue = TRUNC(vValue, getVectorType(mInt8Ty, 8));905906Value *pStack = STACKSAVE();907Value *pTmpPtr = ALLOCA(mInt8Ty, C(4)); // used for dummy read/write for lane masking908909for (uint32_t lane = 0; lane < mVWidth; ++lane) {910Value *vLaneOffset = VEXTRACT(vOffset, C(lane));911Value *pStream = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});912Value *pStreamOffset = GEP(pStream, vLaneOffset);913914Value *pLaneMask = VEXTRACT(vMask1, C(lane));915pStreamOffset = SELECT(pLaneMask, pStreamOffset, pTmpPtr);916917Value *vVal = LOAD(pStreamOffset);918vVal = OR(vVal, VEXTRACT(vValue, C(lane)));919STORE(vVal, pStreamOffset);920}921922STACKRESTORE(pStack);923}924925void926BuilderSWR::swr_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,927LLVMValueRef total_emitted_vertices_vec,928LLVMValueRef emitted_prims_vec, unsigned stream)929{930swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;931932IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));933934// Store emit count to each output stream in the first DWORD935for (uint32_t lane = 0; lane < mVWidth; ++lane)936{937Value* pStream = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});938pStream = BITCAST(pStream, mInt32PtrTy);939Value* pLaneCount = VEXTRACT(unwrap(total_emitted_vertices_vec), C(lane));940STORE(pLaneCount, pStream);941}942}943944void945BuilderSWR::swr_tcs_llvm_emit_prologue(struct lp_build_tgsi_soa_context* bld)946{947swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld->tcs_iface;948949Value* loop_var = ALLOCA(mSimdInt32Ty);950STORE(VBROADCAST(C(0)), loop_var);951952iface->loop_var = wrap(loop_var);953954lp_exec_bgnloop(&bld->exec_mask, true);955956IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));957bld->system_values.invocation_id = wrap((LOAD(unwrap(iface->loop_var))));958959if (verbose_tcs_shader_loop) {960lp_build_print_value(gallivm, "Prologue LOOP Iteration BEGIN:", bld->system_values.invocation_id);961}962963}964965void966BuilderSWR::swr_tcs_llvm_emit_epilogue(struct lp_build_tgsi_soa_context* bld)967{968swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld->tcs_iface;969970struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;971972STORE(ADD(LOAD(unwrap(iface->loop_var)), VBROADCAST(C(1))), unwrap(iface->loop_var));973if (verbose_tcs_shader_loop) {974lp_build_print_value(gallivm, "Epilogue LOOP: ", wrap(LOAD(unwrap(iface->loop_var))));975}976977LLVMValueRef tmp = lp_build_cmp(uint_bld, PIPE_FUNC_GEQUAL, wrap(LOAD(unwrap(iface->loop_var))),978wrap(VBROADCAST(C(iface->output_vertices))));979lp_exec_mask_cond_push(&bld->exec_mask, tmp);980lp_exec_break(&bld->exec_mask, &bld->bld_base.pc, false);981lp_exec_mask_cond_pop(&bld->exec_mask);982lp_exec_endloop(bld->bld_base.base.gallivm, &bld->exec_mask);983}984985LLVMValueRef986BuilderSWR::swr_tcs_llvm_fetch_input(const struct lp_build_tcs_iface *tcs_iface,987struct lp_build_tgsi_context * bld_base,988boolean is_vindex_indirect,989LLVMValueRef vertex_index,990boolean is_aindex_indirect,991LLVMValueRef attrib_index,992LLVMValueRef swizzle_index)993{994swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;995996Value *vert_index = unwrap(vertex_index);997Value *attr_index = unwrap(attrib_index);998999IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));10001001if (verbose_tcs_shader_in) {1002lp_build_printf(gallivm, "[TCS IN][VTX] ======================================\n");1003lp_build_print_value(gallivm, "[TCS IN][VTX] vertex_index: ", vertex_index);1004lp_build_print_value(gallivm, "[TCS IN][VTX] attrib_index: ", attrib_index);1005lp_build_printf(gallivm, "[TCS IN][VTX] --------------------------------------\n");1006}10071008Value *res = unwrap(bld_base->base.zero);1009if (is_vindex_indirect || is_aindex_indirect) {1010int i;1011struct lp_type type = bld_base->base.type;10121013for (i = 0; i < type.length; i++) {1014Value *vert_chan_index = vert_index;1015Value *attr_chan_index = attr_index;10161017if (is_vindex_indirect) {1018vert_chan_index = VEXTRACT(vert_index, C(i));1019}1020if (is_aindex_indirect) {1021attr_chan_index = VEXTRACT(attr_index, C(i));1022}10231024Value *attrib =1025LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_chan_index}));10261027Value *pBase = GEP(iface->pTcsCtx,1028{ C(0), C(SWR_HS_CONTEXT_vert), vert_chan_index,1029C(simdvertex_attrib), attrib, unwrap(swizzle_index), C(i) });10301031Value *val = LOAD(pBase);10321033if (verbose_tcs_shader_in) {1034lp_build_print_value(gallivm, "[TCS IN][VTX] vert_chan_index: ", wrap(vert_chan_index));1035lp_build_print_value(gallivm, "[TCS IN][VTX] attrib_index: ", attrib_index);1036lp_build_print_value(gallivm, "[TCS IN][VTX] attr_chan_index: ", wrap(attr_index));1037lp_build_print_value(gallivm, "[TCS IN][VTX] attrib read from map: ", wrap(attrib));1038lp_build_print_value(gallivm, "[TCS IN][VTX] swizzle_index: ", swizzle_index);1039lp_build_print_value(gallivm, "[TCS IN][VTX] Loaded: ", wrap(val));1040}1041res = VINSERT(res, val, C(i));1042}1043} else {1044Value *attrib = LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_index}));10451046Value *pBase = GEP(iface->pTcsCtx,1047{ C(0), C(SWR_HS_CONTEXT_vert), vert_index,1048C(simdvertex_attrib), attrib, unwrap(swizzle_index) });10491050res = LOAD(pBase);10511052if (verbose_tcs_shader_in) {1053lp_build_print_value(gallivm, "[TCS IN][VTX] attrib_index: ", attrib_index);1054lp_build_print_value(gallivm, "[TCS IN][VTX] attr_chan_index: ", wrap(attr_index));1055lp_build_print_value(gallivm, "[TCS IN][VTX] attrib read from map: ", wrap(attrib));1056lp_build_print_value(gallivm, "[TCS IN][VTX] swizzle_index: ", swizzle_index);1057lp_build_print_value(gallivm, "[TCS IN][VTX] Loaded: ", wrap(res));1058}1059}1060if (verbose_tcs_shader_in) {1061lp_build_print_value(gallivm, "[TCS IN][VTX] returning: ", wrap(res));1062}1063return wrap(res);1064}10651066LLVMValueRef1067BuilderSWR::swr_tcs_llvm_fetch_output(const struct lp_build_tcs_iface *tcs_iface,1068struct lp_build_tgsi_context * bld_base,1069boolean is_vindex_indirect,1070LLVMValueRef vertex_index,1071boolean is_aindex_indirect,1072LLVMValueRef attrib_index,1073LLVMValueRef swizzle_index,1074uint32_t name)1075{1076swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;10771078Value *vert_index = unwrap(vertex_index);1079Value *attr_index = unwrap(attrib_index);10801081IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));10821083if (verbose_tcs_shader_in) {1084lp_build_print_value(gallivm, "[TCS INOUT] Vertex index: ", vertex_index);1085lp_build_print_value(gallivm, "[TCS INOUT] Attrib index: ", wrap(attr_index));1086lp_build_print_value(gallivm, "[TCS INOUT] Swizzle index: ", swizzle_index);1087}10881089Value* res = unwrap(bld_base->base.zero);10901091for (uint32_t lane = 0; lane < mVWidth; lane++) {1092Value* p1 = LOAD(iface->pTcsCtx, {0, SWR_HS_CONTEXT_pCPout});1093Value* pCpOut = GEP(p1, {lane});10941095Value *vert_chan_index = vert_index;1096Value *attr_chan_index = attr_index;10971098if (is_vindex_indirect) {1099vert_chan_index = VEXTRACT(vert_index, C(lane));1100if (verbose_tcs_shader_in) {1101lp_build_print_value(gallivm, "[TCS INOUT] Extracted vertex index: ", wrap(vert_chan_index));1102}1103}11041105if (is_aindex_indirect) {1106attr_chan_index = VEXTRACT(attr_index, C(lane));1107if (verbose_tcs_shader_in) {1108lp_build_print_value(gallivm, "[TCS INOUT] Extracted attrib index: ", wrap(attr_chan_index));1109}1110}11111112if (name == TGSI_SEMANTIC_TESSOUTER || name == TGSI_SEMANTIC_TESSINNER) {1113Value* tessFactors = GEP(pCpOut, {(uint32_t)0, ScalarPatch_tessFactors});1114Value* tessFactorArray = nullptr;1115if (name == TGSI_SEMANTIC_TESSOUTER) {1116tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_OuterTessFactors});1117} else {1118tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_InnerTessFactors});1119}1120Value* tessFactor = GEP(tessFactorArray, {C(0), unwrap(swizzle_index)});1121res = VINSERT(res, LOAD(tessFactor), C(lane));1122if (verbose_tcs_shader_in) {1123lp_build_print_value(gallivm, "[TCS INOUT][FACTOR] lane (patch-id): ", wrap(C(lane)));1124lp_build_print_value(gallivm, "[TCS INOUT][FACTOR] loaded value: ", wrap(res));1125}1126} else if (name == TGSI_SEMANTIC_PATCH) {1127Value* attr_index_from_map = LOAD(GEP(iface->pPatchOutputAttribMap, {C(0), attr_chan_index}));1128Value* attr_value = GEP(pCpOut, {C(0), C(ScalarPatch_patchData), C(ScalarCPoint_attrib), attr_index_from_map, unwrap(swizzle_index)});1129res = VINSERT(res, LOAD(attr_value), C(lane));1130if (verbose_tcs_shader_in) {1131lp_build_print_value(gallivm, "[TCS INOUT][PATCH] attr index loaded from map: ", wrap(attr_index_from_map));1132lp_build_print_value(gallivm, "[TCS INOUT][PATCH] lane (patch-id): ", wrap(C(lane)));1133lp_build_print_value(gallivm, "[TCS INOUT][PATCH] loaded value: ", wrap(res));1134}1135} else {1136// Generic attribute1137Value *attrib =1138LOAD(GEP(iface->pVtxOutputAttribMap, {C(0), attr_chan_index}));1139if (verbose_tcs_shader_in) {1140lp_build_print_value(gallivm, "[TCS INOUT][VTX] Attrib index from map: ", wrap(attrib));1141}1142Value* attr_chan = GEP(pCpOut, {C(0), C(ScalarPatch_cp), vert_chan_index,1143C(ScalarCPoint_attrib), attrib, unwrap(swizzle_index)});11441145res = VINSERT(res, LOAD(attr_chan), C(lane));1146if (verbose_tcs_shader_in) {1147lp_build_print_value(gallivm, "[TCS INOUT][VTX] loaded value: ", wrap(res));1148}1149}1150}11511152return wrap(res);1153}11541155void1156BuilderSWR::swr_tcs_llvm_store_output(const struct lp_build_tcs_iface *tcs_iface,1157struct lp_build_tgsi_context *bld_base,1158unsigned name,1159boolean is_vindex_indirect,1160LLVMValueRef vertex_index,1161boolean is_aindex_indirect,1162LLVMValueRef attrib_index,1163LLVMValueRef swizzle_index,1164LLVMValueRef value,1165LLVMValueRef mask_vec)1166{1167swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;1168struct lp_build_tgsi_soa_context* bld = (struct lp_build_tgsi_soa_context*)bld_base;11691170IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));11711172if (verbose_tcs_shader_out) {1173lp_build_printf(gallivm, "[TCS OUT] =============================================\n");1174}11751176if (verbose_tcs_shader_out) {1177lp_build_print_value(gallivm, "[TCS OUT] Store mask: ", bld->exec_mask.exec_mask);1178lp_build_print_value(gallivm, "[TCS OUT] Store value: ", value);1179}11801181Value *vert_index = unwrap(vertex_index);1182Value *attr_index = unwrap(attrib_index);11831184if (verbose_tcs_shader_out) {1185lp_build_print_value(gallivm, "[TCS OUT] Vertex index: ", vertex_index);1186lp_build_print_value(gallivm, "[TCS OUT] Attrib index: ", wrap(attr_index));1187lp_build_print_value(gallivm, "[TCS OUT] Swizzle index: ", swizzle_index);1188}11891190if (is_vindex_indirect) {1191vert_index = VEXTRACT(vert_index, C(0));1192if (verbose_tcs_shader_out) {1193lp_build_print_value(gallivm, "[TCS OUT] Extracted vertex index: ", vertex_index);1194}1195}11961197if (is_aindex_indirect) {1198attr_index = VEXTRACT(attr_index, C(0));1199if (verbose_tcs_shader_out) {1200lp_build_print_value(gallivm, "[TCS OUT] Extracted attrib index: ", wrap(attr_index));1201}1202}12031204if (verbose_tcs_shader_out) {1205if (bld->exec_mask.has_mask) {1206lp_build_print_value(gallivm, "[TCS OUT] Exec mask: ", bld->exec_mask.exec_mask);1207}1208else {1209lp_build_printf(gallivm, "[TCS OUT] has no mask\n");1210}1211}1212for (uint32_t lane = 0; lane < mVWidth; lane++) {1213Value* p1 = LOAD(iface->pTcsCtx, {0, SWR_HS_CONTEXT_pCPout});1214Value* pCpOut = GEP(p1, {lane});12151216if (name == TGSI_SEMANTIC_TESSOUTER || name == TGSI_SEMANTIC_TESSINNER) {1217Value* tessFactors = GEP(pCpOut, {(uint32_t)0, ScalarPatch_tessFactors});1218Value* tessFactorArray = nullptr;1219if (name == TGSI_SEMANTIC_TESSOUTER) {1220tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_OuterTessFactors});1221} else {1222tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_InnerTessFactors});1223}1224Value* tessFactor = GEP(tessFactorArray, {C(0), unwrap(swizzle_index)});1225Value* valueToStore = VEXTRACT(unwrap(value), C(lane));1226valueToStore = BITCAST(valueToStore, mFP32Ty);1227if (mask_vec) {1228Value *originalVal = LOAD(tessFactor);1229Value *vMask = TRUNC(VEXTRACT(unwrap(mask_vec), C(lane)), mInt1Ty);1230valueToStore = SELECT(vMask, valueToStore, originalVal);1231}1232STORE(valueToStore, tessFactor);1233if (verbose_tcs_shader_out)1234{1235lp_build_print_value(gallivm, "[TCS OUT][FACTOR] Mask_vec mask: ", mask_vec);1236lp_build_print_value(gallivm, "[TCS OUT][FACTOR] Stored value: ", wrap(valueToStore));1237}1238} else if (name == TGSI_SEMANTIC_PATCH) {1239Value* attrib = LOAD(GEP(iface->pPatchOutputAttribMap, {C(0), attr_index}));1240if (verbose_tcs_shader_out) {1241lp_build_print_value(gallivm, "[TCS OUT][PATCH] vert_index: ", wrap(vert_index));1242lp_build_print_value(gallivm, "[TCS OUT][PATCH] attr_index: ", wrap(attr_index));1243lp_build_print_value(gallivm, "[TCS OUT][PATCH] vert_index_indirect: ", wrap(C(is_vindex_indirect)));1244lp_build_print_value(gallivm, "[TCS OUT][PATCH] attr_index_indirect: ", wrap(C(is_aindex_indirect)));1245lp_build_print_value(gallivm, "[TCS OUT][PATCH] attr index loaded from map: ", wrap(attrib));1246}1247Value* attr = GEP(pCpOut, {C(0), C(ScalarPatch_patchData), C(ScalarCPoint_attrib), attrib});1248Value* value_to_store = VEXTRACT(unwrap(value), C(lane));1249if (verbose_tcs_shader_out) {1250lp_build_print_value(gallivm, "[TCS OUT][PATCH] lane (patch-id): ", wrap(C(lane)));1251lp_build_print_value(gallivm, "[TCS OUT][PATCH] value to store: ", value);1252lp_build_print_value(gallivm, "[TCS OUT][PATCH] per-patch value to store: ", wrap(value_to_store));1253lp_build_print_value(gallivm, "[TCS OUT][PATCH] chan_index: ", swizzle_index);1254}1255value_to_store = BITCAST(value_to_store, mFP32Ty);1256if (mask_vec) {1257Value *originalVal = LOADV(attr, {C(0), unwrap(swizzle_index)});1258Value *vMask = TRUNC(VEXTRACT(unwrap(mask_vec), C(lane)), mInt1Ty);1259value_to_store = SELECT(vMask, value_to_store, originalVal);1260if (verbose_tcs_shader_out) {1261lp_build_print_value(gallivm, "[TCS OUT][PATCH] store mask: ", mask_vec);1262lp_build_print_value(gallivm, "[TCS OUT][PATCH] loaded original value: ", wrap(originalVal));1263lp_build_print_value(gallivm, "[TCS OUT][PATCH] vMask: ", wrap(vMask));1264lp_build_print_value(gallivm, "[TCS OUT][PATCH] selected value to store: ", wrap(value_to_store));1265}1266}1267STOREV(value_to_store, attr, {C(0), unwrap(swizzle_index)});1268if (verbose_tcs_shader_out) {1269lp_build_print_value(gallivm, "[TCS OUT][PATCH] stored value: ", wrap(value_to_store));1270}1271} else {1272Value* value_to_store = VEXTRACT(unwrap(value), C(lane));1273Value* attrib = LOAD(GEP(iface->pVtxOutputAttribMap, {C(0), attr_index}));12741275if (verbose_tcs_shader_out) {1276lp_build_printf(gallivm, "[TCS OUT] Writting attribute\n");1277lp_build_print_value(gallivm, "[TCS OUT][VTX] invocation_id: ", bld->system_values.invocation_id);1278lp_build_print_value(gallivm, "[TCS OUT][VTX] attribIndex: ", wrap(attr_index));1279lp_build_print_value(gallivm, "[TCS OUT][VTX] attrib read from map: ", wrap(attrib));1280lp_build_print_value(gallivm, "[TCS OUT][VTX] chan_index: ", swizzle_index);1281lp_build_print_value(gallivm, "[TCS OUT][VTX] value: ", value);1282lp_build_print_value(gallivm, "[TCS OUT][VTX] value_to_store: ", wrap(value_to_store));1283}12841285Value* attr_chan = GEP(pCpOut, {C(0), C(ScalarPatch_cp),1286VEXTRACT(unwrap(bld->system_values.invocation_id), C(0)),1287C(ScalarCPoint_attrib), attrib, unwrap(swizzle_index)});12881289// Mask output values if needed1290value_to_store = BITCAST(value_to_store, mFP32Ty);1291if (mask_vec) {1292Value *originalVal = LOAD(attr_chan);1293Value *vMask = TRUNC(VEXTRACT(unwrap(mask_vec), C(lane)), mInt1Ty);1294value_to_store = SELECT(vMask, value_to_store, originalVal);1295}1296STORE(value_to_store, attr_chan);1297if (verbose_tcs_shader_out) {1298lp_build_print_value(gallivm, "[TCS OUT][VTX] Mask_vec mask: ", mask_vec);1299lp_build_print_value(gallivm, "[TCS OUT][VTX] stored: ", wrap(value_to_store));1300}1301}1302}1303}13041305void1306BuilderSWR::swr_tcs_llvm_emit_barrier(const struct lp_build_tcs_iface *tcs_iface,1307struct lp_build_tgsi_context *bld_base)1308{1309swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;1310struct lp_build_tgsi_soa_context* bld = (struct lp_build_tgsi_soa_context*)bld_base;13111312if (verbose_tcs_shader_loop) {1313lp_build_print_value(gallivm, "Barrier LOOP: Iteration %d END\n", iface->loop_var);1314}13151316struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;13171318STORE(ADD(LOAD(unwrap(iface->loop_var)), VBROADCAST(C(1))), unwrap(iface->loop_var));13191320LLVMValueRef tmp = lp_build_cmp(uint_bld, PIPE_FUNC_GEQUAL, wrap(LOAD(unwrap(iface->loop_var))),1321wrap(VBROADCAST(C(iface->output_vertices))));13221323lp_exec_mask_cond_push(&bld->exec_mask, tmp);1324lp_exec_break(&bld->exec_mask, &bld->bld_base.pc, false);1325lp_exec_mask_cond_pop(&bld->exec_mask);1326lp_exec_endloop(bld->bld_base.base.gallivm, &bld->exec_mask);13271328IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));13291330STORE(VBROADCAST(C(0)), unwrap(iface->loop_var));1331lp_exec_bgnloop(&bld->exec_mask, true);13321333IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));13341335bld->system_values.invocation_id = wrap((LOAD(unwrap(iface->loop_var))));13361337if (verbose_tcs_shader_loop) {1338lp_build_print_value(gallivm, "Barrier LOOP: Iteration BEGIN: ", iface->loop_var);1339lp_build_print_value(gallivm, "Barrier LOOP: InvocationId: \n", bld->system_values.invocation_id);1340}1341}134213431344LLVMValueRef1345BuilderSWR::swr_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface *tes_iface,1346struct lp_build_tgsi_context * bld_base,1347boolean is_aindex_indirect,1348LLVMValueRef attrib_index,1349LLVMValueRef swizzle_index)1350{1351swr_tes_llvm_iface *iface = (swr_tes_llvm_iface*)tes_iface;1352Value *attr_index = unwrap(attrib_index);1353Value *res = unwrap(bld_base->base.zero);13541355IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));13561357if (verbose_shader) {1358lp_build_printf(gallivm, "[TES IN][PATCH] --------------------------------------\n");1359}13601361if (is_aindex_indirect) {1362int i;1363struct lp_type type = bld_base->base.type;13641365for (i = 0; i < type.length; i++) {1366Value *attr_chan_index = attr_index;13671368if (is_aindex_indirect) {1369attr_chan_index = VEXTRACT(attr_index, C(i));1370}13711372Value *attrib =1373LOAD(GEP(iface->pPatchAttribMap, {C(0), attr_chan_index}));13741375Value *pCpIn = LOAD(iface->pTesCtx, {0, SWR_DS_CONTEXT_pCpIn}, "pCpIn");1376Value *pPatchData = GEP(pCpIn, {(uint32_t)0, ScalarPatch_patchData});1377Value *pAttr = GEP(pPatchData, {(uint32_t)0, ScalarCPoint_attrib});1378Value *Val = LOADV(pAttr, {C(0), attrib, unwrap(swizzle_index)});1379if (verbose_shader) {1380lp_build_print_value(gallivm, "[TES IN][PATCH] attrib_index: ", attrib_index);1381lp_build_print_value(gallivm, "[TES IN][PATCH] attr_chan_index: ", wrap(attr_chan_index));1382lp_build_print_value(gallivm, "[TES IN][PATCH] attrib read from map: ", wrap(attrib));1383lp_build_print_value(gallivm, "[TES IN][PATCH] swizzle_index: ", swizzle_index);1384lp_build_print_value(gallivm, "[TES IN][PATCH] Loaded: ", wrap(Val));1385}1386res = VINSERT(res, Val, C(i));1387}1388} else {1389Value *attrib = LOAD(GEP(iface->pPatchAttribMap, {C(0), attr_index}));13901391Value *pCpIn = LOAD(iface->pTesCtx, {(uint32_t)0, SWR_DS_CONTEXT_pCpIn}, "pCpIn");1392Value *pPatchData = GEP(pCpIn, {(uint32_t)0, ScalarPatch_patchData});1393Value *pAttr = GEP(pPatchData, {(uint32_t)0, ScalarCPoint_attrib});1394Value *Val = LOADV(pAttr, {C(0), attrib, unwrap(swizzle_index)});1395if (verbose_shader) {1396lp_build_print_value(gallivm, "[TES IN][PATCH] attrib_index: ", attrib_index);1397lp_build_print_value(gallivm, "[TES IN][PATCH] attr_chan_index: ", wrap(attr_index));1398lp_build_print_value(gallivm, "[TES IN][PATCH] attrib read from map: ", wrap(attrib));1399lp_build_print_value(gallivm, "[TES IN][PATCH] swizzle_index: ", swizzle_index);1400lp_build_print_value(gallivm, "[TES IN][PATCH] Loaded: ", wrap(Val));1401}1402res = VBROADCAST(Val);1403}1404if (verbose_shader) {1405lp_build_print_value(gallivm, "[TES IN][PATCH] returning: ", wrap(res));1406}1407return wrap(res);1408}1409141014111412LLVMValueRef1413BuilderSWR::swr_tes_llvm_fetch_vtx_input(const struct lp_build_tes_iface *tes_iface,1414struct lp_build_tgsi_context * bld_base,1415boolean is_vindex_indirect,1416LLVMValueRef vertex_index,1417boolean is_aindex_indirect,1418LLVMValueRef attrib_index,1419LLVMValueRef swizzle_index)1420{1421swr_tes_llvm_iface *iface = (swr_tes_llvm_iface*)tes_iface;1422Value *vert_index = unwrap(vertex_index);1423Value *attr_index = unwrap(attrib_index);14241425IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));14261427if (verbose_shader) {1428lp_build_printf(gallivm, "[TES IN][VTX] --------------------------------------\n");1429}14301431Value *res = unwrap(bld_base->base.zero);1432if (is_vindex_indirect || is_aindex_indirect) {1433int i;1434struct lp_type type = bld_base->base.type;14351436for (i = 0; i < type.length; i++) {1437Value *vert_chan_index = vert_index;1438Value *attr_chan_index = attr_index;14391440if (is_vindex_indirect) {1441vert_chan_index = VEXTRACT(vert_index, C(i));1442}1443if (is_aindex_indirect) {1444attr_chan_index = VEXTRACT(attr_index, C(i));1445}14461447Value *attrib =1448LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_chan_index}));14491450Value *pCpIn = LOAD(iface->pTesCtx, {0, SWR_DS_CONTEXT_pCpIn}, "pCpIn");1451Value *pCp = GEP(pCpIn, {0, ScalarPatch_cp});1452Value *pVertex = GEP(pCp, {(Value*)C(0), vert_chan_index});1453Value *pAttrTab = GEP(pVertex, {uint32_t(0), uint32_t(0)});1454Value *pAttr = GEP(pAttrTab, {(Value*)C(0), attrib});1455Value *Val = LOADV(pAttr, {C(0), unwrap(swizzle_index)});1456if (verbose_shader) {1457lp_build_print_value(gallivm, "[TES IN][VTX] attrib_index: ", attrib_index);1458lp_build_print_value(gallivm, "[TES IN][VTX] attr_chan_index: ", wrap(attr_index));1459lp_build_print_value(gallivm, "[TES IN][VTX] attrib read from map: ", wrap(attrib));1460lp_build_print_value(gallivm, "[TES IN][VTX] swizzle_index: ", swizzle_index);1461lp_build_print_value(gallivm, "[TES IN][VTX] Loaded: ", wrap(Val));1462}1463res = VINSERT(res, Val, C(i));1464}1465} else {1466Value *attrib = LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_index}));14671468Value *pCpIn = LOAD(iface->pTesCtx, {0, SWR_DS_CONTEXT_pCpIn}, "pCpIn");1469Value *pCp = GEP(pCpIn, {0, ScalarPatch_cp});1470Value *pVertex = GEP(pCp, {(Value*)C(0), vert_index});1471Value *pAttrTab = GEP(pVertex, {uint32_t(0), uint32_t(0)});1472Value *pAttr = GEP(pAttrTab, {(Value*)C(0), attrib});1473Value *Val = LOADV(pAttr, {C(0), unwrap(swizzle_index)});1474if (verbose_shader) {1475lp_build_print_value(gallivm, "[TES IN][VTX] attrib_index: ", attrib_index);1476lp_build_print_value(gallivm, "[TES IN][VTX] attr_chan_index: ", wrap(attr_index));1477lp_build_print_value(gallivm, "[TES IN][VTX] attrib read from map: ", wrap(attrib));1478lp_build_print_value(gallivm, "[TES IN][VTX] swizzle_index: ", swizzle_index);1479lp_build_print_value(gallivm, "[TES IN][VTX] Loaded: ", wrap(Val));1480}1481res = VBROADCAST(Val);1482}1483if (verbose_shader) {1484lp_build_print_value(gallivm, "[TES IN][VTX] returning: ", wrap(res));1485}1486return wrap(res);1487}14881489149014911492PFN_GS_FUNC1493BuilderSWR::CompileGS(struct swr_context *ctx, swr_jit_gs_key &key)1494{1495SWR_GS_STATE *pGS = &ctx->gs->gsState;1496struct tgsi_shader_info *info = &ctx->gs->info.base;14971498memset(pGS, 0, sizeof(*pGS));14991500pGS->gsEnable = true;15011502pGS->numInputAttribs = (VERTEX_ATTRIB_START_SLOT - VERTEX_POSITION_SLOT) + info->num_inputs;1503pGS->outputTopology =1504swr_convert_prim_topology(info->properties[TGSI_PROPERTY_GS_OUTPUT_PRIM], 0);15051506/* It's +1 because emit_vertex in swr is always called exactly one time more1507* than max_vertices passed in Geometry Shader. We need to allocate more memory1508* to avoid crash/memory overwritten.1509*/1510pGS->maxNumVerts = info->properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES] + 1;1511pGS->instanceCount = info->properties[TGSI_PROPERTY_GS_INVOCATIONS];15121513// If point primitive then assume to use multiple streams1514if(pGS->outputTopology == TOP_POINT_LIST) {1515pGS->isSingleStream = false;1516} else {1517pGS->isSingleStream = true;1518pGS->singleStreamID = 0;1519}15201521pGS->vertexAttribOffset = VERTEX_POSITION_SLOT;1522pGS->inputVertStride = pGS->numInputAttribs + pGS->vertexAttribOffset;1523pGS->outputVertexSize = SWR_VTX_NUM_SLOTS;1524pGS->controlDataSize = 8; // GS outputs max of 8 32B units1525pGS->controlDataOffset = VERTEX_COUNT_SIZE;1526pGS->outputVertexOffset = pGS->controlDataOffset + CONTROL_HEADER_SIZE;15271528pGS->allocationSize =1529VERTEX_COUNT_SIZE + // vertex count1530CONTROL_HEADER_SIZE + // control header1531(SWR_VTX_NUM_SLOTS * 16) * // sizeof vertex1532pGS->maxNumVerts; // num verts15331534struct swr_geometry_shader *gs = ctx->gs;15351536LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];1537LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];15381539memset(outputs, 0, sizeof(outputs));15401541AttrBuilder attrBuilder;1542attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));15431544std::vector<Type *> gsArgs{PointerType::get(Gen_swr_draw_context(JM()), 0),1545PointerType::get(mInt8Ty, 0),1546PointerType::get(Gen_SWR_GS_CONTEXT(JM()), 0)};1547FunctionType *vsFuncType =1548FunctionType::get(Type::getVoidTy(JM()->mContext), gsArgs, false);15491550// create new vertex shader function1551auto pFunction = Function::Create(vsFuncType,1552GlobalValue::ExternalLinkage,1553"GS",1554JM()->mpCurrentModule);1555#if LLVM_VERSION_MAJOR < 51556AttributeSet attrSet = AttributeSet::get(1557JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);1558pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);1559#else1560pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);1561#endif15621563BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);1564IRB()->SetInsertPoint(block);1565LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));15661567auto argitr = pFunction->arg_begin();1568Value *hPrivateData = &*argitr++;1569hPrivateData->setName("hPrivateData");1570Value *pWorkerData = &*argitr++;1571pWorkerData->setName("pWorkerData");1572Value *pGsCtx = &*argitr++;1573pGsCtx->setName("gsCtx");15741575Value *consts_ptr =1576GEP(hPrivateData, {C(0), C(swr_draw_context_constantGS)});1577consts_ptr->setName("gs_constants");1578Value *const_sizes_ptr =1579GEP(hPrivateData, {0, swr_draw_context_num_constantsGS});1580const_sizes_ptr->setName("num_gs_constants");15811582struct lp_build_sampler_soa *sampler =1583swr_sampler_soa_create(key.sampler, PIPE_SHADER_GEOMETRY);1584assert(sampler != nullptr);15851586struct lp_bld_tgsi_system_values system_values;1587memset(&system_values, 0, sizeof(system_values));1588system_values.prim_id = wrap(LOAD(pGsCtx, {0, SWR_GS_CONTEXT_PrimitiveID}));1589system_values.invocation_id = wrap(LOAD(pGsCtx, {0, SWR_GS_CONTEXT_InstanceID}));15901591std::vector<Constant*> mapConstants;1592Value *vtxAttribMap = ALLOCA(ArrayType::get(mInt32Ty, PIPE_MAX_SHADER_INPUTS));1593for (unsigned slot = 0; slot < info->num_inputs; slot++) {1594ubyte semantic_name = info->input_semantic_name[slot];1595ubyte semantic_idx = info->input_semantic_index[slot];15961597unsigned vs_slot = locate_linkage(semantic_name, semantic_idx, &ctx->vs->info.base);1598assert(vs_slot < PIPE_MAX_SHADER_OUTPUTS);15991600vs_slot += VERTEX_ATTRIB_START_SLOT;16011602if (ctx->vs->info.base.output_semantic_name[0] == TGSI_SEMANTIC_POSITION)1603vs_slot--;16041605if (semantic_name == TGSI_SEMANTIC_POSITION)1606vs_slot = VERTEX_POSITION_SLOT;16071608STORE(C(vs_slot), vtxAttribMap, {0, slot});1609mapConstants.push_back(C(vs_slot));1610}16111612struct lp_build_mask_context mask;1613Value *mask_val = LOAD(pGsCtx, {0, SWR_GS_CONTEXT_mask}, "gsMask");1614lp_build_mask_begin(&mask, gallivm,1615lp_type_float_vec(32, 32 * 8), wrap(mask_val));16161617// zero out cut buffer so we can load/modify/store bits1618for (uint32_t lane = 0; lane < mVWidth; ++lane)1619{1620Value* pStream = LOAD(pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});1621#if LLVM_VERSION_MAJOR >= 101622MEMSET(pStream, C((char)0), VERTEX_COUNT_SIZE + CONTROL_HEADER_SIZE, MaybeAlign(sizeof(float) * KNOB_SIMD_WIDTH));1623#else1624MEMSET(pStream, C((char)0), VERTEX_COUNT_SIZE + CONTROL_HEADER_SIZE, sizeof(float) * KNOB_SIMD_WIDTH);1625#endif1626}16271628struct swr_gs_llvm_iface gs_iface;1629gs_iface.base.fetch_input = ::swr_gs_llvm_fetch_input;1630gs_iface.base.emit_vertex = ::swr_gs_llvm_emit_vertex;1631gs_iface.base.end_primitive = ::swr_gs_llvm_end_primitive;1632gs_iface.base.gs_epilogue = ::swr_gs_llvm_epilogue;1633gs_iface.pBuilder = this;1634gs_iface.pGsCtx = pGsCtx;1635gs_iface.pGsState = pGS;1636gs_iface.num_outputs = gs->info.base.num_outputs;1637gs_iface.num_verts_per_prim =1638u_vertices_per_prim((pipe_prim_type)info->properties[TGSI_PROPERTY_GS_OUTPUT_PRIM]);1639gs_iface.info = info;1640gs_iface.pVtxAttribMap = vtxAttribMap;16411642struct lp_build_tgsi_params params;1643memset(¶ms, 0, sizeof(params));1644params.type = lp_type_float_vec(32, 32 * 8);1645params.mask = & mask;1646params.consts_ptr = wrap(consts_ptr);1647params.const_sizes_ptr = wrap(const_sizes_ptr);1648params.system_values = &system_values;1649params.inputs = inputs;1650params.context_ptr = wrap(hPrivateData);1651params.sampler = sampler;1652params.info = &gs->info.base;1653params.gs_iface = &gs_iface.base;16541655lp_build_tgsi_soa(gallivm,1656gs->pipe.tokens,1657¶ms,1658outputs);16591660lp_build_mask_end(&mask);16611662sampler->destroy(sampler);16631664IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));16651666RET_VOID();16671668gallivm_verify_function(gallivm, wrap(pFunction));1669gallivm_compile_module(gallivm);16701671PFN_GS_FUNC pFunc =1672(PFN_GS_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));16731674debug_printf("geom shader %p\n", pFunc);1675assert(pFunc && "Error: GeomShader = NULL");16761677JM()->mIsModuleFinalized = true;16781679return pFunc;1680}16811682PFN_TES_FUNC1683BuilderSWR::CompileTES(struct swr_context *ctx, swr_jit_tes_key &key)1684{1685SWR_TS_STATE *pTS = &ctx->tsState;1686struct tgsi_shader_info *info = &ctx->tes->info.base;16871688// tessellation is enabled if TES is present1689// clear tessellation state here then1690memset(pTS, 0, sizeof(*pTS));16911692pTS->tsEnable = true;16931694unsigned tes_prim_mode = info->properties[TGSI_PROPERTY_TES_PRIM_MODE];1695unsigned tes_spacing = info->properties[TGSI_PROPERTY_TES_SPACING];1696bool tes_vertex_order_cw = info->properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW];1697bool tes_point_mode = info->properties[TGSI_PROPERTY_TES_POINT_MODE];1698SWR_TS_DOMAIN type = SWR_TS_ISOLINE;1699SWR_TS_PARTITIONING partitioning = SWR_TS_EVEN_FRACTIONAL;1700SWR_TS_OUTPUT_TOPOLOGY topology = SWR_TS_OUTPUT_POINT;1701PRIMITIVE_TOPOLOGY postDSTopology = TOP_POINT_LIST;17021703// TESS_TODO: move this to helper functions to improve readability1704switch (tes_prim_mode) {1705case PIPE_PRIM_LINES:1706type = SWR_TS_ISOLINE;1707postDSTopology = TOP_LINE_LIST;1708break;1709case PIPE_PRIM_TRIANGLES:1710type = SWR_TS_TRI;1711postDSTopology = TOP_TRIANGLE_LIST;1712break;1713case PIPE_PRIM_QUADS:1714type = SWR_TS_QUAD;1715// See OpenGL spec - quads are tessellated into triangles1716postDSTopology = TOP_TRIANGLE_LIST;1717break;1718default:1719assert(0);1720}17211722switch (tes_spacing) {1723case PIPE_TESS_SPACING_FRACTIONAL_ODD:1724partitioning = SWR_TS_ODD_FRACTIONAL;1725break;1726case PIPE_TESS_SPACING_FRACTIONAL_EVEN:1727partitioning = SWR_TS_EVEN_FRACTIONAL;1728break;1729case PIPE_TESS_SPACING_EQUAL:1730partitioning = SWR_TS_INTEGER;1731break;1732default:1733assert(0);1734}17351736if (tes_point_mode) {1737topology = SWR_TS_OUTPUT_POINT;1738postDSTopology = TOP_POINT_LIST;1739}1740else if (tes_prim_mode == PIPE_PRIM_LINES) {1741topology = SWR_TS_OUTPUT_LINE;1742}1743else if (tes_vertex_order_cw) {1744topology = SWR_TS_OUTPUT_TRI_CW;1745}1746else {1747topology = SWR_TS_OUTPUT_TRI_CCW;1748}17491750pTS->domain = type;1751pTS->tsOutputTopology = topology;1752pTS->partitioning = partitioning;1753pTS->numDsOutputAttribs = info->num_outputs;1754pTS->postDSTopology = postDSTopology;17551756pTS->dsAllocationSize = SWR_VTX_NUM_SLOTS * MAX_NUM_VERTS_PER_PRIM;1757pTS->vertexAttribOffset = VERTEX_ATTRIB_START_SLOT;1758pTS->srcVertexAttribOffset = VERTEX_ATTRIB_START_SLOT;1759pTS->dsOutVtxAttribOffset = VERTEX_ATTRIB_START_SLOT;17601761struct swr_tess_evaluation_shader *tes = ctx->tes;17621763LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];1764LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];17651766memset(outputs, 0, sizeof(outputs));17671768AttrBuilder attrBuilder;1769attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));17701771std::vector<Type *> tesArgs{PointerType::get(Gen_swr_draw_context(JM()), 0),1772PointerType::get(mInt8Ty, 0),1773PointerType::get(Gen_SWR_DS_CONTEXT(JM()), 0)};1774FunctionType *tesFuncType =1775FunctionType::get(Type::getVoidTy(JM()->mContext), tesArgs, false);17761777// create new vertex shader function1778auto pFunction = Function::Create(tesFuncType,1779GlobalValue::ExternalLinkage,1780"TES",1781JM()->mpCurrentModule);17821783#if LLVM_VERSION_MAJOR < 51784AttributeSet attrSet = AttributeSet::get(1785JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);1786pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);1787#else1788pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);1789#endif17901791BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);1792IRB()->SetInsertPoint(block);1793LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));17941795auto argitr = pFunction->arg_begin();1796Value *hPrivateData = &*argitr++;1797hPrivateData->setName("hPrivateData");1798Value *pWorkerData = &*argitr++;1799pWorkerData->setName("pWorkerData");1800Value *pTesCtx = &*argitr++;1801pTesCtx->setName("tesCtx");18021803Value *consts_ptr =1804GEP(hPrivateData, {C(0), C(swr_draw_context_constantTES)});1805consts_ptr->setName("tes_constants");1806Value *const_sizes_ptr =1807GEP(hPrivateData, {0, swr_draw_context_num_constantsTES});1808const_sizes_ptr->setName("num_tes_constants");18091810struct lp_build_sampler_soa *sampler =1811swr_sampler_soa_create(key.sampler, PIPE_SHADER_TESS_EVAL);1812assert(sampler != nullptr);18131814struct lp_bld_tgsi_system_values system_values;1815memset(&system_values, 0, sizeof(system_values));18161817// Load and calculate system values1818// Tessellation coordinates (gl_TessCoord)1819Value *vecOffset = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_vectorOffset}, "vecOffset");1820Value *vecStride = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_vectorStride}, "vecStride");1821Value *vecIndex = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_vectorOffset});18221823Value* tess_coord = ALLOCA(ArrayType::get(mSimdFP32Ty, 3));18241825Value *tessCoordU = LOADV(LOAD(pTesCtx, {0, SWR_DS_CONTEXT_pDomainU}), {vecIndex}, "tessCoordU");1826STORE(tessCoordU, tess_coord, {0, 0});1827Value *tessCoordV = LOADV(LOAD(pTesCtx, {0, SWR_DS_CONTEXT_pDomainV}), {vecIndex}, "tessCoordV");1828STORE(tessCoordV, tess_coord, {0, 1});1829Value *tessCoordW = FSUB(FSUB(VIMMED1(1.0f), tessCoordU), tessCoordV, "tessCoordW");1830STORE(tessCoordW, tess_coord, {0, 2});1831system_values.tess_coord = wrap(tess_coord);18321833// Primitive ID1834system_values.prim_id = wrap(VBROADCAST(LOAD(pTesCtx, {0, SWR_DS_CONTEXT_PrimitiveID}), "PrimitiveID"));18351836// Tessellation factors1837Value* pPatch = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_pCpIn});1838Value* pTessFactors = GEP(pPatch, {C(0), C(ScalarPatch_tessFactors)});18391840assert(SWR_NUM_OUTER_TESS_FACTORS == 4);1841Value* sys_value_outer_factors = UndefValue::get(getVectorType(mFP32Ty, 4));1842for (unsigned i = 0; i < SWR_NUM_OUTER_TESS_FACTORS; i++) {1843Value* v = LOAD(pTessFactors, {0, SWR_TESSELLATION_FACTORS_OuterTessFactors, i});1844sys_value_outer_factors = VINSERT(sys_value_outer_factors, v, i, "gl_TessLevelOuter");1845}1846system_values.tess_outer = wrap(sys_value_outer_factors);18471848assert(SWR_NUM_INNER_TESS_FACTORS == 2);1849Value* sys_value_inner_factors = UndefValue::get(getVectorType(mFP32Ty, 4));1850for (unsigned i = 0; i < SWR_NUM_INNER_TESS_FACTORS; i++) {1851Value* v = LOAD(pTessFactors, {0, SWR_TESSELLATION_FACTORS_InnerTessFactors, i});1852sys_value_inner_factors = VINSERT(sys_value_inner_factors, v, i, "gl_TessLevelInner");1853}1854system_values.tess_inner = wrap(sys_value_inner_factors);18551856if (verbose_shader)1857{1858lp_build_print_value(gallivm, "tess_coord = ", system_values.tess_coord);1859}18601861struct tgsi_shader_info *pPrevShader = nullptr;18621863if (ctx->tcs) {1864pPrevShader = &ctx->tcs->info.base;1865}1866else {1867pPrevShader = &ctx->vs->info.base;1868}18691870// Figure out how many per-patch attributes we have1871unsigned perPatchAttrs = 0;1872unsigned genericAttrs = 0;1873unsigned tessLevelAttrs = 0;1874unsigned sgvAttrs = 0;1875for (unsigned slot = 0; slot < pPrevShader->num_outputs; slot++) {1876switch (pPrevShader->output_semantic_name[slot]) {1877case TGSI_SEMANTIC_PATCH:1878perPatchAttrs++;1879break;1880case TGSI_SEMANTIC_GENERIC:1881genericAttrs++;1882break;1883case TGSI_SEMANTIC_TESSINNER:1884case TGSI_SEMANTIC_TESSOUTER:1885tessLevelAttrs++;1886break;1887case TGSI_SEMANTIC_POSITION:1888case TGSI_SEMANTIC_CLIPDIST:1889case TGSI_SEMANTIC_PSIZE:1890sgvAttrs++;1891break;1892default:1893assert(!"Unknown semantic input in TES");1894}1895}18961897std::vector<Constant *> mapConstants;1898Value *vtxAttribMap = ALLOCA(ArrayType::get(mInt32Ty, PIPE_MAX_SHADER_INPUTS));1899Value *patchAttribMap = ALLOCA(ArrayType::get(mInt32Ty, PIPE_MAX_SHADER_INPUTS));1900for (unsigned slot = 0; slot < info->num_inputs; slot++) {1901ubyte semantic_name = info->input_semantic_name[slot];1902ubyte semantic_idx = info->input_semantic_index[slot];19031904// Where in TCS output is my attribute?1905// TESS_TODO: revisit after implement pass-through TCS1906unsigned tcs_slot = locate_linkage(semantic_name, semantic_idx, pPrevShader);1907assert(tcs_slot < PIPE_MAX_SHADER_OUTPUTS);19081909// Skip tessellation levels - these go to the tessellator, not TES1910switch (semantic_name) {1911case TGSI_SEMANTIC_GENERIC:1912tcs_slot = tcs_slot + VERTEX_ATTRIB_START_SLOT - sgvAttrs - tessLevelAttrs;1913break;1914case TGSI_SEMANTIC_PATCH:1915tcs_slot = semantic_idx;1916break;1917case TGSI_SEMANTIC_POSITION:1918tcs_slot = VERTEX_POSITION_SLOT;1919break;1920case TGSI_SEMANTIC_CLIPDIST:1921case TGSI_SEMANTIC_PSIZE:1922break;1923default:1924assert(!"Unexpected semantic found while building TES input map");1925}1926if (semantic_name == TGSI_SEMANTIC_PATCH) {1927STORE(C(tcs_slot), patchAttribMap, {0, slot});1928} else {1929STORE(C(tcs_slot), vtxAttribMap, {0, slot});1930}1931mapConstants.push_back(C(tcs_slot));1932}19331934// Build execution mask1935struct lp_build_mask_context mask;1936Value *mask_val = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_mask}, "tesMask");19371938if (verbose_shader)1939lp_build_print_value(gallivm, "TES execution mask: ", wrap(mask_val));19401941lp_build_mask_begin(&mask, gallivm,1942lp_type_float_vec(32, 32 * 8), wrap(mask_val));19431944struct swr_tes_llvm_iface tes_iface;19451946tes_iface.base.fetch_vertex_input = ::swr_tes_llvm_fetch_vtx_input;1947tes_iface.base.fetch_patch_input = ::swr_tes_llvm_fetch_patch_input;19481949tes_iface.pBuilder = this;1950tes_iface.pTesCtx = pTesCtx;1951tes_iface.pTsState = pTS;1952tes_iface.num_outputs = tes->info.base.num_outputs;1953tes_iface.info = info;1954tes_iface.pVtxAttribMap = vtxAttribMap;1955tes_iface.pPatchAttribMap = patchAttribMap;19561957struct lp_build_tgsi_params params;1958memset(¶ms, 0, sizeof(params));1959params.type = lp_type_float_vec(32, 32 * 8);1960params.mask = & mask;1961params.consts_ptr = wrap(consts_ptr);1962params.const_sizes_ptr = wrap(const_sizes_ptr);1963params.system_values = &system_values;1964params.inputs = inputs;1965params.context_ptr = wrap(hPrivateData);1966params.sampler = sampler;1967params.info = &tes->info.base;1968params.tes_iface = &tes_iface.base;19691970// Build LLVM IR1971lp_build_tgsi_soa(gallivm,1972tes->pipe.tokens,1973¶ms,1974outputs);19751976lp_build_mask_end(&mask);19771978sampler->destroy(sampler);19791980IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));19811982// Write output attributes1983Value *dclOut = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_pOutputData}, "dclOut");19841985for (uint32_t attrib = 0; attrib < PIPE_MAX_SHADER_OUTPUTS; attrib++) {1986for (uint32_t channel = 0; channel < TGSI_NUM_CHANNELS; channel++) {1987if (!outputs[attrib][channel])1988continue;19891990Value *val = LOAD(unwrap(outputs[attrib][channel]));;1991Value *attribOffset =1992LOAD(pTesCtx, {0, SWR_DS_CONTEXT_outVertexAttribOffset});19931994// Assume we write possition1995Value* outputSlot = C(VERTEX_POSITION_SLOT);1996if (tes->info.base.output_semantic_name[attrib] != TGSI_SEMANTIC_POSITION) {1997// No, it's a generic attribute, not a position - let's calculate output slot1998uint32_t outSlot = attrib;1999if (tes->info.base.output_semantic_name[0] == TGSI_SEMANTIC_POSITION) {2000// this shader will write position, so in shader's term2001// output starts at attrib 1, but we will handle that separately,2002// so let's fix the outSlot2003outSlot--;2004}2005outputSlot = ADD(attribOffset, C(outSlot));2006}20072008Value *attribVecIndex =2009ADD(MUL(vecStride, MUL(outputSlot, C(4))), vecOffset);20102011uint32_t outputComponent = 0;2012uint32_t curComp = outputComponent + channel;2013auto outValIndex = ADD(attribVecIndex, MUL(vecStride, C(curComp)));2014STOREV(val, dclOut, {outValIndex});20152016if (verbose_shader) {2017lp_build_printf(gallivm,2018"TES output [%d][%d]",2019C(attrib),2020C(channel));2021lp_build_print_value(gallivm, " = ", wrap(val));2022}2023}2024}20252026RET_VOID();20272028JM()->DumpToFile(pFunction, "src");2029gallivm_verify_function(gallivm, wrap(pFunction));20302031gallivm_compile_module(gallivm);2032JM()->DumpToFile(pFunction, "optimized");20332034PFN_TES_FUNC pFunc =2035(PFN_TES_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));20362037debug_printf("tess evaluation shader %p\n", pFunc);2038assert(pFunc && "Error: TessEvaluationShader = NULL");20392040JM()->DumpAsm(pFunction, "asm");20412042JM()->mIsModuleFinalized = true;20432044return pFunc;2045}20462047PFN_TCS_FUNC2048BuilderSWR::CompileTCS(struct swr_context *ctx, swr_jit_tcs_key &key)2049{2050SWR_TS_STATE *pTS = &ctx->tsState;2051struct tgsi_shader_info *info = &ctx->tcs->info.base;20522053pTS->numHsInputAttribs = info->num_inputs;2054pTS->numHsOutputAttribs = info->num_outputs;20552056pTS->hsAllocationSize = sizeof(ScalarPatch);20572058pTS->vertexAttribOffset = VERTEX_ATTRIB_START_SLOT;2059pTS->srcVertexAttribOffset = VERTEX_ATTRIB_START_SLOT;20602061struct swr_tess_control_shader *tcs = ctx->tcs;20622063LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];2064LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];20652066memset(outputs, 0, sizeof(outputs));20672068AttrBuilder attrBuilder;2069attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));20702071std::vector<Type *> tcsArgs{2072PointerType::get(Gen_swr_draw_context(JM()), 0),2073PointerType::get(mInt8Ty, 0),2074PointerType::get(Gen_SWR_HS_CONTEXT(JM()), 0)};2075FunctionType *tcsFuncType =2076FunctionType::get(Type::getVoidTy(JM()->mContext), tcsArgs, false);20772078// create new vertex shader function2079auto pFunction = Function::Create(tcsFuncType,2080GlobalValue::ExternalLinkage,2081"TCS",2082JM()->mpCurrentModule);20832084#if LLVM_VERSION_MAJOR < 52085AttributeSet attrSet = AttributeSet::get(2086JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);2087pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);2088#else2089pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);2090#endif20912092BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);2093IRB()->SetInsertPoint(block);2094LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));20952096auto argitr = pFunction->arg_begin();2097Value *hPrivateData = &*argitr++;2098hPrivateData->setName("hPrivateData");2099Value *pWorkerData = &*argitr++;2100pWorkerData->setName("pWorkerData");2101Value *pTcsCtx = &*argitr++;2102pTcsCtx->setName("tcsCtx");21032104Value *consts_ptr =2105GEP(hPrivateData, {C(0), C(swr_draw_context_constantTCS)});2106consts_ptr->setName("tcs_constants");2107Value *const_sizes_ptr =2108GEP(hPrivateData, {0, swr_draw_context_num_constantsTCS});2109const_sizes_ptr->setName("num_tcs_constants");21102111struct lp_build_sampler_soa *sampler =2112swr_sampler_soa_create(key.sampler, PIPE_SHADER_TESS_CTRL);2113assert(sampler != nullptr);21142115struct lp_bld_tgsi_system_values system_values;2116memset(&system_values, 0, sizeof(system_values));21172118system_values.prim_id =2119wrap(LOAD(pTcsCtx, {0, SWR_HS_CONTEXT_PrimitiveID}));21202121system_values.invocation_id = wrap(VBROADCAST(C(0)));2122system_values.vertices_in = wrap(C(tcs->vertices_per_patch));21232124if (verbose_shader) {2125lp_build_print_value(gallivm, "TCS::prim_id = ", system_values.prim_id);2126lp_build_print_value(gallivm, "TCS::invocation_id = ", system_values.invocation_id);2127lp_build_print_value(gallivm, "TCS::vertices_in = ", system_values.vertices_in);2128}21292130std::vector<Constant *> mapConstants;2131Value *vtxAttribMap =2132ALLOCA(ArrayType::get(mInt32Ty, PIPE_MAX_SHADER_INPUTS));21332134for (unsigned slot = 0; slot < info->num_inputs; slot++) {2135ubyte semantic_name = info->input_semantic_name[slot];2136ubyte semantic_idx = info->input_semantic_index[slot];21372138unsigned vs_slot =2139locate_linkage(semantic_name, semantic_idx, &ctx->vs->info.base);2140assert(vs_slot < PIPE_MAX_SHADER_OUTPUTS);21412142vs_slot += VERTEX_ATTRIB_START_SLOT;21432144if (ctx->vs->info.base.output_semantic_name[0]2145== TGSI_SEMANTIC_POSITION)2146vs_slot--;21472148if (semantic_name == TGSI_SEMANTIC_POSITION)2149vs_slot = VERTEX_POSITION_SLOT;21502151STORE(C(vs_slot), vtxAttribMap, {0, slot});2152mapConstants.push_back(C(vs_slot));2153}21542155// Prepare map of output attributes. Needed when shader instance wants2156// to read own output or output of other instance, which is allowed in TCS2157Value *vtxOutputAttribMap =2158ALLOCA(ArrayType::get(mInt32Ty, PIPE_MAX_SHADER_INPUTS));2159// Map for per-patch attributes2160Value *patchOutputAttribMap =2161ALLOCA(ArrayType::get(mInt32Ty, PIPE_MAX_SHADER_INPUTS));2162for (unsigned slot = 0; slot < info->num_outputs; slot++) {2163ubyte name = info->output_semantic_name[slot];2164int32_t idx = info->output_semantic_index[slot];2165if (name == TGSI_SEMANTIC_PATCH) {2166STORE(C(idx), patchOutputAttribMap, {0, slot});2167} else {2168int32_t target_slot = slot;2169if (name == TGSI_SEMANTIC_GENERIC) {2170target_slot += VERTEX_ATTRIB_START_SLOT;2171}2172// Now normalize target slot2173for (ubyte as = 0; as < slot; as++) {2174ubyte name = info->output_semantic_name[as];2175switch (name) {2176case TGSI_SEMANTIC_TESSOUTER:2177case TGSI_SEMANTIC_TESSINNER:2178case TGSI_SEMANTIC_PATCH:2179case TGSI_SEMANTIC_POSITION:2180target_slot--;2181}2182}2183if (name == TGSI_SEMANTIC_POSITION) {2184target_slot = VERTEX_POSITION_SLOT;2185}2186STORE(C(target_slot), vtxOutputAttribMap, {0, slot});2187mapConstants.push_back(C(target_slot));2188}2189}21902191struct lp_build_mask_context mask;2192Value *mask_val = LOAD(pTcsCtx, {0, SWR_HS_CONTEXT_mask}, "tcsMask");2193lp_build_mask_begin(2194&mask, gallivm, lp_type_float_vec(32, 32 * 8), wrap(mask_val));21952196struct swr_tcs_llvm_iface tcs_iface;21972198tcs_iface.base.emit_store_output = ::swr_tcs_llvm_store_output;2199tcs_iface.base.emit_fetch_input = ::swr_tcs_llvm_fetch_input;2200tcs_iface.base.emit_fetch_output = ::swr_tcs_llvm_fetch_output;2201tcs_iface.base.emit_barrier = ::swr_tcs_llvm_emit_barrier;2202tcs_iface.base.emit_prologue = ::swr_tcs_llvm_emit_prologue;2203tcs_iface.base.emit_epilogue = ::swr_tcs_llvm_emit_epilogue;22042205tcs_iface.pBuilder = this;2206tcs_iface.pTcsCtx = pTcsCtx;2207tcs_iface.pTsState = pTS;2208tcs_iface.output_vertices = info->properties[TGSI_PROPERTY_TCS_VERTICES_OUT];2209tcs_iface.info = info;2210tcs_iface.pVtxAttribMap = vtxAttribMap;2211tcs_iface.pVtxOutputAttribMap = vtxOutputAttribMap;2212tcs_iface.pPatchOutputAttribMap = patchOutputAttribMap;22132214struct lp_build_tgsi_params params;2215memset(¶ms, 0, sizeof(params));2216params.type = lp_type_float_vec(32, 32 * 8);2217params.mask = &mask;2218params.consts_ptr = wrap(consts_ptr);2219params.const_sizes_ptr = wrap(const_sizes_ptr);2220params.system_values = &system_values;2221params.inputs = inputs;2222params.context_ptr = wrap(hPrivateData);2223params.sampler = sampler;2224params.info = &tcs->info.base;2225params.tcs_iface = &tcs_iface.base;22262227lp_build_tgsi_soa(gallivm, tcs->pipe.tokens, ¶ms, outputs);22282229lp_build_mask_end(&mask);22302231sampler->destroy(sampler);22322233IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));2234RET_VOID();22352236JM()->DumpToFile(pFunction, "src");2237gallivm_verify_function(gallivm, wrap(pFunction));2238gallivm_compile_module(gallivm);2239JM()->DumpToFile(pFunction, "optimized");22402241PFN_TCS_FUNC pFunc =2242(PFN_TCS_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));22432244debug_printf("tess control shader %p\n", pFunc);2245assert(pFunc && "Error: TessControlShader = NULL");2246JM()->DumpAsm(pFunction, "asm");22472248JM()->mIsModuleFinalized = true;22492250return pFunc;2251}225222532254PFN_GS_FUNC2255swr_compile_gs(struct swr_context *ctx, swr_jit_gs_key &key)2256{2257BuilderSWR builder(2258reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),2259"GS");2260PFN_GS_FUNC func = builder.CompileGS(ctx, key);22612262ctx->gs->map.insert(std::make_pair(key, std::unique_ptr<VariantGS>(new VariantGS(builder.gallivm, func))));2263return func;2264}22652266PFN_TCS_FUNC2267swr_compile_tcs(struct swr_context *ctx, swr_jit_tcs_key &key)2268{2269BuilderSWR builder(2270reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),2271"TCS");2272PFN_TCS_FUNC func = builder.CompileTCS(ctx, key);22732274ctx->tcs->map.insert(2275std::make_pair(key, std::unique_ptr<VariantTCS>(new VariantTCS(builder.gallivm, func))));22762277return func;2278}22792280PFN_TES_FUNC2281swr_compile_tes(struct swr_context *ctx, swr_jit_tes_key &key)2282{2283BuilderSWR builder(2284reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),2285"TES");2286PFN_TES_FUNC func = builder.CompileTES(ctx, key);22872288ctx->tes->map.insert(2289std::make_pair(key, std::unique_ptr<VariantTES>(new VariantTES(builder.gallivm, func))));22902291return func;2292}22932294void2295BuilderSWR::WriteVS(Value *pVal, Value *pVsContext, Value *pVtxOutput, unsigned slot, unsigned channel)2296{2297#if USE_SIMD16_FRONTEND && !USE_SIMD16_VS2298// interleave the simdvertex components into the dest simd16vertex2299// slot16offset = slot8offset * 22300// comp16offset = comp8offset * 2 + alternateOffset23012302Value *offset = LOAD(pVsContext, { 0, SWR_VS_CONTEXT_AlternateOffset });2303Value *pOut = GEP(pVtxOutput, { C(0), C(0), C(slot * 2), offset } );2304STORE(pVal, pOut, {channel * 2});2305#else2306Value *pOut = GEP(pVtxOutput, {0, 0, slot});2307STORE(pVal, pOut, {0, channel});2308if (verbose_vs_shader) {2309lp_build_printf(gallivm, "VS: Storing on slot %d, channel %d: ", C(slot), C(channel));2310lp_build_print_value(gallivm, "", wrap(pVal));2311}2312#endif2313}23142315PFN_VERTEX_FUNC2316BuilderSWR::CompileVS(struct swr_context *ctx, swr_jit_vs_key &key)2317{2318struct swr_vertex_shader *swr_vs = ctx->vs;23192320LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];2321LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];23222323memset(outputs, 0, sizeof(outputs));23242325AttrBuilder attrBuilder;2326attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));23272328std::vector<Type *> vsArgs{PointerType::get(Gen_swr_draw_context(JM()), 0),2329PointerType::get(mInt8Ty, 0),2330PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};2331FunctionType *vsFuncType =2332FunctionType::get(Type::getVoidTy(JM()->mContext), vsArgs, false);23332334// create new vertex shader function2335auto pFunction = Function::Create(vsFuncType,2336GlobalValue::ExternalLinkage,2337"VS",2338JM()->mpCurrentModule);2339#if LLVM_VERSION_MAJOR < 52340AttributeSet attrSet = AttributeSet::get(2341JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);2342pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);2343#else2344pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);2345#endif23462347BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);2348IRB()->SetInsertPoint(block);2349LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));23502351auto argitr = pFunction->arg_begin();2352Value *hPrivateData = &*argitr++;2353hPrivateData->setName("hPrivateData");2354Value *pWorkerData = &*argitr++;2355pWorkerData->setName("pWorkerData");2356Value *pVsCtx = &*argitr++;2357pVsCtx->setName("vsCtx");23582359Value *consts_ptr = GEP(hPrivateData, {C(0), C(swr_draw_context_constantVS)});23602361consts_ptr->setName("vs_constants");2362Value *const_sizes_ptr =2363GEP(hPrivateData, {0, swr_draw_context_num_constantsVS});2364const_sizes_ptr->setName("num_vs_constants");23652366Value *vtxInput = LOAD(pVsCtx, {0, SWR_VS_CONTEXT_pVin});2367#if USE_SIMD16_VS2368vtxInput = BITCAST(vtxInput, PointerType::get(Gen_simd16vertex(JM()), 0));2369#endif23702371for (uint32_t attrib = 0; attrib < PIPE_MAX_SHADER_INPUTS; attrib++) {2372const unsigned mask = swr_vs->info.base.input_usage_mask[attrib];2373for (uint32_t channel = 0; channel < TGSI_NUM_CHANNELS; channel++) {2374if (mask & (1 << channel)) {2375inputs[attrib][channel] =2376wrap(LOAD(vtxInput, {0, 0, attrib, channel}));2377}2378}2379}23802381struct lp_build_sampler_soa *sampler =2382swr_sampler_soa_create(key.sampler, PIPE_SHADER_VERTEX);2383assert(sampler != nullptr);23842385struct lp_bld_tgsi_system_values system_values;2386memset(&system_values, 0, sizeof(system_values));2387system_values.instance_id = wrap(LOAD(pVsCtx, {0, SWR_VS_CONTEXT_InstanceID}));23882389#if USE_SIMD16_VS2390system_values.vertex_id = wrap(LOAD(pVsCtx, {0, SWR_VS_CONTEXT_VertexID16}));2391#else2392system_values.vertex_id = wrap(LOAD(pVsCtx, {0, SWR_VS_CONTEXT_VertexID}));2393#endif23942395#if USE_SIMD16_VS2396uint32_t vectorWidth = mVWidth16;2397#else2398uint32_t vectorWidth = mVWidth;2399#endif24002401struct lp_build_tgsi_params params;2402memset(¶ms, 0, sizeof(params));2403params.type = lp_type_float_vec(32, 32 * vectorWidth);2404params.consts_ptr = wrap(consts_ptr);2405params.const_sizes_ptr = wrap(const_sizes_ptr);2406params.system_values = &system_values;2407params.inputs = inputs;2408params.context_ptr = wrap(hPrivateData);2409params.sampler = sampler;2410params.info = &swr_vs->info.base;24112412lp_build_tgsi_soa(gallivm,2413swr_vs->pipe.tokens,2414¶ms,2415outputs);24162417sampler->destroy(sampler);24182419IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));24202421Value *vtxOutput = LOAD(pVsCtx, {0, SWR_VS_CONTEXT_pVout});2422#if USE_SIMD16_VS2423vtxOutput = BITCAST(vtxOutput, PointerType::get(Gen_simd16vertex(JM()), 0));2424#endif24252426for (uint32_t channel = 0; channel < TGSI_NUM_CHANNELS; channel++) {2427for (uint32_t attrib = 0; attrib < PIPE_MAX_SHADER_OUTPUTS; attrib++) {2428if (!outputs[attrib][channel])2429continue;24302431Value *val;2432uint32_t outSlot;24332434if (swr_vs->info.base.output_semantic_name[attrib] == TGSI_SEMANTIC_PSIZE) {2435if (channel != VERTEX_SGV_POINT_SIZE_COMP)2436continue;2437val = LOAD(unwrap(outputs[attrib][0]));2438outSlot = VERTEX_SGV_SLOT;2439} else if (swr_vs->info.base.output_semantic_name[attrib] == TGSI_SEMANTIC_POSITION) {2440val = LOAD(unwrap(outputs[attrib][channel]));2441outSlot = VERTEX_POSITION_SLOT;2442} else {2443val = LOAD(unwrap(outputs[attrib][channel]));2444outSlot = VERTEX_ATTRIB_START_SLOT + attrib;2445if (swr_vs->info.base.output_semantic_name[0] == TGSI_SEMANTIC_POSITION)2446outSlot--;2447}24482449WriteVS(val, pVsCtx, vtxOutput, outSlot, channel);2450}2451}24522453if (ctx->rasterizer->clip_plane_enable ||2454swr_vs->info.base.culldist_writemask) {2455unsigned clip_mask = ctx->rasterizer->clip_plane_enable;24562457unsigned cv = 0;2458if (swr_vs->info.base.writes_clipvertex) {2459cv = locate_linkage(TGSI_SEMANTIC_CLIPVERTEX, 0,2460&swr_vs->info.base);2461} else {2462for (int i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {2463if (swr_vs->info.base.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&2464swr_vs->info.base.output_semantic_index[i] == 0) {2465cv = i;2466break;2467}2468}2469}2470assert(cv < PIPE_MAX_SHADER_OUTPUTS);2471LLVMValueRef cx = LLVMBuildLoad(gallivm->builder, outputs[cv][0], "");2472LLVMValueRef cy = LLVMBuildLoad(gallivm->builder, outputs[cv][1], "");2473LLVMValueRef cz = LLVMBuildLoad(gallivm->builder, outputs[cv][2], "");2474LLVMValueRef cw = LLVMBuildLoad(gallivm->builder, outputs[cv][3], "");24752476tgsi_shader_info *pLastFE = &ctx->vs->info.base;24772478if (ctx->gs) {2479pLastFE = &ctx->gs->info.base;2480}2481else if (ctx->tes) {2482pLastFE = &ctx->tes->info.base;2483}2484else if (ctx->tcs) {2485pLastFE = &ctx->tcs->info.base;2486}24872488for (unsigned val = 0; val < PIPE_MAX_CLIP_PLANES; val++) {2489// clip distance overrides user clip planes2490if ((pLastFE->clipdist_writemask & clip_mask & (1 << val)) ||2491((pLastFE->culldist_writemask << pLastFE->num_written_clipdistance) & (1 << val))) {2492unsigned cv = locate_linkage(TGSI_SEMANTIC_CLIPDIST, val < 4 ? 0 : 1, pLastFE);2493assert(cv < PIPE_MAX_SHADER_OUTPUTS);2494if (val < 4) {2495LLVMValueRef dist = LLVMBuildLoad(gallivm->builder, outputs[cv][val], "");2496WriteVS(unwrap(dist), pVsCtx, vtxOutput, VERTEX_CLIPCULL_DIST_LO_SLOT, val);2497} else {2498LLVMValueRef dist = LLVMBuildLoad(gallivm->builder, outputs[cv][val - 4], "");2499WriteVS(unwrap(dist), pVsCtx, vtxOutput, VERTEX_CLIPCULL_DIST_HI_SLOT, val - 4);2500}2501continue;2502}25032504if (!(clip_mask & (1 << val)))2505continue;25062507Value *px = LOAD(GEP(hPrivateData, {0, swr_draw_context_userClipPlanes, val, 0}));2508Value *py = LOAD(GEP(hPrivateData, {0, swr_draw_context_userClipPlanes, val, 1}));2509Value *pz = LOAD(GEP(hPrivateData, {0, swr_draw_context_userClipPlanes, val, 2}));2510Value *pw = LOAD(GEP(hPrivateData, {0, swr_draw_context_userClipPlanes, val, 3}));2511#if USE_SIMD16_VS2512Value *bpx = VBROADCAST_16(px);2513Value *bpy = VBROADCAST_16(py);2514Value *bpz = VBROADCAST_16(pz);2515Value *bpw = VBROADCAST_16(pw);2516#else2517Value *bpx = VBROADCAST(px);2518Value *bpy = VBROADCAST(py);2519Value *bpz = VBROADCAST(pz);2520Value *bpw = VBROADCAST(pw);2521#endif2522Value *dist = FADD(FMUL(unwrap(cx), bpx),2523FADD(FMUL(unwrap(cy), bpy),2524FADD(FMUL(unwrap(cz), bpz),2525FMUL(unwrap(cw), bpw))));25262527if (val < 4)2528WriteVS(dist, pVsCtx, vtxOutput, VERTEX_CLIPCULL_DIST_LO_SLOT, val);2529else2530WriteVS(dist, pVsCtx, vtxOutput, VERTEX_CLIPCULL_DIST_HI_SLOT, val - 4);2531}2532}25332534RET_VOID();25352536JM()->DumpToFile(pFunction, "vs_function1");2537gallivm_verify_function(gallivm, wrap(pFunction));2538gallivm_compile_module(gallivm);2539JM()->DumpToFile(pFunction, "vs_function2");25402541// lp_debug_dump_value(func);25422543PFN_VERTEX_FUNC pFunc =2544(PFN_VERTEX_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));25452546JM()->DumpAsm(pFunction, "vs_function_asm");2547debug_printf("vert shader %p\n", pFunc);2548assert(pFunc && "Error: VertShader = NULL");25492550JM()->mIsModuleFinalized = true;25512552return pFunc;2553}25542555PFN_VERTEX_FUNC2556swr_compile_vs(struct swr_context *ctx, swr_jit_vs_key &key)2557{2558if (!ctx->vs->pipe.tokens)2559return NULL;25602561BuilderSWR builder(2562reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),2563"VS");2564PFN_VERTEX_FUNC func = builder.CompileVS(ctx, key);25652566ctx->vs->map.insert(std::make_pair(key, std::unique_ptr<VariantVS>(new VariantVS(builder.gallivm, func))));2567return func;2568}25692570unsigned2571swr_so_adjust_attrib(unsigned in_attrib,2572swr_vertex_shader *swr_vs)2573{2574ubyte semantic_name;2575unsigned attrib;25762577attrib = in_attrib + VERTEX_ATTRIB_START_SLOT;25782579if (swr_vs) {2580semantic_name = swr_vs->info.base.output_semantic_name[in_attrib];2581if (semantic_name == TGSI_SEMANTIC_POSITION) {2582attrib = VERTEX_POSITION_SLOT;2583} else if (semantic_name == TGSI_SEMANTIC_PSIZE) {2584attrib = VERTEX_SGV_SLOT;2585} else if (semantic_name == TGSI_SEMANTIC_LAYER) {2586attrib = VERTEX_SGV_SLOT;2587} else {2588if (swr_vs->info.base.writes_position) {2589attrib--;2590}2591}2592}25932594return attrib;2595}25962597static unsigned2598locate_linkage(ubyte name, ubyte index, struct tgsi_shader_info *info)2599{2600for (int i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {2601if ((info->output_semantic_name[i] == name)2602&& (info->output_semantic_index[i] == index)) {2603return i;2604}2605}26062607return 0xFFFFFFFF;2608}26092610PFN_PIXEL_KERNEL2611BuilderSWR::CompileFS(struct swr_context *ctx, swr_jit_fs_key &key)2612{2613struct swr_fragment_shader *swr_fs = ctx->fs;26142615struct tgsi_shader_info *pPrevShader;2616if (ctx->gs)2617pPrevShader = &ctx->gs->info.base;2618else if (ctx->tes)2619pPrevShader = &ctx->tes->info.base;2620else2621pPrevShader = &ctx->vs->info.base;26222623LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];2624LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];26252626memset(inputs, 0, sizeof(inputs));2627memset(outputs, 0, sizeof(outputs));26282629struct lp_build_sampler_soa *sampler = NULL;26302631AttrBuilder attrBuilder;2632attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));26332634std::vector<Type *> fsArgs{PointerType::get(Gen_swr_draw_context(JM()), 0),2635PointerType::get(mInt8Ty, 0),2636PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};2637FunctionType *funcType =2638FunctionType::get(Type::getVoidTy(JM()->mContext), fsArgs, false);26392640auto pFunction = Function::Create(funcType,2641GlobalValue::ExternalLinkage,2642"FS",2643JM()->mpCurrentModule);2644#if LLVM_VERSION_MAJOR < 52645AttributeSet attrSet = AttributeSet::get(2646JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);2647pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);2648#else2649pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);2650#endif26512652BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);2653IRB()->SetInsertPoint(block);2654LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));26552656auto args = pFunction->arg_begin();2657Value *hPrivateData = &*args++;2658hPrivateData->setName("hPrivateData");2659Value *pWorkerData = &*args++;2660pWorkerData->setName("pWorkerData");2661Value *pPS = &*args++;2662pPS->setName("psCtx");26632664Value *consts_ptr = GEP(hPrivateData, {0, swr_draw_context_constantFS});2665consts_ptr->setName("fs_constants");2666Value *const_sizes_ptr =2667GEP(hPrivateData, {0, swr_draw_context_num_constantsFS});2668const_sizes_ptr->setName("num_fs_constants");26692670// load *pAttribs, *pPerspAttribs2671Value *pRawAttribs = LOAD(pPS, {0, SWR_PS_CONTEXT_pAttribs}, "pRawAttribs");2672Value *pPerspAttribs =2673LOAD(pPS, {0, SWR_PS_CONTEXT_pPerspAttribs}, "pPerspAttribs");26742675swr_fs->constantMask = 0;2676swr_fs->flatConstantMask = 0;2677swr_fs->pointSpriteMask = 0;26782679for (int attrib = 0; attrib < PIPE_MAX_SHADER_INPUTS; attrib++) {2680const unsigned mask = swr_fs->info.base.input_usage_mask[attrib];2681const unsigned interpMode = swr_fs->info.base.input_interpolate[attrib];2682const unsigned interpLoc = swr_fs->info.base.input_interpolate_loc[attrib];26832684if (!mask)2685continue;26862687// load i,j2688Value *vi = nullptr, *vj = nullptr;2689switch (interpLoc) {2690case TGSI_INTERPOLATE_LOC_CENTER:2691vi = LOAD(pPS, {0, SWR_PS_CONTEXT_vI, PixelPositions_center}, "i");2692vj = LOAD(pPS, {0, SWR_PS_CONTEXT_vJ, PixelPositions_center}, "j");2693break;2694case TGSI_INTERPOLATE_LOC_CENTROID:2695vi = LOAD(pPS, {0, SWR_PS_CONTEXT_vI, PixelPositions_centroid}, "i");2696vj = LOAD(pPS, {0, SWR_PS_CONTEXT_vJ, PixelPositions_centroid}, "j");2697break;2698case TGSI_INTERPOLATE_LOC_SAMPLE:2699vi = LOAD(pPS, {0, SWR_PS_CONTEXT_vI, PixelPositions_sample}, "i");2700vj = LOAD(pPS, {0, SWR_PS_CONTEXT_vJ, PixelPositions_sample}, "j");2701break;2702}27032704// load/compute w2705Value *vw = nullptr, *pAttribs;2706if (interpMode == TGSI_INTERPOLATE_PERSPECTIVE ||2707interpMode == TGSI_INTERPOLATE_COLOR) {2708pAttribs = pPerspAttribs;2709switch (interpLoc) {2710case TGSI_INTERPOLATE_LOC_CENTER:2711vw = VRCP(LOAD(pPS, {0, SWR_PS_CONTEXT_vOneOverW, PixelPositions_center}));2712break;2713case TGSI_INTERPOLATE_LOC_CENTROID:2714vw = VRCP(LOAD(pPS, {0, SWR_PS_CONTEXT_vOneOverW, PixelPositions_centroid}));2715break;2716case TGSI_INTERPOLATE_LOC_SAMPLE:2717vw = VRCP(LOAD(pPS, {0, SWR_PS_CONTEXT_vOneOverW, PixelPositions_sample}));2718break;2719}2720} else {2721pAttribs = pRawAttribs;2722vw = VIMMED1(1.f);2723}27242725vw->setName("w");27262727ubyte semantic_name = swr_fs->info.base.input_semantic_name[attrib];2728ubyte semantic_idx = swr_fs->info.base.input_semantic_index[attrib];27292730if (semantic_name == TGSI_SEMANTIC_FACE) {2731Value *ff =2732UI_TO_FP(LOAD(pPS, {0, SWR_PS_CONTEXT_frontFace}), mFP32Ty);2733ff = FSUB(FMUL(ff, C(2.0f)), C(1.0f));2734ff = VECTOR_SPLAT(JM()->mVWidth, ff, "vFrontFace");27352736inputs[attrib][0] = wrap(ff);2737inputs[attrib][1] = wrap(VIMMED1(0.0f));2738inputs[attrib][2] = wrap(VIMMED1(0.0f));2739inputs[attrib][3] = wrap(VIMMED1(1.0f));2740continue;2741} else if (semantic_name == TGSI_SEMANTIC_POSITION) { // gl_FragCoord2742if (swr_fs->info.base.properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER] ==2743TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER) {2744inputs[attrib][0] = wrap(LOAD(pPS, {0, SWR_PS_CONTEXT_vX, PixelPositions_center}, "vX"));2745inputs[attrib][1] = wrap(LOAD(pPS, {0, SWR_PS_CONTEXT_vY, PixelPositions_center}, "vY"));2746} else {2747inputs[attrib][0] = wrap(LOAD(pPS, {0, SWR_PS_CONTEXT_vX, PixelPositions_UL}, "vX"));2748inputs[attrib][1] = wrap(LOAD(pPS, {0, SWR_PS_CONTEXT_vY, PixelPositions_UL}, "vY"));2749}2750inputs[attrib][2] = wrap(LOAD(pPS, {0, SWR_PS_CONTEXT_vZ}, "vZ"));2751inputs[attrib][3] =2752wrap(LOAD(pPS, {0, SWR_PS_CONTEXT_vOneOverW, PixelPositions_center}, "vOneOverW"));2753continue;2754} else if (semantic_name == TGSI_SEMANTIC_LAYER) { // gl_Layer2755Value *ff = LOAD(pPS, {0, SWR_PS_CONTEXT_renderTargetArrayIndex});2756ff = VECTOR_SPLAT(JM()->mVWidth, ff, "vRenderTargetArrayIndex");2757inputs[attrib][0] = wrap(ff);2758inputs[attrib][1] = wrap(VIMMED1(0.0f));2759inputs[attrib][2] = wrap(VIMMED1(0.0f));2760inputs[attrib][3] = wrap(VIMMED1(0.0f));2761continue;2762} else if (semantic_name == TGSI_SEMANTIC_VIEWPORT_INDEX) { // gl_ViewportIndex2763Value *ff = LOAD(pPS, {0, SWR_PS_CONTEXT_viewportIndex});2764ff = VECTOR_SPLAT(JM()->mVWidth, ff, "vViewportIndex");2765inputs[attrib][0] = wrap(ff);2766inputs[attrib][1] = wrap(VIMMED1(0.0f));2767inputs[attrib][2] = wrap(VIMMED1(0.0f));2768inputs[attrib][3] = wrap(VIMMED1(0.0f));2769continue;2770}2771unsigned linkedAttrib =2772locate_linkage(semantic_name, semantic_idx, pPrevShader) - 1;27732774uint32_t extraAttribs = 0;2775if (semantic_name == TGSI_SEMANTIC_PRIMID && !ctx->gs) {2776/* non-gs generated primID - need to grab from swizzleMap override */2777linkedAttrib = pPrevShader->num_outputs - 1;2778swr_fs->constantMask |= 1 << linkedAttrib;2779extraAttribs++;2780} else if (semantic_name == TGSI_SEMANTIC_GENERIC &&2781key.sprite_coord_enable & (1 << semantic_idx)) {2782/* we add an extra attrib to the backendState in swr_update_derived. */2783linkedAttrib = pPrevShader->num_outputs + extraAttribs - 1;2784swr_fs->pointSpriteMask |= (1 << linkedAttrib);2785extraAttribs++;2786} else if (linkedAttrib + 1 == 0xFFFFFFFF) {2787inputs[attrib][0] = wrap(VIMMED1(0.0f));2788inputs[attrib][1] = wrap(VIMMED1(0.0f));2789inputs[attrib][2] = wrap(VIMMED1(0.0f));2790inputs[attrib][3] = wrap(VIMMED1(1.0f));2791/* If we're reading in color and 2-sided lighting is enabled, we have2792* to keep going.2793*/2794if (semantic_name != TGSI_SEMANTIC_COLOR || !key.light_twoside)2795continue;2796} else {2797if (interpMode == TGSI_INTERPOLATE_CONSTANT) {2798swr_fs->constantMask |= 1 << linkedAttrib;2799} else if (interpMode == TGSI_INTERPOLATE_COLOR) {2800swr_fs->flatConstantMask |= 1 << linkedAttrib;2801}2802}28032804unsigned bcolorAttrib = 0xFFFFFFFF;2805Value *offset = NULL;2806if (semantic_name == TGSI_SEMANTIC_COLOR && key.light_twoside) {2807bcolorAttrib = locate_linkage(2808TGSI_SEMANTIC_BCOLOR, semantic_idx, pPrevShader);2809/* Neither front nor back colors were available. Nothing to load. */2810if (bcolorAttrib == 0xFFFFFFFF && linkedAttrib == 0xFFFFFFFF)2811continue;2812/* If there is no front color, just always use the back color. */2813if (linkedAttrib + 1 == 0xFFFFFFFF)2814linkedAttrib = bcolorAttrib;28152816if (bcolorAttrib != 0xFFFFFFFF) {2817bcolorAttrib -= 1;2818if (interpMode == TGSI_INTERPOLATE_CONSTANT) {2819swr_fs->constantMask |= 1 << bcolorAttrib;2820} else if (interpMode == TGSI_INTERPOLATE_COLOR) {2821swr_fs->flatConstantMask |= 1 << bcolorAttrib;2822}28232824unsigned diff = 12 * (bcolorAttrib - linkedAttrib);28252826if (diff) {2827Value *back =2828XOR(C(1), LOAD(pPS, {0, SWR_PS_CONTEXT_frontFace}), "backFace");28292830offset = MUL(back, C(diff));2831offset->setName("offset");2832}2833}2834}28352836for (int channel = 0; channel < TGSI_NUM_CHANNELS; channel++) {2837if (mask & (1 << channel)) {2838Value *indexA = C(linkedAttrib * 12 + channel);2839Value *indexB = C(linkedAttrib * 12 + channel + 4);2840Value *indexC = C(linkedAttrib * 12 + channel + 8);28412842if (offset) {2843indexA = ADD(indexA, offset);2844indexB = ADD(indexB, offset);2845indexC = ADD(indexC, offset);2846}28472848Value *va = VBROADCAST(LOAD(GEP(pAttribs, indexA)));2849Value *vb = VBROADCAST(LOAD(GEP(pAttribs, indexB)));2850Value *vc = VBROADCAST(LOAD(GEP(pAttribs, indexC)));28512852if (interpMode == TGSI_INTERPOLATE_CONSTANT) {2853inputs[attrib][channel] = wrap(va);2854} else {2855Value *vk = FSUB(FSUB(VIMMED1(1.0f), vi), vj);28562857vc = FMUL(vk, vc);28582859Value *interp = FMUL(va, vi);2860Value *interp1 = FMUL(vb, vj);2861interp = FADD(interp, interp1);2862interp = FADD(interp, vc);2863if (interpMode == TGSI_INTERPOLATE_PERSPECTIVE ||2864interpMode == TGSI_INTERPOLATE_COLOR)2865interp = FMUL(interp, vw);2866inputs[attrib][channel] = wrap(interp);2867}2868}2869}2870}28712872sampler = swr_sampler_soa_create(key.sampler, PIPE_SHADER_FRAGMENT);2873assert(sampler != nullptr);28742875struct lp_bld_tgsi_system_values system_values;2876memset(&system_values, 0, sizeof(system_values));28772878struct lp_build_mask_context mask;2879bool uses_mask = false;28802881if (swr_fs->info.base.uses_kill ||2882key.poly_stipple_enable) {2883Value *vActiveMask = NULL;2884if (swr_fs->info.base.uses_kill) {2885vActiveMask = LOAD(pPS, {0, SWR_PS_CONTEXT_activeMask}, "activeMask");2886}2887if (key.poly_stipple_enable) {2888// first get fragment xy coords and clip to stipple bounds2889Value *vXf = LOAD(pPS, {0, SWR_PS_CONTEXT_vX, PixelPositions_UL});2890Value *vYf = LOAD(pPS, {0, SWR_PS_CONTEXT_vY, PixelPositions_UL});2891Value *vXu = FP_TO_UI(vXf, mSimdInt32Ty);2892Value *vYu = FP_TO_UI(vYf, mSimdInt32Ty);28932894// stipple pattern is 32x32, which means that one line of stipple2895// is stored in one word:2896// vXstipple is bit offset inside 32-bit stipple word2897// vYstipple is word index is stipple array2898Value *vXstipple = AND(vXu, VIMMED1(0x1f)); // & (32-1)2899Value *vYstipple = AND(vYu, VIMMED1(0x1f)); // & (32-1)29002901// grab stipple pattern base address2902Value *stipplePtr = GEP(hPrivateData, {0, swr_draw_context_polyStipple, 0});2903stipplePtr = BITCAST(stipplePtr, mInt8PtrTy);29042905// peform a gather to grab stipple words for each lane2906Value *vStipple = GATHERDD(VUNDEF_I(), stipplePtr, vYstipple,2907VIMMED1(0xffffffff), 4);29082909// create a mask with one bit corresponding to the x stipple2910// and AND it with the pattern, to see if we have a bit2911Value *vBitMask = LSHR(VIMMED1(0x80000000), vXstipple);2912Value *vStippleMask = AND(vStipple, vBitMask);2913vStippleMask = ICMP_NE(vStippleMask, VIMMED1(0));2914vStippleMask = VMASK(vStippleMask);29152916if (swr_fs->info.base.uses_kill) {2917vActiveMask = AND(vActiveMask, vStippleMask);2918} else {2919vActiveMask = vStippleMask;2920}2921}2922lp_build_mask_begin(2923&mask, gallivm, lp_type_float_vec(32, 32 * 8), wrap(vActiveMask));2924uses_mask = true;2925}29262927struct lp_build_tgsi_params params;2928memset(¶ms, 0, sizeof(params));2929params.type = lp_type_float_vec(32, 32 * 8);2930params.mask = uses_mask ? &mask : NULL;2931params.consts_ptr = wrap(consts_ptr);2932params.const_sizes_ptr = wrap(const_sizes_ptr);2933params.system_values = &system_values;2934params.inputs = inputs;2935params.context_ptr = wrap(hPrivateData);2936params.sampler = sampler;2937params.info = &swr_fs->info.base;29382939lp_build_tgsi_soa(gallivm,2940swr_fs->pipe.tokens,2941¶ms,2942outputs);29432944sampler->destroy(sampler);29452946IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));29472948for (uint32_t attrib = 0; attrib < swr_fs->info.base.num_outputs;2949attrib++) {2950switch (swr_fs->info.base.output_semantic_name[attrib]) {2951case TGSI_SEMANTIC_POSITION: {2952// write z2953LLVMValueRef outZ =2954LLVMBuildLoad(gallivm->builder, outputs[attrib][2], "");2955STORE(unwrap(outZ), pPS, {0, SWR_PS_CONTEXT_vZ});2956break;2957}2958case TGSI_SEMANTIC_COLOR: {2959for (uint32_t channel = 0; channel < TGSI_NUM_CHANNELS; channel++) {2960if (!outputs[attrib][channel])2961continue;29622963LLVMValueRef out =2964LLVMBuildLoad(gallivm->builder, outputs[attrib][channel], "");2965if (swr_fs->info.base.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&2966swr_fs->info.base.output_semantic_index[attrib] == 0) {2967for (uint32_t rt = 0; rt < key.nr_cbufs; rt++) {2968STORE(unwrap(out),2969pPS,2970{0, SWR_PS_CONTEXT_shaded, rt, channel});2971}2972} else {2973STORE(unwrap(out),2974pPS,2975{0,2976SWR_PS_CONTEXT_shaded,2977swr_fs->info.base.output_semantic_index[attrib],2978channel});2979}2980}2981break;2982}2983default: {2984fprintf(stderr,2985"unknown output from FS %s[%d]\n",2986tgsi_semantic_names[swr_fs->info.base2987.output_semantic_name[attrib]],2988swr_fs->info.base.output_semantic_index[attrib]);2989break;2990}2991}2992}29932994LLVMValueRef mask_result = 0;2995if (uses_mask) {2996mask_result = lp_build_mask_end(&mask);2997}29982999IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));30003001if (uses_mask) {3002STORE(unwrap(mask_result), pPS, {0, SWR_PS_CONTEXT_activeMask});3003}30043005RET_VOID();30063007gallivm_verify_function(gallivm, wrap(pFunction));30083009gallivm_compile_module(gallivm);30103011// after the gallivm passes, we have to lower the core's intrinsics3012llvm::legacy::FunctionPassManager lowerPass(JM()->mpCurrentModule);3013lowerPass.add(createLowerX86Pass(this));3014lowerPass.run(*pFunction);30153016PFN_PIXEL_KERNEL kernel =3017(PFN_PIXEL_KERNEL)gallivm_jit_function(gallivm, wrap(pFunction));3018debug_printf("frag shader %p\n", kernel);3019assert(kernel && "Error: FragShader = NULL");30203021JM()->mIsModuleFinalized = true;30223023return kernel;3024}30253026PFN_PIXEL_KERNEL3027swr_compile_fs(struct swr_context *ctx, swr_jit_fs_key &key)3028{3029if (!ctx->fs->pipe.tokens)3030return NULL;30313032BuilderSWR builder(3033reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),3034"FS");3035PFN_PIXEL_KERNEL func = builder.CompileFS(ctx, key);30363037ctx->fs->map.insert(std::make_pair(key, std::unique_ptr<VariantFS>(new VariantFS(builder.gallivm, func))));3038return func;3039}304030413042