1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * @brief Implementation of the fetch jitter
29 ******************************************************************************/
30 #include "jit_pch.hpp"
33 #include "fetch_jit.h"
34 #include "gen_state_llvm.h"
36 //#define FETCH_DUMP_VERTEX 1
38 using namespace SwrJit
;
40 bool isComponentEnabled(ComponentEnable enableMask
, uint8_t component
);
51 #if USE_SIMD16_SHADERS
52 #define USE_SIMD16_GATHERS 0
55 //////////////////////////////////////////////////////////////////////////
56 /// Interface to Jitting a fetch shader
57 //////////////////////////////////////////////////////////////////////////
61 FetchJit(JitManager
* pJitMgr
) :
65 Function
* Create(const FETCH_COMPILE_STATE
& fetchState
);
67 Value
* GetSimdValid32bitIndices(Value
* vIndices
, Value
* pLastIndex
);
68 Value
* GetSimdValid16bitIndices(Value
* vIndices
, Value
* pLastIndex
);
69 Value
* GetSimdValid8bitIndices(Value
* vIndices
, Value
* pLastIndex
);
71 // package up Shuffle*bpcGatherd args into a tuple for convenience
72 typedef std::tuple
<Value
*&, Value
*, const Instruction::CastOps
, const ConversionType
,
73 uint32_t&, uint32_t&, const ComponentEnable
, const ComponentControl(&)[4], Value
*(&)[4],
74 const uint32_t(&)[4]> Shuffle8bpcArgs
;
76 #if USE_SIMD16_SHADERS
77 #if USE_SIMD16_GATHERS
78 void Shuffle8bpcGatherd16(Shuffle8bpcArgs
&args
);
80 void Shuffle8bpcGatherd(Shuffle8bpcArgs
&args
, bool useVertexID2
);
83 void Shuffle8bpcGatherd(Shuffle8bpcArgs
&args
);
86 typedef std::tuple
<Value
*(&)[2], Value
*, const Instruction::CastOps
, const ConversionType
,
87 uint32_t&, uint32_t&, const ComponentEnable
, const ComponentControl(&)[4], Value
*(&)[4]> Shuffle16bpcArgs
;
89 #if USE_SIMD16_SHADERS
90 #if USE_SIMD16_GATHERS
91 void Shuffle16bpcGather16(Shuffle16bpcArgs
&args
);
93 void Shuffle16bpcGather(Shuffle16bpcArgs
&args
, bool useVertexID2
);
96 void Shuffle16bpcGather(Shuffle16bpcArgs
&args
);
99 #if USE_SIMD16_GATHERS
100 void StoreVertexElements16(Value
* pVtxOut
, const uint32_t outputElt
, const uint32_t numEltsToStore
, Value
* (&vVertexElements
)[4]);
102 void StoreVertexElements(Value
* pVtxOut
, const uint32_t outputElt
, const uint32_t numEltsToStore
, Value
* (&vVertexElements
)[4]);
105 #if USE_SIMD16_SHADERS
106 #if USE_SIMD16_GATHERS
107 Value
*GenerateCompCtrlVector16(const ComponentControl ctrl
);
109 Value
*GenerateCompCtrlVector(const ComponentControl ctrl
, bool useVertexID2
);
112 Value
*GenerateCompCtrlVector(const ComponentControl ctrl
);
115 void JitLoadVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
* streams
, Value
* vIndices
, Value
* pVtxOut
);
117 #if USE_SIMD16_SHADERS
118 #if USE_SIMD16_GATHERS
119 void JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
*streams
, Value
*vIndices
, Value
*vIndices2
, Value
*pVtxOut
, bool useVertexID2
);
121 void JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
* streams
, Value
* vIndices
, Value
* pVtxOut
, bool useVertexID2
);
124 void JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
* streams
, Value
* vIndices
, Value
* pVtxOut
);
127 bool IsOddFormat(SWR_FORMAT format
);
128 bool IsUniformFormat(SWR_FORMAT format
);
129 void UnpackComponents(SWR_FORMAT format
, Value
* vInput
, Value
* result
[4]);
130 void CreateGatherOddFormats(SWR_FORMAT format
, Value
* pMask
, Value
* pBase
, Value
* offsets
, Value
* result
[4]);
131 void ConvertFormat(SWR_FORMAT format
, Value
*texels
[4]);
136 Function
* FetchJit::Create(const FETCH_COMPILE_STATE
& fetchState
)
138 std::stringstream
fnName("FCH_", std::ios_base::in
| std::ios_base::out
| std::ios_base::ate
);
139 fnName
<< ComputeCRC(0, &fetchState
, sizeof(fetchState
));
141 Function
* fetch
= Function::Create(JM()->mFetchShaderTy
, GlobalValue::ExternalLinkage
, fnName
.str(), JM()->mpCurrentModule
);
142 BasicBlock
* entry
= BasicBlock::Create(JM()->mContext
, "entry", fetch
);
144 fetch
->getParent()->setModuleIdentifier(fetch
->getName());
146 IRB()->SetInsertPoint(entry
);
148 auto argitr
= fetch
->arg_begin();
150 // Fetch shader arguments
151 mpPrivateContext
= &*argitr
; ++argitr
;
152 mpPrivateContext
->setName("privateContext");
154 mpFetchInfo
= &*argitr
; ++argitr
;
155 mpFetchInfo
->setName("fetchInfo");
156 Value
* pVtxOut
= &*argitr
;
157 pVtxOut
->setName("vtxOutput");
158 // this is just shorthand to tell LLVM to get a pointer to the base address of simdvertex
159 // index 0(just the pointer to the simdvertex structure
160 // index 1(which element of the simdvertex structure to offset to(in this case 0)
161 // so the indices being i32's doesn't matter
162 // TODO: generated this GEP with a VECTOR structure type so this makes sense
163 std::vector
<Value
*> vtxInputIndices(2, C(0));
165 pVtxOut
= GEP(pVtxOut
, C(0));
166 #if USE_SIMD16_SHADERS
167 #if 0// USE_SIMD16_BUILDER
168 pVtxOut
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
170 pVtxOut
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth
), 0));
173 pVtxOut
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth
), 0));
176 // SWR_FETCH_CONTEXT::pStreams
177 Value
* streams
= LOAD(mpFetchInfo
,{0, SWR_FETCH_CONTEXT_pStreams
});
178 streams
->setName("pStreams");
180 // SWR_FETCH_CONTEXT::pIndices
181 Value
* indices
= LOAD(mpFetchInfo
,{0, SWR_FETCH_CONTEXT_pIndices
});
182 indices
->setName("pIndices");
184 // SWR_FETCH_CONTEXT::pLastIndex
185 Value
* pLastIndex
= LOAD(mpFetchInfo
,{0, SWR_FETCH_CONTEXT_pLastIndex
});
186 pLastIndex
->setName("pLastIndex");
190 #if USE_SIMD16_SHADERS
194 switch(fetchState
.indexType
)
197 indices
= BITCAST(indices
, Type::getInt8PtrTy(JM()->mContext
, 0));
198 #if USE_SIMD16_SHADERS
199 indices2
= GEP(indices
, C(8));
201 if(fetchState
.bDisableIndexOOBCheck
)
203 vIndices
= LOAD(BITCAST(indices
, PointerType::get(VectorType::get(mInt8Ty
, mpJitMgr
->mVWidth
), 0)), {(uint32_t)0});
204 vIndices
= Z_EXT(vIndices
, mSimdInt32Ty
);
205 #if USE_SIMD16_SHADERS
206 vIndices2
= LOAD(BITCAST(indices2
, PointerType::get(VectorType::get(mInt8Ty
, mpJitMgr
->mVWidth
), 0)), { (uint32_t)0 });
207 vIndices2
= Z_EXT(vIndices2
, mSimdInt32Ty
);
212 pLastIndex
= BITCAST(pLastIndex
, Type::getInt8PtrTy(JM()->mContext
, 0));
213 vIndices
= GetSimdValid8bitIndices(indices
, pLastIndex
);
214 #if USE_SIMD16_SHADERS
215 pLastIndex
= BITCAST(pLastIndex
, Type::getInt8PtrTy(JM()->mContext
, 0));
216 vIndices2
= GetSimdValid8bitIndices(indices2
, pLastIndex
);
221 indices
= BITCAST(indices
, Type::getInt16PtrTy(JM()->mContext
, 0));
222 #if USE_SIMD16_SHADERS
223 indices2
= GEP(indices
, C(8));
225 if(fetchState
.bDisableIndexOOBCheck
)
227 vIndices
= LOAD(BITCAST(indices
, PointerType::get(VectorType::get(mInt16Ty
, mpJitMgr
->mVWidth
), 0)), {(uint32_t)0});
228 vIndices
= Z_EXT(vIndices
, mSimdInt32Ty
);
229 #if USE_SIMD16_SHADERS
230 vIndices2
= LOAD(BITCAST(indices2
, PointerType::get(VectorType::get(mInt16Ty
, mpJitMgr
->mVWidth
), 0)), { (uint32_t)0 });
231 vIndices2
= Z_EXT(vIndices2
, mSimdInt32Ty
);
236 pLastIndex
= BITCAST(pLastIndex
, Type::getInt16PtrTy(JM()->mContext
, 0));
237 vIndices
= GetSimdValid16bitIndices(indices
, pLastIndex
);
238 #if USE_SIMD16_SHADERS
239 pLastIndex
= BITCAST(pLastIndex
, Type::getInt16PtrTy(JM()->mContext
, 0));
240 vIndices2
= GetSimdValid16bitIndices(indices2
, pLastIndex
);
245 #if USE_SIMD16_SHADERS
246 indices2
= GEP(indices
, C(8));
248 (fetchState
.bDisableIndexOOBCheck
) ? vIndices
= LOAD(BITCAST(indices
, PointerType::get(mSimdInt32Ty
,0)),{(uint32_t)0})
249 : vIndices
= GetSimdValid32bitIndices(indices
, pLastIndex
);
250 #if USE_SIMD16_SHADERS
251 (fetchState
.bDisableIndexOOBCheck
) ? vIndices2
= LOAD(BITCAST(indices2
, PointerType::get(mSimdInt32Ty
, 0)), { (uint32_t)0 })
252 : vIndices2
= GetSimdValid32bitIndices(indices2
, pLastIndex
);
254 break; // incoming type is already 32bit int
255 default: SWR_INVALID("Unsupported index type"); vIndices
= nullptr; break;
258 if(fetchState
.bForceSequentialAccessEnable
)
260 Value
* pOffsets
= C({ 0, 1, 2, 3, 4, 5, 6, 7 });
262 // VertexData buffers are accessed sequentially, the index is equal to the vertex number
263 vIndices
= VBROADCAST(LOAD(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_StartVertex
}));
264 vIndices
= ADD(vIndices
, pOffsets
);
265 #if USE_SIMD16_SHADERS
266 vIndices2
= ADD(vIndices
, VIMMED1(8));
270 Value
* vVertexId
= vIndices
;
271 #if USE_SIMD16_SHADERS
272 Value
* vVertexId2
= vIndices2
;
274 if (fetchState
.bVertexIDOffsetEnable
)
276 // Assuming one of baseVertex or startVertex is 0, so adding both should be functionally correct
277 Value
* vBaseVertex
= VBROADCAST(LOAD(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_BaseVertex
}));
278 Value
* vStartVertex
= VBROADCAST(LOAD(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_StartVertex
}));
279 vVertexId
= ADD(vIndices
, vBaseVertex
);
280 vVertexId
= ADD(vVertexId
, vStartVertex
);
281 #if USE_SIMD16_SHADERS
282 vVertexId2
= ADD(vIndices2
, vBaseVertex
);
283 vVertexId2
= ADD(vVertexId2
, vStartVertex
);
287 // store out vertex IDs
288 STORE(vVertexId
, GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID
}));
289 #if USE_SIMD16_SHADERS
290 STORE(vVertexId2
, GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID2
}));
293 // store out cut mask if enabled
294 if (fetchState
.bEnableCutIndex
)
296 Value
* vCutIndex
= VIMMED1(fetchState
.cutIndex
);
297 Value
* cutMask
= VMASK(ICMP_EQ(vIndices
, vCutIndex
));
298 STORE(cutMask
, GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_CutMask
}));
299 #if USE_SIMD16_SHADERS
300 Value
* cutMask2
= VMASK(ICMP_EQ(vIndices2
, vCutIndex
));
301 STORE(cutMask2
, GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_CutMask2
}));
305 // Fetch attributes from memory and output to a simdvertex struct
306 // since VGATHER has a perf penalty on HSW vs BDW, allow client to choose which fetch method to use
307 #if USE_SIMD16_SHADERS
308 if (fetchState
.bDisableVGATHER
)
310 JitLoadVertices(fetchState
, streams
, vIndices
, pVtxOut
);
311 JitLoadVertices(fetchState
, streams
, vIndices2
, GEP(pVtxOut
, C(1)));
315 #if USE_SIMD16_GATHERS
316 JitGatherVertices(fetchState
, streams
, vIndices
, vIndices2
, pVtxOut
, false);
318 JitGatherVertices(fetchState
, streams
, vIndices
, pVtxOut
, false);
319 JitGatherVertices(fetchState
, streams
, vIndices2
, GEP(pVtxOut
, C(1)), true);
323 (fetchState
.bDisableVGATHER
) ? JitLoadVertices(fetchState
, streams
, vIndices
, pVtxOut
)
324 : JitGatherVertices(fetchState
, streams
, vIndices
, pVtxOut
);
329 JitManager::DumpToFile(fetch
, "src");
332 verifyFunction(*fetch
);
335 ::FunctionPassManager
setupPasses(JM()->mpCurrentModule
);
337 ///@todo We don't need the CFG passes for fetch. (e.g. BreakCriticalEdges and CFGSimplification)
338 setupPasses
.add(createBreakCriticalEdgesPass());
339 setupPasses
.add(createCFGSimplificationPass());
340 setupPasses
.add(createEarlyCSEPass());
341 setupPasses
.add(createPromoteMemoryToRegisterPass());
343 setupPasses
.run(*fetch
);
345 JitManager::DumpToFile(fetch
, "se");
347 ::FunctionPassManager
optPasses(JM()->mpCurrentModule
);
349 ///@todo Haven't touched these either. Need to remove some of these and add others.
350 optPasses
.add(createCFGSimplificationPass());
351 optPasses
.add(createEarlyCSEPass());
352 optPasses
.add(createInstructionCombiningPass());
353 optPasses
.add(createInstructionSimplifierPass());
354 optPasses
.add(createConstantPropagationPass());
355 optPasses
.add(createSCCPPass());
356 optPasses
.add(createAggressiveDCEPass());
358 optPasses
.run(*fetch
);
359 optPasses
.run(*fetch
);
361 JitManager::DumpToFile(fetch
, "opt");
367 //////////////////////////////////////////////////////////////////////////
368 /// @brief Loads attributes from memory using LOADs, shuffling the
369 /// components into SOA form.
370 /// *Note* currently does not support component control,
371 /// component packing, instancing
372 /// @param fetchState - info about attributes to be fetched from memory
373 /// @param streams - value pointer to the current vertex stream
374 /// @param vIndices - vector value of indices to load
375 /// @param pVtxOut - value pointer to output simdvertex struct
376 void FetchJit::JitLoadVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
* streams
, Value
* vIndices
, Value
* pVtxOut
)
378 // Zack shuffles; a variant of the Charleston.
380 std::vector
<Value
*> vectors(16);
381 std::vector
<Constant
*> pMask(mVWidth
);
382 for(uint32_t i
= 0; i
< mVWidth
; ++i
)
384 pMask
[i
] = (C(i
< 4 ? i
: 4));
386 Constant
* promoteMask
= ConstantVector::get(pMask
);
387 Constant
* uwvec
= UndefValue::get(VectorType::get(mFP32Ty
, 4));
389 Value
* startVertex
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_StartVertex
});
390 Value
* startInstance
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_StartInstance
});
391 Value
* curInstance
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_CurInstance
});
392 Value
* vBaseVertex
= VBROADCAST(LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_BaseVertex
}));
393 curInstance
->setName("curInstance");
395 for(uint32_t nelt
= 0; nelt
< fetchState
.numAttribs
; ++nelt
)
397 Value
* elements
[4] = {0};
398 const INPUT_ELEMENT_DESC
& ied
= fetchState
.layout
[nelt
];
399 const SWR_FORMAT_INFO
&info
= GetFormatInfo((SWR_FORMAT
)ied
.Format
);
400 SWR_ASSERT((info
.bpp
!= 0), "Unsupported format in JitLoadVertices.");
401 uint32_t numComponents
= info
.numComps
;
402 uint32_t bpc
= info
.bpp
/ info
.numComps
; ///@todo Code below assumes all components are same size. Need to fix.
404 // load path doesn't support component packing
405 SWR_ASSERT(ied
.ComponentPacking
== ComponentEnable::XYZW
, "Fetch load path doesn't support component packing.");
409 if (fetchState
.bInstanceIDOffsetEnable
)
411 SWR_ASSERT((0), "TODO: Fill out more once driver sends this down");
416 if(ied
.InstanceEnable
)
418 Value
* stepRate
= C(ied
.InstanceAdvancementState
);
420 // prevent a div by 0 for 0 step rate
421 Value
* isNonZeroStep
= ICMP_UGT(stepRate
, C(0));
422 stepRate
= SELECT(isNonZeroStep
, stepRate
, C(1));
424 // calc the current offset into instanced data buffer
425 Value
* calcInstance
= UDIV(curInstance
, stepRate
);
427 // if step rate is 0, every instance gets instance 0
428 calcInstance
= SELECT(isNonZeroStep
, calcInstance
, C(0));
430 vCurIndices
= VBROADCAST(calcInstance
);
432 startOffset
= startInstance
;
434 else if (ied
.InstanceStrideEnable
)
436 SWR_ASSERT((0), "TODO: Fill out more once driver sends this down.");
440 // offset indices by baseVertex
441 vCurIndices
= ADD(vIndices
, vBaseVertex
);
443 startOffset
= startVertex
;
446 // load SWR_VERTEX_BUFFER_STATE::pData
447 Value
*stream
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_pData
});
449 // load SWR_VERTEX_BUFFER_STATE::pitch
450 Value
*stride
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_pitch
});
451 stride
= Z_EXT(stride
, mInt64Ty
);
453 // load SWR_VERTEX_BUFFER_STATE::size
454 Value
*size
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_size
});
455 size
= Z_EXT(size
, mInt64Ty
);
457 Value
* startVertexOffset
= MUL(Z_EXT(startOffset
, mInt64Ty
), stride
);
459 Value
*minVertex
= NULL
;
460 Value
*minVertexOffset
= NULL
;
461 if (fetchState
.bPartialVertexBuffer
) {
462 // fetch min index for low bounds checking
463 minVertex
= GEP(streams
, {C(ied
.StreamIndex
), C(SWR_VERTEX_BUFFER_STATE_minVertex
)});
464 minVertex
= LOAD(minVertex
);
465 if (!fetchState
.bDisableIndexOOBCheck
) {
466 minVertexOffset
= MUL(Z_EXT(minVertex
, mInt64Ty
), stride
);
470 // Load from the stream.
471 for(uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
474 Value
* index
= VEXTRACT(vCurIndices
, C(lane
));
476 if (fetchState
.bPartialVertexBuffer
) {
477 // clamp below minvertex
478 Value
*isBelowMin
= ICMP_SLT(index
, minVertex
);
479 index
= SELECT(isBelowMin
, minVertex
, index
);
482 index
= Z_EXT(index
, mInt64Ty
);
484 Value
* offset
= MUL(index
, stride
);
485 offset
= ADD(offset
, C((int64_t)ied
.AlignedByteOffset
));
486 offset
= ADD(offset
, startVertexOffset
);
488 if (!fetchState
.bDisableIndexOOBCheck
) {
489 // check for out of bound access, including partial OOB, and replace them with minVertex
490 Value
*endOffset
= ADD(offset
, C((int64_t)info
.Bpp
));
491 Value
*oob
= ICMP_ULE(endOffset
, size
);
492 if (fetchState
.bPartialVertexBuffer
) {
493 offset
= SELECT(oob
, offset
, minVertexOffset
);
495 offset
= SELECT(oob
, offset
, ConstantInt::get(mInt64Ty
, 0));
499 Value
* pointer
= GEP(stream
, offset
);
500 // We use a full-lane, but don't actually care.
503 // get a pointer to a 4 component attrib in default address space
506 case 8: vptr
= BITCAST(pointer
, PointerType::get(VectorType::get(mInt8Ty
, 4), 0)); break;
507 case 16: vptr
= BITCAST(pointer
, PointerType::get(VectorType::get(mInt16Ty
, 4), 0)); break;
508 case 32: vptr
= BITCAST(pointer
, PointerType::get(VectorType::get(mFP32Ty
, 4), 0)); break;
509 default: SWR_INVALID("Unsupported underlying bpp!");
512 // load 4 components of attribute
513 Value
* vec
= ALIGNED_LOAD(vptr
, 1, false);
515 // Convert To FP32 internally
522 vec
= UI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
523 vec
= FMUL(vec
, ConstantVector::get(std::vector
<Constant
*>(4, ConstantFP::get(mFP32Ty
, 1.0 / 255.0))));
526 vec
= UI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
527 vec
= FMUL(vec
, ConstantVector::get(std::vector
<Constant
*>(4, ConstantFP::get(mFP32Ty
, 1.0 / 65535.0))));
530 SWR_INVALID("Unsupported underlying type!");
538 vec
= SI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
539 vec
= FMUL(vec
, ConstantVector::get(std::vector
<Constant
*>(4, ConstantFP::get(mFP32Ty
, 1.0 / 128.0))));
542 vec
= SI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
543 vec
= FMUL(vec
, ConstantVector::get(std::vector
<Constant
*>(4, ConstantFP::get(mFP32Ty
, 1.0 / 32768.0))));
546 SWR_INVALID("Unsupported underlying type!");
551 // Zero extend uint32_t types.
556 vec
= Z_EXT(vec
, VectorType::get(mInt32Ty
, 4));
557 vec
= BITCAST(vec
, VectorType::get(mFP32Ty
, 4));
560 break; // Pass through unchanged.
562 SWR_INVALID("Unsupported underlying type!");
567 // Sign extend SINT types.
572 vec
= S_EXT(vec
, VectorType::get(mInt32Ty
, 4));
573 vec
= BITCAST(vec
, VectorType::get(mFP32Ty
, 4));
576 break; // Pass through unchanged.
578 SWR_INVALID("Unsupported underlying type!");
586 break; // Pass through unchanged.
588 SWR_INVALID("Unsupported underlying type!");
591 case SWR_TYPE_USCALED
:
592 vec
= UI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
594 case SWR_TYPE_SSCALED
:
595 vec
= SI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
597 case SWR_TYPE_SFIXED
:
598 vec
= FMUL(SI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4)), VBROADCAST(C(1/65536.0f
)));
600 case SWR_TYPE_UNKNOWN
:
601 case SWR_TYPE_UNUSED
:
602 SWR_INVALID("Unsupported type %d!", info
.type
[0]);
605 // promote mask: sse(0,1,2,3) | avx(0,1,2,3,4,4,4,4)
606 // uwvec: 4 x F32, undef value
607 Value
* wvec
= VSHUFFLE(vec
, uwvec
, promoteMask
);
608 vectors
.push_back(wvec
);
611 std::vector
<Constant
*> v01Mask(mVWidth
);
612 std::vector
<Constant
*> v23Mask(mVWidth
);
613 std::vector
<Constant
*> v02Mask(mVWidth
);
614 std::vector
<Constant
*> v13Mask(mVWidth
);
616 // Concatenate the vectors together.
617 elements
[0] = VUNDEF_F();
618 elements
[1] = VUNDEF_F();
619 elements
[2] = VUNDEF_F();
620 elements
[3] = VUNDEF_F();
621 for(uint32_t b
= 0, num4Wide
= mVWidth
/ 4; b
< num4Wide
; ++b
)
623 v01Mask
[4 * b
+ 0] = C(0 + 4 * b
);
624 v01Mask
[4 * b
+ 1] = C(1 + 4 * b
);
625 v01Mask
[4 * b
+ 2] = C(0 + 4 * b
+ mVWidth
);
626 v01Mask
[4 * b
+ 3] = C(1 + 4 * b
+ mVWidth
);
628 v23Mask
[4 * b
+ 0] = C(2 + 4 * b
);
629 v23Mask
[4 * b
+ 1] = C(3 + 4 * b
);
630 v23Mask
[4 * b
+ 2] = C(2 + 4 * b
+ mVWidth
);
631 v23Mask
[4 * b
+ 3] = C(3 + 4 * b
+ mVWidth
);
633 v02Mask
[4 * b
+ 0] = C(0 + 4 * b
);
634 v02Mask
[4 * b
+ 1] = C(2 + 4 * b
);
635 v02Mask
[4 * b
+ 2] = C(0 + 4 * b
+ mVWidth
);
636 v02Mask
[4 * b
+ 3] = C(2 + 4 * b
+ mVWidth
);
638 v13Mask
[4 * b
+ 0] = C(1 + 4 * b
);
639 v13Mask
[4 * b
+ 1] = C(3 + 4 * b
);
640 v13Mask
[4 * b
+ 2] = C(1 + 4 * b
+ mVWidth
);
641 v13Mask
[4 * b
+ 3] = C(3 + 4 * b
+ mVWidth
);
643 std::vector
<Constant
*> iMask(mVWidth
);
644 for(uint32_t i
= 0; i
< mVWidth
; ++i
)
646 if(((4 * b
) <= i
) && (i
< (4 * (b
+ 1))))
648 iMask
[i
] = C(i
% 4 + mVWidth
);
655 Constant
* insertMask
= ConstantVector::get(iMask
);
656 elements
[0] = VSHUFFLE(elements
[0], vectors
[4 * b
+ 0], insertMask
);
657 elements
[1] = VSHUFFLE(elements
[1], vectors
[4 * b
+ 1], insertMask
);
658 elements
[2] = VSHUFFLE(elements
[2], vectors
[4 * b
+ 2], insertMask
);
659 elements
[3] = VSHUFFLE(elements
[3], vectors
[4 * b
+ 3], insertMask
);
662 Value
* x0y0x1y1
= VSHUFFLE(elements
[0], elements
[1], ConstantVector::get(v01Mask
));
663 Value
* x2y2x3y3
= VSHUFFLE(elements
[2], elements
[3], ConstantVector::get(v01Mask
));
664 Value
* z0w0z1w1
= VSHUFFLE(elements
[0], elements
[1], ConstantVector::get(v23Mask
));
665 Value
* z2w3z2w3
= VSHUFFLE(elements
[2], elements
[3], ConstantVector::get(v23Mask
));
666 elements
[0] = VSHUFFLE(x0y0x1y1
, x2y2x3y3
, ConstantVector::get(v02Mask
));
667 elements
[1] = VSHUFFLE(x0y0x1y1
, x2y2x3y3
, ConstantVector::get(v13Mask
));
668 elements
[2] = VSHUFFLE(z0w0z1w1
, z2w3z2w3
, ConstantVector::get(v02Mask
));
669 elements
[3] = VSHUFFLE(z0w0z1w1
, z2w3z2w3
, ConstantVector::get(v13Mask
));
671 switch(numComponents
+ 1)
673 case 1: elements
[0] = VIMMED1(0.0f
);
674 case 2: elements
[1] = VIMMED1(0.0f
);
675 case 3: elements
[2] = VIMMED1(0.0f
);
676 case 4: elements
[3] = VIMMED1(1.0f
);
679 for(uint32_t c
= 0; c
< 4; ++c
)
681 #if USE_SIMD16_SHADERS
682 Value
* dest
= GEP(pVtxOut
, C(nelt
* 8 + c
* 2), "destGEP");
684 Value
* dest
= GEP(pVtxOut
, C(nelt
* 4 + c
), "destGEP");
686 STORE(elements
[c
], dest
);
691 // returns true for odd formats that require special state.gather handling
692 bool FetchJit::IsOddFormat(SWR_FORMAT format
)
694 const SWR_FORMAT_INFO
& info
= GetFormatInfo(format
);
695 if (info
.bpc
[0] != 8 && info
.bpc
[0] != 16 && info
.bpc
[0] != 32 && info
.bpc
[0] != 64)
702 // format is uniform if all components are the same size and type
703 bool FetchJit::IsUniformFormat(SWR_FORMAT format
)
705 const SWR_FORMAT_INFO
& info
= GetFormatInfo(format
);
706 uint32_t bpc0
= info
.bpc
[0];
707 uint32_t type0
= info
.type
[0];
709 for (uint32_t c
= 1; c
< info
.numComps
; ++c
)
711 if (bpc0
!= info
.bpc
[c
] || type0
!= info
.type
[c
])
719 // unpacks components based on format
720 // foreach component in the pixel
721 // mask off everything but this component
722 // shift component to LSB
723 void FetchJit::UnpackComponents(SWR_FORMAT format
, Value
* vInput
, Value
* result
[4])
725 const SWR_FORMAT_INFO
& info
= GetFormatInfo(format
);
727 uint32_t bitOffset
= 0;
728 for (uint32_t c
= 0; c
< info
.numComps
; ++c
)
730 uint32_t swizzledIndex
= info
.swizzle
[c
];
731 uint32_t compBits
= info
.bpc
[c
];
732 uint32_t bitmask
= ((1 << compBits
) - 1) << bitOffset
;
733 Value
* comp
= AND(vInput
, bitmask
);
734 comp
= LSHR(comp
, bitOffset
);
736 result
[swizzledIndex
] = comp
;
737 bitOffset
+= compBits
;
741 // gather for odd component size formats
742 // gather SIMD full pixels per lane then shift/mask to move each component to their
744 void FetchJit::CreateGatherOddFormats(SWR_FORMAT format
, Value
* pMask
, Value
* pBase
, Value
* pOffsets
, Value
* pResult
[4])
746 const SWR_FORMAT_INFO
&info
= GetFormatInfo(format
);
748 // only works if pixel size is <= 32bits
749 SWR_ASSERT(info
.bpp
<= 32);
754 pGather
= GATHERDD(VIMMED1(0), pBase
, pOffsets
, pMask
);
758 // Can't use 32-bit gather for items less than 32-bits, could cause page faults.
759 Value
*pMem
= ALLOCA(mSimdInt32Ty
);
760 STORE(VIMMED1(0u), pMem
);
762 pBase
= BITCAST(pBase
, PointerType::get(mInt8Ty
, 0));
763 Value
* pDstMem
= BITCAST(pMem
, mInt32PtrTy
);
765 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
768 Value
* index
= VEXTRACT(pOffsets
, C(lane
));
769 Value
* mask
= VEXTRACT(pMask
, C(lane
));
774 Value
* pDst
= BITCAST(GEP(pDstMem
, C(lane
)), PointerType::get(mInt8Ty
, 0));
775 Value
* pSrc
= BITCAST(GEP(pBase
, index
), PointerType::get(mInt8Ty
, 0));
776 STORE(LOAD(SELECT(mask
, pSrc
, pDst
)), pDst
);
782 Value
* pDst
= BITCAST(GEP(pDstMem
, C(lane
)), PointerType::get(mInt16Ty
, 0));
783 Value
* pSrc
= BITCAST(GEP(pBase
, index
), PointerType::get(mInt16Ty
, 0));
784 STORE(LOAD(SELECT(mask
, pSrc
, pDst
)), pDst
);
791 // First 16-bits of data
792 Value
* pDst
= BITCAST(GEP(pDstMem
, C(lane
)), PointerType::get(mInt16Ty
, 0));
793 Value
* pSrc
= BITCAST(GEP(pBase
, index
), PointerType::get(mInt16Ty
, 0));
794 STORE(LOAD(SELECT(mask
, pSrc
, pDst
)), pDst
);
796 // Last 8-bits of data
797 pDst
= BITCAST(GEP(pDst
, C(1)), PointerType::get(mInt8Ty
, 0));
798 pSrc
= BITCAST(GEP(pSrc
, C(1)), PointerType::get(mInt8Ty
, 0));
799 STORE(LOAD(SELECT(mask
, pSrc
, pDst
)), pDst
);
804 SWR_INVALID("Shouldn't have BPP = %d now", info
.bpp
);
809 pGather
= LOAD(pMem
);
812 for (uint32_t comp
= 0; comp
< 4; ++comp
)
814 pResult
[comp
] = VIMMED1((int)info
.defaults
[comp
]);
817 UnpackComponents(format
, pGather
, pResult
);
820 pResult
[0] = BITCAST(pResult
[0], mSimdFP32Ty
);
821 pResult
[1] = BITCAST(pResult
[1], mSimdFP32Ty
);
822 pResult
[2] = BITCAST(pResult
[2], mSimdFP32Ty
);
823 pResult
[3] = BITCAST(pResult
[3], mSimdFP32Ty
);
826 void FetchJit::ConvertFormat(SWR_FORMAT format
, Value
*texels
[4])
828 const SWR_FORMAT_INFO
&info
= GetFormatInfo(format
);
830 for (uint32_t c
= 0; c
< info
.numComps
; ++c
)
832 uint32_t compIndex
= info
.swizzle
[c
];
834 // skip any conversion on UNUSED components
835 if (info
.type
[c
] == SWR_TYPE_UNUSED
)
840 if (info
.isNormalized
[c
])
842 if (info
.type
[c
] == SWR_TYPE_SNORM
)
844 /// @todo The most-negative value maps to -1.0f. e.g. the 5-bit value 10000 maps to -1.0f.
846 /// result = c * (1.0f / (2^(n-1) - 1);
847 uint32_t n
= info
.bpc
[c
];
848 uint32_t pow2
= 1 << (n
- 1);
849 float scale
= 1.0f
/ (float)(pow2
- 1);
850 Value
*vScale
= VIMMED1(scale
);
851 texels
[compIndex
] = BITCAST(texels
[compIndex
], mSimdInt32Ty
);
852 texels
[compIndex
] = SI_TO_FP(texels
[compIndex
], mSimdFP32Ty
);
853 texels
[compIndex
] = FMUL(texels
[compIndex
], vScale
);
857 SWR_ASSERT(info
.type
[c
] == SWR_TYPE_UNORM
);
859 /// result = c * (1.0f / (2^n - 1))
860 uint32_t n
= info
.bpc
[c
];
861 uint32_t pow2
= 1 << n
;
862 // special case 24bit unorm format, which requires a full divide to meet ULP requirement
865 float scale
= (float)(pow2
- 1);
866 Value
* vScale
= VIMMED1(scale
);
867 texels
[compIndex
] = BITCAST(texels
[compIndex
], mSimdInt32Ty
);
868 texels
[compIndex
] = SI_TO_FP(texels
[compIndex
], mSimdFP32Ty
);
869 texels
[compIndex
] = FDIV(texels
[compIndex
], vScale
);
873 float scale
= 1.0f
/ (float)(pow2
- 1);
874 Value
*vScale
= VIMMED1(scale
);
875 texels
[compIndex
] = BITCAST(texels
[compIndex
], mSimdInt32Ty
);
876 texels
[compIndex
] = UI_TO_FP(texels
[compIndex
], mSimdFP32Ty
);
877 texels
[compIndex
] = FMUL(texels
[compIndex
], vScale
);
885 //////////////////////////////////////////////////////////////////////////
886 /// @brief Loads attributes from memory using AVX2 GATHER(s)
887 /// @param fetchState - info about attributes to be fetched from memory
888 /// @param streams - value pointer to the current vertex stream
889 /// @param vIndices - vector value of indices to gather
890 /// @param pVtxOut - value pointer to output simdvertex struct
891 #if USE_SIMD16_SHADERS
892 #if USE_SIMD16_GATHERS
893 void FetchJit::JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
,
894 Value
*streams
, Value
*vIndices
, Value
*vIndices2
, Value
*pVtxOut
, bool useVertexID2
)
896 void FetchJit::JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
,
897 Value
* streams
, Value
* vIndices
, Value
* pVtxOut
, bool useVertexID2
)
900 void FetchJit::JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
,
901 Value
* streams
, Value
* vIndices
, Value
* pVtxOut
)
904 uint32_t currentVertexElement
= 0;
905 uint32_t outputElt
= 0;
906 Value
* vVertexElements
[4];
907 #if USE_SIMD16_GATHERS
911 Value
* startVertex
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_StartVertex
});
912 Value
* startInstance
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_StartInstance
});
913 Value
* curInstance
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_CurInstance
});
914 #if USE_SIMD16_GATHERS
915 Value
* vBaseVertex16
= VBROADCAST_16(LOAD(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_BaseVertex
}));
917 Value
* vBaseVertex
= VBROADCAST(LOAD(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_BaseVertex
}));
919 curInstance
->setName("curInstance");
921 for (uint32_t nInputElt
= 0; nInputElt
< fetchState
.numAttribs
; nInputElt
+= 1)
923 const INPUT_ELEMENT_DESC
& ied
= fetchState
.layout
[nInputElt
];
925 // skip element if all components are disabled
926 if (ied
.ComponentPacking
== ComponentEnable::NONE
)
931 const SWR_FORMAT_INFO
&info
= GetFormatInfo((SWR_FORMAT
)ied
.Format
);
932 SWR_ASSERT((info
.bpp
!= 0), "Unsupported format in JitGatherVertices.");
933 uint32_t bpc
= info
.bpp
/ info
.numComps
; ///@todo Code below assumes all components are same size. Need to fix.
935 Value
*stream
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_pData
});
937 // VGATHER* takes an *i8 src pointer
938 Value
*pStreamBase
= BITCAST(stream
, PointerType::get(mInt8Ty
, 0));
940 Value
*stride
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_pitch
});
941 #if USE_SIMD16_GATHERS
942 Value
*vStride16
= VBROADCAST_16(stride
);
944 Value
*vStride
= VBROADCAST(stride
);
947 // max vertex index that is fully in bounds
948 Value
*maxVertex
= GEP(streams
, {C(ied
.StreamIndex
), C(SWR_VERTEX_BUFFER_STATE_maxVertex
)});
949 maxVertex
= LOAD(maxVertex
);
951 Value
*minVertex
= NULL
;
952 if (fetchState
.bPartialVertexBuffer
)
954 // min vertex index for low bounds OOB checking
955 minVertex
= GEP(streams
, {C(ied
.StreamIndex
), C(SWR_VERTEX_BUFFER_STATE_minVertex
)});
956 minVertex
= LOAD(minVertex
);
959 if (fetchState
.bInstanceIDOffsetEnable
)
961 // the InstanceID (curInstance) value is offset by StartInstanceLocation
962 curInstance
= ADD(curInstance
, startInstance
);
965 #if USE_SIMD16_GATHERS
966 Value
*vCurIndices16
;
971 #if USE_SIMD16_GATHERS
972 Value
*vInstanceStride16
= VIMMED1_16(0);
974 Value
*vInstanceStride
= VIMMED1(0);
977 if (ied
.InstanceEnable
)
979 Value
* stepRate
= C(ied
.InstanceAdvancementState
);
981 // prevent a div by 0 for 0 step rate
982 Value
* isNonZeroStep
= ICMP_UGT(stepRate
, C(0));
983 stepRate
= SELECT(isNonZeroStep
, stepRate
, C(1));
985 // calc the current offset into instanced data buffer
986 Value
* calcInstance
= UDIV(curInstance
, stepRate
);
988 // if step rate is 0, every instance gets instance 0
989 calcInstance
= SELECT(isNonZeroStep
, calcInstance
, C(0));
991 #if USE_SIMD16_GATHERS
992 vCurIndices16
= VBROADCAST_16(calcInstance
);
994 vCurIndices
= VBROADCAST(calcInstance
);
997 startOffset
= startInstance
;
999 else if (ied
.InstanceStrideEnable
)
1001 // grab the instance advancement state, determines stride in bytes from one instance to the next
1002 Value
* stepRate
= C(ied
.InstanceAdvancementState
);
1003 #if USE_SIMD16_GATHERS
1004 vInstanceStride16
= VBROADCAST_16(MUL(curInstance
, stepRate
));
1006 vInstanceStride
= VBROADCAST(MUL(curInstance
, stepRate
));
1009 // offset indices by baseVertex
1010 #if USE_SIMD16_GATHERS
1011 Value
*vIndices16
= JOIN_16(vIndices
, vIndices2
);
1013 vCurIndices16
= ADD(vIndices16
, vBaseVertex16
);
1015 vCurIndices
= ADD(vIndices
, vBaseVertex
);
1018 startOffset
= startVertex
;
1019 SWR_ASSERT((0), "TODO: Fill out more once driver sends this down.");
1023 // offset indices by baseVertex
1024 #if USE_SIMD16_GATHERS
1025 Value
*vIndices16
= JOIN_16(vIndices
, vIndices2
);
1027 vCurIndices16
= ADD(vIndices16
, vBaseVertex16
);
1029 vCurIndices
= ADD(vIndices
, vBaseVertex
);
1032 startOffset
= startVertex
;
1035 // All of the OOB calculations are in vertices, not VB offsets, to prevent having to
1036 // do 64bit address offset calculations.
1038 // calculate byte offset to the start of the VB
1039 Value
* baseOffset
= MUL(Z_EXT(startOffset
, mInt64Ty
), Z_EXT(stride
, mInt64Ty
));
1040 pStreamBase
= GEP(pStreamBase
, baseOffset
);
1042 // if we have a start offset, subtract from max vertex. Used for OOB check
1043 maxVertex
= SUB(Z_EXT(maxVertex
, mInt64Ty
), Z_EXT(startOffset
, mInt64Ty
));
1044 Value
* maxNeg
= ICMP_SLT(maxVertex
, C((int64_t)0));
1045 // if we have a negative value, we're already OOB. clamp at 0.
1046 maxVertex
= SELECT(maxNeg
, C(0), TRUNC(maxVertex
, mInt32Ty
));
1048 if (fetchState
.bPartialVertexBuffer
)
1050 // similary for min vertex
1051 minVertex
= SUB(Z_EXT(minVertex
, mInt64Ty
), Z_EXT(startOffset
, mInt64Ty
));
1052 Value
*minNeg
= ICMP_SLT(minVertex
, C((int64_t)0));
1053 minVertex
= SELECT(minNeg
, C(0), TRUNC(minVertex
, mInt32Ty
));
1056 // Load the in bounds size of a partially valid vertex
1057 Value
*partialInboundsSize
= GEP(streams
, {C(ied
.StreamIndex
), C(SWR_VERTEX_BUFFER_STATE_partialInboundsSize
)});
1058 partialInboundsSize
= LOAD(partialInboundsSize
);
1059 #if USE_SIMD16_GATHERS
1060 Value
*vPartialVertexSize
= VBROADCAST_16(partialInboundsSize
);
1061 Value
*vBpp
= VBROADCAST_16(C(info
.Bpp
));
1062 Value
*vAlignmentOffsets
= VBROADCAST_16(C(ied
.AlignedByteOffset
));
1064 Value
*vPartialVertexSize
= VBROADCAST(partialInboundsSize
);
1065 Value
*vBpp
= VBROADCAST(C(info
.Bpp
));
1066 Value
*vAlignmentOffsets
= VBROADCAST(C(ied
.AlignedByteOffset
));
1069 // is the element is <= the partially valid size
1070 Value
*vElementInBoundsMask
= ICMP_SLE(vBpp
, SUB(vPartialVertexSize
, vAlignmentOffsets
));
1072 #if USE_SIMD16_GATHERS
1073 // override cur indices with 0 if pitch is 0
1074 Value
*pZeroPitchMask16
= ICMP_EQ(vStride16
, VIMMED1_16(0));
1075 vCurIndices16
= SELECT(pZeroPitchMask16
, VIMMED1_16(0), vCurIndices16
);
1077 // are vertices partially OOB?
1078 Value
*vMaxVertex16
= VBROADCAST_16(maxVertex
);
1079 Value
*vPartialOOBMask
= ICMP_EQ(vCurIndices16
, vMaxVertex16
);
1081 // are vertices fully in bounds?
1082 Value
*vMaxGatherMask16
= ICMP_ULT(vCurIndices16
, vMaxVertex16
);
1084 Value
*vGatherMask16
;
1086 if (fetchState
.bPartialVertexBuffer
)
1088 // are vertices below minVertex limit?
1089 Value
*vMinVertex16
= VBROADCAST_16(minVertex
);
1090 Value
*vMinGatherMask16
= ICMP_UGE(vCurIndices16
, vMinVertex16
);
1092 // only fetch lanes that pass both tests
1093 vGatherMask16
= AND(vMaxGatherMask16
, vMinGatherMask16
);
1097 vGatherMask16
= vMaxGatherMask16
;
1100 // blend in any partially OOB indices that have valid elements
1101 vGatherMask16
= SELECT(vPartialOOBMask
, vElementInBoundsMask
, vGatherMask16
);
1103 // calculate the actual offsets into the VB
1104 Value
*vOffsets16
= MUL(vCurIndices16
, vStride16
);
1105 vOffsets16
= ADD(vOffsets16
, vAlignmentOffsets
);
1107 // if instance stride enable is:
1108 // true - add product of the instanceID and advancement state to the offst into the VB
1109 // false - value of vInstanceStride has been initialialized to zero
1110 vOffsets16
= ADD(vOffsets16
, vInstanceStride16
);
1112 // TODO: remove the following simd8 interop stuff once all code paths are fully widened to SIMD16..
1114 Value
*vGatherMask
= EXTRACT_16(vGatherMask16
, 0);
1115 Value
*vGatherMask2
= EXTRACT_16(vGatherMask16
, 1);
1117 Value
*vOffsets
= EXTRACT_16(vOffsets16
, 0);
1118 Value
*vOffsets2
= EXTRACT_16(vOffsets16
, 1);
1120 // override cur indices with 0 if pitch is 0
1121 Value
* pZeroPitchMask
= ICMP_EQ(vStride
, VIMMED1(0));
1122 vCurIndices
= SELECT(pZeroPitchMask
, VIMMED1(0), vCurIndices
);
1124 // are vertices partially OOB?
1125 Value
* vMaxVertex
= VBROADCAST(maxVertex
);
1126 Value
* vPartialOOBMask
= ICMP_EQ(vCurIndices
, vMaxVertex
);
1128 // are vertices fully in bounds?
1129 Value
* vMaxGatherMask
= ICMP_ULT(vCurIndices
, vMaxVertex
);
1132 if (fetchState
.bPartialVertexBuffer
)
1134 // are vertices below minVertex limit?
1135 Value
*vMinVertex
= VBROADCAST(minVertex
);
1136 Value
*vMinGatherMask
= ICMP_UGE(vCurIndices
, vMinVertex
);
1138 // only fetch lanes that pass both tests
1139 vGatherMask
= AND(vMaxGatherMask
, vMinGatherMask
);
1143 vGatherMask
= vMaxGatherMask
;
1146 // blend in any partially OOB indices that have valid elements
1147 vGatherMask
= SELECT(vPartialOOBMask
, vElementInBoundsMask
, vGatherMask
);
1149 // calculate the actual offsets into the VB
1150 Value
* vOffsets
= MUL(vCurIndices
, vStride
);
1151 vOffsets
= ADD(vOffsets
, vAlignmentOffsets
);
1153 // if instance stride enable is:
1154 // true - add product of the instanceID and advancement state to the offst into the VB
1155 // false - value of vInstanceStride has been initialialized to zero
1156 vOffsets
= ADD(vOffsets
, vInstanceStride
);
1159 // Packing and component control
1160 ComponentEnable compMask
= (ComponentEnable
)ied
.ComponentPacking
;
1161 const ComponentControl compCtrl
[4] { (ComponentControl
)ied
.ComponentControl0
, (ComponentControl
)ied
.ComponentControl1
,
1162 (ComponentControl
)ied
.ComponentControl2
, (ComponentControl
)ied
.ComponentControl3
};
1164 // Special gather/conversion for formats without equal component sizes
1165 if (IsOddFormat((SWR_FORMAT
)ied
.Format
))
1167 #if USE_SIMD16_GATHERS
1169 Value
*pResults2
[4];
1170 CreateGatherOddFormats((SWR_FORMAT
)ied
.Format
, vGatherMask
, pStreamBase
, vOffsets
, pResults
);
1171 CreateGatherOddFormats((SWR_FORMAT
)ied
.Format
, vGatherMask2
, pStreamBase
, vOffsets2
, pResults2
);
1172 ConvertFormat((SWR_FORMAT
)ied
.Format
, pResults
);
1173 ConvertFormat((SWR_FORMAT
)ied
.Format
, pResults2
);
1175 for (uint32_t c
= 0; c
< 4; c
+= 1)
1177 if (isComponentEnabled(compMask
, c
))
1179 // pack adjacent pairs of SIMD8s into SIMD16s
1180 pVtxSrc2
[currentVertexElement
++] = JOIN_16(pResults
[c
], pResults2
[c
]);
1182 if (currentVertexElement
> 3)
1185 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
1187 StoreVertexElements16(pVtxOut2
, outputElt
++, 4, pVtxSrc2
);
1188 // reset to the next vVertexElement to output
1189 currentVertexElement
= 0;
1195 CreateGatherOddFormats((SWR_FORMAT
)ied
.Format
, vGatherMask
, pStreamBase
, vOffsets
, pResults
);
1196 ConvertFormat((SWR_FORMAT
)ied
.Format
, pResults
);
1198 for (uint32_t c
= 0; c
< 4; c
+= 1)
1200 if (isComponentEnabled(compMask
, c
))
1202 vVertexElements
[currentVertexElement
++] = pResults
[c
];
1203 if (currentVertexElement
> 3)
1205 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
1206 // reset to the next vVertexElement to output
1207 currentVertexElement
= 0;
1213 else if(info
.type
[0] == SWR_TYPE_FLOAT
)
1215 ///@todo: support 64 bit vb accesses
1216 Value
*gatherSrc
= VIMMED1(0.0f
);
1217 #if USE_SIMD16_GATHERS
1218 Value
*gatherSrc16
= VIMMED1_16(0.0f
);
1221 SWR_ASSERT(IsUniformFormat((SWR_FORMAT
)ied
.Format
),
1222 "Unsupported format for standard gather fetch.");
1224 // Gather components from memory to store in a simdvertex structure
1229 #if USE_SIMD16_GATHERS
1230 Value
*gatherResult
[2];
1232 // if we have at least one component out of x or y to fetch
1233 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
1235 gatherResult
[0] = GATHERPS_16(gatherSrc16
, pStreamBase
, vOffsets16
, vGatherMask16
);
1237 // e.g. result of first 8x32bit integer gather for 16bit components
1238 // 256i - 0 1 2 3 4 5 6 7
1239 // xyxy xyxy xyxy xyxy xyxy xyxy xyxy xyxy
1244 gatherResult
[0] = VUNDEF_I_16();
1247 // if we have at least one component out of z or w to fetch
1248 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
1250 // offset base to the next components(zw) in the vertex to gather
1251 pStreamBase
= GEP(pStreamBase
, C((char)4));
1253 gatherResult
[1] = GATHERPS_16(gatherSrc16
, pStreamBase
, vOffsets16
, vGatherMask16
);
1255 // e.g. result of second 8x32bit integer gather for 16bit components
1256 // 256i - 0 1 2 3 4 5 6 7
1257 // zwzw zwzw zwzw zwzw zwzw zwzw zwzw zwzw
1262 gatherResult
[1] = VUNDEF_I_16();
1265 // if we have at least one component to shuffle into place
1268 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
1270 Shuffle16bpcArgs args
= std::forward_as_tuple(gatherResult
, pVtxOut2
, Instruction::CastOps::FPExt
, CONVERT_NONE
,
1271 currentVertexElement
, outputElt
, compMask
, compCtrl
, pVtxSrc2
);
1273 // Shuffle gathered components into place in simdvertex struct
1274 Shuffle16bpcGather16(args
); // outputs to vVertexElements ref
1277 Value
*vGatherResult
[2];
1279 // if we have at least one component out of x or y to fetch
1280 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
1282 vGatherResult
[0] = GATHERPS(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1283 // e.g. result of first 8x32bit integer gather for 16bit components
1284 // 256i - 0 1 2 3 4 5 6 7
1285 // xyxy xyxy xyxy xyxy xyxy xyxy xyxy xyxy
1289 // if we have at least one component out of z or w to fetch
1290 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
1292 // offset base to the next components(zw) in the vertex to gather
1293 pStreamBase
= GEP(pStreamBase
, C((char)4));
1295 vGatherResult
[1] = GATHERPS(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1296 // e.g. result of second 8x32bit integer gather for 16bit components
1297 // 256i - 0 1 2 3 4 5 6 7
1298 // zwzw zwzw zwzw zwzw zwzw zwzw zwzw zwzw
1302 // if we have at least one component to shuffle into place
1305 Shuffle16bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, Instruction::CastOps::FPExt
, CONVERT_NONE
,
1306 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
);
1308 // Shuffle gathered components into place in simdvertex struct
1309 #if USE_SIMD16_SHADERS
1310 Shuffle16bpcGather(args
, useVertexID2
); // outputs to vVertexElements ref
1312 Shuffle16bpcGather(args
); // outputs to vVertexElements ref
1320 for (uint32_t i
= 0; i
< 4; i
+= 1)
1322 #if USE_SIMD16_GATHERS
1323 if (isComponentEnabled(compMask
, i
))
1325 // if we need to gather the component
1326 if (compCtrl
[i
] == StoreSrc
)
1328 // Gather a SIMD of vertices
1329 // APIs allow a 4GB range for offsets
1330 // However, GATHERPS uses signed 32-bit offsets, so only a 2GB range :(
1331 // But, we know that elements must be aligned for FETCH. :)
1332 // Right shift the offset by a bit and then scale by 2 to remove the sign extension.
1333 Value
*shiftedOffsets16
= LSHR(vOffsets16
, 1);
1334 pVtxSrc2
[currentVertexElement
++] = GATHERPS_16(gatherSrc16
, pStreamBase
, shiftedOffsets16
, vGatherMask16
, 2);
1338 pVtxSrc2
[currentVertexElement
++] = GenerateCompCtrlVector16(compCtrl
[i
]);
1341 if (currentVertexElement
> 3)
1344 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
1346 StoreVertexElements16(pVtxOut2
, outputElt
++, 4, pVtxSrc2
);
1347 // reset to the next vVertexElement to output
1348 currentVertexElement
= 0;
1352 // offset base to the next component in the vertex to gather
1353 pStreamBase
= GEP(pStreamBase
, C((char)4));
1355 if (isComponentEnabled(compMask
, i
))
1357 // if we need to gather the component
1358 if (compCtrl
[i
] == StoreSrc
)
1360 // Gather a SIMD of vertices
1361 // APIs allow a 4GB range for offsets
1362 // However, GATHERPS uses signed 32-bit offsets, so only a 2GB range :(
1363 // But, we know that elements must be aligned for FETCH. :)
1364 // Right shift the offset by a bit and then scale by 2 to remove the sign extension.
1365 Value
*vShiftedOffsets
= LSHR(vOffsets
, 1);
1366 vVertexElements
[currentVertexElement
++] = GATHERPS(gatherSrc
, pStreamBase
, vShiftedOffsets
, vGatherMask
, 2);
1370 #if USE_SIMD16_SHADERS
1371 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
1373 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
1377 if (currentVertexElement
> 3)
1379 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
1380 // reset to the next vVertexElement to output
1381 currentVertexElement
= 0;
1385 // offset base to the next component in the vertex to gather
1386 pStreamBase
= GEP(pStreamBase
, C((char)4));
1393 for (uint32_t i
= 0; i
< 4; i
+= 1)
1395 #if USE_SIMD16_GATHERS
1396 if (isComponentEnabled(compMask
, i
))
1398 // if we need to gather the component
1399 if (compCtrl
[i
] == StoreSrc
)
1401 Value
*vMaskLo
= VSHUFFLE(vGatherMask
, VUNDEF(mInt1Ty
, 8), C({ 0, 1, 2, 3 }));
1402 Value
*vMaskLo2
= VSHUFFLE(vGatherMask2
, VUNDEF(mInt1Ty
, 8), C({ 0, 1, 2, 3 }));
1403 Value
*vMaskHi
= VSHUFFLE(vGatherMask
, VUNDEF(mInt1Ty
, 8), C({ 4, 5, 6, 7 }));
1404 Value
*vMaskHi2
= VSHUFFLE(vGatherMask2
, VUNDEF(mInt1Ty
, 8), C({ 4, 5, 6, 7 }));
1406 Value
*vOffsetsLo
= VEXTRACTI128(vOffsets
, C(0));
1407 Value
*vOffsetsLo2
= VEXTRACTI128(vOffsets2
, C(0));
1408 Value
*vOffsetsHi
= VEXTRACTI128(vOffsets
, C(1));
1409 Value
*vOffsetsHi2
= VEXTRACTI128(vOffsets2
, C(1));
1411 Value
*vZeroDouble
= VECTOR_SPLAT(4, ConstantFP::get(IRB()->getDoubleTy(), 0.0f
));
1413 Value
* pGatherLo
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsLo
, vMaskLo
);
1414 Value
* pGatherLo2
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsLo2
, vMaskLo2
);
1415 Value
* pGatherHi
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsHi
, vMaskHi
);
1416 Value
* pGatherHi2
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsHi2
, vMaskHi2
);
1418 pGatherLo
= VCVTPD2PS(pGatherLo
);
1419 pGatherLo2
= VCVTPD2PS(pGatherLo2
);
1420 pGatherHi
= VCVTPD2PS(pGatherHi
);
1421 pGatherHi2
= VCVTPD2PS(pGatherHi2
);
1423 Value
*pGather
= VSHUFFLE(pGatherLo
, pGatherHi
, C({ 0, 1, 2, 3, 4, 5, 6, 7 }));
1424 Value
*pGather2
= VSHUFFLE(pGatherLo2
, pGatherHi2
, C({ 0, 1, 2, 3, 4, 5, 6, 7 }));
1426 // pack adjacent pairs of SIMD8s into SIMD16s
1427 pVtxSrc2
[currentVertexElement
++] = JOIN_16(pGather
, pGather2
);
1431 pVtxSrc2
[currentVertexElement
++] = GenerateCompCtrlVector16(compCtrl
[i
]);
1434 if (currentVertexElement
> 3)
1437 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
1439 StoreVertexElements16(pVtxOut2
, outputElt
++, 4, pVtxSrc2
);
1440 // reset to the next vVertexElement to output
1441 currentVertexElement
= 0;
1445 // offset base to the next component in the vertex to gather
1446 pStreamBase
= GEP(pStreamBase
, C((char)8));
1448 if (isComponentEnabled(compMask
, i
))
1450 // if we need to gather the component
1451 if (compCtrl
[i
] == StoreSrc
)
1453 Value
*vMaskLo
= VSHUFFLE(vGatherMask
, VUNDEF(mInt1Ty
, 8), C({0, 1, 2, 3}));
1454 Value
*vMaskHi
= VSHUFFLE(vGatherMask
, VUNDEF(mInt1Ty
, 8), C({4, 5, 6, 7}));
1456 Value
*vOffsetsLo
= VEXTRACTI128(vOffsets
, C(0));
1457 Value
*vOffsetsHi
= VEXTRACTI128(vOffsets
, C(1));
1459 Value
*vZeroDouble
= VECTOR_SPLAT(4, ConstantFP::get(IRB()->getDoubleTy(), 0.0f
));
1461 Value
* pGatherLo
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsLo
, vMaskLo
);
1462 Value
* pGatherHi
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsHi
, vMaskHi
);
1464 pGatherLo
= VCVTPD2PS(pGatherLo
);
1465 pGatherHi
= VCVTPD2PS(pGatherHi
);
1467 Value
*pGather
= VSHUFFLE(pGatherLo
, pGatherHi
, C({0, 1, 2, 3, 4, 5, 6, 7}));
1469 vVertexElements
[currentVertexElement
++] = pGather
;
1473 #if USE_SIMD16_SHADERS
1474 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
1476 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
1480 if (currentVertexElement
> 3)
1482 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
1483 // reset to the next vVertexElement to output
1484 currentVertexElement
= 0;
1488 // offset base to the next component in the vertex to gather
1489 pStreamBase
= GEP(pStreamBase
, C((char)8));
1495 SWR_INVALID("Tried to fetch invalid FP format");
1501 Instruction::CastOps extendCastType
= Instruction::CastOps::CastOpsEnd
;
1502 ConversionType conversionType
= CONVERT_NONE
;
1504 SWR_ASSERT(IsUniformFormat((SWR_FORMAT
)ied
.Format
),
1505 "Unsupported format for standard gather fetch.");
1507 switch(info
.type
[0])
1509 case SWR_TYPE_UNORM
:
1510 conversionType
= CONVERT_NORMALIZED
;
1512 extendCastType
= Instruction::CastOps::ZExt
;
1514 case SWR_TYPE_SNORM
:
1515 conversionType
= CONVERT_NORMALIZED
;
1517 extendCastType
= Instruction::CastOps::SExt
;
1519 case SWR_TYPE_USCALED
:
1520 conversionType
= CONVERT_USCALED
;
1521 extendCastType
= Instruction::CastOps::UIToFP
;
1523 case SWR_TYPE_SSCALED
:
1524 conversionType
= CONVERT_SSCALED
;
1525 extendCastType
= Instruction::CastOps::SIToFP
;
1527 case SWR_TYPE_SFIXED
:
1528 conversionType
= CONVERT_SFIXED
;
1529 extendCastType
= Instruction::CastOps::SExt
;
1535 // value substituted when component of gather is masked
1536 Value
* gatherSrc
= VIMMED1(0);
1537 #if USE_SIMD16_GATHERS
1538 Value
*gatherSrc16
= VIMMED1_16(0);
1541 // Gather components from memory to store in a simdvertex structure
1546 // if we have at least one component to fetch
1549 #if USE_SIMD16_GATHERS
1550 Value
*gatherResult
= GATHERDD_16(gatherSrc16
, pStreamBase
, vOffsets16
, vGatherMask16
);
1552 // e.g. result of an 8x32bit integer gather for 8bit components
1553 // 256i - 0 1 2 3 4 5 6 7
1554 // xyzw xyzw xyzw xyzw xyzw xyzw xyzw xyzw
1556 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
1558 Shuffle8bpcArgs args
= std::forward_as_tuple(gatherResult
, pVtxOut2
, extendCastType
, conversionType
,
1559 currentVertexElement
, outputElt
, compMask
, compCtrl
, pVtxSrc2
, info
.swizzle
);
1561 // Shuffle gathered components into place in simdvertex struct
1562 Shuffle8bpcGatherd16(args
); // outputs to vVertexElements ref
1564 Value
*vGatherResult
= GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1565 // e.g. result of an 8x32bit integer gather for 8bit components
1566 // 256i - 0 1 2 3 4 5 6 7
1567 // xyzw xyzw xyzw xyzw xyzw xyzw xyzw xyzw
1569 Shuffle8bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, extendCastType
, conversionType
,
1570 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
, info
.swizzle
);
1572 // Shuffle gathered components into place in simdvertex struct
1573 #if USE_SIMD16_SHADERS
1574 Shuffle8bpcGatherd(args
, useVertexID2
); // outputs to vVertexElements ref
1576 Shuffle8bpcGatherd(args
); // outputs to vVertexElements ref
1584 #if USE_SIMD16_GATHERS
1585 Value
*gatherResult
[2];
1587 // if we have at least one component out of x or y to fetch
1588 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
1590 gatherResult
[0] = GATHERDD_16(gatherSrc16
, pStreamBase
, vOffsets16
, vGatherMask16
);
1592 // e.g. result of first 8x32bit integer gather for 16bit components
1593 // 256i - 0 1 2 3 4 5 6 7
1594 // xyxy xyxy xyxy xyxy xyxy xyxy xyxy xyxy
1599 gatherResult
[0] = VUNDEF_I_16();
1602 // if we have at least one component out of z or w to fetch
1603 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
1605 // offset base to the next components(zw) in the vertex to gather
1606 pStreamBase
= GEP(pStreamBase
, C((char)4));
1608 gatherResult
[1] = GATHERDD_16(gatherSrc16
, pStreamBase
, vOffsets16
, vGatherMask16
);
1610 // e.g. result of second 8x32bit integer gather for 16bit components
1611 // 256i - 0 1 2 3 4 5 6 7
1612 // zwzw zwzw zwzw zwzw zwzw zwzw zwzw zwzw
1617 gatherResult
[1] = VUNDEF_I_16();
1620 // if we have at least one component to shuffle into place
1623 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
1625 Shuffle16bpcArgs args
= std::forward_as_tuple(gatherResult
, pVtxOut2
, extendCastType
, conversionType
,
1626 currentVertexElement
, outputElt
, compMask
, compCtrl
, pVtxSrc2
);
1628 // Shuffle gathered components into place in simdvertex struct
1629 Shuffle16bpcGather16(args
); // outputs to vVertexElements ref
1632 Value
*vGatherResult
[2];
1634 // if we have at least one component out of x or y to fetch
1635 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
1637 vGatherResult
[0] = GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1638 // e.g. result of first 8x32bit integer gather for 16bit components
1639 // 256i - 0 1 2 3 4 5 6 7
1640 // xyxy xyxy xyxy xyxy xyxy xyxy xyxy xyxy
1644 // if we have at least one component out of z or w to fetch
1645 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
1647 // offset base to the next components(zw) in the vertex to gather
1648 pStreamBase
= GEP(pStreamBase
, C((char)4));
1650 vGatherResult
[1] = GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1651 // e.g. result of second 8x32bit integer gather for 16bit components
1652 // 256i - 0 1 2 3 4 5 6 7
1653 // zwzw zwzw zwzw zwzw zwzw zwzw zwzw zwzw
1657 // if we have at least one component to shuffle into place
1660 Shuffle16bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, extendCastType
, conversionType
,
1661 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
);
1663 // Shuffle gathered components into place in simdvertex struct
1664 #if USE_SIMD16_SHADERS
1665 Shuffle16bpcGather(args
, useVertexID2
); // outputs to vVertexElements ref
1667 Shuffle16bpcGather(args
); // outputs to vVertexElements ref
1675 // Gathered components into place in simdvertex struct
1676 for (uint32_t i
= 0; i
< 4; i
++)
1678 if (isComponentEnabled(compMask
, i
))
1680 // if we need to gather the component
1681 if (compCtrl
[i
] == StoreSrc
)
1683 #if USE_SIMD16_GATHERS
1684 Value
*pGather
= GATHERDD_16(gatherSrc16
, pStreamBase
, vOffsets16
, vGatherMask16
);
1686 if (conversionType
== CONVERT_USCALED
)
1688 pGather
= UI_TO_FP(pGather
, mSimd16FP32Ty
);
1690 else if (conversionType
== CONVERT_SSCALED
)
1692 pGather
= SI_TO_FP(pGather
, mSimd16FP32Ty
);
1694 else if (conversionType
== CONVERT_SFIXED
)
1696 pGather
= FMUL(SI_TO_FP(pGather
, mSimd16FP32Ty
), VBROADCAST_16(C(1 / 65536.0f
)));
1699 pVtxSrc2
[currentVertexElement
++] = pGather
;
1701 // e.g. result of a single 8x32bit integer gather for 32bit components
1702 // 256i - 0 1 2 3 4 5 6 7
1703 // xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
1705 Value
* pGather
= GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1707 if (conversionType
== CONVERT_USCALED
)
1709 pGather
= UI_TO_FP(pGather
, mSimdFP32Ty
);
1711 else if (conversionType
== CONVERT_SSCALED
)
1713 pGather
= SI_TO_FP(pGather
, mSimdFP32Ty
);
1715 else if (conversionType
== CONVERT_SFIXED
)
1717 pGather
= FMUL(SI_TO_FP(pGather
, mSimdFP32Ty
), VBROADCAST(C(1/65536.0f
)));
1720 vVertexElements
[currentVertexElement
++] = pGather
;
1722 // e.g. result of a single 8x32bit integer gather for 32bit components
1723 // 256i - 0 1 2 3 4 5 6 7
1724 // xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
1729 #if USE_SIMD16_GATHERS
1730 pVtxSrc2
[currentVertexElement
++] = GenerateCompCtrlVector16(compCtrl
[i
]);
1732 #if USE_SIMD16_SHADERS
1733 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
1735 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
1740 if (currentVertexElement
> 3)
1742 #if USE_SIMD16_GATHERS
1744 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
1746 StoreVertexElements16(pVtxOut2
, outputElt
++, 4, pVtxSrc2
);
1748 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
1751 // reset to the next vVertexElement to output
1752 currentVertexElement
= 0;
1757 // offset base to the next component in the vertex to gather
1758 pStreamBase
= GEP(pStreamBase
, C((char)4));
1766 // if we have a partially filled vVertexElement struct, output it
1767 if (currentVertexElement
> 0)
1769 #if USE_SIMD16_GATHERS
1771 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth16
), 0));
1773 StoreVertexElements16(pVtxOut2
, outputElt
++, currentVertexElement
, pVtxSrc2
);
1775 StoreVertexElements(pVtxOut
, outputElt
++, currentVertexElement
, vVertexElements
);
1780 //////////////////////////////////////////////////////////////////////////
1781 /// @brief Loads a simd of valid indices. OOB indices are set to 0
1782 /// *Note* have to do 16bit index checking in scalar until we have AVX-512
1784 /// @param pIndices - pointer to 8 bit indices
1785 /// @param pLastIndex - pointer to last valid index
1786 Value
* FetchJit::GetSimdValid8bitIndices(Value
* pIndices
, Value
* pLastIndex
)
1788 // can fit 2 16 bit integers per vWidth lane
1789 Value
* vIndices
= VUNDEF_I();
1791 // store 0 index on stack to be used to conditionally load from if index address is OOB
1792 Value
* pZeroIndex
= ALLOCA(mInt8Ty
);
1793 STORE(C((uint8_t)0), pZeroIndex
);
1795 // Load a SIMD of index pointers
1796 for(int64_t lane
= 0; lane
< mVWidth
; lane
++)
1798 // Calculate the address of the requested index
1799 Value
*pIndex
= GEP(pIndices
, C(lane
));
1801 // check if the address is less than the max index,
1802 Value
* mask
= ICMP_ULT(pIndex
, pLastIndex
);
1804 // if valid, load the index. if not, load 0 from the stack
1805 Value
* pValid
= SELECT(mask
, pIndex
, pZeroIndex
);
1806 Value
*index
= LOAD(pValid
, "valid index");
1808 // zero extended index to 32 bits and insert into the correct simd lane
1809 index
= Z_EXT(index
, mInt32Ty
);
1810 vIndices
= VINSERT(vIndices
, index
, lane
);
1815 //////////////////////////////////////////////////////////////////////////
1816 /// @brief Loads a simd of valid indices. OOB indices are set to 0
1817 /// *Note* have to do 16bit index checking in scalar until we have AVX-512
1819 /// @param pIndices - pointer to 16 bit indices
1820 /// @param pLastIndex - pointer to last valid index
1821 Value
* FetchJit::GetSimdValid16bitIndices(Value
* pIndices
, Value
* pLastIndex
)
1823 // can fit 2 16 bit integers per vWidth lane
1824 Value
* vIndices
= VUNDEF_I();
1826 // store 0 index on stack to be used to conditionally load from if index address is OOB
1827 Value
* pZeroIndex
= ALLOCA(mInt16Ty
);
1828 STORE(C((uint16_t)0), pZeroIndex
);
1830 // Load a SIMD of index pointers
1831 for(int64_t lane
= 0; lane
< mVWidth
; lane
++)
1833 // Calculate the address of the requested index
1834 Value
*pIndex
= GEP(pIndices
, C(lane
));
1836 // check if the address is less than the max index,
1837 Value
* mask
= ICMP_ULT(pIndex
, pLastIndex
);
1839 // if valid, load the index. if not, load 0 from the stack
1840 Value
* pValid
= SELECT(mask
, pIndex
, pZeroIndex
);
1841 Value
*index
= LOAD(pValid
, "valid index");
1843 // zero extended index to 32 bits and insert into the correct simd lane
1844 index
= Z_EXT(index
, mInt32Ty
);
1845 vIndices
= VINSERT(vIndices
, index
, lane
);
1850 //////////////////////////////////////////////////////////////////////////
1851 /// @brief Loads a simd of valid indices. OOB indices are set to 0
1852 /// @param pIndices - pointer to 32 bit indices
1853 /// @param pLastIndex - pointer to last valid index
1854 Value
* FetchJit::GetSimdValid32bitIndices(Value
* pIndices
, Value
* pLastIndex
)
1856 DataLayout
dL(JM()->mpCurrentModule
);
1857 unsigned int ptrSize
= dL
.getPointerSize() * 8; // ptr size in bits
1858 Value
* iLastIndex
= PTR_TO_INT(pLastIndex
, Type::getIntNTy(JM()->mContext
, ptrSize
));
1859 Value
* iIndices
= PTR_TO_INT(pIndices
, Type::getIntNTy(JM()->mContext
, ptrSize
));
1861 // get the number of indices left in the buffer (endPtr - curPtr) / sizeof(index)
1862 Value
* numIndicesLeft
= SUB(iLastIndex
,iIndices
);
1863 numIndicesLeft
= TRUNC(numIndicesLeft
, mInt32Ty
);
1864 numIndicesLeft
= SDIV(numIndicesLeft
, C(4));
1866 // create a vector of index counts from the base index ptr passed into the fetch
1867 const std::vector
<Constant
*> vecIndices
{C(0), C(1), C(2), C(3), C(4), C(5), C(6), C(7)};
1868 Constant
* vIndexOffsets
= ConstantVector::get(vecIndices
);
1870 // compare index count to the max valid index
1871 // e.g vMaxIndex 4 4 4 4 4 4 4 4 : 4 indices left to load
1872 // vIndexOffsets 0 1 2 3 4 5 6 7
1873 // ------------------------------
1874 // vIndexMask -1-1-1-1 0 0 0 0 : offsets < max pass
1875 // vLoadedIndices 0 1 2 3 0 0 0 0 : offsets >= max masked to 0
1876 Value
* vMaxIndex
= VBROADCAST(numIndicesLeft
);
1877 Value
* vIndexMask
= VPCMPGTD(vMaxIndex
,vIndexOffsets
);
1879 // VMASKLOAD takes an *i8 src pointer
1880 pIndices
= BITCAST(pIndices
,PointerType::get(mInt8Ty
,0));
1882 // Load the indices; OOB loads 0
1883 return MASKLOADD(pIndices
,vIndexMask
);
1886 //////////////////////////////////////////////////////////////////////////
1887 /// @brief Takes a SIMD of gathered 8bpc verts, zero or sign extends,
1888 /// denormalizes if needed, converts to F32 if needed, and positions in
1889 // the proper SIMD rows to be output to the simdvertex structure
1890 /// @param args: (tuple of args, listed below)
1891 /// @param vGatherResult - 8 gathered 8bpc vertices
1892 /// @param pVtxOut - base pointer to output simdvertex struct
1893 /// @param extendType - sign extend or zero extend
1894 /// @param bNormalized - do we need to denormalize?
1895 /// @param currentVertexElement - reference to the current vVertexElement
1896 /// @param outputElt - reference to the current offset from simdvertex we're o
1897 /// @param compMask - component packing mask
1898 /// @param compCtrl - component control val
1899 /// @param vVertexElements[4] - vertex components to output
1900 /// @param swizzle[4] - component swizzle location
1901 #if USE_SIMD16_GATHERS
1902 void FetchJit::Shuffle8bpcGatherd16(Shuffle8bpcArgs
&args
)
1904 // Unpack tuple args
1905 Value
*& vGatherResult
= std::get
<0>(args
);
1906 Value
* pVtxOut
= std::get
<1>(args
);
1907 const Instruction::CastOps extendType
= std::get
<2>(args
);
1908 const ConversionType conversionType
= std::get
<3>(args
);
1909 uint32_t ¤tVertexElement
= std::get
<4>(args
);
1910 uint32_t &outputElt
= std::get
<5>(args
);
1911 const ComponentEnable compMask
= std::get
<6>(args
);
1912 const ComponentControl(&compCtrl
)[4] = std::get
<7>(args
);
1913 Value
* (&vVertexElements
)[4] = std::get
<8>(args
);
1914 const uint32_t(&swizzle
)[4] = std::get
<9>(args
);
1917 Type
*vGatherTy
= mSimdInt32Ty
;
1918 Type
*v32x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 4); // vwidth is units of 32 bits
1920 // have to do extra work for sign extending
1921 if ((extendType
== Instruction::CastOps::SExt
) || (extendType
== Instruction::CastOps::SIToFP
))
1923 Type
*v16x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 2); // 8x16bit ints in a 128bit lane
1924 Type
*v128Ty
= VectorType::get(IntegerType::getIntNTy(JM()->mContext
, 128), mVWidth
/ 4); // vwidth is units of 32 bits
1926 // shuffle mask, including any swizzling
1927 const char x
= (char)swizzle
[0]; const char y
= (char)swizzle
[1];
1928 const char z
= (char)swizzle
[2]; const char w
= (char)swizzle
[3];
1929 Value
*vConstMask
= C
<char>({ char(x
), char(x
+ 4), char(x
+ 8), char(x
+ 12),
1930 char(y
), char(y
+ 4), char(y
+ 8), char(y
+ 12),
1931 char(z
), char(z
+ 4), char(z
+ 8), char(z
+ 12),
1932 char(w
), char(w
+ 4), char(w
+ 8), char(w
+ 12),
1933 char(x
), char(x
+ 4), char(x
+ 8), char(x
+ 12),
1934 char(y
), char(y
+ 4), char(y
+ 8), char(y
+ 12),
1935 char(z
), char(z
+ 4), char(z
+ 8), char(z
+ 12),
1936 char(w
), char(w
+ 4), char(w
+ 8), char(w
+ 12) });
1938 // SIMD16 PSHUFB isnt part of AVX-512F, so split into SIMD8 for the sake of KNL, for now..
1940 Value
*vGatherResult_lo
= EXTRACT_16(vGatherResult
, 0);
1941 Value
*vGatherResult_hi
= EXTRACT_16(vGatherResult
, 1);
1943 Value
*vShufResult_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
), vGatherTy
);
1944 Value
*vShufResult_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
), vGatherTy
);
1946 // after pshufb: group components together in each 128bit lane
1947 // 256i - 0 1 2 3 4 5 6 7
1948 // xxxx yyyy zzzz wwww xxxx yyyy zzzz wwww
1950 Value
*vi128XY_lo
= nullptr;
1951 Value
*vi128XY_hi
= nullptr;
1952 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
1954 vi128XY_lo
= BITCAST(PERMD(vShufResult_lo
, C
<int32_t>({ 0, 4, 0, 0, 1, 5, 0, 0 })), v128Ty
);
1955 vi128XY_hi
= BITCAST(PERMD(vShufResult_hi
, C
<int32_t>({ 0, 4, 0, 0, 1, 5, 0, 0 })), v128Ty
);
1957 // after PERMD: move and pack xy and zw components in low 64 bits of each 128bit lane
1958 // 256i - 0 1 2 3 4 5 6 7
1959 // xxxx xxxx dcdc dcdc yyyy yyyy dcdc dcdc (dc - don't care)
1962 // do the same for zw components
1963 Value
*vi128ZW_lo
= nullptr;
1964 Value
*vi128ZW_hi
= nullptr;
1965 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
1967 vi128ZW_lo
= BITCAST(PERMD(vShufResult_lo
, C
<int32_t>({ 2, 6, 0, 0, 3, 7, 0, 0 })), v128Ty
);
1968 vi128ZW_hi
= BITCAST(PERMD(vShufResult_hi
, C
<int32_t>({ 2, 6, 0, 0, 3, 7, 0, 0 })), v128Ty
);
1971 // init denormalize variables if needed
1972 Instruction::CastOps fpCast
;
1973 Value
*conversionFactor
;
1975 switch (conversionType
)
1977 case CONVERT_NORMALIZED
:
1978 fpCast
= Instruction::CastOps::SIToFP
;
1979 conversionFactor
= VIMMED1((float)(1.0 / 127.0));
1981 case CONVERT_SSCALED
:
1982 fpCast
= Instruction::CastOps::SIToFP
;
1983 conversionFactor
= VIMMED1((float)(1.0));
1985 case CONVERT_USCALED
:
1986 SWR_INVALID("Type should not be sign extended!");
1987 conversionFactor
= nullptr;
1990 SWR_ASSERT(conversionType
== CONVERT_NONE
);
1991 conversionFactor
= nullptr;
1995 // sign extend all enabled components. If we have a fill vVertexElements, output to current simdvertex
1996 for (uint32_t i
= 0; i
< 4; i
++)
1998 if (isComponentEnabled(compMask
, i
))
2000 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2002 // if x or z, extract 128bits from lane 0, else for y or w, extract from lane 1
2003 uint32_t lane
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2004 // if x or y, use vi128XY permute result, else use vi128ZW
2005 Value
*selectedPermute_lo
= (i
< 2) ? vi128XY_lo
: vi128ZW_lo
;
2006 Value
*selectedPermute_hi
= (i
< 2) ? vi128XY_hi
: vi128ZW_hi
;
2009 Value
*temp_lo
= PMOVSXBD(BITCAST(VEXTRACT(selectedPermute_lo
, C(lane
)), v16x8Ty
));
2010 Value
*temp_hi
= PMOVSXBD(BITCAST(VEXTRACT(selectedPermute_hi
, C(lane
)), v16x8Ty
));
2012 // denormalize if needed
2013 if (conversionType
!= CONVERT_NONE
)
2015 temp_lo
= FMUL(CAST(fpCast
, temp_lo
, mSimdFP32Ty
), conversionFactor
);
2016 temp_hi
= FMUL(CAST(fpCast
, temp_hi
, mSimdFP32Ty
), conversionFactor
);
2019 vVertexElements
[currentVertexElement
] = JOIN_16(temp_lo
, temp_hi
);
2021 currentVertexElement
+= 1;
2025 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector16(compCtrl
[i
]);
2028 if (currentVertexElement
> 3)
2030 StoreVertexElements16(pVtxOut
, outputElt
++, 4, vVertexElements
);
2031 // reset to the next vVertexElement to output
2032 currentVertexElement
= 0;
2038 else if ((extendType
== Instruction::CastOps::ZExt
) || (extendType
== Instruction::CastOps::UIToFP
))
2040 // init denormalize variables if needed
2041 Instruction::CastOps fpCast
;
2042 Value
*conversionFactor
;
2044 switch (conversionType
)
2046 case CONVERT_NORMALIZED
:
2047 fpCast
= Instruction::CastOps::UIToFP
;
2048 conversionFactor
= VIMMED1((float)(1.0 / 255.0));
2050 case CONVERT_USCALED
:
2051 fpCast
= Instruction::CastOps::UIToFP
;
2052 conversionFactor
= VIMMED1((float)(1.0));
2054 case CONVERT_SSCALED
:
2055 SWR_INVALID("Type should not be zero extended!");
2056 conversionFactor
= nullptr;
2059 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2060 conversionFactor
= nullptr;
2064 // shuffle enabled components into lower byte of each 32bit lane, 0 extending to 32 bits
2065 for (uint32_t i
= 0; i
< 4; i
++)
2067 if (isComponentEnabled(compMask
, i
))
2069 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2071 // pshufb masks for each component
2077 vConstMask
= C
<char>({ 0, -1, -1, -1, 4, -1, -1, -1, 8, -1, -1, -1, 12, -1, -1, -1,
2078 0, -1, -1, -1, 4, -1, -1, -1, 8, -1, -1, -1, 12, -1, -1, -1 });
2082 vConstMask
= C
<char>({ 1, -1, -1, -1, 5, -1, -1, -1, 9, -1, -1, -1, 13, -1, -1, -1,
2083 1, -1, -1, -1, 5, -1, -1, -1, 9, -1, -1, -1, 13, -1, -1, -1 });
2087 vConstMask
= C
<char>({ 2, -1, -1, -1, 6, -1, -1, -1, 10, -1, -1, -1, 14, -1, -1, -1,
2088 2, -1, -1, -1, 6, -1, -1, -1, 10, -1, -1, -1, 14, -1, -1, -1 });
2092 vConstMask
= C
<char>({ 3, -1, -1, -1, 7, -1, -1, -1, 11, -1, -1, -1, 15, -1, -1, -1,
2093 3, -1, -1, -1, 7, -1, -1, -1, 11, -1, -1, -1, 15, -1, -1, -1 });
2096 vConstMask
= nullptr;
2100 Value
*vGatherResult_lo
= EXTRACT_16(vGatherResult
, 0);
2101 Value
*vGatherResult_hi
= EXTRACT_16(vGatherResult
, 1);
2103 Value
*temp_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
), vGatherTy
);
2104 Value
*temp_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
), vGatherTy
);
2106 // after pshufb for x channel
2107 // 256i - 0 1 2 3 4 5 6 7
2108 // x000 x000 x000 x000 x000 x000 x000 x000
2110 // denormalize if needed
2111 if (conversionType
!= CONVERT_NONE
)
2113 temp_lo
= FMUL(CAST(fpCast
, temp_lo
, mSimdFP32Ty
), conversionFactor
);
2114 temp_hi
= FMUL(CAST(fpCast
, temp_hi
, mSimdFP32Ty
), conversionFactor
);
2117 vVertexElements
[currentVertexElement
] = JOIN_16(temp_lo
, temp_hi
);
2119 currentVertexElement
+= 1;
2123 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector16(compCtrl
[i
]);
2126 if (currentVertexElement
> 3)
2128 StoreVertexElements16(pVtxOut
, outputElt
++, 4, vVertexElements
);
2129 // reset to the next vVertexElement to output
2130 currentVertexElement
= 0;
2137 SWR_INVALID("Unsupported conversion type");
2142 #if USE_SIMD16_SHADERS
2143 void FetchJit::Shuffle8bpcGatherd(Shuffle8bpcArgs
&args
, bool useVertexID2
)
2145 void FetchJit::Shuffle8bpcGatherd(Shuffle8bpcArgs
&args
)
2148 // Unpack tuple args
2149 Value
*& vGatherResult
= std::get
<0>(args
);
2150 Value
* pVtxOut
= std::get
<1>(args
);
2151 const Instruction::CastOps extendType
= std::get
<2>(args
);
2152 const ConversionType conversionType
= std::get
<3>(args
);
2153 uint32_t ¤tVertexElement
= std::get
<4>(args
);
2154 uint32_t &outputElt
= std::get
<5>(args
);
2155 const ComponentEnable compMask
= std::get
<6>(args
);
2156 const ComponentControl(&compCtrl
)[4] = std::get
<7>(args
);
2157 Value
* (&vVertexElements
)[4] = std::get
<8>(args
);
2158 const uint32_t(&swizzle
)[4] = std::get
<9>(args
);
2161 Type
* v32x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 4); // vwidth is units of 32 bits
2163 for (uint32_t i
= 0; i
< 4; i
++)
2165 if (!isComponentEnabled(compMask
, i
))
2168 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2170 std::vector
<uint32_t> vShuffleMasks
[4] = {
2171 { 0, 4, 8, 12, 16, 20, 24, 28 }, // x
2172 { 1, 5, 9, 13, 17, 21, 25, 29 }, // y
2173 { 2, 6, 10, 14, 18, 22, 26, 30 }, // z
2174 { 3, 7, 11, 15, 19, 23, 27, 31 }, // w
2177 Value
*val
= VSHUFFLE(BITCAST(vGatherResult
, v32x8Ty
),
2178 UndefValue::get(v32x8Ty
),
2179 vShuffleMasks
[swizzle
[i
]]);
2181 if ((extendType
== Instruction::CastOps::SExt
) ||
2182 (extendType
== Instruction::CastOps::SIToFP
)) {
2183 switch (conversionType
)
2185 case CONVERT_NORMALIZED
:
2186 val
= FMUL(SI_TO_FP(val
, mSimdFP32Ty
), VIMMED1((float)(1.0 / 127.0)));
2188 case CONVERT_SSCALED
:
2189 val
= SI_TO_FP(val
, mSimdFP32Ty
);
2191 case CONVERT_USCALED
:
2192 SWR_INVALID("Type should not be sign extended!");
2195 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2196 val
= S_EXT(val
, mSimdInt32Ty
);
2200 else if ((extendType
== Instruction::CastOps::ZExt
) ||
2201 (extendType
== Instruction::CastOps::UIToFP
)) {
2202 switch (conversionType
)
2204 case CONVERT_NORMALIZED
:
2205 val
= FMUL(UI_TO_FP(val
, mSimdFP32Ty
), VIMMED1((float)(1.0 / 255.0)));
2207 case CONVERT_SSCALED
:
2208 SWR_INVALID("Type should not be zero extended!");
2210 case CONVERT_USCALED
:
2211 val
= UI_TO_FP(val
, mSimdFP32Ty
);
2214 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2215 val
= Z_EXT(val
, mSimdInt32Ty
);
2221 SWR_INVALID("Unsupported conversion type");
2224 vVertexElements
[currentVertexElement
++] = val
;
2228 #if USE_SIMD16_SHADERS
2229 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
2231 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
2235 if (currentVertexElement
> 3)
2237 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
2238 // reset to the next vVertexElement to output
2239 currentVertexElement
= 0;
2245 //////////////////////////////////////////////////////////////////////////
2246 /// @brief Takes a SIMD of gathered 16bpc verts, zero or sign extends,
2247 /// denormalizes if needed, converts to F32 if needed, and positions in
2248 // the proper SIMD rows to be output to the simdvertex structure
2249 /// @param args: (tuple of args, listed below)
2250 /// @param vGatherResult[2] - array of gathered 16bpc vertices, 4 per index
2251 /// @param pVtxOut - base pointer to output simdvertex struct
2252 /// @param extendType - sign extend or zero extend
2253 /// @param bNormalized - do we need to denormalize?
2254 /// @param currentVertexElement - reference to the current vVertexElement
2255 /// @param outputElt - reference to the current offset from simdvertex we're o
2256 /// @param compMask - component packing mask
2257 /// @param compCtrl - component control val
2258 /// @param vVertexElements[4] - vertex components to output
2259 #if USE_SIMD16_GATHERS
2260 void FetchJit::Shuffle16bpcGather16(Shuffle16bpcArgs
&args
)
2262 // Unpack tuple args
2263 Value
* (&vGatherResult
)[2] = std::get
<0>(args
);
2264 Value
* pVtxOut
= std::get
<1>(args
);
2265 const Instruction::CastOps extendType
= std::get
<2>(args
);
2266 const ConversionType conversionType
= std::get
<3>(args
);
2267 uint32_t ¤tVertexElement
= std::get
<4>(args
);
2268 uint32_t &outputElt
= std::get
<5>(args
);
2269 const ComponentEnable compMask
= std::get
<6>(args
);
2270 const ComponentControl(&compCtrl
)[4] = std::get
<7>(args
);
2271 Value
* (&vVertexElements
)[4] = std::get
<8>(args
);
2274 Type
*vGatherTy
= VectorType::get(IntegerType::getInt32Ty(JM()->mContext
), mVWidth
);
2275 Type
*v32x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 4); // vwidth is units of 32 bits
2277 // have to do extra work for sign extending
2278 if ((extendType
== Instruction::CastOps::SExt
) || (extendType
== Instruction::CastOps::SIToFP
) || (extendType
== Instruction::CastOps::FPExt
))
2280 // is this PP float?
2281 bool bFP
= (extendType
== Instruction::CastOps::FPExt
) ? true : false;
2283 Type
*v8x16Ty
= VectorType::get(mInt16Ty
, 8); // 8x16bit in a 128bit lane
2284 Type
*v128bitTy
= VectorType::get(IntegerType::getIntNTy(JM()->mContext
, 128), mVWidth
/ 4); // vwidth is units of 32 bits
2287 Value
*vConstMask
= C
<char>({ 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15,
2288 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15 });
2289 Value
*vi128XY_lo
= nullptr;
2290 Value
*vi128XY_hi
= nullptr;
2291 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
2293 // SIMD16 PSHUFB isnt part of AVX-512F, so split into SIMD8 for the sake of KNL, for now..
2295 Value
*vGatherResult_lo
= EXTRACT_16(vGatherResult
[0], 0);
2296 Value
*vGatherResult_hi
= EXTRACT_16(vGatherResult
[0], 1);
2298 Value
*vShufResult_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
), vGatherTy
);
2299 Value
*vShufResult_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
), vGatherTy
);
2301 // after pshufb: group components together in each 128bit lane
2302 // 256i - 0 1 2 3 4 5 6 7
2303 // xxxx xxxx yyyy yyyy xxxx xxxx yyyy yyyy
2305 vi128XY_lo
= BITCAST(PERMD(vShufResult_lo
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2306 vi128XY_hi
= BITCAST(PERMD(vShufResult_hi
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2308 // after PERMD: move and pack xy components into each 128bit lane
2309 // 256i - 0 1 2 3 4 5 6 7
2310 // xxxx xxxx xxxx xxxx yyyy yyyy yyyy yyyy
2313 // do the same for zw components
2314 Value
*vi128ZW_lo
= nullptr;
2315 Value
*vi128ZW_hi
= nullptr;
2316 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
2318 Value
*vGatherResult_lo
= EXTRACT_16(vGatherResult
[1], 0);
2319 Value
*vGatherResult_hi
= EXTRACT_16(vGatherResult
[1], 1);
2321 Value
*vShufResult_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
), vGatherTy
);
2322 Value
*vShufResult_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
), vGatherTy
);
2324 vi128ZW_lo
= BITCAST(PERMD(vShufResult_lo
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2325 vi128ZW_hi
= BITCAST(PERMD(vShufResult_hi
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2328 // init denormalize variables if needed
2329 Instruction::CastOps IntToFpCast
;
2330 Value
*conversionFactor
;
2332 switch (conversionType
)
2334 case CONVERT_NORMALIZED
:
2335 IntToFpCast
= Instruction::CastOps::SIToFP
;
2336 conversionFactor
= VIMMED1((float)(1.0 / 32767.0));
2338 case CONVERT_SSCALED
:
2339 IntToFpCast
= Instruction::CastOps::SIToFP
;
2340 conversionFactor
= VIMMED1((float)(1.0));
2342 case CONVERT_USCALED
:
2343 SWR_INVALID("Type should not be sign extended!");
2344 conversionFactor
= nullptr;
2347 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2348 conversionFactor
= nullptr;
2352 // sign extend all enabled components. If we have a fill vVertexElements, output to current simdvertex
2353 for (uint32_t i
= 0; i
< 4; i
++)
2355 if (isComponentEnabled(compMask
, i
))
2357 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2359 // if x or z, extract 128bits from lane 0, else for y or w, extract from lane 1
2360 uint32_t lane
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2361 // if x or y, use vi128XY permute result, else use vi128ZW
2362 Value
*selectedPermute_lo
= (i
< 2) ? vi128XY_lo
: vi128ZW_lo
;
2363 Value
*selectedPermute_hi
= (i
< 2) ? vi128XY_hi
: vi128ZW_hi
;
2367 // extract 128 bit lanes to sign extend each component
2368 Value
*temp_lo
= CVTPH2PS(BITCAST(VEXTRACT(selectedPermute_lo
, C(lane
)), v8x16Ty
));
2369 Value
*temp_hi
= CVTPH2PS(BITCAST(VEXTRACT(selectedPermute_hi
, C(lane
)), v8x16Ty
));
2371 vVertexElements
[currentVertexElement
] = JOIN_16(temp_lo
, temp_hi
);
2375 // extract 128 bit lanes to sign extend each component
2376 Value
*temp_lo
= PMOVSXWD(BITCAST(VEXTRACT(selectedPermute_lo
, C(lane
)), v8x16Ty
));
2377 Value
*temp_hi
= PMOVSXWD(BITCAST(VEXTRACT(selectedPermute_hi
, C(lane
)), v8x16Ty
));
2379 // denormalize if needed
2380 if (conversionType
!= CONVERT_NONE
)
2382 temp_lo
= FMUL(CAST(IntToFpCast
, temp_lo
, mSimdFP32Ty
), conversionFactor
);
2383 temp_hi
= FMUL(CAST(IntToFpCast
, temp_hi
, mSimdFP32Ty
), conversionFactor
);
2386 vVertexElements
[currentVertexElement
] = JOIN_16(temp_lo
, temp_hi
);
2389 currentVertexElement
+= 1;
2393 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector16(compCtrl
[i
]);
2396 if (currentVertexElement
> 3)
2398 StoreVertexElements16(pVtxOut
, outputElt
++, 4, vVertexElements
);
2399 // reset to the next vVertexElement to output
2400 currentVertexElement
= 0;
2406 else if ((extendType
== Instruction::CastOps::ZExt
) || (extendType
== Instruction::CastOps::UIToFP
))
2408 // pshufb masks for each component
2409 Value
*vConstMask
[2];
2411 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 2))
2414 vConstMask
[0] = C
<char>({ 0, 1, -1, -1, 4, 5, -1, -1, 8, 9, -1, -1, 12, 13, -1, -1,
2415 0, 1, -1, -1, 4, 5, -1, -1, 8, 9, -1, -1, 12, 13, -1, -1, });
2418 if (isComponentEnabled(compMask
, 1) || isComponentEnabled(compMask
, 3))
2421 vConstMask
[1] = C
<char>({ 2, 3, -1, -1, 6, 7, -1, -1, 10, 11, -1, -1, 14, 15, -1, -1,
2422 2, 3, -1, -1, 6, 7, -1, -1, 10, 11, -1, -1, 14, 15, -1, -1 });
2425 // init denormalize variables if needed
2426 Instruction::CastOps fpCast
;
2427 Value
* conversionFactor
;
2429 switch (conversionType
)
2431 case CONVERT_NORMALIZED
:
2432 fpCast
= Instruction::CastOps::UIToFP
;
2433 conversionFactor
= VIMMED1((float)(1.0 / 65535.0));
2435 case CONVERT_USCALED
:
2436 fpCast
= Instruction::CastOps::UIToFP
;
2437 conversionFactor
= VIMMED1((float)(1.0f
));
2439 case CONVERT_SSCALED
:
2440 SWR_INVALID("Type should not be zero extended!");
2441 conversionFactor
= nullptr;
2444 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2445 conversionFactor
= nullptr;
2449 // shuffle enabled components into lower word of each 32bit lane, 0 extending to 32 bits
2450 for (uint32_t i
= 0; i
< 4; i
++)
2452 if (isComponentEnabled(compMask
, i
))
2454 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2456 // select correct constMask for x/z or y/w pshufb
2457 uint32_t selectedMask
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2458 // if x or y, use vi128XY permute result, else use vi128ZW
2459 uint32_t selectedGather
= (i
< 2) ? 0 : 1;
2461 // SIMD16 PSHUFB isnt part of AVX-512F, so split into SIMD8 for the sake of KNL, for now..
2463 Value
*vGatherResult_lo
= EXTRACT_16(vGatherResult
[selectedGather
], 0);
2464 Value
*vGatherResult_hi
= EXTRACT_16(vGatherResult
[selectedGather
], 1);
2466 Value
*temp_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
[selectedMask
]), vGatherTy
);
2467 Value
*temp_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
[selectedMask
]), vGatherTy
);
2469 // after pshufb mask for x channel; z uses the same shuffle from the second gather
2470 // 256i - 0 1 2 3 4 5 6 7
2471 // xx00 xx00 xx00 xx00 xx00 xx00 xx00 xx00
2473 // denormalize if needed
2474 if (conversionType
!= CONVERT_NONE
)
2476 temp_lo
= FMUL(CAST(fpCast
, temp_lo
, mSimdFP32Ty
), conversionFactor
);
2477 temp_hi
= FMUL(CAST(fpCast
, temp_hi
, mSimdFP32Ty
), conversionFactor
);
2480 vVertexElements
[currentVertexElement
] = JOIN_16(temp_lo
, temp_hi
);
2482 currentVertexElement
+= 1;
2486 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector16(compCtrl
[i
]);
2489 if (currentVertexElement
> 3)
2491 StoreVertexElements16(pVtxOut
, outputElt
++, 4, vVertexElements
);
2492 // reset to the next vVertexElement to output
2493 currentVertexElement
= 0;
2500 SWR_INVALID("Unsupported conversion type");
2505 #if USE_SIMD16_SHADERS
2506 void FetchJit::Shuffle16bpcGather(Shuffle16bpcArgs
&args
, bool useVertexID2
)
2508 void FetchJit::Shuffle16bpcGather(Shuffle16bpcArgs
&args
)
2511 // Unpack tuple args
2512 Value
* (&vGatherResult
)[2] = std::get
<0>(args
);
2513 Value
* pVtxOut
= std::get
<1>(args
);
2514 const Instruction::CastOps extendType
= std::get
<2>(args
);
2515 const ConversionType conversionType
= std::get
<3>(args
);
2516 uint32_t ¤tVertexElement
= std::get
<4>(args
);
2517 uint32_t &outputElt
= std::get
<5>(args
);
2518 const ComponentEnable compMask
= std::get
<6>(args
);
2519 const ComponentControl(&compCtrl
)[4] = std::get
<7>(args
);
2520 Value
* (&vVertexElements
)[4] = std::get
<8>(args
);
2523 Type
* vGatherTy
= VectorType::get(IntegerType::getInt32Ty(JM()->mContext
), mVWidth
);
2524 Type
* v32x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 4); // vwidth is units of 32 bits
2526 // have to do extra work for sign extending
2527 if ((extendType
== Instruction::CastOps::SExt
) || (extendType
== Instruction::CastOps::SIToFP
) ||
2528 (extendType
== Instruction::CastOps::FPExt
))
2530 // is this PP float?
2531 bool bFP
= (extendType
== Instruction::CastOps::FPExt
) ? true : false;
2533 Type
* v8x16Ty
= VectorType::get(mInt16Ty
, 8); // 8x16bit in a 128bit lane
2534 Type
* v128bitTy
= VectorType::get(IntegerType::getIntNTy(JM()->mContext
, 128), mVWidth
/ 4); // vwidth is units of 32 bits
2537 Value
* vConstMask
= C
<char>({ 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15,
2538 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15 });
2539 Value
* vi128XY
= nullptr;
2540 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1)) {
2541 Value
* vShufResult
= BITCAST(PSHUFB(BITCAST(vGatherResult
[0], v32x8Ty
), vConstMask
), vGatherTy
);
2542 // after pshufb: group components together in each 128bit lane
2543 // 256i - 0 1 2 3 4 5 6 7
2544 // xxxx xxxx yyyy yyyy xxxx xxxx yyyy yyyy
2546 vi128XY
= BITCAST(PERMD(vShufResult
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2547 // after PERMD: move and pack xy components into each 128bit lane
2548 // 256i - 0 1 2 3 4 5 6 7
2549 // xxxx xxxx xxxx xxxx yyyy yyyy yyyy yyyy
2552 // do the same for zw components
2553 Value
* vi128ZW
= nullptr;
2554 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3)) {
2555 Value
* vShufResult
= BITCAST(PSHUFB(BITCAST(vGatherResult
[1], v32x8Ty
), vConstMask
), vGatherTy
);
2556 vi128ZW
= BITCAST(PERMD(vShufResult
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2559 // init denormalize variables if needed
2560 Instruction::CastOps IntToFpCast
;
2561 Value
* conversionFactor
;
2563 switch (conversionType
)
2565 case CONVERT_NORMALIZED
:
2566 IntToFpCast
= Instruction::CastOps::SIToFP
;
2567 conversionFactor
= VIMMED1((float)(1.0 / 32767.0));
2569 case CONVERT_SSCALED
:
2570 IntToFpCast
= Instruction::CastOps::SIToFP
;
2571 conversionFactor
= VIMMED1((float)(1.0));
2573 case CONVERT_USCALED
:
2574 SWR_INVALID("Type should not be sign extended!");
2575 conversionFactor
= nullptr;
2578 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2579 conversionFactor
= nullptr;
2583 // sign extend all enabled components. If we have a fill vVertexElements, output to current simdvertex
2584 for (uint32_t i
= 0; i
< 4; i
++)
2586 if (isComponentEnabled(compMask
, i
))
2588 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2590 // if x or z, extract 128bits from lane 0, else for y or w, extract from lane 1
2591 uint32_t lane
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2592 // if x or y, use vi128XY permute result, else use vi128ZW
2593 Value
* selectedPermute
= (i
< 2) ? vi128XY
: vi128ZW
;
2596 // extract 128 bit lanes to sign extend each component
2597 vVertexElements
[currentVertexElement
] = CVTPH2PS(BITCAST(VEXTRACT(selectedPermute
, C(lane
)), v8x16Ty
));
2600 // extract 128 bit lanes to sign extend each component
2601 vVertexElements
[currentVertexElement
] = PMOVSXWD(BITCAST(VEXTRACT(selectedPermute
, C(lane
)), v8x16Ty
));
2603 // denormalize if needed
2604 if (conversionType
!= CONVERT_NONE
) {
2605 vVertexElements
[currentVertexElement
] = FMUL(CAST(IntToFpCast
, vVertexElements
[currentVertexElement
], mSimdFP32Ty
), conversionFactor
);
2608 currentVertexElement
++;
2612 #if USE_SIMD16_SHADERS
2613 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
2615 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
2619 if (currentVertexElement
> 3)
2621 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
2622 // reset to the next vVertexElement to output
2623 currentVertexElement
= 0;
2629 else if ((extendType
== Instruction::CastOps::ZExt
) || (extendType
== Instruction::CastOps::UIToFP
))
2631 // pshufb masks for each component
2632 Value
* vConstMask
[2];
2633 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 2)) {
2635 vConstMask
[0] = C
<char>({ 0, 1, -1, -1, 4, 5, -1, -1, 8, 9, -1, -1, 12, 13, -1, -1,
2636 0, 1, -1, -1, 4, 5, -1, -1, 8, 9, -1, -1, 12, 13, -1, -1, });
2639 if (isComponentEnabled(compMask
, 1) || isComponentEnabled(compMask
, 3)) {
2641 vConstMask
[1] = C
<char>({ 2, 3, -1, -1, 6, 7, -1, -1, 10, 11, -1, -1, 14, 15, -1, -1,
2642 2, 3, -1, -1, 6, 7, -1, -1, 10, 11, -1, -1, 14, 15, -1, -1 });
2645 // init denormalize variables if needed
2646 Instruction::CastOps fpCast
;
2647 Value
* conversionFactor
;
2649 switch (conversionType
)
2651 case CONVERT_NORMALIZED
:
2652 fpCast
= Instruction::CastOps::UIToFP
;
2653 conversionFactor
= VIMMED1((float)(1.0 / 65535.0));
2655 case CONVERT_USCALED
:
2656 fpCast
= Instruction::CastOps::UIToFP
;
2657 conversionFactor
= VIMMED1((float)(1.0f
));
2659 case CONVERT_SSCALED
:
2660 SWR_INVALID("Type should not be zero extended!");
2661 conversionFactor
= nullptr;
2664 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2665 conversionFactor
= nullptr;
2669 // shuffle enabled components into lower word of each 32bit lane, 0 extending to 32 bits
2670 for (uint32_t i
= 0; i
< 4; i
++)
2672 if (isComponentEnabled(compMask
, i
))
2674 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2676 // select correct constMask for x/z or y/w pshufb
2677 uint32_t selectedMask
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2678 // if x or y, use vi128XY permute result, else use vi128ZW
2679 uint32_t selectedGather
= (i
< 2) ? 0 : 1;
2681 vVertexElements
[currentVertexElement
] = BITCAST(PSHUFB(BITCAST(vGatherResult
[selectedGather
], v32x8Ty
), vConstMask
[selectedMask
]), vGatherTy
);
2682 // after pshufb mask for x channel; z uses the same shuffle from the second gather
2683 // 256i - 0 1 2 3 4 5 6 7
2684 // xx00 xx00 xx00 xx00 xx00 xx00 xx00 xx00
2686 // denormalize if needed
2687 if (conversionType
!= CONVERT_NONE
)
2689 vVertexElements
[currentVertexElement
] = FMUL(CAST(fpCast
, vVertexElements
[currentVertexElement
], mSimdFP32Ty
), conversionFactor
);
2691 currentVertexElement
++;
2695 #if USE_SIMD16_SHADERS
2696 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
2698 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
2702 if (currentVertexElement
> 3)
2704 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
2705 // reset to the next vVertexElement to output
2706 currentVertexElement
= 0;
2713 SWR_INVALID("Unsupported conversion type");
2718 //////////////////////////////////////////////////////////////////////////
2719 /// @brief Output a simdvertex worth of elements to the current outputElt
2720 /// @param pVtxOut - base address of VIN output struct
2721 /// @param outputElt - simdvertex offset in VIN to write to
2722 /// @param numEltsToStore - number of simdvertex rows to write out
2723 /// @param vVertexElements - LLVM Value*[] simdvertex to write out
2724 #if USE_SIMD16_GATHERS
2725 void FetchJit::StoreVertexElements16(Value
* pVtxOut
, const uint32_t outputElt
, const uint32_t numEltsToStore
, Value
* (&vVertexElements
)[4])
2727 SWR_ASSERT(numEltsToStore
<= 4, "Invalid element count.");
2729 for (uint32_t c
= 0; c
< numEltsToStore
; ++c
)
2731 // STORE expects FP32 x vWidth type, just bitcast if needed
2732 if (!vVertexElements
[c
]->getType()->getScalarType()->isFloatTy())
2734 #if FETCH_DUMP_VERTEX
2735 PRINT("vVertexElements[%d]: 0x%x\n", { C(c
), vVertexElements
[c
] });
2737 vVertexElements
[c
] = BITCAST(vVertexElements
[c
], mSimd16FP32Ty
);
2739 #if FETCH_DUMP_VERTEX
2742 PRINT("vVertexElements[%d]: %f\n", { C(c
), vVertexElements
[c
] });
2745 // outputElt * 4 = offsetting by the size of a simdvertex
2746 // + c offsets to a 32bit x vWidth row within the current vertex
2747 Value
* dest
= GEP(pVtxOut
, C(outputElt
* 4 + c
), "destGEP");
2748 STORE(vVertexElements
[c
], dest
);
2753 void FetchJit::StoreVertexElements(Value
* pVtxOut
, const uint32_t outputElt
, const uint32_t numEltsToStore
, Value
* (&vVertexElements
)[4])
2755 SWR_ASSERT(numEltsToStore
<= 4, "Invalid element count.");
2757 for (uint32_t c
= 0; c
< numEltsToStore
; ++c
)
2759 // STORE expects FP32 x vWidth type, just bitcast if needed
2760 if (!vVertexElements
[c
]->getType()->getScalarType()->isFloatTy())
2762 #if FETCH_DUMP_VERTEX
2763 PRINT("vVertexElements[%d]: 0x%x\n", { C(c
), vVertexElements
[c
] });
2765 vVertexElements
[c
] = BITCAST(vVertexElements
[c
], mSimdFP32Ty
);
2767 #if FETCH_DUMP_VERTEX
2770 PRINT("vVertexElements[%d]: %f\n", { C(c
), vVertexElements
[c
] });
2773 // outputElt * 4 = offsetting by the size of a simdvertex
2774 // + c offsets to a 32bit x vWidth row within the current vertex
2775 #if USE_SIMD16_SHADERS
2776 Value
* dest
= GEP(pVtxOut
, C(outputElt
* 8 + c
* 2), "destGEP");
2778 Value
* dest
= GEP(pVtxOut
, C(outputElt
* 4 + c
), "destGEP");
2780 STORE(vVertexElements
[c
], dest
);
2785 //////////////////////////////////////////////////////////////////////////
2786 /// @brief Generates a constant vector of values based on the
2787 /// ComponentControl value
2788 /// @param ctrl - ComponentControl value
2789 #if USE_SIMD16_GATHERS
2790 Value
*FetchJit::GenerateCompCtrlVector16(const ComponentControl ctrl
)
2795 return VUNDEF_I_16();
2797 return VIMMED1_16(0);
2799 return VIMMED1_16(1.0f
);
2801 return VIMMED1_16(1);
2804 Value
*pId_lo
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID
})), mSimdFP32Ty
);
2805 Value
*pId_hi
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID2
})), mSimdFP32Ty
);
2807 Value
*pId
= JOIN_16(pId_lo
, pId_hi
);
2811 case StoreInstanceId
:
2813 Value
*pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_CurInstance
})), mFP32Ty
);
2814 return VBROADCAST_16(pId
);
2820 SWR_INVALID("Invalid component control");
2821 return VUNDEF_I_16();
2826 #if USE_SIMD16_SHADERS
2827 Value
*FetchJit::GenerateCompCtrlVector(const ComponentControl ctrl
, bool useVertexID2
)
2829 Value
*FetchJit::GenerateCompCtrlVector(const ComponentControl ctrl
)
2839 return VIMMED1(1.0f
);
2844 #if USE_SIMD16_SHADERS
2848 pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID2
})), mSimdFP32Ty
);
2852 pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID
})), mSimdFP32Ty
);
2855 Value
*pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID
})), mSimdFP32Ty
);
2859 case StoreInstanceId
:
2861 Value
*pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_CurInstance
})), mFP32Ty
);
2862 return VBROADCAST(pId
);
2868 SWR_INVALID("Invalid component control");
2874 //////////////////////////////////////////////////////////////////////////
2875 /// @brief Returns the enable mask for the specified component.
2876 /// @param enableMask - enable bits
2877 /// @param component - component to check if enabled.
2878 bool isComponentEnabled(ComponentEnable enableMask
, uint8_t component
)
2883 case 0: return (enableMask
& ComponentEnable::X
);
2885 case 1: return (enableMask
& ComponentEnable::Y
);
2887 case 2: return (enableMask
& ComponentEnable::Z
);
2889 case 3: return (enableMask
& ComponentEnable::W
);
2891 default: return false;
2895 // Don't want two threads compiling the same fetch shader simultaneously
2896 // Has problems in the JIT cache implementation
2897 // This is only a problem for fetch right now.
2898 static std::mutex gFetchCodegenMutex
;
2900 //////////////////////////////////////////////////////////////////////////
2901 /// @brief JITs from fetch shader IR
2902 /// @param hJitMgr - JitManager handle
2903 /// @param func - LLVM function IR
2904 /// @return PFN_FETCH_FUNC - pointer to fetch code
2905 PFN_FETCH_FUNC
JitFetchFunc(HANDLE hJitMgr
, const HANDLE hFunc
)
2907 const llvm::Function
* func
= (const llvm::Function
*)hFunc
;
2908 JitManager
* pJitMgr
= reinterpret_cast<JitManager
*>(hJitMgr
);
2909 PFN_FETCH_FUNC pfnFetch
;
2911 gFetchCodegenMutex
.lock();
2912 pfnFetch
= (PFN_FETCH_FUNC
)(pJitMgr
->mpExec
->getFunctionAddress(func
->getName().str()));
2913 // MCJIT finalizes modules the first time you JIT code from them. After finalized, you cannot add new IR to the module
2914 pJitMgr
->mIsModuleFinalized
= true;
2916 #if defined(KNOB_SWRC_TRACING)
2918 const char *funcName
= func
->getName().data();
2919 sprintf(fName
, "%s.bin", funcName
);
2920 FILE *fd
= fopen(fName
, "wb");
2921 fwrite((void *)pfnFetch
, 1, 2048, fd
);
2925 pJitMgr
->DumpAsm(const_cast<llvm::Function
*>(func
), "final");
2926 gFetchCodegenMutex
.unlock();
2933 //////////////////////////////////////////////////////////////////////////
2934 /// @brief JIT compiles fetch shader
2935 /// @param hJitMgr - JitManager handle
2936 /// @param state - fetch state to build function from
2937 extern "C" PFN_FETCH_FUNC JITCALL
JitCompileFetch(HANDLE hJitMgr
, const FETCH_COMPILE_STATE
& state
)
2939 JitManager
* pJitMgr
= reinterpret_cast<JitManager
*>(hJitMgr
);
2941 pJitMgr
->SetupNewModule();
2943 FetchJit
theJit(pJitMgr
);
2944 HANDLE hFunc
= theJit
.Create(state
);
2946 return JitFetchFunc(hJitMgr
, hFunc
);