+static void
+swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface *gs_base,
+ struct lp_build_context * bld,
+ LLVMValueRef (*outputs)[4],
+ LLVMValueRef emitted_vertices_vec,
+ LLVMValueRef mask_vec,
+ LLVMValueRef stream_id)
+{
+ swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
+
+ iface->pBuilder->swr_gs_llvm_emit_vertex(gs_base, bld,
+ outputs,
+ emitted_vertices_vec,
+ stream_id);
+}
+
+static void
+swr_gs_llvm_end_primitive(const struct lp_build_gs_iface *gs_base,
+ struct lp_build_context * bld,
+ LLVMValueRef total_emitted_vertices_vec_ptr,
+ LLVMValueRef verts_per_prim_vec,
+ LLVMValueRef emitted_prims_vec,
+ LLVMValueRef mask_vec, unsigned stream_id)
+{
+ swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
+
+ iface->pBuilder->swr_gs_llvm_end_primitive(gs_base, bld,
+ total_emitted_vertices_vec_ptr,
+ verts_per_prim_vec,
+ emitted_prims_vec,
+ mask_vec);
+}
+
+static void
+swr_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,
+ LLVMValueRef total_emitted_vertices_vec,
+ LLVMValueRef emitted_prims_vec, unsigned stream)
+{
+ swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
+
+ iface->pBuilder->swr_gs_llvm_epilogue(gs_base,
+ total_emitted_vertices_vec,
+ emitted_prims_vec, stream);
+}
+
+static LLVMValueRef
+swr_tcs_llvm_fetch_input(const struct lp_build_tcs_iface *tcs_iface,
+ struct lp_build_context * bld,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;
+ struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;
+
+ return iface->pBuilder->swr_tcs_llvm_fetch_input(tcs_iface, bld_base,
+ is_vindex_indirect,
+ vertex_index,
+ is_aindex_indirect,
+ attrib_index,
+ swizzle_index);
+}
+
+static LLVMValueRef
+swr_tcs_llvm_fetch_output(const struct lp_build_tcs_iface *tcs_iface,
+ struct lp_build_context * bld,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index,
+ uint32_t name)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;
+ struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;
+
+ return iface->pBuilder->swr_tcs_llvm_fetch_output(tcs_iface, bld_base,
+ is_vindex_indirect,
+ vertex_index,
+ is_aindex_indirect,
+ attrib_index,
+ swizzle_index,
+ name);
+}
+
+
+static void
+swr_tcs_llvm_emit_prologue(struct lp_build_context* bld)
+{
+ lp_build_tgsi_soa_context* bld_base = (lp_build_tgsi_soa_context*)bld;
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld_base->tcs_iface;
+ iface->pBuilder->swr_tcs_llvm_emit_prologue(bld_base);
+}
+
+static void
+swr_tcs_llvm_emit_epilogue(struct lp_build_context* bld)
+{
+ lp_build_tgsi_soa_context* bld_base = (lp_build_tgsi_soa_context*)bld;
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld_base->tcs_iface;
+ iface->pBuilder->swr_tcs_llvm_emit_epilogue(bld_base);
+}
+
+static
+void swr_tcs_llvm_store_output(const struct lp_build_tcs_iface *tcs_iface,
+ struct lp_build_context * bld,
+ unsigned name,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ boolean is_sindex_indirect,
+ LLVMValueRef swizzle_index,
+ LLVMValueRef value,
+ LLVMValueRef mask_vec)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;
+ struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;
+
+ iface->pBuilder->swr_tcs_llvm_store_output(tcs_iface,
+ bld_base,
+ name,
+ is_vindex_indirect,
+ vertex_index,
+ is_aindex_indirect,
+ attrib_index,
+ swizzle_index,
+ value,
+ mask_vec);
+}
+
+
+static
+void swr_tcs_llvm_emit_barrier(struct lp_build_context *bld)
+{
+ lp_build_tgsi_soa_context* bld_base = (lp_build_tgsi_soa_context*)bld;
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld_base->tcs_iface;
+
+ iface->pBuilder->swr_tcs_llvm_emit_barrier(bld_base->tcs_iface, &bld_base->bld_base);
+}
+
+
+static LLVMValueRef
+swr_tes_llvm_fetch_vtx_input(const struct lp_build_tes_iface *tes_iface,
+ struct lp_build_context * bld,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index)
+{
+ swr_tes_llvm_iface *iface = (swr_tes_llvm_iface*)tes_iface;
+ struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;
+
+ return iface->pBuilder->swr_tes_llvm_fetch_vtx_input(tes_iface, bld_base,
+ is_vindex_indirect,
+ vertex_index,
+ is_aindex_indirect,
+ attrib_index,
+ swizzle_index);
+}
+
+static LLVMValueRef
+swr_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface *tes_iface,
+ struct lp_build_context * bld,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index)
+{
+ swr_tes_llvm_iface *iface = (swr_tes_llvm_iface*)tes_iface;
+ struct lp_build_tgsi_context *bld_base = (struct lp_build_tgsi_context*)bld;
+
+ return iface->pBuilder->swr_tes_llvm_fetch_patch_input(tes_iface, bld_base,
+ is_aindex_indirect,
+ attrib_index,
+ swizzle_index);
+}
+
+LLVMValueRef
+BuilderSWR::swr_gs_llvm_fetch_input(const struct lp_build_gs_iface *gs_iface,
+ struct lp_build_context * bld,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index)
+{
+ swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_iface;
+ Value *vert_index = unwrap(vertex_index);
+ Value *attr_index = unwrap(attrib_index);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ if (is_vindex_indirect || is_aindex_indirect) {
+ int i;
+ Value *res = unwrap(bld->zero);
+ struct lp_type type = bld->type;
+
+ for (i = 0; i < type.length; i++) {
+ Value *vert_chan_index = vert_index;
+ Value *attr_chan_index = attr_index;
+
+ if (is_vindex_indirect) {
+ vert_chan_index = VEXTRACT(vert_index, C(i));
+ }
+ if (is_aindex_indirect) {
+ attr_chan_index = VEXTRACT(attr_index, C(i));
+ }
+
+ Value *attrib =
+ LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_chan_index}));
+
+ Value *pVertex = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pVerts});
+ Value *pInputVertStride = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_inputVertStride});
+
+ Value *pVector = ADD(MUL(vert_chan_index, pInputVertStride), attrib);
+ Value *pInput = LOAD(GEP(pVertex, {pVector, unwrap(swizzle_index)}));
+
+ Value *value = VEXTRACT(pInput, C(i));
+ res = VINSERT(res, value, C(i));
+ }
+
+ return wrap(res);
+ } else {
+ Value *attrib = LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_index}));
+
+ Value *pVertex = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pVerts});
+ Value *pInputVertStride = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_inputVertStride});
+
+ Value *pVector = ADD(MUL(vert_index, pInputVertStride), attrib);
+
+ Value *pInput = LOAD(GEP(pVertex, {pVector, unwrap(swizzle_index)}));
+
+ return wrap(pInput);
+ }
+}
+
+// GS output stream layout
+#define VERTEX_COUNT_SIZE 32
+#define CONTROL_HEADER_SIZE (8*32)
+
+void
+BuilderSWR::swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface *gs_base,
+ struct lp_build_context * bld,
+ LLVMValueRef (*outputs)[4],
+ LLVMValueRef emitted_vertices_vec,
+ LLVMValueRef stream_id)
+{
+ swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+ const uint32_t headerSize = VERTEX_COUNT_SIZE + CONTROL_HEADER_SIZE;
+ const uint32_t attribSize = 4 * sizeof(float);
+ const uint32_t vertSize = attribSize * SWR_VTX_NUM_SLOTS;
+ Value *pVertexOffset = MUL(unwrap(emitted_vertices_vec), VIMMED1(vertSize));
+
+ Value *vMask = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_mask});
+ Value *vMask1 = TRUNC(vMask, getVectorType(mInt1Ty, mVWidth));
+
+ Value *pStack = STACKSAVE();
+ Value *pTmpPtr = ALLOCA(mFP32Ty, C(4)); // used for dummy write for lane masking
+
+ for (uint32_t attrib = 0; attrib < iface->num_outputs; ++attrib) {
+ uint32_t attribSlot = attrib;
+ uint32_t sgvChannel = 0;
+ if (iface->info->output_semantic_name[attrib] == TGSI_SEMANTIC_PSIZE) {
+ attribSlot = VERTEX_SGV_SLOT;
+ sgvChannel = VERTEX_SGV_POINT_SIZE_COMP;
+ } else if (iface->info->output_semantic_name[attrib] == TGSI_SEMANTIC_LAYER) {
+ attribSlot = VERTEX_SGV_SLOT;
+ sgvChannel = VERTEX_SGV_RTAI_COMP;
+ } else if (iface->info->output_semantic_name[attrib] == TGSI_SEMANTIC_VIEWPORT_INDEX) {
+ attribSlot = VERTEX_SGV_SLOT;
+ sgvChannel = VERTEX_SGV_VAI_COMP;
+ } else if (iface->info->output_semantic_name[attrib] == TGSI_SEMANTIC_POSITION) {
+ attribSlot = VERTEX_POSITION_SLOT;
+ } else {
+ attribSlot = VERTEX_ATTRIB_START_SLOT + attrib;
+ if (iface->info->writes_position) {
+ attribSlot--;
+ }
+ }
+
+ Value *pOutputOffset = ADD(pVertexOffset, VIMMED1(headerSize + attribSize * attribSlot)); // + sgvChannel ?
+
+ for (uint32_t lane = 0; lane < mVWidth; ++lane) {
+ Value *pLaneOffset = VEXTRACT(pOutputOffset, C(lane));
+ Value *pStream = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});
+ Value *pStreamOffset = GEP(pStream, pLaneOffset);
+ pStreamOffset = BITCAST(pStreamOffset, mFP32PtrTy);
+
+ Value *pLaneMask = VEXTRACT(vMask1, C(lane));
+ pStreamOffset = SELECT(pLaneMask, pStreamOffset, pTmpPtr);
+
+ for (uint32_t channel = 0; channel < 4; ++channel) {
+ Value *vData;
+
+ if (attribSlot == VERTEX_SGV_SLOT)
+ vData = LOAD(unwrap(outputs[attrib][0]));
+ else
+ vData = LOAD(unwrap(outputs[attrib][channel]));
+
+ if (attribSlot != VERTEX_SGV_SLOT ||
+ sgvChannel == channel) {
+ vData = VEXTRACT(vData, C(lane));
+ STORE(vData, pStreamOffset);
+ }
+ pStreamOffset = GEP(pStreamOffset, C(1));
+ }
+ }
+ }
+
+ /* When the output type is not points, the geometry shader may not
+ * output data to multiple streams. So early exit here.
+ */
+ if(iface->pGsState->outputTopology != TOP_POINT_LIST) {
+ STACKRESTORE(pStack);
+ return;
+ }
+
+ // Info about stream id for each vertex
+ // is coded in 2 bits (4 vert per byte "box"):
+ // ----------------- ----------------- ----
+ // |d|d|c|c|b|b|a|a| |h|h|g|g|f|f|e|e| |...
+ // ----------------- ----------------- ----
+
+ // Calculate where need to put stream id for current vert
+ // in 1 byte "box".
+ Value *pShiftControl = MUL(unwrap(emitted_vertices_vec), VIMMED1(2));
+
+ // Calculate in which box put stream id for current vert.
+ Value *pOffsetControl = LSHR(unwrap(emitted_vertices_vec), VIMMED1(2));
+
+ // Skip count header
+ Value *pStreamIdOffset = ADD(pOffsetControl, VIMMED1(VERTEX_COUNT_SIZE));
+
+ for (uint32_t lane = 0; lane < mVWidth; ++lane) {
+ Value *pShift = TRUNC(VEXTRACT(pShiftControl, C(lane)), mInt8Ty);
+ Value *pStream = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});
+
+ Value *pStreamOffset = GEP(pStream, VEXTRACT(pStreamIdOffset, C(lane)));
+
+ // Just make sure that not overflow max - stream id = (0,1,2,3)
+ Value *vVal = TRUNC(AND(VEXTRACT(unwrap(stream_id), C(0)), C(0x3)), mInt8Ty);
+
+ // Shift it to correct position in byte "box"
+ vVal = SHL(vVal, pShift);
+
+ // Info about other vertices can be already stored
+ // so we need to read and add bits from current vert info.
+ Value *storedValue = LOAD(pStreamOffset);
+ vVal = OR(storedValue, vVal);
+ STORE(vVal, pStreamOffset);
+ }
+
+ STACKRESTORE(pStack);
+}
+
+void
+BuilderSWR::swr_gs_llvm_end_primitive(const struct lp_build_gs_iface *gs_base,
+ struct lp_build_context * bld,
+ LLVMValueRef total_emitted_vertices_vec,
+ LLVMValueRef verts_per_prim_vec,
+ LLVMValueRef emitted_prims_vec,
+ LLVMValueRef mask_vec)
+{
+ swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
+
+ /* When the output type is points, the geometry shader may output data
+ * to multiple streams, and end_primitive has no effect. Info about
+ * stream id for vertices is stored into the same place in memory where
+ * end primitive info is stored so early exit in this case.
+ */
+ if (iface->pGsState->outputTopology == TOP_POINT_LIST) {
+ return;
+ }
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ Value *vMask = LOAD(iface->pGsCtx, { 0, SWR_GS_CONTEXT_mask });
+ Value *vMask1 = TRUNC(vMask, getVectorType(mInt1Ty, 8));
+
+ uint32_t vertsPerPrim = iface->num_verts_per_prim;
+
+ Value *vCount =
+ ADD(MUL(unwrap(emitted_prims_vec), VIMMED1(vertsPerPrim)),
+ unwrap(verts_per_prim_vec));
+
+ vCount = unwrap(total_emitted_vertices_vec);
+
+ Value *mask = unwrap(mask_vec);
+ Value *cmpMask = VMASK(ICMP_NE(unwrap(verts_per_prim_vec), VIMMED1(0)));
+ mask = AND(mask, cmpMask);
+ vMask1 = TRUNC(mask, getVectorType(mInt1Ty, 8));
+
+ vCount = SUB(vCount, VIMMED1(1));
+ Value *vOffset = ADD(UDIV(vCount, VIMMED1(8)), VIMMED1(VERTEX_COUNT_SIZE));
+ Value *vValue = SHL(VIMMED1(1), UREM(vCount, VIMMED1(8)));
+
+ vValue = TRUNC(vValue, getVectorType(mInt8Ty, 8));
+
+ Value *pStack = STACKSAVE();
+ Value *pTmpPtr = ALLOCA(mInt8Ty, C(4)); // used for dummy read/write for lane masking
+
+ for (uint32_t lane = 0; lane < mVWidth; ++lane) {
+ Value *vLaneOffset = VEXTRACT(vOffset, C(lane));
+ Value *pStream = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});
+ Value *pStreamOffset = GEP(pStream, vLaneOffset);
+
+ Value *pLaneMask = VEXTRACT(vMask1, C(lane));
+ pStreamOffset = SELECT(pLaneMask, pStreamOffset, pTmpPtr);
+
+ Value *vVal = LOAD(pStreamOffset);
+ vVal = OR(vVal, VEXTRACT(vValue, C(lane)));
+ STORE(vVal, pStreamOffset);
+ }
+
+ STACKRESTORE(pStack);
+}
+
+void
+BuilderSWR::swr_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,
+ LLVMValueRef total_emitted_vertices_vec,
+ LLVMValueRef emitted_prims_vec, unsigned stream)
+{
+ swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ // Store emit count to each output stream in the first DWORD
+ for (uint32_t lane = 0; lane < mVWidth; ++lane)
+ {
+ Value* pStream = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});
+ pStream = BITCAST(pStream, mInt32PtrTy);
+ Value* pLaneCount = VEXTRACT(unwrap(total_emitted_vertices_vec), C(lane));
+ STORE(pLaneCount, pStream);
+ }
+}
+
+void
+BuilderSWR::swr_tcs_llvm_emit_prologue(struct lp_build_tgsi_soa_context* bld)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld->tcs_iface;
+
+ Value* loop_var = ALLOCA(mSimdInt32Ty);
+ STORE(VBROADCAST(C(0)), loop_var);
+
+ iface->loop_var = wrap(loop_var);
+
+ lp_exec_bgnloop(&bld->exec_mask, true);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+ bld->system_values.invocation_id = wrap((LOAD(unwrap(iface->loop_var))));
+
+ if (verbose_tcs_shader_loop) {
+ lp_build_print_value(gallivm, "Prologue LOOP Iteration BEGIN:", bld->system_values.invocation_id);
+ }
+
+}
+
+void
+BuilderSWR::swr_tcs_llvm_emit_epilogue(struct lp_build_tgsi_soa_context* bld)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld->tcs_iface;
+
+ struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;
+
+ STORE(ADD(LOAD(unwrap(iface->loop_var)), VBROADCAST(C(1))), unwrap(iface->loop_var));
+ if (verbose_tcs_shader_loop) {
+ lp_build_print_value(gallivm, "Epilogue LOOP: ", wrap(LOAD(unwrap(iface->loop_var))));
+ }
+
+ LLVMValueRef tmp = lp_build_cmp(uint_bld, PIPE_FUNC_GEQUAL, wrap(LOAD(unwrap(iface->loop_var))),
+ wrap(VBROADCAST(C(iface->output_vertices))));
+ lp_exec_mask_cond_push(&bld->exec_mask, tmp);
+ lp_exec_break(&bld->exec_mask, &bld->bld_base.pc, false);
+ lp_exec_mask_cond_pop(&bld->exec_mask);
+ lp_exec_endloop(bld->bld_base.base.gallivm, &bld->exec_mask);
+}
+
+LLVMValueRef
+BuilderSWR::swr_tcs_llvm_fetch_input(const struct lp_build_tcs_iface *tcs_iface,
+ struct lp_build_tgsi_context * bld_base,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;
+
+ Value *vert_index = unwrap(vertex_index);
+ Value *attr_index = unwrap(attrib_index);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ if (verbose_tcs_shader_in) {
+ lp_build_printf(gallivm, "[TCS IN][VTX] ======================================\n");
+ lp_build_print_value(gallivm, "[TCS IN][VTX] vertex_index: ", vertex_index);
+ lp_build_print_value(gallivm, "[TCS IN][VTX] attrib_index: ", attrib_index);
+ lp_build_printf(gallivm, "[TCS IN][VTX] --------------------------------------\n");
+ }
+
+ Value *res = unwrap(bld_base->base.zero);
+ if (is_vindex_indirect || is_aindex_indirect) {
+ int i;
+ struct lp_type type = bld_base->base.type;
+
+ for (i = 0; i < type.length; i++) {
+ Value *vert_chan_index = vert_index;
+ Value *attr_chan_index = attr_index;
+
+ if (is_vindex_indirect) {
+ vert_chan_index = VEXTRACT(vert_index, C(i));
+ }
+ if (is_aindex_indirect) {
+ attr_chan_index = VEXTRACT(attr_index, C(i));
+ }
+
+ Value *attrib =
+ LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_chan_index}));
+
+ Value *pBase = GEP(iface->pTcsCtx,
+ { C(0), C(SWR_HS_CONTEXT_vert), vert_chan_index,
+ C(simdvertex_attrib), attrib, unwrap(swizzle_index), C(i) });
+
+ Value *val = LOAD(pBase);
+
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS IN][VTX] vert_chan_index: ", wrap(vert_chan_index));
+ lp_build_print_value(gallivm, "[TCS IN][VTX] attrib_index: ", attrib_index);
+ lp_build_print_value(gallivm, "[TCS IN][VTX] attr_chan_index: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TCS IN][VTX] attrib read from map: ", wrap(attrib));
+ lp_build_print_value(gallivm, "[TCS IN][VTX] swizzle_index: ", swizzle_index);
+ lp_build_print_value(gallivm, "[TCS IN][VTX] Loaded: ", wrap(val));
+ }
+ res = VINSERT(res, val, C(i));
+ }
+ } else {
+ Value *attrib = LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_index}));
+
+ Value *pBase = GEP(iface->pTcsCtx,
+ { C(0), C(SWR_HS_CONTEXT_vert), vert_index,
+ C(simdvertex_attrib), attrib, unwrap(swizzle_index) });
+
+ res = LOAD(pBase);
+
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS IN][VTX] attrib_index: ", attrib_index);
+ lp_build_print_value(gallivm, "[TCS IN][VTX] attr_chan_index: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TCS IN][VTX] attrib read from map: ", wrap(attrib));
+ lp_build_print_value(gallivm, "[TCS IN][VTX] swizzle_index: ", swizzle_index);
+ lp_build_print_value(gallivm, "[TCS IN][VTX] Loaded: ", wrap(res));
+ }
+ }
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS IN][VTX] returning: ", wrap(res));
+ }
+ return wrap(res);
+}
+
+LLVMValueRef
+BuilderSWR::swr_tcs_llvm_fetch_output(const struct lp_build_tcs_iface *tcs_iface,
+ struct lp_build_tgsi_context * bld_base,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index,
+ uint32_t name)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;
+
+ Value *vert_index = unwrap(vertex_index);
+ Value *attr_index = unwrap(attrib_index);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS INOUT] Vertex index: ", vertex_index);
+ lp_build_print_value(gallivm, "[TCS INOUT] Attrib index: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TCS INOUT] Swizzle index: ", swizzle_index);
+ }
+
+ Value* res = unwrap(bld_base->base.zero);
+
+ for (uint32_t lane = 0; lane < mVWidth; lane++) {
+ Value* p1 = LOAD(iface->pTcsCtx, {0, SWR_HS_CONTEXT_pCPout});
+ Value* pCpOut = GEP(p1, {lane});
+
+ Value *vert_chan_index = vert_index;
+ Value *attr_chan_index = attr_index;
+
+ if (is_vindex_indirect) {
+ vert_chan_index = VEXTRACT(vert_index, C(lane));
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS INOUT] Extracted vertex index: ", wrap(vert_chan_index));
+ }
+ }
+
+ if (is_aindex_indirect) {
+ attr_chan_index = VEXTRACT(attr_index, C(lane));
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS INOUT] Extracted attrib index: ", wrap(attr_chan_index));
+ }
+ }
+
+ if (name == TGSI_SEMANTIC_TESSOUTER || name == TGSI_SEMANTIC_TESSINNER) {
+ Value* tessFactors = GEP(pCpOut, {(uint32_t)0, ScalarPatch_tessFactors});
+ Value* tessFactorArray = nullptr;
+ if (name == TGSI_SEMANTIC_TESSOUTER) {
+ tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_OuterTessFactors});
+ } else {
+ tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_InnerTessFactors});
+ }
+ Value* tessFactor = GEP(tessFactorArray, {C(0), unwrap(swizzle_index)});
+ res = VINSERT(res, LOAD(tessFactor), C(lane));
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS INOUT][FACTOR] lane (patch-id): ", wrap(C(lane)));
+ lp_build_print_value(gallivm, "[TCS INOUT][FACTOR] loaded value: ", wrap(res));
+ }
+ } else if (name == TGSI_SEMANTIC_PATCH) {
+ Value* attr_index_from_map = LOAD(GEP(iface->pPatchOutputAttribMap, {C(0), attr_chan_index}));
+ Value* attr_value = GEP(pCpOut, {C(0), C(ScalarPatch_patchData), C(ScalarCPoint_attrib), attr_index_from_map, unwrap(swizzle_index)});
+ res = VINSERT(res, LOAD(attr_value), C(lane));
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS INOUT][PATCH] attr index loaded from map: ", wrap(attr_index_from_map));
+ lp_build_print_value(gallivm, "[TCS INOUT][PATCH] lane (patch-id): ", wrap(C(lane)));
+ lp_build_print_value(gallivm, "[TCS INOUT][PATCH] loaded value: ", wrap(res));
+ }
+ } else {
+ // Generic attribute
+ Value *attrib =
+ LOAD(GEP(iface->pVtxOutputAttribMap, {C(0), attr_chan_index}));
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS INOUT][VTX] Attrib index from map: ", wrap(attrib));
+ }
+ Value* attr_chan = GEP(pCpOut, {C(0), C(ScalarPatch_cp), vert_chan_index,
+ C(ScalarCPoint_attrib), attrib, unwrap(swizzle_index)});
+
+ res = VINSERT(res, LOAD(attr_chan), C(lane));
+ if (verbose_tcs_shader_in) {
+ lp_build_print_value(gallivm, "[TCS INOUT][VTX] loaded value: ", wrap(res));
+ }
+ }
+ }
+
+ return wrap(res);
+}
+
+void
+BuilderSWR::swr_tcs_llvm_store_output(const struct lp_build_tcs_iface *tcs_iface,
+ struct lp_build_tgsi_context *bld_base,
+ unsigned name,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index,
+ LLVMValueRef value,
+ LLVMValueRef mask_vec)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;
+ struct lp_build_tgsi_soa_context* bld = (struct lp_build_tgsi_soa_context*)bld_base;
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ if (verbose_tcs_shader_out) {
+ lp_build_printf(gallivm, "[TCS OUT] =============================================\n");
+ }
+
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT] Store mask: ", bld->exec_mask.exec_mask);
+ lp_build_print_value(gallivm, "[TCS OUT] Store value: ", value);
+ }
+
+ Value *vert_index = unwrap(vertex_index);
+ Value *attr_index = unwrap(attrib_index);
+
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT] Vertex index: ", vertex_index);
+ lp_build_print_value(gallivm, "[TCS OUT] Attrib index: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TCS OUT] Swizzle index: ", swizzle_index);
+ }
+
+ if (is_vindex_indirect) {
+ vert_index = VEXTRACT(vert_index, C(0));
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT] Extracted vertex index: ", vertex_index);
+ }
+ }
+
+ if (is_aindex_indirect) {
+ attr_index = VEXTRACT(attr_index, C(0));
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT] Extracted attrib index: ", wrap(attr_index));
+ }
+ }
+
+ if (verbose_tcs_shader_out) {
+ if (bld->exec_mask.has_mask) {
+ lp_build_print_value(gallivm, "[TCS OUT] Exec mask: ", bld->exec_mask.exec_mask);
+ }
+ else {
+ lp_build_printf(gallivm, "[TCS OUT] has no mask\n");
+ }
+ }
+ for (uint32_t lane = 0; lane < mVWidth; lane++) {
+ Value* p1 = LOAD(iface->pTcsCtx, {0, SWR_HS_CONTEXT_pCPout});
+ Value* pCpOut = GEP(p1, {lane});
+
+ if (name == TGSI_SEMANTIC_TESSOUTER || name == TGSI_SEMANTIC_TESSINNER) {
+ Value* tessFactors = GEP(pCpOut, {(uint32_t)0, ScalarPatch_tessFactors});
+ Value* tessFactorArray = nullptr;
+ if (name == TGSI_SEMANTIC_TESSOUTER) {
+ tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_OuterTessFactors});
+ } else {
+ tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_InnerTessFactors});
+ }
+ Value* tessFactor = GEP(tessFactorArray, {C(0), unwrap(swizzle_index)});
+ Value* valueToStore = VEXTRACT(unwrap(value), C(lane));
+ valueToStore = BITCAST(valueToStore, mFP32Ty);
+ if (mask_vec) {
+ Value *originalVal = LOAD(tessFactor);
+ Value *vMask = TRUNC(VEXTRACT(unwrap(mask_vec), C(lane)), mInt1Ty);
+ valueToStore = SELECT(vMask, valueToStore, originalVal);
+ }
+ STORE(valueToStore, tessFactor);
+ if (verbose_tcs_shader_out)
+ {
+ lp_build_print_value(gallivm, "[TCS OUT][FACTOR] Mask_vec mask: ", mask_vec);
+ lp_build_print_value(gallivm, "[TCS OUT][FACTOR] Stored value: ", wrap(valueToStore));
+ }
+ } else if (name == TGSI_SEMANTIC_PATCH) {
+ Value* attrib = LOAD(GEP(iface->pPatchOutputAttribMap, {C(0), attr_index}));
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] vert_index: ", wrap(vert_index));
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] attr_index: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] vert_index_indirect: ", wrap(C(is_vindex_indirect)));
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] attr_index_indirect: ", wrap(C(is_aindex_indirect)));
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] attr index loaded from map: ", wrap(attrib));
+ }
+ Value* attr = GEP(pCpOut, {C(0), C(ScalarPatch_patchData), C(ScalarCPoint_attrib), attrib});
+ Value* value_to_store = VEXTRACT(unwrap(value), C(lane));
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] lane (patch-id): ", wrap(C(lane)));
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] value to store: ", value);
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] per-patch value to store: ", wrap(value_to_store));
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] chan_index: ", swizzle_index);
+ }
+ value_to_store = BITCAST(value_to_store, mFP32Ty);
+ if (mask_vec) {
+ Value *originalVal = LOADV(attr, {C(0), unwrap(swizzle_index)});
+ Value *vMask = TRUNC(VEXTRACT(unwrap(mask_vec), C(lane)), mInt1Ty);
+ value_to_store = SELECT(vMask, value_to_store, originalVal);
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] store mask: ", mask_vec);
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] loaded original value: ", wrap(originalVal));
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] vMask: ", wrap(vMask));
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] selected value to store: ", wrap(value_to_store));
+ }
+ }
+ STOREV(value_to_store, attr, {C(0), unwrap(swizzle_index)});
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT][PATCH] stored value: ", wrap(value_to_store));
+ }
+ } else {
+ Value* value_to_store = VEXTRACT(unwrap(value), C(lane));
+ Value* attrib = LOAD(GEP(iface->pVtxOutputAttribMap, {C(0), attr_index}));
+
+ if (verbose_tcs_shader_out) {
+ lp_build_printf(gallivm, "[TCS OUT] Writting attribute\n");
+ lp_build_print_value(gallivm, "[TCS OUT][VTX] invocation_id: ", bld->system_values.invocation_id);
+ lp_build_print_value(gallivm, "[TCS OUT][VTX] attribIndex: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TCS OUT][VTX] attrib read from map: ", wrap(attrib));
+ lp_build_print_value(gallivm, "[TCS OUT][VTX] chan_index: ", swizzle_index);
+ lp_build_print_value(gallivm, "[TCS OUT][VTX] value: ", value);
+ lp_build_print_value(gallivm, "[TCS OUT][VTX] value_to_store: ", wrap(value_to_store));
+ }
+
+ Value* attr_chan = GEP(pCpOut, {C(0), C(ScalarPatch_cp),
+ VEXTRACT(unwrap(bld->system_values.invocation_id), C(0)),
+ C(ScalarCPoint_attrib), attrib, unwrap(swizzle_index)});
+
+ // Mask output values if needed
+ value_to_store = BITCAST(value_to_store, mFP32Ty);
+ if (mask_vec) {
+ Value *originalVal = LOAD(attr_chan);
+ Value *vMask = TRUNC(VEXTRACT(unwrap(mask_vec), C(lane)), mInt1Ty);
+ value_to_store = SELECT(vMask, value_to_store, originalVal);
+ }
+ STORE(value_to_store, attr_chan);
+ if (verbose_tcs_shader_out) {
+ lp_build_print_value(gallivm, "[TCS OUT][VTX] Mask_vec mask: ", mask_vec);
+ lp_build_print_value(gallivm, "[TCS OUT][VTX] stored: ", wrap(value_to_store));
+ }
+ }
+ }
+}
+
+void
+BuilderSWR::swr_tcs_llvm_emit_barrier(const struct lp_build_tcs_iface *tcs_iface,
+ struct lp_build_tgsi_context *bld_base)
+{
+ swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;
+ struct lp_build_tgsi_soa_context* bld = (struct lp_build_tgsi_soa_context*)bld_base;
+
+ if (verbose_tcs_shader_loop) {
+ lp_build_print_value(gallivm, "Barrier LOOP: Iteration %d END\n", iface->loop_var);
+ }
+
+ struct lp_build_context *uint_bld = &bld->bld_base.uint_bld;
+
+ STORE(ADD(LOAD(unwrap(iface->loop_var)), VBROADCAST(C(1))), unwrap(iface->loop_var));
+
+ LLVMValueRef tmp = lp_build_cmp(uint_bld, PIPE_FUNC_GEQUAL, wrap(LOAD(unwrap(iface->loop_var))),
+ wrap(VBROADCAST(C(iface->output_vertices))));
+
+ lp_exec_mask_cond_push(&bld->exec_mask, tmp);
+ lp_exec_break(&bld->exec_mask, &bld->bld_base.pc, false);
+ lp_exec_mask_cond_pop(&bld->exec_mask);
+ lp_exec_endloop(bld->bld_base.base.gallivm, &bld->exec_mask);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ STORE(VBROADCAST(C(0)), unwrap(iface->loop_var));
+ lp_exec_bgnloop(&bld->exec_mask, true);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ bld->system_values.invocation_id = wrap((LOAD(unwrap(iface->loop_var))));
+
+ if (verbose_tcs_shader_loop) {
+ lp_build_print_value(gallivm, "Barrier LOOP: Iteration BEGIN: ", iface->loop_var);
+ lp_build_print_value(gallivm, "Barrier LOOP: InvocationId: \n", bld->system_values.invocation_id);
+ }
+}
+
+
+LLVMValueRef
+BuilderSWR::swr_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface *tes_iface,
+ struct lp_build_tgsi_context * bld_base,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index)
+{
+ swr_tes_llvm_iface *iface = (swr_tes_llvm_iface*)tes_iface;
+ Value *attr_index = unwrap(attrib_index);
+ Value *res = unwrap(bld_base->base.zero);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ if (verbose_shader) {
+ lp_build_printf(gallivm, "[TES IN][PATCH] --------------------------------------\n");
+ }
+
+ if (is_aindex_indirect) {
+ int i;
+ struct lp_type type = bld_base->base.type;
+
+ for (i = 0; i < type.length; i++) {
+ Value *attr_chan_index = attr_index;
+
+ if (is_aindex_indirect) {
+ attr_chan_index = VEXTRACT(attr_index, C(i));
+ }
+
+ Value *attrib =
+ LOAD(GEP(iface->pPatchAttribMap, {C(0), attr_chan_index}));
+
+ Value *pCpIn = LOAD(iface->pTesCtx, {0, SWR_DS_CONTEXT_pCpIn}, "pCpIn");
+ Value *pPatchData = GEP(pCpIn, {(uint32_t)0, ScalarPatch_patchData});
+ Value *pAttr = GEP(pPatchData, {(uint32_t)0, ScalarCPoint_attrib});
+ Value *Val = LOADV(pAttr, {C(0), attrib, unwrap(swizzle_index)});
+ if (verbose_shader) {
+ lp_build_print_value(gallivm, "[TES IN][PATCH] attrib_index: ", attrib_index);
+ lp_build_print_value(gallivm, "[TES IN][PATCH] attr_chan_index: ", wrap(attr_chan_index));
+ lp_build_print_value(gallivm, "[TES IN][PATCH] attrib read from map: ", wrap(attrib));
+ lp_build_print_value(gallivm, "[TES IN][PATCH] swizzle_index: ", swizzle_index);
+ lp_build_print_value(gallivm, "[TES IN][PATCH] Loaded: ", wrap(Val));
+ }
+ res = VINSERT(res, Val, C(i));
+ }
+ } else {
+ Value *attrib = LOAD(GEP(iface->pPatchAttribMap, {C(0), attr_index}));
+
+ Value *pCpIn = LOAD(iface->pTesCtx, {(uint32_t)0, SWR_DS_CONTEXT_pCpIn}, "pCpIn");
+ Value *pPatchData = GEP(pCpIn, {(uint32_t)0, ScalarPatch_patchData});
+ Value *pAttr = GEP(pPatchData, {(uint32_t)0, ScalarCPoint_attrib});
+ Value *Val = LOADV(pAttr, {C(0), attrib, unwrap(swizzle_index)});
+ if (verbose_shader) {
+ lp_build_print_value(gallivm, "[TES IN][PATCH] attrib_index: ", attrib_index);
+ lp_build_print_value(gallivm, "[TES IN][PATCH] attr_chan_index: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TES IN][PATCH] attrib read from map: ", wrap(attrib));
+ lp_build_print_value(gallivm, "[TES IN][PATCH] swizzle_index: ", swizzle_index);
+ lp_build_print_value(gallivm, "[TES IN][PATCH] Loaded: ", wrap(Val));
+ }
+ res = VBROADCAST(Val);
+ }
+ if (verbose_shader) {
+ lp_build_print_value(gallivm, "[TES IN][PATCH] returning: ", wrap(res));
+ }
+ return wrap(res);
+}
+
+
+
+LLVMValueRef
+BuilderSWR::swr_tes_llvm_fetch_vtx_input(const struct lp_build_tes_iface *tes_iface,
+ struct lp_build_tgsi_context * bld_base,
+ boolean is_vindex_indirect,
+ LLVMValueRef vertex_index,
+ boolean is_aindex_indirect,
+ LLVMValueRef attrib_index,
+ LLVMValueRef swizzle_index)
+{
+ swr_tes_llvm_iface *iface = (swr_tes_llvm_iface*)tes_iface;
+ Value *vert_index = unwrap(vertex_index);
+ Value *attr_index = unwrap(attrib_index);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ if (verbose_shader) {
+ lp_build_printf(gallivm, "[TES IN][VTX] --------------------------------------\n");
+ }
+
+ Value *res = unwrap(bld_base->base.zero);
+ if (is_vindex_indirect || is_aindex_indirect) {
+ int i;
+ struct lp_type type = bld_base->base.type;
+
+ for (i = 0; i < type.length; i++) {
+ Value *vert_chan_index = vert_index;
+ Value *attr_chan_index = attr_index;
+
+ if (is_vindex_indirect) {
+ vert_chan_index = VEXTRACT(vert_index, C(i));
+ }
+ if (is_aindex_indirect) {
+ attr_chan_index = VEXTRACT(attr_index, C(i));
+ }
+
+ Value *attrib =
+ LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_chan_index}));
+
+ Value *pCpIn = LOAD(iface->pTesCtx, {0, SWR_DS_CONTEXT_pCpIn}, "pCpIn");
+ Value *pCp = GEP(pCpIn, {0, ScalarPatch_cp});
+ Value *pVertex = GEP(pCp, {(Value*)C(0), vert_chan_index});
+ Value *pAttrTab = GEP(pVertex, {uint32_t(0), uint32_t(0)});
+ Value *pAttr = GEP(pAttrTab, {(Value*)C(0), attrib});
+ Value *Val = LOADV(pAttr, {C(0), unwrap(swizzle_index)});
+ if (verbose_shader) {
+ lp_build_print_value(gallivm, "[TES IN][VTX] attrib_index: ", attrib_index);
+ lp_build_print_value(gallivm, "[TES IN][VTX] attr_chan_index: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TES IN][VTX] attrib read from map: ", wrap(attrib));
+ lp_build_print_value(gallivm, "[TES IN][VTX] swizzle_index: ", swizzle_index);
+ lp_build_print_value(gallivm, "[TES IN][VTX] Loaded: ", wrap(Val));
+ }
+ res = VINSERT(res, Val, C(i));
+ }
+ } else {
+ Value *attrib = LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_index}));
+
+ Value *pCpIn = LOAD(iface->pTesCtx, {0, SWR_DS_CONTEXT_pCpIn}, "pCpIn");
+ Value *pCp = GEP(pCpIn, {0, ScalarPatch_cp});
+ Value *pVertex = GEP(pCp, {(Value*)C(0), vert_index});
+ Value *pAttrTab = GEP(pVertex, {uint32_t(0), uint32_t(0)});
+ Value *pAttr = GEP(pAttrTab, {(Value*)C(0), attrib});
+ Value *Val = LOADV(pAttr, {C(0), unwrap(swizzle_index)});
+ if (verbose_shader) {
+ lp_build_print_value(gallivm, "[TES IN][VTX] attrib_index: ", attrib_index);
+ lp_build_print_value(gallivm, "[TES IN][VTX] attr_chan_index: ", wrap(attr_index));
+ lp_build_print_value(gallivm, "[TES IN][VTX] attrib read from map: ", wrap(attrib));
+ lp_build_print_value(gallivm, "[TES IN][VTX] swizzle_index: ", swizzle_index);
+ lp_build_print_value(gallivm, "[TES IN][VTX] Loaded: ", wrap(Val));
+ }
+ res = VBROADCAST(Val);
+ }
+ if (verbose_shader) {
+ lp_build_print_value(gallivm, "[TES IN][VTX] returning: ", wrap(res));
+ }
+ return wrap(res);
+}
+
+
+
+
+PFN_GS_FUNC
+BuilderSWR::CompileGS(struct swr_context *ctx, swr_jit_gs_key &key)
+{
+ SWR_GS_STATE *pGS = &ctx->gs->gsState;
+ struct tgsi_shader_info *info = &ctx->gs->info.base;
+
+ memset(pGS, 0, sizeof(*pGS));
+
+ pGS->gsEnable = true;
+
+ pGS->numInputAttribs = (VERTEX_ATTRIB_START_SLOT - VERTEX_POSITION_SLOT) + info->num_inputs;
+ pGS->outputTopology =
+ swr_convert_prim_topology(info->properties[TGSI_PROPERTY_GS_OUTPUT_PRIM], 0);
+
+ /* It's +1 because emit_vertex in swr is always called exactly one time more
+ * than max_vertices passed in Geometry Shader. We need to allocate more memory
+ * to avoid crash/memory overwritten.
+ */
+ pGS->maxNumVerts = info->properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES] + 1;
+ pGS->instanceCount = info->properties[TGSI_PROPERTY_GS_INVOCATIONS];
+
+ // If point primitive then assume to use multiple streams
+ if(pGS->outputTopology == TOP_POINT_LIST) {
+ pGS->isSingleStream = false;
+ } else {
+ pGS->isSingleStream = true;
+ pGS->singleStreamID = 0;
+ }
+
+ pGS->vertexAttribOffset = VERTEX_POSITION_SLOT;
+ pGS->inputVertStride = pGS->numInputAttribs + pGS->vertexAttribOffset;
+ pGS->outputVertexSize = SWR_VTX_NUM_SLOTS;
+ pGS->controlDataSize = 8; // GS ouputs max of 8 32B units
+ pGS->controlDataOffset = VERTEX_COUNT_SIZE;
+ pGS->outputVertexOffset = pGS->controlDataOffset + CONTROL_HEADER_SIZE;
+
+ pGS->allocationSize =
+ VERTEX_COUNT_SIZE + // vertex count
+ CONTROL_HEADER_SIZE + // control header
+ (SWR_VTX_NUM_SLOTS * 16) * // sizeof vertex
+ pGS->maxNumVerts; // num verts
+
+ struct swr_geometry_shader *gs = ctx->gs;
+
+ LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
+ LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
+
+ memset(outputs, 0, sizeof(outputs));
+
+ AttrBuilder attrBuilder;
+ attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));
+
+ std::vector<Type *> gsArgs{PointerType::get(Gen_swr_draw_context(JM()), 0),
+ PointerType::get(mInt8Ty, 0),
+ PointerType::get(Gen_SWR_GS_CONTEXT(JM()), 0)};
+ FunctionType *vsFuncType =
+ FunctionType::get(Type::getVoidTy(JM()->mContext), gsArgs, false);
+
+ // create new vertex shader function
+ auto pFunction = Function::Create(vsFuncType,
+ GlobalValue::ExternalLinkage,
+ "GS",
+ JM()->mpCurrentModule);
+#if LLVM_VERSION_MAJOR < 5
+ AttributeSet attrSet = AttributeSet::get(
+ JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);
+ pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);
+#else
+ pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);
+#endif
+
+ BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);
+ IRB()->SetInsertPoint(block);
+ LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));
+
+ auto argitr = pFunction->arg_begin();
+ Value *hPrivateData = &*argitr++;
+ hPrivateData->setName("hPrivateData");
+ Value *pWorkerData = &*argitr++;
+ pWorkerData->setName("pWorkerData");
+ Value *pGsCtx = &*argitr++;
+ pGsCtx->setName("gsCtx");
+
+ Value *consts_ptr =
+ GEP(hPrivateData, {C(0), C(swr_draw_context_constantGS)});
+ consts_ptr->setName("gs_constants");
+ Value *const_sizes_ptr =
+ GEP(hPrivateData, {0, swr_draw_context_num_constantsGS});
+ const_sizes_ptr->setName("num_gs_constants");
+
+ struct lp_build_sampler_soa *sampler =
+ swr_sampler_soa_create(key.sampler, PIPE_SHADER_GEOMETRY);
+ assert(sampler != nullptr);
+
+ struct lp_bld_tgsi_system_values system_values;
+ memset(&system_values, 0, sizeof(system_values));
+ system_values.prim_id = wrap(LOAD(pGsCtx, {0, SWR_GS_CONTEXT_PrimitiveID}));
+ system_values.invocation_id = wrap(LOAD(pGsCtx, {0, SWR_GS_CONTEXT_InstanceID}));
+
+ std::vector<Constant*> mapConstants;
+ Value *vtxAttribMap = ALLOCA(ArrayType::get(mInt32Ty, PIPE_MAX_SHADER_INPUTS));
+ for (unsigned slot = 0; slot < info->num_inputs; slot++) {
+ ubyte semantic_name = info->input_semantic_name[slot];
+ ubyte semantic_idx = info->input_semantic_index[slot];
+
+ unsigned vs_slot = locate_linkage(semantic_name, semantic_idx, &ctx->vs->info.base);
+ assert(vs_slot < PIPE_MAX_SHADER_OUTPUTS);
+
+ vs_slot += VERTEX_ATTRIB_START_SLOT;
+
+ if (ctx->vs->info.base.output_semantic_name[0] == TGSI_SEMANTIC_POSITION)
+ vs_slot--;
+
+ if (semantic_name == TGSI_SEMANTIC_POSITION)
+ vs_slot = VERTEX_POSITION_SLOT;
+
+ STORE(C(vs_slot), vtxAttribMap, {0, slot});
+ mapConstants.push_back(C(vs_slot));
+ }
+
+ struct lp_build_mask_context mask;
+ Value *mask_val = LOAD(pGsCtx, {0, SWR_GS_CONTEXT_mask}, "gsMask");
+ lp_build_mask_begin(&mask, gallivm,
+ lp_type_float_vec(32, 32 * 8), wrap(mask_val));
+
+ // zero out cut buffer so we can load/modify/store bits
+ for (uint32_t lane = 0; lane < mVWidth; ++lane)
+ {
+ Value* pStream = LOAD(pGsCtx, {0, SWR_GS_CONTEXT_pStreams, lane});
+#if LLVM_VERSION_MAJOR >= 10
+ MEMSET(pStream, C((char)0), VERTEX_COUNT_SIZE + CONTROL_HEADER_SIZE, MaybeAlign(sizeof(float) * KNOB_SIMD_WIDTH));
+#else
+ MEMSET(pStream, C((char)0), VERTEX_COUNT_SIZE + CONTROL_HEADER_SIZE, sizeof(float) * KNOB_SIMD_WIDTH);
+#endif
+ }
+
+ struct swr_gs_llvm_iface gs_iface;
+ gs_iface.base.fetch_input = ::swr_gs_llvm_fetch_input;
+ gs_iface.base.emit_vertex = ::swr_gs_llvm_emit_vertex;
+ gs_iface.base.end_primitive = ::swr_gs_llvm_end_primitive;
+ gs_iface.base.gs_epilogue = ::swr_gs_llvm_epilogue;
+ gs_iface.pBuilder = this;
+ gs_iface.pGsCtx = pGsCtx;
+ gs_iface.pGsState = pGS;
+ gs_iface.num_outputs = gs->info.base.num_outputs;
+ gs_iface.num_verts_per_prim =
+ u_vertices_per_prim((pipe_prim_type)info->properties[TGSI_PROPERTY_GS_OUTPUT_PRIM]);
+ gs_iface.info = info;
+ gs_iface.pVtxAttribMap = vtxAttribMap;
+
+ struct lp_build_tgsi_params params;
+ memset(¶ms, 0, sizeof(params));
+ params.type = lp_type_float_vec(32, 32 * 8);
+ params.mask = & mask;
+ params.consts_ptr = wrap(consts_ptr);
+ params.const_sizes_ptr = wrap(const_sizes_ptr);
+ params.system_values = &system_values;
+ params.inputs = inputs;
+ params.context_ptr = wrap(hPrivateData);
+ params.sampler = sampler;
+ params.info = &gs->info.base;
+ params.gs_iface = &gs_iface.base;
+
+ lp_build_tgsi_soa(gallivm,
+ gs->pipe.tokens,
+ ¶ms,
+ outputs);
+
+ lp_build_mask_end(&mask);
+
+ sampler->destroy(sampler);
+
+ IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
+
+ RET_VOID();
+
+ gallivm_verify_function(gallivm, wrap(pFunction));
+ gallivm_compile_module(gallivm);
+
+ PFN_GS_FUNC pFunc =
+ (PFN_GS_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));
+
+ debug_printf("geom shader %p\n", pFunc);
+ assert(pFunc && "Error: GeomShader = NULL");
+
+ JM()->mIsModuleFinalized = true;
+
+ return pFunc;
+}
+
+PFN_TES_FUNC
+BuilderSWR::CompileTES(struct swr_context *ctx, swr_jit_tes_key &key)
+{
+ SWR_TS_STATE *pTS = &ctx->tsState;
+ struct tgsi_shader_info *info = &ctx->tes->info.base;
+
+ // tessellation is enabled if TES is present
+ // clear tessellation state here then
+ memset(pTS, 0, sizeof(*pTS));
+
+ pTS->tsEnable = true;
+
+ unsigned tes_prim_mode = info->properties[TGSI_PROPERTY_TES_PRIM_MODE];
+ unsigned tes_spacing = info->properties[TGSI_PROPERTY_TES_SPACING];
+ bool tes_vertex_order_cw = info->properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW];
+ bool tes_point_mode = info->properties[TGSI_PROPERTY_TES_POINT_MODE];
+ SWR_TS_DOMAIN type = SWR_TS_ISOLINE;
+ SWR_TS_PARTITIONING partitioning = SWR_TS_EVEN_FRACTIONAL;
+ SWR_TS_OUTPUT_TOPOLOGY topology = SWR_TS_OUTPUT_POINT;
+ PRIMITIVE_TOPOLOGY postDSTopology = TOP_POINT_LIST;
+
+ // TESS_TODO: move this to helper functions to improve readability
+ switch (tes_prim_mode) {
+ case PIPE_PRIM_LINES:
+ type = SWR_TS_ISOLINE;
+ postDSTopology = TOP_LINE_LIST;
+ break;
+ case PIPE_PRIM_TRIANGLES:
+ type = SWR_TS_TRI;
+ postDSTopology = TOP_TRIANGLE_LIST;
+ break;
+ case PIPE_PRIM_QUADS:
+ type = SWR_TS_QUAD;
+ // See OpenGL spec - quads are tessellated into triangles
+ postDSTopology = TOP_TRIANGLE_LIST;
+ break;
+ default:
+ assert(0);
+ }
+
+ switch (tes_spacing) {
+ case PIPE_TESS_SPACING_FRACTIONAL_ODD:
+ partitioning = SWR_TS_ODD_FRACTIONAL;
+ break;
+ case PIPE_TESS_SPACING_FRACTIONAL_EVEN:
+ partitioning = SWR_TS_EVEN_FRACTIONAL;
+ break;
+ case PIPE_TESS_SPACING_EQUAL:
+ partitioning = SWR_TS_INTEGER;
+ break;
+ default:
+ assert(0);
+ }
+
+ if (tes_point_mode) {
+ topology = SWR_TS_OUTPUT_POINT;
+ postDSTopology = TOP_POINT_LIST;
+ }
+ else if (tes_prim_mode == PIPE_PRIM_LINES) {
+ topology = SWR_TS_OUTPUT_LINE;
+ }
+ else if (tes_vertex_order_cw) {
+ topology = SWR_TS_OUTPUT_TRI_CW;
+ }
+ else {
+ topology = SWR_TS_OUTPUT_TRI_CCW;
+ }
+
+ pTS->domain = type;
+ pTS->tsOutputTopology = topology;
+ pTS->partitioning = partitioning;
+ pTS->numDsOutputAttribs = info->num_outputs;
+ pTS->postDSTopology = postDSTopology;
+
+ pTS->dsAllocationSize = SWR_VTX_NUM_SLOTS * MAX_NUM_VERTS_PER_PRIM;
+ pTS->vertexAttribOffset = VERTEX_ATTRIB_START_SLOT;
+ pTS->srcVertexAttribOffset = VERTEX_ATTRIB_START_SLOT;
+ pTS->dsOutVtxAttribOffset = VERTEX_ATTRIB_START_SLOT;
+
+ struct swr_tess_evaluation_shader *tes = ctx->tes;
+
+ LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
+ LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
+
+ memset(outputs, 0, sizeof(outputs));
+
+ AttrBuilder attrBuilder;
+ attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));
+
+ std::vector<Type *> tesArgs{PointerType::get(Gen_swr_draw_context(JM()), 0),
+ PointerType::get(mInt8Ty, 0),
+ PointerType::get(Gen_SWR_DS_CONTEXT(JM()), 0)};
+ FunctionType *tesFuncType =
+ FunctionType::get(Type::getVoidTy(JM()->mContext), tesArgs, false);
+
+ // create new vertex shader function
+ auto pFunction = Function::Create(tesFuncType,
+ GlobalValue::ExternalLinkage,
+ "TES",
+ JM()->mpCurrentModule);
+
+#if LLVM_VERSION_MAJOR < 5
+ AttributeSet attrSet = AttributeSet::get(
+ JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);
+ pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);
+#else
+ pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);
+#endif
+
+ BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);
+ IRB()->SetInsertPoint(block);
+ LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));