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 ******************************************************************************/
32 #include "fetch_jit.h"
33 #include "gen_state_llvm.h"
37 //#define FETCH_DUMP_VERTEX 1
39 using namespace SwrJit
;
41 bool isComponentEnabled(ComponentEnable enableMask
, uint8_t component
);
52 //////////////////////////////////////////////////////////////////////////
53 /// Interface to Jitting a fetch shader
54 //////////////////////////////////////////////////////////////////////////
55 struct FetchJit
: public Builder
57 FetchJit(JitManager
* pJitMgr
) : Builder(pJitMgr
){};
59 Function
* Create(const FETCH_COMPILE_STATE
& fetchState
);
60 Value
* GetSimdValid32bitIndices(Value
* vIndices
, Value
* pLastIndex
);
61 Value
* GetSimdValid16bitIndices(Value
* vIndices
, Value
* pLastIndex
);
62 Value
* GetSimdValid8bitIndices(Value
* vIndices
, Value
* pLastIndex
);
64 // package up Shuffle*bpcGatherd args into a tuple for convenience
65 typedef std::tuple
<Value
*&, Value
*, const Instruction::CastOps
, const ConversionType
,
66 uint32_t&, uint32_t&, const ComponentEnable
, const ComponentControl(&)[4], Value
*(&)[4],
67 const uint32_t(&)[4]> Shuffle8bpcArgs
;
68 #if USE_SIMD16_SHADERS
69 void Shuffle8bpcGatherd(Shuffle8bpcArgs
&args
, bool useVertexID2
);
71 void Shuffle8bpcGatherd(Shuffle8bpcArgs
&args
);
73 #if USE_SIMD16_BUILDER
74 void Shuffle8bpcGatherd2(Shuffle8bpcArgs
&args
);
77 typedef std::tuple
<Value
*(&)[2], Value
*, const Instruction::CastOps
, const ConversionType
,
78 uint32_t&, uint32_t&, const ComponentEnable
, const ComponentControl(&)[4], Value
*(&)[4]> Shuffle16bpcArgs
;
79 #if USE_SIMD16_SHADERS
80 void Shuffle16bpcGather(Shuffle16bpcArgs
&args
, bool useVertexID2
);
82 void Shuffle16bpcGather(Shuffle16bpcArgs
&args
);
84 #if USE_SIMD16_BUILDER
85 void Shuffle16bpcGather2(Shuffle16bpcArgs
&args
);
88 void StoreVertexElements(Value
* pVtxOut
, const uint32_t outputElt
, const uint32_t numEltsToStore
, Value
* (&vVertexElements
)[4]);
89 #if USE_SIMD16_BUILDER
90 void StoreVertexElements2(Value
* pVtxOut
, const uint32_t outputElt
, const uint32_t numEltsToStore
, Value
* (&vVertexElements
)[4]);
93 #if USE_SIMD16_SHADERS
94 Value
* GenerateCompCtrlVector(const ComponentControl ctrl
, bool useVertexID2
);
96 Value
* GenerateCompCtrlVector(const ComponentControl ctrl
);
98 #if USE_SIMD16_BUILDER
99 Value
* GenerateCompCtrlVector2(const ComponentControl ctrl
);
102 void JitLoadVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
* streams
, Value
* vIndices
, Value
* pVtxOut
);
103 #if USE_SIMD16_SHADERS
104 #define USE_SIMD16_GATHERS 0
106 #if USE_SIMD16_GATHERS
107 void JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
*streams
, Value
*vIndices
, Value
*vIndices2
, Value
*pVtxOut
, bool useVertexID2
);
109 void JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
* streams
, Value
* vIndices
, Value
* pVtxOut
, bool useVertexID2
);
112 void JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
* streams
, Value
* vIndices
, Value
* pVtxOut
);
115 bool IsOddFormat(SWR_FORMAT format
);
116 bool IsUniformFormat(SWR_FORMAT format
);
117 void UnpackComponents(SWR_FORMAT format
, Value
* vInput
, Value
* result
[4]);
118 void CreateGatherOddFormats(SWR_FORMAT format
, Value
* pMask
, Value
* pBase
, Value
* offsets
, Value
* result
[4]);
119 void ConvertFormat(SWR_FORMAT format
, Value
*texels
[4]);
124 Function
* FetchJit::Create(const FETCH_COMPILE_STATE
& fetchState
)
126 std::stringstream
fnName("FetchShader_", std::ios_base::in
| std::ios_base::out
| std::ios_base::ate
);
127 fnName
<< ComputeCRC(0, &fetchState
, sizeof(fetchState
));
129 Function
* fetch
= Function::Create(JM()->mFetchShaderTy
, GlobalValue::ExternalLinkage
, fnName
.str(), JM()->mpCurrentModule
);
130 BasicBlock
* entry
= BasicBlock::Create(JM()->mContext
, "entry", fetch
);
132 fetch
->getParent()->setModuleIdentifier(fetch
->getName());
134 IRB()->SetInsertPoint(entry
);
136 auto argitr
= fetch
->arg_begin();
138 // Fetch shader arguments
139 mpFetchInfo
= &*argitr
; ++argitr
;
140 mpFetchInfo
->setName("fetchInfo");
141 Value
* pVtxOut
= &*argitr
;
142 pVtxOut
->setName("vtxOutput");
143 // this is just shorthand to tell LLVM to get a pointer to the base address of simdvertex
144 // index 0(just the pointer to the simdvertex structure
145 // index 1(which element of the simdvertex structure to offset to(in this case 0)
146 // so the indices being i32's doesn't matter
147 // TODO: generated this GEP with a VECTOR structure type so this makes sense
148 std::vector
<Value
*> vtxInputIndices(2, C(0));
150 pVtxOut
= GEP(pVtxOut
, C(0));
151 #if USE_SIMD16_SHADERS
152 #if 0// USE_SIMD16_BUILDER
153 pVtxOut
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
155 pVtxOut
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth
), 0));
158 pVtxOut
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth
), 0));
161 // SWR_FETCH_CONTEXT::pStreams
162 Value
* streams
= LOAD(mpFetchInfo
,{0, SWR_FETCH_CONTEXT_pStreams
});
163 streams
->setName("pStreams");
165 // SWR_FETCH_CONTEXT::pIndices
166 Value
* indices
= LOAD(mpFetchInfo
,{0, SWR_FETCH_CONTEXT_pIndices
});
167 indices
->setName("pIndices");
169 // SWR_FETCH_CONTEXT::pLastIndex
170 Value
* pLastIndex
= LOAD(mpFetchInfo
,{0, SWR_FETCH_CONTEXT_pLastIndex
});
171 pLastIndex
->setName("pLastIndex");
175 #if USE_SIMD16_SHADERS
179 switch(fetchState
.indexType
)
182 indices
= BITCAST(indices
, Type::getInt8PtrTy(JM()->mContext
, 0));
183 #if USE_SIMD16_SHADERS
184 indices2
= GEP(indices
, C(8));
186 if(fetchState
.bDisableIndexOOBCheck
)
188 vIndices
= LOAD(BITCAST(indices
, PointerType::get(VectorType::get(mInt8Ty
, mpJitMgr
->mVWidth
), 0)), {(uint32_t)0});
189 vIndices
= Z_EXT(vIndices
, mSimdInt32Ty
);
190 #if USE_SIMD16_SHADERS
191 vIndices2
= LOAD(BITCAST(indices2
, PointerType::get(VectorType::get(mInt8Ty
, mpJitMgr
->mVWidth
), 0)), { (uint32_t)0 });
192 vIndices2
= Z_EXT(vIndices2
, mSimdInt32Ty
);
197 pLastIndex
= BITCAST(pLastIndex
, Type::getInt8PtrTy(JM()->mContext
, 0));
198 vIndices
= GetSimdValid8bitIndices(indices
, pLastIndex
);
199 #if USE_SIMD16_SHADERS
200 pLastIndex
= BITCAST(pLastIndex
, Type::getInt8PtrTy(JM()->mContext
, 0));
201 vIndices2
= GetSimdValid8bitIndices(indices2
, pLastIndex
);
206 indices
= BITCAST(indices
, Type::getInt16PtrTy(JM()->mContext
, 0));
207 #if USE_SIMD16_SHADERS
208 indices2
= GEP(indices
, C(8));
210 if(fetchState
.bDisableIndexOOBCheck
)
212 vIndices
= LOAD(BITCAST(indices
, PointerType::get(VectorType::get(mInt16Ty
, mpJitMgr
->mVWidth
), 0)), {(uint32_t)0});
213 vIndices
= Z_EXT(vIndices
, mSimdInt32Ty
);
214 #if USE_SIMD16_SHADERS
215 vIndices2
= LOAD(BITCAST(indices2
, PointerType::get(VectorType::get(mInt16Ty
, mpJitMgr
->mVWidth
), 0)), { (uint32_t)0 });
216 vIndices2
= Z_EXT(vIndices2
, mSimdInt32Ty
);
221 pLastIndex
= BITCAST(pLastIndex
, Type::getInt16PtrTy(JM()->mContext
, 0));
222 vIndices
= GetSimdValid16bitIndices(indices
, pLastIndex
);
223 #if USE_SIMD16_SHADERS
224 pLastIndex
= BITCAST(pLastIndex
, Type::getInt16PtrTy(JM()->mContext
, 0));
225 vIndices2
= GetSimdValid16bitIndices(indices2
, pLastIndex
);
230 #if USE_SIMD16_SHADERS
231 indices2
= GEP(indices
, C(8));
233 (fetchState
.bDisableIndexOOBCheck
) ? vIndices
= LOAD(BITCAST(indices
, PointerType::get(mSimdInt32Ty
,0)),{(uint32_t)0})
234 : vIndices
= GetSimdValid32bitIndices(indices
, pLastIndex
);
235 #if USE_SIMD16_SHADERS
236 (fetchState
.bDisableIndexOOBCheck
) ? vIndices2
= LOAD(BITCAST(indices2
, PointerType::get(mSimdInt32Ty
, 0)), { (uint32_t)0 })
237 : vIndices2
= GetSimdValid32bitIndices(indices2
, pLastIndex
);
239 break; // incoming type is already 32bit int
240 default: SWR_INVALID("Unsupported index type"); vIndices
= nullptr; break;
243 if(fetchState
.bForceSequentialAccessEnable
)
245 Value
* pOffsets
= C({ 0, 1, 2, 3, 4, 5, 6, 7 });
247 // VertexData buffers are accessed sequentially, the index is equal to the vertex number
248 vIndices
= VBROADCAST(LOAD(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_StartVertex
}));
249 vIndices
= ADD(vIndices
, pOffsets
);
250 #if USE_SIMD16_SHADERS
251 vIndices2
= ADD(vIndices
, VIMMED1(8));
255 Value
* vVertexId
= vIndices
;
256 #if USE_SIMD16_SHADERS
257 Value
* vVertexId2
= vIndices2
;
259 if (fetchState
.bVertexIDOffsetEnable
)
261 // Assuming one of baseVertex or startVertex is 0, so adding both should be functionally correct
262 Value
* vBaseVertex
= VBROADCAST(LOAD(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_BaseVertex
}));
263 Value
* vStartVertex
= VBROADCAST(LOAD(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_StartVertex
}));
264 vVertexId
= ADD(vIndices
, vBaseVertex
);
265 vVertexId
= ADD(vVertexId
, vStartVertex
);
266 #if USE_SIMD16_SHADERS
267 vVertexId2
= ADD(vIndices2
, vBaseVertex
);
268 vVertexId2
= ADD(vVertexId2
, vStartVertex
);
272 // store out vertex IDs
273 STORE(vVertexId
, GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID
}));
274 #if USE_SIMD16_SHADERS
275 STORE(vVertexId2
, GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID2
}));
278 // store out cut mask if enabled
279 if (fetchState
.bEnableCutIndex
)
281 Value
* vCutIndex
= VIMMED1(fetchState
.cutIndex
);
282 Value
* cutMask
= VMASK(ICMP_EQ(vIndices
, vCutIndex
));
283 STORE(cutMask
, GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_CutMask
}));
284 #if USE_SIMD16_SHADERS
285 Value
* cutMask2
= VMASK(ICMP_EQ(vIndices2
, vCutIndex
));
286 STORE(cutMask2
, GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_CutMask2
}));
290 // Fetch attributes from memory and output to a simdvertex struct
291 // since VGATHER has a perf penalty on HSW vs BDW, allow client to choose which fetch method to use
292 #if USE_SIMD16_SHADERS
293 if (fetchState
.bDisableVGATHER
)
295 JitLoadVertices(fetchState
, streams
, vIndices
, pVtxOut
);
296 JitLoadVertices(fetchState
, streams
, vIndices2
, GEP(pVtxOut
, C(1)));
300 #if USE_SIMD16_GATHERS
301 JitGatherVertices(fetchState
, streams
, vIndices
, vIndices2
, pVtxOut
, false);
303 JitGatherVertices(fetchState
, streams
, vIndices
, pVtxOut
, false);
304 JitGatherVertices(fetchState
, streams
, vIndices2
, GEP(pVtxOut
, C(1)), true);
308 (fetchState
.bDisableVGATHER
) ? JitLoadVertices(fetchState
, streams
, vIndices
, pVtxOut
)
309 : JitGatherVertices(fetchState
, streams
, vIndices
, pVtxOut
);
314 JitManager::DumpToFile(fetch
, "src");
317 verifyFunction(*fetch
);
320 ::FunctionPassManager
setupPasses(JM()->mpCurrentModule
);
322 ///@todo We don't need the CFG passes for fetch. (e.g. BreakCriticalEdges and CFGSimplification)
323 setupPasses
.add(createBreakCriticalEdgesPass());
324 setupPasses
.add(createCFGSimplificationPass());
325 setupPasses
.add(createEarlyCSEPass());
326 setupPasses
.add(createPromoteMemoryToRegisterPass());
328 setupPasses
.run(*fetch
);
330 JitManager::DumpToFile(fetch
, "se");
332 ::FunctionPassManager
optPasses(JM()->mpCurrentModule
);
334 ///@todo Haven't touched these either. Need to remove some of these and add others.
335 optPasses
.add(createCFGSimplificationPass());
336 optPasses
.add(createEarlyCSEPass());
337 optPasses
.add(createInstructionCombiningPass());
338 optPasses
.add(createInstructionSimplifierPass());
339 optPasses
.add(createConstantPropagationPass());
340 optPasses
.add(createSCCPPass());
341 optPasses
.add(createAggressiveDCEPass());
343 optPasses
.run(*fetch
);
344 optPasses
.run(*fetch
);
346 JitManager::DumpToFile(fetch
, "opt");
351 //////////////////////////////////////////////////////////////////////////
352 /// @brief Loads attributes from memory using LOADs, shuffling the
353 /// components into SOA form.
354 /// *Note* currently does not support component control,
355 /// component packing, instancing
356 /// @param fetchState - info about attributes to be fetched from memory
357 /// @param streams - value pointer to the current vertex stream
358 /// @param vIndices - vector value of indices to load
359 /// @param pVtxOut - value pointer to output simdvertex struct
360 void FetchJit::JitLoadVertices(const FETCH_COMPILE_STATE
&fetchState
, Value
* streams
, Value
* vIndices
, Value
* pVtxOut
)
362 // Zack shuffles; a variant of the Charleston.
364 std::vector
<Value
*> vectors(16);
365 std::vector
<Constant
*> pMask(mVWidth
);
366 for(uint32_t i
= 0; i
< mVWidth
; ++i
)
368 pMask
[i
] = (C(i
< 4 ? i
: 4));
370 Constant
* promoteMask
= ConstantVector::get(pMask
);
371 Constant
* uwvec
= UndefValue::get(VectorType::get(mFP32Ty
, 4));
373 Value
* startVertex
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_StartVertex
});
374 Value
* startInstance
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_StartInstance
});
375 Value
* curInstance
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_CurInstance
});
376 Value
* vBaseVertex
= VBROADCAST(LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_BaseVertex
}));
377 curInstance
->setName("curInstance");
379 for(uint32_t nelt
= 0; nelt
< fetchState
.numAttribs
; ++nelt
)
381 Value
* elements
[4] = {0};
382 const INPUT_ELEMENT_DESC
& ied
= fetchState
.layout
[nelt
];
383 const SWR_FORMAT_INFO
&info
= GetFormatInfo((SWR_FORMAT
)ied
.Format
);
384 SWR_ASSERT((info
.bpp
!= 0), "Unsupported format in JitLoadVertices.");
385 uint32_t numComponents
= info
.numComps
;
386 uint32_t bpc
= info
.bpp
/ info
.numComps
; ///@todo Code below assumes all components are same size. Need to fix.
388 // load path doesn't support component packing
389 SWR_ASSERT(ied
.ComponentPacking
== ComponentEnable::XYZW
, "Fetch load path doesn't support component packing.");
393 if (fetchState
.bInstanceIDOffsetEnable
)
395 SWR_ASSERT((0), "TODO: Fill out more once driver sends this down");
400 if(ied
.InstanceEnable
)
402 Value
* stepRate
= C(ied
.InstanceAdvancementState
);
404 // prevent a div by 0 for 0 step rate
405 Value
* isNonZeroStep
= ICMP_UGT(stepRate
, C(0));
406 stepRate
= SELECT(isNonZeroStep
, stepRate
, C(1));
408 // calc the current offset into instanced data buffer
409 Value
* calcInstance
= UDIV(curInstance
, stepRate
);
411 // if step rate is 0, every instance gets instance 0
412 calcInstance
= SELECT(isNonZeroStep
, calcInstance
, C(0));
414 vCurIndices
= VBROADCAST(calcInstance
);
416 startOffset
= startInstance
;
418 else if (ied
.InstanceStrideEnable
)
420 SWR_ASSERT((0), "TODO: Fill out more once driver sends this down.");
424 // offset indices by baseVertex
425 vCurIndices
= ADD(vIndices
, vBaseVertex
);
427 startOffset
= startVertex
;
430 // load SWR_VERTEX_BUFFER_STATE::pData
431 Value
*stream
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_pData
});
433 // load SWR_VERTEX_BUFFER_STATE::pitch
434 Value
*stride
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_pitch
});
435 stride
= Z_EXT(stride
, mInt64Ty
);
437 // load SWR_VERTEX_BUFFER_STATE::size
438 Value
*size
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_size
});
439 size
= Z_EXT(size
, mInt64Ty
);
441 Value
* startVertexOffset
= MUL(Z_EXT(startOffset
, mInt64Ty
), stride
);
443 Value
*minVertex
= NULL
;
444 Value
*minVertexOffset
= NULL
;
445 if (fetchState
.bPartialVertexBuffer
) {
446 // fetch min index for low bounds checking
447 minVertex
= GEP(streams
, {C(ied
.StreamIndex
), C(SWR_VERTEX_BUFFER_STATE_minVertex
)});
448 minVertex
= LOAD(minVertex
);
449 if (!fetchState
.bDisableIndexOOBCheck
) {
450 minVertexOffset
= MUL(Z_EXT(minVertex
, mInt64Ty
), stride
);
454 // Load from the stream.
455 for(uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
458 Value
* index
= VEXTRACT(vCurIndices
, C(lane
));
460 if (fetchState
.bPartialVertexBuffer
) {
461 // clamp below minvertex
462 Value
*isBelowMin
= ICMP_SLT(index
, minVertex
);
463 index
= SELECT(isBelowMin
, minVertex
, index
);
466 index
= Z_EXT(index
, mInt64Ty
);
468 Value
* offset
= MUL(index
, stride
);
469 offset
= ADD(offset
, C((int64_t)ied
.AlignedByteOffset
));
470 offset
= ADD(offset
, startVertexOffset
);
472 if (!fetchState
.bDisableIndexOOBCheck
) {
473 // check for out of bound access, including partial OOB, and replace them with minVertex
474 Value
*endOffset
= ADD(offset
, C((int64_t)info
.Bpp
));
475 Value
*oob
= ICMP_ULE(endOffset
, size
);
476 if (fetchState
.bPartialVertexBuffer
) {
477 offset
= SELECT(oob
, offset
, minVertexOffset
);
479 offset
= SELECT(oob
, offset
, ConstantInt::get(mInt64Ty
, 0));
483 Value
* pointer
= GEP(stream
, offset
);
484 // We use a full-lane, but don't actually care.
487 // get a pointer to a 4 component attrib in default address space
490 case 8: vptr
= BITCAST(pointer
, PointerType::get(VectorType::get(mInt8Ty
, 4), 0)); break;
491 case 16: vptr
= BITCAST(pointer
, PointerType::get(VectorType::get(mInt16Ty
, 4), 0)); break;
492 case 32: vptr
= BITCAST(pointer
, PointerType::get(VectorType::get(mFP32Ty
, 4), 0)); break;
493 default: SWR_INVALID("Unsupported underlying bpp!");
496 // load 4 components of attribute
497 Value
* vec
= ALIGNED_LOAD(vptr
, 1, false);
499 // Convert To FP32 internally
506 vec
= UI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
507 vec
= FMUL(vec
, ConstantVector::get(std::vector
<Constant
*>(4, ConstantFP::get(mFP32Ty
, 1.0 / 255.0))));
510 vec
= UI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
511 vec
= FMUL(vec
, ConstantVector::get(std::vector
<Constant
*>(4, ConstantFP::get(mFP32Ty
, 1.0 / 65535.0))));
514 SWR_INVALID("Unsupported underlying type!");
522 vec
= SI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
523 vec
= FMUL(vec
, ConstantVector::get(std::vector
<Constant
*>(4, ConstantFP::get(mFP32Ty
, 1.0 / 128.0))));
526 vec
= SI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
527 vec
= FMUL(vec
, ConstantVector::get(std::vector
<Constant
*>(4, ConstantFP::get(mFP32Ty
, 1.0 / 32768.0))));
530 SWR_INVALID("Unsupported underlying type!");
535 // Zero extend uint32_t types.
540 vec
= Z_EXT(vec
, VectorType::get(mInt32Ty
, 4));
541 vec
= BITCAST(vec
, VectorType::get(mFP32Ty
, 4));
544 break; // Pass through unchanged.
546 SWR_INVALID("Unsupported underlying type!");
551 // Sign extend SINT types.
556 vec
= S_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!");
570 break; // Pass through unchanged.
572 SWR_INVALID("Unsupported underlying type!");
575 case SWR_TYPE_USCALED
:
576 vec
= UI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
578 case SWR_TYPE_SSCALED
:
579 vec
= SI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4));
581 case SWR_TYPE_SFIXED
:
582 vec
= FMUL(SI_TO_FP(vec
, VectorType::get(mFP32Ty
, 4)), VBROADCAST(C(1/65536.0f
)));
584 case SWR_TYPE_UNKNOWN
:
585 case SWR_TYPE_UNUSED
:
586 SWR_INVALID("Unsupported type %d!", info
.type
[0]);
589 // promote mask: sse(0,1,2,3) | avx(0,1,2,3,4,4,4,4)
590 // uwvec: 4 x F32, undef value
591 Value
* wvec
= VSHUFFLE(vec
, uwvec
, promoteMask
);
592 vectors
.push_back(wvec
);
595 std::vector
<Constant
*> v01Mask(mVWidth
);
596 std::vector
<Constant
*> v23Mask(mVWidth
);
597 std::vector
<Constant
*> v02Mask(mVWidth
);
598 std::vector
<Constant
*> v13Mask(mVWidth
);
600 // Concatenate the vectors together.
601 elements
[0] = VUNDEF_F();
602 elements
[1] = VUNDEF_F();
603 elements
[2] = VUNDEF_F();
604 elements
[3] = VUNDEF_F();
605 for(uint32_t b
= 0, num4Wide
= mVWidth
/ 4; b
< num4Wide
; ++b
)
607 v01Mask
[4 * b
+ 0] = C(0 + 4 * b
);
608 v01Mask
[4 * b
+ 1] = C(1 + 4 * b
);
609 v01Mask
[4 * b
+ 2] = C(0 + 4 * b
+ mVWidth
);
610 v01Mask
[4 * b
+ 3] = C(1 + 4 * b
+ mVWidth
);
612 v23Mask
[4 * b
+ 0] = C(2 + 4 * b
);
613 v23Mask
[4 * b
+ 1] = C(3 + 4 * b
);
614 v23Mask
[4 * b
+ 2] = C(2 + 4 * b
+ mVWidth
);
615 v23Mask
[4 * b
+ 3] = C(3 + 4 * b
+ mVWidth
);
617 v02Mask
[4 * b
+ 0] = C(0 + 4 * b
);
618 v02Mask
[4 * b
+ 1] = C(2 + 4 * b
);
619 v02Mask
[4 * b
+ 2] = C(0 + 4 * b
+ mVWidth
);
620 v02Mask
[4 * b
+ 3] = C(2 + 4 * b
+ mVWidth
);
622 v13Mask
[4 * b
+ 0] = C(1 + 4 * b
);
623 v13Mask
[4 * b
+ 1] = C(3 + 4 * b
);
624 v13Mask
[4 * b
+ 2] = C(1 + 4 * b
+ mVWidth
);
625 v13Mask
[4 * b
+ 3] = C(3 + 4 * b
+ mVWidth
);
627 std::vector
<Constant
*> iMask(mVWidth
);
628 for(uint32_t i
= 0; i
< mVWidth
; ++i
)
630 if(((4 * b
) <= i
) && (i
< (4 * (b
+ 1))))
632 iMask
[i
] = C(i
% 4 + mVWidth
);
639 Constant
* insertMask
= ConstantVector::get(iMask
);
640 elements
[0] = VSHUFFLE(elements
[0], vectors
[4 * b
+ 0], insertMask
);
641 elements
[1] = VSHUFFLE(elements
[1], vectors
[4 * b
+ 1], insertMask
);
642 elements
[2] = VSHUFFLE(elements
[2], vectors
[4 * b
+ 2], insertMask
);
643 elements
[3] = VSHUFFLE(elements
[3], vectors
[4 * b
+ 3], insertMask
);
646 Value
* x0y0x1y1
= VSHUFFLE(elements
[0], elements
[1], ConstantVector::get(v01Mask
));
647 Value
* x2y2x3y3
= VSHUFFLE(elements
[2], elements
[3], ConstantVector::get(v01Mask
));
648 Value
* z0w0z1w1
= VSHUFFLE(elements
[0], elements
[1], ConstantVector::get(v23Mask
));
649 Value
* z2w3z2w3
= VSHUFFLE(elements
[2], elements
[3], ConstantVector::get(v23Mask
));
650 elements
[0] = VSHUFFLE(x0y0x1y1
, x2y2x3y3
, ConstantVector::get(v02Mask
));
651 elements
[1] = VSHUFFLE(x0y0x1y1
, x2y2x3y3
, ConstantVector::get(v13Mask
));
652 elements
[2] = VSHUFFLE(z0w0z1w1
, z2w3z2w3
, ConstantVector::get(v02Mask
));
653 elements
[3] = VSHUFFLE(z0w0z1w1
, z2w3z2w3
, ConstantVector::get(v13Mask
));
655 switch(numComponents
+ 1)
657 case 1: elements
[0] = VIMMED1(0.0f
);
658 case 2: elements
[1] = VIMMED1(0.0f
);
659 case 3: elements
[2] = VIMMED1(0.0f
);
660 case 4: elements
[3] = VIMMED1(1.0f
);
663 for(uint32_t c
= 0; c
< 4; ++c
)
665 #if USE_SIMD16_SHADERS
666 Value
* dest
= GEP(pVtxOut
, C(nelt
* 8 + c
* 2), "destGEP");
668 Value
* dest
= GEP(pVtxOut
, C(nelt
* 4 + c
), "destGEP");
670 STORE(elements
[c
], dest
);
675 // returns true for odd formats that require special state.gather handling
676 bool FetchJit::IsOddFormat(SWR_FORMAT format
)
678 const SWR_FORMAT_INFO
& info
= GetFormatInfo(format
);
679 if (info
.bpc
[0] != 8 && info
.bpc
[0] != 16 && info
.bpc
[0] != 32 && info
.bpc
[0] != 64)
686 // format is uniform if all components are the same size and type
687 bool FetchJit::IsUniformFormat(SWR_FORMAT format
)
689 const SWR_FORMAT_INFO
& info
= GetFormatInfo(format
);
690 uint32_t bpc0
= info
.bpc
[0];
691 uint32_t type0
= info
.type
[0];
693 for (uint32_t c
= 1; c
< info
.numComps
; ++c
)
695 if (bpc0
!= info
.bpc
[c
] || type0
!= info
.type
[c
])
703 // unpacks components based on format
704 // foreach component in the pixel
705 // mask off everything but this component
706 // shift component to LSB
707 void FetchJit::UnpackComponents(SWR_FORMAT format
, Value
* vInput
, Value
* result
[4])
709 const SWR_FORMAT_INFO
& info
= GetFormatInfo(format
);
711 uint32_t bitOffset
= 0;
712 for (uint32_t c
= 0; c
< info
.numComps
; ++c
)
714 uint32_t swizzledIndex
= info
.swizzle
[c
];
715 uint32_t compBits
= info
.bpc
[c
];
716 uint32_t bitmask
= ((1 << compBits
) - 1) << bitOffset
;
717 Value
* comp
= AND(vInput
, bitmask
);
718 comp
= LSHR(comp
, bitOffset
);
720 result
[swizzledIndex
] = comp
;
721 bitOffset
+= compBits
;
725 // gather for odd component size formats
726 // gather SIMD full pixels per lane then shift/mask to move each component to their
728 void FetchJit::CreateGatherOddFormats(SWR_FORMAT format
, Value
* pMask
, Value
* pBase
, Value
* pOffsets
, Value
* pResult
[4])
730 const SWR_FORMAT_INFO
&info
= GetFormatInfo(format
);
732 // only works if pixel size is <= 32bits
733 SWR_ASSERT(info
.bpp
<= 32);
735 Value
*pGather
= GATHERDD(VIMMED1(0), pBase
, pOffsets
, pMask
);
737 for (uint32_t comp
= 0; comp
< 4; ++comp
)
739 pResult
[comp
] = VIMMED1((int)info
.defaults
[comp
]);
742 UnpackComponents(format
, pGather
, pResult
);
745 pResult
[0] = BITCAST(pResult
[0], mSimdFP32Ty
);
746 pResult
[1] = BITCAST(pResult
[1], mSimdFP32Ty
);
747 pResult
[2] = BITCAST(pResult
[2], mSimdFP32Ty
);
748 pResult
[3] = BITCAST(pResult
[3], mSimdFP32Ty
);
751 void FetchJit::ConvertFormat(SWR_FORMAT format
, Value
*texels
[4])
753 const SWR_FORMAT_INFO
&info
= GetFormatInfo(format
);
755 for (uint32_t c
= 0; c
< info
.numComps
; ++c
)
757 uint32_t compIndex
= info
.swizzle
[c
];
759 // skip any conversion on UNUSED components
760 if (info
.type
[c
] == SWR_TYPE_UNUSED
)
765 if (info
.isNormalized
[c
])
767 if (info
.type
[c
] == SWR_TYPE_SNORM
)
769 /// @todo The most-negative value maps to -1.0f. e.g. the 5-bit value 10000 maps to -1.0f.
771 /// result = c * (1.0f / (2^(n-1) - 1);
772 uint32_t n
= info
.bpc
[c
];
773 uint32_t pow2
= 1 << (n
- 1);
774 float scale
= 1.0f
/ (float)(pow2
- 1);
775 Value
*vScale
= VIMMED1(scale
);
776 texels
[compIndex
] = BITCAST(texels
[compIndex
], mSimdInt32Ty
);
777 texels
[compIndex
] = SI_TO_FP(texels
[compIndex
], mSimdFP32Ty
);
778 texels
[compIndex
] = FMUL(texels
[compIndex
], vScale
);
782 SWR_ASSERT(info
.type
[c
] == SWR_TYPE_UNORM
);
784 /// result = c * (1.0f / (2^n - 1))
785 uint32_t n
= info
.bpc
[c
];
786 uint32_t pow2
= 1 << n
;
787 // special case 24bit unorm format, which requires a full divide to meet ULP requirement
790 float scale
= (float)(pow2
- 1);
791 Value
* vScale
= VIMMED1(scale
);
792 texels
[compIndex
] = BITCAST(texels
[compIndex
], mSimdInt32Ty
);
793 texels
[compIndex
] = SI_TO_FP(texels
[compIndex
], mSimdFP32Ty
);
794 texels
[compIndex
] = FDIV(texels
[compIndex
], vScale
);
798 float scale
= 1.0f
/ (float)(pow2
- 1);
799 Value
*vScale
= VIMMED1(scale
);
800 texels
[compIndex
] = BITCAST(texels
[compIndex
], mSimdInt32Ty
);
801 texels
[compIndex
] = UI_TO_FP(texels
[compIndex
], mSimdFP32Ty
);
802 texels
[compIndex
] = FMUL(texels
[compIndex
], vScale
);
810 //////////////////////////////////////////////////////////////////////////
811 /// @brief Loads attributes from memory using AVX2 GATHER(s)
812 /// @param fetchState - info about attributes to be fetched from memory
813 /// @param streams - value pointer to the current vertex stream
814 /// @param vIndices - vector value of indices to gather
815 /// @param pVtxOut - value pointer to output simdvertex struct
816 #if USE_SIMD16_SHADERS
817 #if USE_SIMD16_GATHERS
818 void FetchJit::JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
,
819 Value
*streams
, Value
*vIndices
, Value
*vIndices2
, Value
*pVtxOut
, bool useVertexID2
)
821 void FetchJit::JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
,
822 Value
* streams
, Value
* vIndices
, Value
* pVtxOut
, bool useVertexID2
)
825 void FetchJit::JitGatherVertices(const FETCH_COMPILE_STATE
&fetchState
,
826 Value
* streams
, Value
* vIndices
, Value
* pVtxOut
)
829 uint32_t currentVertexElement
= 0;
830 uint32_t outputElt
= 0;
831 Value
* vVertexElements
[4];
832 #if USE_SIMD16_GATHERS
833 Value
* vVertexElements2
[4];
834 #if USE_SIMD16_BUILDER
839 Value
* startVertex
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_StartVertex
});
840 Value
* startInstance
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_StartInstance
});
841 Value
* curInstance
= LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_CurInstance
});
842 Value
* vBaseVertex
= VBROADCAST(LOAD(mpFetchInfo
, {0, SWR_FETCH_CONTEXT_BaseVertex
}));
843 curInstance
->setName("curInstance");
845 for (uint32_t nInputElt
= 0; nInputElt
< fetchState
.numAttribs
; nInputElt
+= 1)
847 const INPUT_ELEMENT_DESC
& ied
= fetchState
.layout
[nInputElt
];
849 // skip element if all components are disabled
850 if (ied
.ComponentPacking
== ComponentEnable::NONE
)
855 const SWR_FORMAT_INFO
&info
= GetFormatInfo((SWR_FORMAT
)ied
.Format
);
856 SWR_ASSERT((info
.bpp
!= 0), "Unsupported format in JitGatherVertices.");
857 uint32_t bpc
= info
.bpp
/ info
.numComps
; ///@todo Code below assumes all components are same size. Need to fix.
859 Value
*stream
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_pData
});
861 // VGATHER* takes an *i8 src pointer
862 Value
* pStreamBase
= BITCAST(stream
, PointerType::get(mInt8Ty
, 0));
864 Value
*stride
= LOAD(streams
, {ied
.StreamIndex
, SWR_VERTEX_BUFFER_STATE_pitch
});
865 Value
*vStride
= VBROADCAST(stride
);
867 // max vertex index that is fully in bounds
868 Value
*maxVertex
= GEP(streams
, {C(ied
.StreamIndex
), C(SWR_VERTEX_BUFFER_STATE_maxVertex
)});
869 maxVertex
= LOAD(maxVertex
);
871 Value
*minVertex
= NULL
;
872 if (fetchState
.bPartialVertexBuffer
)
874 // min vertex index for low bounds OOB checking
875 minVertex
= GEP(streams
, {C(ied
.StreamIndex
), C(SWR_VERTEX_BUFFER_STATE_minVertex
)});
876 minVertex
= LOAD(minVertex
);
879 if (fetchState
.bInstanceIDOffsetEnable
)
881 // the InstanceID (curInstance) value is offset by StartInstanceLocation
882 curInstance
= ADD(curInstance
, startInstance
);
886 #if USE_SIMD16_GATHERS
890 Value
*vInstanceStride
= VIMMED1(0);
892 if (ied
.InstanceEnable
)
894 Value
* stepRate
= C(ied
.InstanceAdvancementState
);
896 // prevent a div by 0 for 0 step rate
897 Value
* isNonZeroStep
= ICMP_UGT(stepRate
, C(0));
898 stepRate
= SELECT(isNonZeroStep
, stepRate
, C(1));
900 // calc the current offset into instanced data buffer
901 Value
* calcInstance
= UDIV(curInstance
, stepRate
);
903 // if step rate is 0, every instance gets instance 0
904 calcInstance
= SELECT(isNonZeroStep
, calcInstance
, C(0));
906 vCurIndices
= VBROADCAST(calcInstance
);
907 #if USE_SIMD16_GATHERS
908 vCurIndices2
= VBROADCAST(calcInstance
);
911 startOffset
= startInstance
;
913 else if (ied
.InstanceStrideEnable
)
915 // grab the instance advancement state, determines stride in bytes from one instance to the next
916 Value
* stepRate
= C(ied
.InstanceAdvancementState
);
917 vInstanceStride
= VBROADCAST(MUL(curInstance
, stepRate
));
919 // offset indices by baseVertex
920 vCurIndices
= ADD(vIndices
, vBaseVertex
);
921 #if USE_SIMD16_GATHERS
922 vCurIndices2
= ADD(vIndices2
, vBaseVertex
);
925 startOffset
= startVertex
;
926 SWR_ASSERT((0), "TODO: Fill out more once driver sends this down.");
930 // offset indices by baseVertex
931 vCurIndices
= ADD(vIndices
, vBaseVertex
);
932 #if USE_SIMD16_GATHERS
933 vCurIndices2
= ADD(vIndices2
, vBaseVertex
);
936 startOffset
= startVertex
;
939 // All of the OOB calculations are in vertices, not VB offsets, to prevent having to
940 // do 64bit address offset calculations.
942 // calculate byte offset to the start of the VB
943 Value
* baseOffset
= MUL(Z_EXT(startOffset
, mInt64Ty
), Z_EXT(stride
, mInt64Ty
));
944 pStreamBase
= GEP(pStreamBase
, baseOffset
);
946 // if we have a start offset, subtract from max vertex. Used for OOB check
947 maxVertex
= SUB(Z_EXT(maxVertex
, mInt64Ty
), Z_EXT(startOffset
, mInt64Ty
));
948 Value
* maxNeg
= ICMP_SLT(maxVertex
, C((int64_t)0));
949 // if we have a negative value, we're already OOB. clamp at 0.
950 maxVertex
= SELECT(maxNeg
, C(0), TRUNC(maxVertex
, mInt32Ty
));
952 if (fetchState
.bPartialVertexBuffer
)
954 // similary for min vertex
955 minVertex
= SUB(Z_EXT(minVertex
, mInt64Ty
), Z_EXT(startOffset
, mInt64Ty
));
956 Value
*minNeg
= ICMP_SLT(minVertex
, C((int64_t)0));
957 minVertex
= SELECT(minNeg
, C(0), TRUNC(minVertex
, mInt32Ty
));
960 // Load the in bounds size of a partially valid vertex
961 Value
*partialInboundsSize
= GEP(streams
, {C(ied
.StreamIndex
), C(SWR_VERTEX_BUFFER_STATE_partialInboundsSize
)});
962 partialInboundsSize
= LOAD(partialInboundsSize
);
963 Value
* vPartialVertexSize
= VBROADCAST(partialInboundsSize
);
964 Value
* vBpp
= VBROADCAST(C(info
.Bpp
));
965 Value
* vAlignmentOffsets
= VBROADCAST(C(ied
.AlignedByteOffset
));
967 // is the element is <= the partially valid size
968 Value
* vElementInBoundsMask
= ICMP_SLE(vBpp
, SUB(vPartialVertexSize
, vAlignmentOffsets
));
970 #if USE_SIMD16_GATHERS
971 // override cur indices with 0 if pitch is 0
972 Value
* pZeroPitchMask
= ICMP_EQ(vStride
, VIMMED1(0));
973 vCurIndices
= SELECT(pZeroPitchMask
, VIMMED1(0), vCurIndices
);
974 vCurIndices2
= SELECT(pZeroPitchMask
, VIMMED1(0), vCurIndices2
);
976 // are vertices partially OOB?
977 Value
* vMaxVertex
= VBROADCAST(maxVertex
);
978 Value
* vPartialOOBMask
= ICMP_EQ(vCurIndices
, vMaxVertex
);
979 Value
* vPartialOOBMask2
= ICMP_EQ(vCurIndices2
, vMaxVertex
);
981 // are vertices fully in bounds?
982 Value
* vMaxGatherMask
= ICMP_ULT(vCurIndices
, vMaxVertex
);
983 Value
* vMaxGatherMask2
= ICMP_ULT(vCurIndices2
, vMaxVertex
);
987 if (fetchState
.bPartialVertexBuffer
)
989 // are vertices below minVertex limit?
990 Value
*vMinVertex
= VBROADCAST(minVertex
);
991 Value
*vMinGatherMask
= ICMP_UGE(vCurIndices
, vMinVertex
);
992 Value
*vMinGatherMask2
= ICMP_UGE(vCurIndices2
, vMinVertex
);
994 // only fetch lanes that pass both tests
995 vGatherMask
= AND(vMaxGatherMask
, vMinGatherMask
);
996 vGatherMask2
= AND(vMaxGatherMask2
, vMinGatherMask2
);
1000 vGatherMask
= vMaxGatherMask
;
1001 vGatherMask2
= vMaxGatherMask2
;
1004 // blend in any partially OOB indices that have valid elements
1005 vGatherMask
= SELECT(vPartialOOBMask
, vElementInBoundsMask
, vGatherMask
);
1006 vGatherMask2
= SELECT(vPartialOOBMask2
, vElementInBoundsMask
, vGatherMask2
);
1008 // calculate the actual offsets into the VB
1009 Value
* vOffsets
= MUL(vCurIndices
, vStride
);
1010 vOffsets
= ADD(vOffsets
, vAlignmentOffsets
);
1012 Value
* vOffsets2
= MUL(vCurIndices2
, vStride
);
1013 vOffsets2
= ADD(vOffsets2
, vAlignmentOffsets
);
1015 // if instance stride enable is:
1016 // true - add product of the instanceID and advancement state to the offst into the VB
1017 // false - value of vInstanceStride has been initialialized to zero
1018 vOffsets
= ADD(vOffsets
, vInstanceStride
);
1019 vOffsets2
= ADD(vOffsets2
, vInstanceStride
);
1022 // override cur indices with 0 if pitch is 0
1023 Value
* pZeroPitchMask
= ICMP_EQ(vStride
, VIMMED1(0));
1024 vCurIndices
= SELECT(pZeroPitchMask
, VIMMED1(0), vCurIndices
);
1026 // are vertices partially OOB?
1027 Value
* vMaxVertex
= VBROADCAST(maxVertex
);
1028 Value
* vPartialOOBMask
= ICMP_EQ(vCurIndices
, vMaxVertex
);
1030 // are vertices fully in bounds?
1031 Value
* vMaxGatherMask
= ICMP_ULT(vCurIndices
, vMaxVertex
);
1034 if (fetchState
.bPartialVertexBuffer
)
1036 // are vertices below minVertex limit?
1037 Value
*vMinVertex
= VBROADCAST(minVertex
);
1038 Value
*vMinGatherMask
= ICMP_UGE(vCurIndices
, vMinVertex
);
1040 // only fetch lanes that pass both tests
1041 vGatherMask
= AND(vMaxGatherMask
, vMinGatherMask
);
1045 vGatherMask
= vMaxGatherMask
;
1048 // blend in any partially OOB indices that have valid elements
1049 vGatherMask
= SELECT(vPartialOOBMask
, vElementInBoundsMask
, vGatherMask
);
1051 // calculate the actual offsets into the VB
1052 Value
* vOffsets
= MUL(vCurIndices
, vStride
);
1053 vOffsets
= ADD(vOffsets
, vAlignmentOffsets
);
1055 // if instance stride enable is:
1056 // true - add product of the instanceID and advancement state to the offst into the VB
1057 // false - value of vInstanceStride has been initialialized to zero
1058 vOffsets
= ADD(vOffsets
, vInstanceStride
);
1061 // Packing and component control
1062 ComponentEnable compMask
= (ComponentEnable
)ied
.ComponentPacking
;
1063 const ComponentControl compCtrl
[4] { (ComponentControl
)ied
.ComponentControl0
, (ComponentControl
)ied
.ComponentControl1
,
1064 (ComponentControl
)ied
.ComponentControl2
, (ComponentControl
)ied
.ComponentControl3
};
1066 // Special gather/conversion for formats without equal component sizes
1067 if (IsOddFormat((SWR_FORMAT
)ied
.Format
))
1069 #if USE_SIMD16_GATHERS
1071 Value
*pResults2
[4];
1072 CreateGatherOddFormats((SWR_FORMAT
)ied
.Format
, vGatherMask
, pStreamBase
, vOffsets
, pResults
);
1073 CreateGatherOddFormats((SWR_FORMAT
)ied
.Format
, vGatherMask2
, pStreamBase
, vOffsets2
, pResults2
);
1074 ConvertFormat((SWR_FORMAT
)ied
.Format
, pResults
);
1075 ConvertFormat((SWR_FORMAT
)ied
.Format
, pResults2
);
1077 for (uint32_t c
= 0; c
< 4; c
+= 1)
1079 if (isComponentEnabled(compMask
, c
))
1081 #if USE_SIMD16_BUILDER
1082 // pack adjacent pairs of SIMD8s into SIMD16s
1083 pVtxSrc2
[currentVertexElement
] = VUNDEF2_F();
1084 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], pResults
[c
], 0);
1085 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], pResults2
[c
], 1);
1088 vVertexElements
[currentVertexElement
] = pResults
[c
];
1089 vVertexElements2
[currentVertexElement
] = pResults2
[c
];
1092 currentVertexElement
+= 1;
1094 if (currentVertexElement
> 3)
1096 #if USE_SIMD16_BUILDER
1098 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
1100 StoreVertexElements2(pVtxOut2
, outputElt
, 4, pVtxSrc2
);
1103 StoreVertexElements(pVtxOut
, outputElt
, 4, vVertexElements
);
1104 StoreVertexElements(GEP(pVtxOut
, C(1)), outputElt
, 4, vVertexElements2
);
1109 // reset to the next vVertexElement to output
1110 currentVertexElement
= 0;
1116 CreateGatherOddFormats((SWR_FORMAT
)ied
.Format
, vGatherMask
, pStreamBase
, vOffsets
, pResults
);
1117 ConvertFormat((SWR_FORMAT
)ied
.Format
, pResults
);
1119 for (uint32_t c
= 0; c
< 4; ++c
)
1121 if (isComponentEnabled(compMask
, c
))
1123 vVertexElements
[currentVertexElement
++] = pResults
[c
];
1124 if (currentVertexElement
> 3)
1126 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
1127 // reset to the next vVertexElement to output
1128 currentVertexElement
= 0;
1134 else if(info
.type
[0] == SWR_TYPE_FLOAT
)
1136 ///@todo: support 64 bit vb accesses
1137 Value
*gatherSrc
= VIMMED1(0.0f
);
1138 #if USE_SIMD16_GATHERS
1139 Value
*gatherSrc2
= VIMMED1(0.0f
);
1140 #if USE_SIMD16_BUILDER
1141 Value
*gatherSrc16
= VIMMED2_1(0.0f
);
1145 SWR_ASSERT(IsUniformFormat((SWR_FORMAT
)ied
.Format
),
1146 "Unsupported format for standard gather fetch.");
1148 // Gather components from memory to store in a simdvertex structure
1153 #if USE_SIMD16_GATHERS
1154 Value
*vGatherResult
[2];
1155 Value
*vGatherResult2
[2];
1157 // if we have at least one component out of x or y to fetch
1158 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
1160 vGatherResult
[0] = GATHERPS(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1161 vGatherResult2
[0] = GATHERPS(gatherSrc2
, pStreamBase
, vOffsets2
, vGatherMask2
);
1162 // e.g. result of first 8x32bit integer gather for 16bit components
1163 // 256i - 0 1 2 3 4 5 6 7
1164 // xyxy xyxy xyxy xyxy xyxy xyxy xyxy xyxy
1169 vGatherResult
[0] = VUNDEF_I();
1170 vGatherResult2
[0] = VUNDEF_I();
1173 // if we have at least one component out of z or w to fetch
1174 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
1176 // offset base to the next components(zw) in the vertex to gather
1177 pStreamBase
= GEP(pStreamBase
, C((char)4));
1179 vGatherResult
[1] = GATHERPS(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1180 vGatherResult2
[1] = GATHERPS(gatherSrc2
, pStreamBase
, vOffsets2
, vGatherMask2
);
1181 // e.g. result of second 8x32bit integer gather for 16bit components
1182 // 256i - 0 1 2 3 4 5 6 7
1183 // zwzw zwzw zwzw zwzw zwzw zwzw zwzw zwzw
1188 vGatherResult
[1] = VUNDEF_I();
1189 vGatherResult2
[1] = VUNDEF_I();
1192 // if we have at least one component to shuffle into place
1195 #if USE_SIMD16_BUILDER
1196 Value
*gatherResult
[2];
1198 gatherResult
[0] = VUNDEF2_I();
1199 gatherResult
[1] = VUNDEF2_I();
1201 gatherResult
[0] = INSERT2_I(gatherResult
[0], vGatherResult
[0], 0);
1202 gatherResult
[0] = INSERT2_I(gatherResult
[0], vGatherResult2
[0], 1);
1204 gatherResult
[1] = INSERT2_I(gatherResult
[1], vGatherResult
[1], 0);
1205 gatherResult
[1] = INSERT2_I(gatherResult
[1], vGatherResult2
[1], 1);
1207 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
1209 Shuffle16bpcArgs args
= std::forward_as_tuple(gatherResult
, pVtxOut2
, Instruction::CastOps::FPExt
, CONVERT_NONE
,
1210 currentVertexElement
, outputElt
, compMask
, compCtrl
, pVtxSrc2
);
1212 // Shuffle gathered components into place in simdvertex struct
1213 Shuffle16bpcGather2(args
); // outputs to vVertexElements ref
1215 Shuffle16bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, Instruction::CastOps::FPExt
, CONVERT_NONE
,
1216 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
);
1217 Shuffle16bpcArgs args2
= std::forward_as_tuple(vGatherResult2
, GEP(pVtxOut
, C(1)), Instruction::CastOps::FPExt
, CONVERT_NONE
,
1218 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements2
);
1220 // Shuffle gathered components into place in simdvertex struct
1221 Shuffle16bpcGather(args
, false); // outputs to vVertexElements ref
1222 Shuffle16bpcGather(args2
, true); // outputs to vVertexElements ref
1226 Value
* vGatherResult
[2];
1228 // if we have at least one component out of x or y to fetch
1229 if(isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1)){
1230 vGatherResult
[0] = GATHERPS(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1231 // e.g. result of first 8x32bit integer gather for 16bit components
1232 // 256i - 0 1 2 3 4 5 6 7
1233 // xyxy xyxy xyxy xyxy xyxy xyxy xyxy xyxy
1237 // if we have at least one component out of z or w to fetch
1238 if(isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3)){
1239 // offset base to the next components(zw) in the vertex to gather
1240 pStreamBase
= GEP(pStreamBase
, C((char)4));
1242 vGatherResult
[1] = GATHERPS(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1243 // e.g. result of second 8x32bit integer gather for 16bit components
1244 // 256i - 0 1 2 3 4 5 6 7
1245 // zwzw zwzw zwzw zwzw zwzw zwzw zwzw zwzw
1249 // if we have at least one component to shuffle into place
1251 Shuffle16bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, Instruction::CastOps::FPExt
, CONVERT_NONE
,
1252 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
);
1254 // Shuffle gathered components into place in simdvertex struct
1255 #if USE_SIMD16_SHADERS
1256 Shuffle16bpcGather(args
, useVertexID2
); // outputs to vVertexElements ref
1258 Shuffle16bpcGather(args
); // outputs to vVertexElements ref
1266 for (uint32_t i
= 0; i
< 4; i
+= 1)
1268 #if USE_SIMD16_GATHERS
1269 if (isComponentEnabled(compMask
, i
))
1271 // if we need to gather the component
1272 if (compCtrl
[i
] == StoreSrc
)
1274 // Gather a SIMD of vertices
1275 // APIs allow a 4GB range for offsets
1276 // However, GATHERPS uses signed 32-bit offsets, so only a 2GB range :(
1277 // But, we know that elements must be aligned for FETCH. :)
1278 // Right shift the offset by a bit and then scale by 2 to remove the sign extension.
1279 Value
*vShiftedOffsets
= VPSRLI(vOffsets
, C(1));
1280 Value
*vShiftedOffsets2
= VPSRLI(vOffsets2
, C(1));
1281 #if USE_SIMD16_BUILDER
1282 Value
*indices
= VUNDEF2_I();
1283 indices
= INSERT2_I(indices
, vShiftedOffsets
, 0);
1284 indices
= INSERT2_I(indices
, vShiftedOffsets2
, 1);
1286 Value
*mask
= VSHUFFLE(vGatherMask
, vGatherMask2
, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
1288 pVtxSrc2
[currentVertexElement
] = GATHERPS2(gatherSrc16
, pStreamBase
, indices
, mask
, 2);
1290 vVertexElements
[currentVertexElement
] = GATHERPS(gatherSrc
, pStreamBase
, vShiftedOffsets
, vGatherMask
, 2);
1291 vVertexElements2
[currentVertexElement
] = GATHERPS(gatherSrc2
, pStreamBase
, vShiftedOffsets2
, vGatherMask2
, 2);
1293 #if USE_SIMD16_BUILDER
1294 // pack adjacent pairs of SIMD8s into SIMD16s
1295 pVtxSrc2
[currentVertexElement
] = VUNDEF2_F();
1296 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], vVertexElements
[currentVertexElement
], 0);
1297 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], vVertexElements2
[currentVertexElement
], 1);
1301 currentVertexElement
+= 1;
1305 #if USE_SIMD16_BUILDER
1306 pVtxSrc2
[currentVertexElement
] = GenerateCompCtrlVector2(compCtrl
[i
]);
1308 vVertexElements
[currentVertexElement
] = GenerateCompCtrlVector(compCtrl
[i
], false);
1309 vVertexElements2
[currentVertexElement
] = GenerateCompCtrlVector(compCtrl
[i
], true);
1311 #if USE_SIMD16_BUILDER
1312 // pack adjacent pairs of SIMD8s into SIMD16s
1313 pVtxSrc2
[currentVertexElement
] = VUNDEF2_F();
1314 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], vVertexElements
[currentVertexElement
], 0);
1315 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], vVertexElements2
[currentVertexElement
], 1);
1319 currentVertexElement
+= 1;
1322 if (currentVertexElement
> 3)
1324 #if USE_SIMD16_BUILDER
1326 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
1328 StoreVertexElements2(pVtxOut2
, outputElt
, 4, pVtxSrc2
);
1331 StoreVertexElements(pVtxOut
, outputElt
, 4, vVertexElements
);
1332 StoreVertexElements(GEP(pVtxOut
, C(1)), outputElt
, 4, vVertexElements2
);
1337 // reset to the next vVertexElement to output
1338 currentVertexElement
= 0;
1342 // offset base to the next component in the vertex to gather
1343 pStreamBase
= GEP(pStreamBase
, C((char)4));
1345 if (isComponentEnabled(compMask
, i
))
1347 // if we need to gather the component
1348 if (compCtrl
[i
] == StoreSrc
)
1350 // Gather a SIMD of vertices
1351 // APIs allow a 4GB range for offsets
1352 // However, GATHERPS uses signed 32-bit offsets, so only a 2GB range :(
1353 // But, we know that elements must be aligned for FETCH. :)
1354 // Right shift the offset by a bit and then scale by 2 to remove the sign extension.
1355 Value
* vShiftedOffsets
= VPSRLI(vOffsets
, C(1));
1356 vVertexElements
[currentVertexElement
++] = GATHERPS(gatherSrc
, pStreamBase
, vShiftedOffsets
, vGatherMask
, 2);
1360 #if USE_SIMD16_SHADERS
1361 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
1363 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
1367 if (currentVertexElement
> 3)
1369 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
1370 // reset to the next vVertexElement to output
1371 currentVertexElement
= 0;
1375 // offset base to the next component in the vertex to gather
1376 pStreamBase
= GEP(pStreamBase
, C((char)4));
1383 for (uint32_t i
= 0; i
< 4; i
+= 1)
1385 #if USE_SIMD16_GATHERS
1386 if (isComponentEnabled(compMask
, i
))
1388 // if we need to gather the component
1389 if (compCtrl
[i
] == StoreSrc
)
1391 Value
*vMaskLo
= VSHUFFLE(vGatherMask
, VUNDEF(mInt1Ty
, 8), C({ 0, 1, 2, 3 }));
1392 Value
*vMaskLo2
= VSHUFFLE(vGatherMask2
, VUNDEF(mInt1Ty
, 8), C({ 0, 1, 2, 3 }));
1393 Value
*vMaskHi
= VSHUFFLE(vGatherMask
, VUNDEF(mInt1Ty
, 8), C({ 4, 5, 6, 7 }));
1394 Value
*vMaskHi2
= VSHUFFLE(vGatherMask2
, VUNDEF(mInt1Ty
, 8), C({ 4, 5, 6, 7 }));
1396 Value
*vOffsetsLo
= VEXTRACTI128(vOffsets
, C(0));
1397 Value
*vOffsetsLo2
= VEXTRACTI128(vOffsets2
, C(0));
1398 Value
*vOffsetsHi
= VEXTRACTI128(vOffsets
, C(1));
1399 Value
*vOffsetsHi2
= VEXTRACTI128(vOffsets2
, C(1));
1401 Value
*vZeroDouble
= VECTOR_SPLAT(4, ConstantFP::get(IRB()->getDoubleTy(), 0.0f
));
1403 Value
* pGatherLo
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsLo
, vMaskLo
);
1404 Value
* pGatherLo2
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsLo2
, vMaskLo2
);
1405 Value
* pGatherHi
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsHi
, vMaskHi
);
1406 Value
* pGatherHi2
= GATHERPD(vZeroDouble
, pStreamBase
, vOffsetsHi2
, vMaskHi2
);
1408 pGatherLo
= VCVTPD2PS(pGatherLo
);
1409 pGatherLo2
= VCVTPD2PS(pGatherLo2
);
1410 pGatherHi
= VCVTPD2PS(pGatherHi
);
1411 pGatherHi2
= VCVTPD2PS(pGatherHi2
);
1413 Value
*pGather
= VSHUFFLE(pGatherLo
, pGatherHi
, C({ 0, 1, 2, 3, 4, 5, 6, 7 }));
1414 Value
*pGather2
= VSHUFFLE(pGatherLo2
, pGatherHi2
, C({ 0, 1, 2, 3, 4, 5, 6, 7 }));
1416 #if USE_SIMD16_BUILDER
1417 // pack adjacent pairs of SIMD8s into SIMD16s
1418 pVtxSrc2
[currentVertexElement
] = VUNDEF2_F();
1419 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], pGather
, 0);
1420 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], pGather2
, 1);
1423 vVertexElements
[currentVertexElement
] = pGather
;
1424 vVertexElements2
[currentVertexElement
] = pGather2
;
1427 currentVertexElement
+= 1;
1431 #if USE_SIMD16_BUILDER
1432 pVtxSrc2
[currentVertexElement
] = GenerateCompCtrlVector2(compCtrl
[i
]);
1435 vVertexElements
[currentVertexElement
] = GenerateCompCtrlVector(compCtrl
[i
], false);
1436 vVertexElements2
[currentVertexElement
] = GenerateCompCtrlVector(compCtrl
[i
], true);
1439 currentVertexElement
+= 1;
1442 if (currentVertexElement
> 3)
1444 #if USE_SIMD16_BUILDER
1446 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
1448 StoreVertexElements2(pVtxOut2
, outputElt
, 4, pVtxSrc2
);
1451 StoreVertexElements(pVtxOut
, outputElt
, 4, vVertexElements
);
1452 StoreVertexElements(GEP(pVtxOut
, C(1)), outputElt
, 4, vVertexElements2
);
1457 // reset to the next vVertexElement to output
1458 currentVertexElement
= 0;
1462 // offset base to the next component in the vertex to gather
1463 pStreamBase
= GEP(pStreamBase
, C((char)8));
1465 if (isComponentEnabled(compMask
, i
))
1467 // if we need to gather the component
1468 if (compCtrl
[i
] == StoreSrc
)
1470 Value
*vMaskLo
= VSHUFFLE(vGatherMask
, VUNDEF(mInt1Ty
, 8), C({0, 1, 2, 3}));
1471 Value
*vMaskHi
= VSHUFFLE(vGatherMask
, VUNDEF(mInt1Ty
, 8), C({4, 5, 6, 7}));
1473 Value
*vOffsetsLo
= VEXTRACTI128(vOffsets
, C(0));
1474 Value
*vOffsetsHi
= VEXTRACTI128(vOffsets
, C(1));
1476 Value
*vZeroDouble
= VECTOR_SPLAT(4, ConstantFP::get(IRB()->getDoubleTy(), 0.0f
));
1478 Value
* pGatherLo
= GATHERPD(vZeroDouble
,
1479 pStreamBase
, vOffsetsLo
, vMaskLo
);
1480 Value
* pGatherHi
= GATHERPD(vZeroDouble
,
1481 pStreamBase
, vOffsetsHi
, vMaskHi
);
1483 pGatherLo
= VCVTPD2PS(pGatherLo
);
1484 pGatherHi
= VCVTPD2PS(pGatherHi
);
1486 Value
*pGather
= VSHUFFLE(pGatherLo
, pGatherHi
, C({0, 1, 2, 3, 4, 5, 6, 7}));
1488 vVertexElements
[currentVertexElement
++] = pGather
;
1492 #if USE_SIMD16_SHADERS
1493 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
1495 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
1499 if (currentVertexElement
> 3)
1501 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
1502 // reset to the next vVertexElement to output
1503 currentVertexElement
= 0;
1507 // offset base to the next component in the vertex to gather
1508 pStreamBase
= GEP(pStreamBase
, C((char)8));
1514 SWR_INVALID("Tried to fetch invalid FP format");
1520 Instruction::CastOps extendCastType
= Instruction::CastOps::CastOpsEnd
;
1521 ConversionType conversionType
= CONVERT_NONE
;
1523 SWR_ASSERT(IsUniformFormat((SWR_FORMAT
)ied
.Format
),
1524 "Unsupported format for standard gather fetch.");
1526 switch(info
.type
[0])
1528 case SWR_TYPE_UNORM
:
1529 conversionType
= CONVERT_NORMALIZED
;
1531 extendCastType
= Instruction::CastOps::ZExt
;
1533 case SWR_TYPE_SNORM
:
1534 conversionType
= CONVERT_NORMALIZED
;
1536 extendCastType
= Instruction::CastOps::SExt
;
1538 case SWR_TYPE_USCALED
:
1539 conversionType
= CONVERT_USCALED
;
1540 extendCastType
= Instruction::CastOps::UIToFP
;
1542 case SWR_TYPE_SSCALED
:
1543 conversionType
= CONVERT_SSCALED
;
1544 extendCastType
= Instruction::CastOps::SIToFP
;
1546 case SWR_TYPE_SFIXED
:
1547 conversionType
= CONVERT_SFIXED
;
1548 extendCastType
= Instruction::CastOps::SExt
;
1554 // value substituted when component of gather is masked
1555 Value
* gatherSrc
= VIMMED1(0);
1556 #if USE_SIMD16_GATHERS
1557 Value
* gatherSrc2
= VIMMED1(0);
1560 // Gather components from memory to store in a simdvertex structure
1565 // if we have at least one component to fetch
1568 #if USE_SIMD16_GATHERS
1569 Value
* vGatherResult
= GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1570 Value
* vGatherResult2
= GATHERDD(gatherSrc2
, pStreamBase
, vOffsets2
, vGatherMask2
);
1572 // e.g. result of an 8x32bit integer gather for 8bit components
1573 // 256i - 0 1 2 3 4 5 6 7
1574 // xyzw xyzw xyzw xyzw xyzw xyzw xyzw xyzw
1576 #if USE_SIMD16_BUILDER
1577 Value
*gatherResult
= VUNDEF2_I();
1579 gatherResult
= INSERT2_I(gatherResult
, vGatherResult
, 0);
1580 gatherResult
= INSERT2_I(gatherResult
, vGatherResult2
, 1);
1582 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
1584 Shuffle8bpcArgs args
= std::forward_as_tuple(gatherResult
, pVtxOut2
, extendCastType
, conversionType
,
1585 currentVertexElement
, outputElt
, compMask
, compCtrl
, pVtxSrc2
, info
.swizzle
);
1587 // Shuffle gathered components into place in simdvertex struct
1588 Shuffle8bpcGatherd2(args
); // outputs to vVertexElements ref
1590 Shuffle8bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, extendCastType
, conversionType
,
1591 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
, info
.swizzle
);
1592 Shuffle8bpcArgs args2
= std::forward_as_tuple(vGatherResult2
, GEP(pVtxOut
, C(1)), extendCastType
, conversionType
,
1593 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements2
, info
.swizzle
);
1595 // Shuffle gathered components into place in simdvertex struct
1596 Shuffle8bpcGatherd(args
, false); // outputs to vVertexElements ref
1597 Shuffle8bpcGatherd(args2
, true); // outputs to vVertexElements ref
1600 Value
* vGatherResult
= GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1601 // e.g. result of an 8x32bit integer gather for 8bit components
1602 // 256i - 0 1 2 3 4 5 6 7
1603 // xyzw xyzw xyzw xyzw xyzw xyzw xyzw xyzw
1605 Shuffle8bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, extendCastType
, conversionType
,
1606 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
, info
.swizzle
);
1608 // Shuffle gathered components into place in simdvertex struct
1609 #if USE_SIMD16_SHADERS
1610 Shuffle8bpcGatherd(args
, useVertexID2
); // outputs to vVertexElements ref
1612 Shuffle8bpcGatherd(args
); // outputs to vVertexElements ref
1620 #if USE_SIMD16_GATHERS
1621 Value
* vGatherResult
[2];
1622 Value
* vGatherResult2
[2];
1624 // if we have at least one component out of x or y to fetch
1625 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
1627 vGatherResult
[0] = GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1628 vGatherResult2
[0] = GATHERDD(gatherSrc2
, pStreamBase
, vOffsets2
, vGatherMask2
);
1629 // e.g. result of first 8x32bit integer gather for 16bit components
1630 // 256i - 0 1 2 3 4 5 6 7
1631 // xyxy xyxy xyxy xyxy xyxy xyxy xyxy xyxy
1636 vGatherResult
[0] = VUNDEF_I();
1637 vGatherResult2
[0] = VUNDEF_I();
1640 // if we have at least one component out of z or w to fetch
1641 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
1643 // offset base to the next components(zw) in the vertex to gather
1644 pStreamBase
= GEP(pStreamBase
, C((char)4));
1646 vGatherResult
[1] = GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1647 vGatherResult2
[1] = GATHERDD(gatherSrc2
, pStreamBase
, vOffsets2
, vGatherMask2
);
1648 // e.g. result of second 8x32bit integer gather for 16bit components
1649 // 256i - 0 1 2 3 4 5 6 7
1650 // zwzw zwzw zwzw zwzw zwzw zwzw zwzw zwzw
1655 vGatherResult
[1] = VUNDEF_I();
1656 vGatherResult2
[1] = VUNDEF_I();
1659 // if we have at least one component to shuffle into place
1662 #if USE_SIMD16_BUILDER
1663 Value
*gatherResult
[2];
1665 gatherResult
[0] = VUNDEF2_I();
1666 gatherResult
[1] = VUNDEF2_I();
1668 gatherResult
[0] = INSERT2_I(gatherResult
[0], vGatherResult
[0], 0);
1669 gatherResult
[0] = INSERT2_I(gatherResult
[0], vGatherResult2
[0], 1);
1671 gatherResult
[1] = INSERT2_I(gatherResult
[1], vGatherResult
[1], 0);
1672 gatherResult
[1] = INSERT2_I(gatherResult
[1], vGatherResult2
[1], 1);
1674 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
1676 Shuffle16bpcArgs args
= std::forward_as_tuple(gatherResult
, pVtxOut2
, extendCastType
, conversionType
,
1677 currentVertexElement
, outputElt
, compMask
, compCtrl
, pVtxSrc2
);
1679 // Shuffle gathered components into place in simdvertex struct
1680 Shuffle16bpcGather2(args
); // outputs to vVertexElements ref
1682 Shuffle16bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, extendCastType
, conversionType
,
1683 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
);
1684 Shuffle16bpcArgs args2
= std::forward_as_tuple(vGatherResult2
, GEP(pVtxOut
, C(1)), extendCastType
, conversionType
,
1685 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements2
);
1687 // Shuffle gathered components into place in simdvertex struct
1688 Shuffle16bpcGather(args
, false); // outputs to vVertexElements ref
1689 Shuffle16bpcGather(args2
, true); // outputs to vVertexElements ref
1693 Value
* vGatherResult
[2];
1695 // if we have at least one component out of x or y to fetch
1696 if(isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1)){
1697 vGatherResult
[0] = GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1698 // e.g. result of first 8x32bit integer gather for 16bit components
1699 // 256i - 0 1 2 3 4 5 6 7
1700 // xyxy xyxy xyxy xyxy xyxy xyxy xyxy xyxy
1704 // if we have at least one component out of z or w to fetch
1705 if(isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3)){
1706 // offset base to the next components(zw) in the vertex to gather
1707 pStreamBase
= GEP(pStreamBase
, C((char)4));
1709 vGatherResult
[1] = GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1710 // e.g. result of second 8x32bit integer gather for 16bit components
1711 // 256i - 0 1 2 3 4 5 6 7
1712 // zwzw zwzw zwzw zwzw zwzw zwzw zwzw zwzw
1716 // if we have at least one component to shuffle into place
1718 Shuffle16bpcArgs args
= std::forward_as_tuple(vGatherResult
, pVtxOut
, extendCastType
, conversionType
,
1719 currentVertexElement
, outputElt
, compMask
, compCtrl
, vVertexElements
);
1721 // Shuffle gathered components into place in simdvertex struct
1722 #if USE_SIMD16_SHADERS
1723 Shuffle16bpcGather(args
, useVertexID2
); // outputs to vVertexElements ref
1725 Shuffle16bpcGather(args
); // outputs to vVertexElements ref
1733 // Gathered components into place in simdvertex struct
1734 for (uint32_t i
= 0; i
< 4; i
++)
1736 if (isComponentEnabled(compMask
, i
))
1738 // if we need to gather the component
1739 if (compCtrl
[i
] == StoreSrc
)
1741 #if USE_SIMD16_GATHERS
1742 Value
*pGather
= GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1743 Value
*pGather2
= GATHERDD(gatherSrc2
, pStreamBase
, vOffsets2
, vGatherMask2
);
1745 if (conversionType
== CONVERT_USCALED
)
1747 pGather
= UI_TO_FP(pGather
, mSimdFP32Ty
);
1748 pGather2
= UI_TO_FP(pGather2
, mSimdFP32Ty
);
1750 else if (conversionType
== CONVERT_SSCALED
)
1752 pGather
= SI_TO_FP(pGather
, mSimdFP32Ty
);
1753 pGather2
= SI_TO_FP(pGather2
, mSimdFP32Ty
);
1755 else if (conversionType
== CONVERT_SFIXED
)
1757 pGather
= FMUL(SI_TO_FP(pGather
, mSimdFP32Ty
), VBROADCAST(C(1 / 65536.0f
)));
1758 pGather2
= FMUL(SI_TO_FP(pGather2
, mSimdFP32Ty
), VBROADCAST(C(1 / 65536.0f
)));
1761 #if USE_SIMD16_BUILDER
1762 // pack adjacent pairs of SIMD8s into SIMD16s
1763 pVtxSrc2
[currentVertexElement
] = VUNDEF2_F();
1764 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], pGather
, 0);
1765 pVtxSrc2
[currentVertexElement
] = INSERT2_F(pVtxSrc2
[currentVertexElement
], pGather2
, 1);
1768 vVertexElements
[currentVertexElement
] = pGather
;
1769 vVertexElements2
[currentVertexElement
] = pGather2
;
1773 // e.g. result of a single 8x32bit integer gather for 32bit components
1774 // 256i - 0 1 2 3 4 5 6 7
1775 // xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
1777 currentVertexElement
+= 1;
1779 Value
* pGather
= GATHERDD(gatherSrc
, pStreamBase
, vOffsets
, vGatherMask
);
1781 if (conversionType
== CONVERT_USCALED
)
1783 pGather
= UI_TO_FP(pGather
, mSimdFP32Ty
);
1785 else if (conversionType
== CONVERT_SSCALED
)
1787 pGather
= SI_TO_FP(pGather
, mSimdFP32Ty
);
1789 else if (conversionType
== CONVERT_SFIXED
)
1791 pGather
= FMUL(SI_TO_FP(pGather
, mSimdFP32Ty
), VBROADCAST(C(1/65536.0f
)));
1794 vVertexElements
[currentVertexElement
++] = pGather
;
1795 // e.g. result of a single 8x32bit integer gather for 32bit components
1796 // 256i - 0 1 2 3 4 5 6 7
1797 // xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
1802 #if USE_SIMD16_SHADERS
1803 #if USE_SIMD16_GATHERS
1804 #if USE_SIMD16_BUILDER
1805 pVtxSrc2
[currentVertexElement
] = GenerateCompCtrlVector2(compCtrl
[i
]);
1808 vVertexElements
[currentVertexElement
] = GenerateCompCtrlVector(compCtrl
[i
], false);
1809 vVertexElements2
[currentVertexElement
] = GenerateCompCtrlVector(compCtrl
[i
], true);
1812 currentVertexElement
+= 1;
1814 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
1817 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
1821 if (currentVertexElement
> 3)
1823 #if USE_SIMD16_GATHERS
1824 #if USE_SIMD16_BUILDER
1826 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
1828 StoreVertexElements2(pVtxOut2
, outputElt
, 4, pVtxSrc2
);
1831 StoreVertexElements(pVtxOut
, outputElt
, 4, vVertexElements
);
1832 StoreVertexElements(GEP(pVtxOut
, C(1)), outputElt
, 4, vVertexElements2
);
1837 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
1840 // reset to the next vVertexElement to output
1841 currentVertexElement
= 0;
1846 // offset base to the next component in the vertex to gather
1847 pStreamBase
= GEP(pStreamBase
, C((char)4));
1855 // if we have a partially filled vVertexElement struct, output it
1856 if (currentVertexElement
> 0)
1858 #if USE_SIMD16_GATHERS
1859 #if USE_SIMD16_BUILDER
1861 Value
*pVtxOut2
= BITCAST(pVtxOut
, PointerType::get(VectorType::get(mFP32Ty
, mVWidth2
), 0));
1863 StoreVertexElements2(pVtxOut2
, outputElt
, currentVertexElement
, pVtxSrc2
);
1866 StoreVertexElements(pVtxOut
, outputElt
, currentVertexElement
, vVertexElements
);
1867 StoreVertexElements(GEP(pVtxOut
, C(1)), outputElt
, currentVertexElement
, vVertexElements2
);
1872 StoreVertexElements(pVtxOut
, outputElt
++, currentVertexElement
, vVertexElements
);
1877 //////////////////////////////////////////////////////////////////////////
1878 /// @brief Loads a simd of valid indices. OOB indices are set to 0
1879 /// *Note* have to do 16bit index checking in scalar until we have AVX-512
1881 /// @param pIndices - pointer to 8 bit indices
1882 /// @param pLastIndex - pointer to last valid index
1883 Value
* FetchJit::GetSimdValid8bitIndices(Value
* pIndices
, Value
* pLastIndex
)
1885 // can fit 2 16 bit integers per vWidth lane
1886 Value
* vIndices
= VUNDEF_I();
1888 // store 0 index on stack to be used to conditionally load from if index address is OOB
1889 Value
* pZeroIndex
= ALLOCA(mInt8Ty
);
1890 STORE(C((uint8_t)0), pZeroIndex
);
1892 // Load a SIMD of index pointers
1893 for(int64_t lane
= 0; lane
< mVWidth
; lane
++)
1895 // Calculate the address of the requested index
1896 Value
*pIndex
= GEP(pIndices
, C(lane
));
1898 // check if the address is less than the max index,
1899 Value
* mask
= ICMP_ULT(pIndex
, pLastIndex
);
1901 // if valid, load the index. if not, load 0 from the stack
1902 Value
* pValid
= SELECT(mask
, pIndex
, pZeroIndex
);
1903 Value
*index
= LOAD(pValid
, "valid index");
1905 // zero extended index to 32 bits and insert into the correct simd lane
1906 index
= Z_EXT(index
, mInt32Ty
);
1907 vIndices
= VINSERT(vIndices
, index
, lane
);
1912 //////////////////////////////////////////////////////////////////////////
1913 /// @brief Loads a simd of valid indices. OOB indices are set to 0
1914 /// *Note* have to do 16bit index checking in scalar until we have AVX-512
1916 /// @param pIndices - pointer to 16 bit indices
1917 /// @param pLastIndex - pointer to last valid index
1918 Value
* FetchJit::GetSimdValid16bitIndices(Value
* pIndices
, Value
* pLastIndex
)
1920 // can fit 2 16 bit integers per vWidth lane
1921 Value
* vIndices
= VUNDEF_I();
1923 // store 0 index on stack to be used to conditionally load from if index address is OOB
1924 Value
* pZeroIndex
= ALLOCA(mInt16Ty
);
1925 STORE(C((uint16_t)0), pZeroIndex
);
1927 // Load a SIMD of index pointers
1928 for(int64_t lane
= 0; lane
< mVWidth
; lane
++)
1930 // Calculate the address of the requested index
1931 Value
*pIndex
= GEP(pIndices
, C(lane
));
1933 // check if the address is less than the max index,
1934 Value
* mask
= ICMP_ULT(pIndex
, pLastIndex
);
1936 // if valid, load the index. if not, load 0 from the stack
1937 Value
* pValid
= SELECT(mask
, pIndex
, pZeroIndex
);
1938 Value
*index
= LOAD(pValid
, "valid index");
1940 // zero extended index to 32 bits and insert into the correct simd lane
1941 index
= Z_EXT(index
, mInt32Ty
);
1942 vIndices
= VINSERT(vIndices
, index
, lane
);
1947 //////////////////////////////////////////////////////////////////////////
1948 /// @brief Loads a simd of valid indices. OOB indices are set to 0
1949 /// @param pIndices - pointer to 32 bit indices
1950 /// @param pLastIndex - pointer to last valid index
1951 Value
* FetchJit::GetSimdValid32bitIndices(Value
* pIndices
, Value
* pLastIndex
)
1953 DataLayout
dL(JM()->mpCurrentModule
);
1954 unsigned int ptrSize
= dL
.getPointerSize() * 8; // ptr size in bits
1955 Value
* iLastIndex
= PTR_TO_INT(pLastIndex
, Type::getIntNTy(JM()->mContext
, ptrSize
));
1956 Value
* iIndices
= PTR_TO_INT(pIndices
, Type::getIntNTy(JM()->mContext
, ptrSize
));
1958 // get the number of indices left in the buffer (endPtr - curPtr) / sizeof(index)
1959 Value
* numIndicesLeft
= SUB(iLastIndex
,iIndices
);
1960 numIndicesLeft
= TRUNC(numIndicesLeft
, mInt32Ty
);
1961 numIndicesLeft
= SDIV(numIndicesLeft
, C(4));
1963 // create a vector of index counts from the base index ptr passed into the fetch
1964 const std::vector
<Constant
*> vecIndices
{C(0), C(1), C(2), C(3), C(4), C(5), C(6), C(7)};
1965 Constant
* vIndexOffsets
= ConstantVector::get(vecIndices
);
1967 // compare index count to the max valid index
1968 // e.g vMaxIndex 4 4 4 4 4 4 4 4 : 4 indices left to load
1969 // vIndexOffsets 0 1 2 3 4 5 6 7
1970 // ------------------------------
1971 // vIndexMask -1-1-1-1 0 0 0 0 : offsets < max pass
1972 // vLoadedIndices 0 1 2 3 0 0 0 0 : offsets >= max masked to 0
1973 Value
* vMaxIndex
= VBROADCAST(numIndicesLeft
);
1974 Value
* vIndexMask
= VPCMPGTD(vMaxIndex
,vIndexOffsets
);
1976 // VMASKLOAD takes an *i8 src pointer
1977 pIndices
= BITCAST(pIndices
,PointerType::get(mInt8Ty
,0));
1979 // Load the indices; OOB loads 0
1980 return MASKLOADD(pIndices
,vIndexMask
);
1983 //////////////////////////////////////////////////////////////////////////
1984 /// @brief Takes a SIMD of gathered 8bpc verts, zero or sign extends,
1985 /// denormalizes if needed, converts to F32 if needed, and positions in
1986 // the proper SIMD rows to be output to the simdvertex structure
1987 /// @param args: (tuple of args, listed below)
1988 /// @param vGatherResult - 8 gathered 8bpc vertices
1989 /// @param pVtxOut - base pointer to output simdvertex struct
1990 /// @param extendType - sign extend or zero extend
1991 /// @param bNormalized - do we need to denormalize?
1992 /// @param currentVertexElement - reference to the current vVertexElement
1993 /// @param outputElt - reference to the current offset from simdvertex we're o
1994 /// @param compMask - component packing mask
1995 /// @param compCtrl - component control val
1996 /// @param vVertexElements[4] - vertex components to output
1997 /// @param swizzle[4] - component swizzle location
1998 #if USE_SIMD16_SHADERS
1999 void FetchJit::Shuffle8bpcGatherd(Shuffle8bpcArgs
&args
, bool useVertexID2
)
2001 void FetchJit::Shuffle8bpcGatherd(Shuffle8bpcArgs
&args
)
2004 // Unpack tuple args
2005 Value
*& vGatherResult
= std::get
<0>(args
);
2006 Value
* pVtxOut
= std::get
<1>(args
);
2007 const Instruction::CastOps extendType
= std::get
<2>(args
);
2008 const ConversionType conversionType
= std::get
<3>(args
);
2009 uint32_t ¤tVertexElement
= std::get
<4>(args
);
2010 uint32_t &outputElt
= std::get
<5>(args
);
2011 const ComponentEnable compMask
= std::get
<6>(args
);
2012 const ComponentControl (&compCtrl
)[4] = std::get
<7>(args
);
2013 Value
* (&vVertexElements
)[4] = std::get
<8>(args
);
2014 const uint32_t (&swizzle
)[4] = std::get
<9>(args
);
2017 Type
* vGatherTy
= mSimdInt32Ty
;
2018 Type
* v32x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 4 ); // vwidth is units of 32 bits
2020 // have to do extra work for sign extending
2021 if ((extendType
== Instruction::CastOps::SExt
) || (extendType
== Instruction::CastOps::SIToFP
)){
2022 Type
* v16x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 2); // 8x16bit ints in a 128bit lane
2023 Type
* v128Ty
= VectorType::get(IntegerType::getIntNTy(JM()->mContext
, 128), mVWidth
/ 4); // vwidth is units of 32 bits
2025 // shuffle mask, including any swizzling
2026 const char x
= (char)swizzle
[0]; const char y
= (char)swizzle
[1];
2027 const char z
= (char)swizzle
[2]; const char w
= (char)swizzle
[3];
2028 Value
* vConstMask
= C
<char>({char(x
), char(x
+4), char(x
+8), char(x
+12),
2029 char(y
), char(y
+4), char(y
+8), char(y
+12),
2030 char(z
), char(z
+4), char(z
+8), char(z
+12),
2031 char(w
), char(w
+4), char(w
+8), char(w
+12),
2032 char(x
), char(x
+4), char(x
+8), char(x
+12),
2033 char(y
), char(y
+4), char(y
+8), char(y
+12),
2034 char(z
), char(z
+4), char(z
+8), char(z
+12),
2035 char(w
), char(w
+4), char(w
+8), char(w
+12)});
2037 Value
* vShufResult
= BITCAST(PSHUFB(BITCAST(vGatherResult
, v32x8Ty
), vConstMask
), vGatherTy
);
2038 // after pshufb: group components together in each 128bit lane
2039 // 256i - 0 1 2 3 4 5 6 7
2040 // xxxx yyyy zzzz wwww xxxx yyyy zzzz wwww
2042 Value
* vi128XY
= nullptr;
2043 if(isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1)){
2044 vi128XY
= BITCAST(PERMD(vShufResult
, C
<int32_t>({0, 4, 0, 0, 1, 5, 0, 0})), v128Ty
);
2045 // after PERMD: move and pack xy and zw components in low 64 bits of each 128bit lane
2046 // 256i - 0 1 2 3 4 5 6 7
2047 // xxxx xxxx dcdc dcdc yyyy yyyy dcdc dcdc (dc - don't care)
2050 // do the same for zw components
2051 Value
* vi128ZW
= nullptr;
2052 if(isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3)){
2053 vi128ZW
= BITCAST(PERMD(vShufResult
, C
<int32_t>({2, 6, 0, 0, 3, 7, 0, 0})), v128Ty
);
2056 // init denormalize variables if needed
2057 Instruction::CastOps fpCast
;
2058 Value
* conversionFactor
;
2060 switch (conversionType
)
2062 case CONVERT_NORMALIZED
:
2063 fpCast
= Instruction::CastOps::SIToFP
;
2064 conversionFactor
= VIMMED1((float)(1.0 / 127.0));
2066 case CONVERT_SSCALED
:
2067 fpCast
= Instruction::CastOps::SIToFP
;
2068 conversionFactor
= VIMMED1((float)(1.0));
2070 case CONVERT_USCALED
:
2071 SWR_INVALID("Type should not be sign extended!");
2072 conversionFactor
= nullptr;
2075 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2076 conversionFactor
= nullptr;
2080 // sign extend all enabled components. If we have a fill vVertexElements, output to current simdvertex
2081 for (uint32_t i
= 0; i
< 4; i
++)
2083 if (isComponentEnabled(compMask
, i
))
2085 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2087 // if x or z, extract 128bits from lane 0, else for y or w, extract from lane 1
2088 uint32_t lane
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2089 // if x or y, use vi128XY permute result, else use vi128ZW
2090 Value
* selectedPermute
= (i
< 2) ? vi128XY
: vi128ZW
;
2093 vVertexElements
[currentVertexElement
] = PMOVSXBD(BITCAST(VEXTRACT(selectedPermute
, C(lane
)), v16x8Ty
));
2095 // denormalize if needed
2096 if (conversionType
!= CONVERT_NONE
)
2098 vVertexElements
[currentVertexElement
] = FMUL(CAST(fpCast
, vVertexElements
[currentVertexElement
], mSimdFP32Ty
), conversionFactor
);
2100 currentVertexElement
++;
2104 #if USE_SIMD16_SHADERS
2105 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
2107 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
2111 if (currentVertexElement
> 3)
2113 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
2114 // reset to the next vVertexElement to output
2115 currentVertexElement
= 0;
2121 else if ((extendType
== Instruction::CastOps::ZExt
) || (extendType
== Instruction::CastOps::UIToFP
))
2123 // init denormalize variables if needed
2124 Instruction::CastOps fpCast
;
2125 Value
* conversionFactor
;
2127 switch (conversionType
)
2129 case CONVERT_NORMALIZED
:
2130 fpCast
= Instruction::CastOps::UIToFP
;
2131 conversionFactor
= VIMMED1((float)(1.0 / 255.0));
2133 case CONVERT_USCALED
:
2134 fpCast
= Instruction::CastOps::UIToFP
;
2135 conversionFactor
= VIMMED1((float)(1.0));
2137 case CONVERT_SSCALED
:
2138 SWR_INVALID("Type should not be zero extended!");
2139 conversionFactor
= nullptr;
2142 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2143 conversionFactor
= nullptr;
2147 // shuffle enabled components into lower byte of each 32bit lane, 0 extending to 32 bits
2148 for (uint32_t i
= 0; i
< 4; i
++)
2150 if (isComponentEnabled(compMask
, i
))
2152 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2154 // pshufb masks for each component
2160 vConstMask
= C
<char>({ 0, -1, -1, -1, 4, -1, -1, -1, 8, -1, -1, -1, 12, -1, -1, -1,
2161 0, -1, -1, -1, 4, -1, -1, -1, 8, -1, -1, -1, 12, -1, -1, -1 });
2165 vConstMask
= C
<char>({ 1, -1, -1, -1, 5, -1, -1, -1, 9, -1, -1, -1, 13, -1, -1, -1,
2166 1, -1, -1, -1, 5, -1, -1, -1, 9, -1, -1, -1, 13, -1, -1, -1 });
2170 vConstMask
= C
<char>({ 2, -1, -1, -1, 6, -1, -1, -1, 10, -1, -1, -1, 14, -1, -1, -1,
2171 2, -1, -1, -1, 6, -1, -1, -1, 10, -1, -1, -1, 14, -1, -1, -1 });
2175 vConstMask
= C
<char>({ 3, -1, -1, -1, 7, -1, -1, -1, 11, -1, -1, -1, 15, -1, -1, -1,
2176 3, -1, -1, -1, 7, -1, -1, -1, 11, -1, -1, -1, 15, -1, -1, -1 });
2179 vConstMask
= nullptr;
2183 vVertexElements
[currentVertexElement
] = BITCAST(PSHUFB(BITCAST(vGatherResult
, v32x8Ty
), vConstMask
), vGatherTy
);
2184 // after pshufb for x channel
2185 // 256i - 0 1 2 3 4 5 6 7
2186 // x000 x000 x000 x000 x000 x000 x000 x000
2188 // denormalize if needed
2189 if (conversionType
!= CONVERT_NONE
)
2191 vVertexElements
[currentVertexElement
] = FMUL(CAST(fpCast
, vVertexElements
[currentVertexElement
], mSimdFP32Ty
), conversionFactor
);
2193 currentVertexElement
++;
2197 #if USE_SIMD16_SHADERS
2198 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
2200 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
2204 if (currentVertexElement
> 3)
2206 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
2207 // reset to the next vVertexElement to output
2208 currentVertexElement
= 0;
2215 SWR_INVALID("Unsupported conversion type");
2219 #if USE_SIMD16_BUILDER
2220 void FetchJit::Shuffle8bpcGatherd2(Shuffle8bpcArgs
&args
)
2222 // Unpack tuple args
2223 Value
*& vGatherResult
= std::get
<0>(args
);
2224 Value
* pVtxOut
= std::get
<1>(args
);
2225 const Instruction::CastOps extendType
= std::get
<2>(args
);
2226 const ConversionType conversionType
= std::get
<3>(args
);
2227 uint32_t ¤tVertexElement
= std::get
<4>(args
);
2228 uint32_t &outputElt
= std::get
<5>(args
);
2229 const ComponentEnable compMask
= std::get
<6>(args
);
2230 const ComponentControl(&compCtrl
)[4] = std::get
<7>(args
);
2231 Value
* (&vVertexElements
)[4] = std::get
<8>(args
);
2232 const uint32_t(&swizzle
)[4] = std::get
<9>(args
);
2235 Type
*vGatherTy
= mSimdInt32Ty
;
2236 Type
*v32x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 4); // vwidth is units of 32 bits
2238 // have to do extra work for sign extending
2239 if ((extendType
== Instruction::CastOps::SExt
) || (extendType
== Instruction::CastOps::SIToFP
))
2241 Type
*v16x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 2); // 8x16bit ints in a 128bit lane
2242 Type
*v128Ty
= VectorType::get(IntegerType::getIntNTy(JM()->mContext
, 128), mVWidth
/ 4); // vwidth is units of 32 bits
2244 // shuffle mask, including any swizzling
2245 const char x
= (char)swizzle
[0]; const char y
= (char)swizzle
[1];
2246 const char z
= (char)swizzle
[2]; const char w
= (char)swizzle
[3];
2247 Value
*vConstMask
= C
<char>({ char(x
), char(x
+ 4), char(x
+ 8), char(x
+ 12),
2248 char(y
), char(y
+ 4), char(y
+ 8), char(y
+ 12),
2249 char(z
), char(z
+ 4), char(z
+ 8), char(z
+ 12),
2250 char(w
), char(w
+ 4), char(w
+ 8), char(w
+ 12),
2251 char(x
), char(x
+ 4), char(x
+ 8), char(x
+ 12),
2252 char(y
), char(y
+ 4), char(y
+ 8), char(y
+ 12),
2253 char(z
), char(z
+ 4), char(z
+ 8), char(z
+ 12),
2254 char(w
), char(w
+ 4), char(w
+ 8), char(w
+ 12) });
2256 // SIMD16 PSHUFB isnt part of AVX-512F, so split into SIMD8 for the sake of KNL, for now..
2258 Value
*vGatherResult_lo
= EXTRACT2_I(vGatherResult
, 0);
2259 Value
*vGatherResult_hi
= EXTRACT2_I(vGatherResult
, 1);
2261 Value
*vShufResult_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
), vGatherTy
);
2262 Value
*vShufResult_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
), vGatherTy
);
2264 // after pshufb: group components together in each 128bit lane
2265 // 256i - 0 1 2 3 4 5 6 7
2266 // xxxx yyyy zzzz wwww xxxx yyyy zzzz wwww
2268 Value
*vi128XY_lo
= nullptr;
2269 Value
*vi128XY_hi
= nullptr;
2270 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
2272 vi128XY_lo
= BITCAST(PERMD(vShufResult_lo
, C
<int32_t>({ 0, 4, 0, 0, 1, 5, 0, 0 })), v128Ty
);
2273 vi128XY_hi
= BITCAST(PERMD(vShufResult_hi
, C
<int32_t>({ 0, 4, 0, 0, 1, 5, 0, 0 })), v128Ty
);
2275 // after PERMD: move and pack xy and zw components in low 64 bits of each 128bit lane
2276 // 256i - 0 1 2 3 4 5 6 7
2277 // xxxx xxxx dcdc dcdc yyyy yyyy dcdc dcdc (dc - don't care)
2280 // do the same for zw components
2281 Value
*vi128ZW_lo
= nullptr;
2282 Value
*vi128ZW_hi
= nullptr;
2283 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
2285 vi128ZW_lo
= BITCAST(PERMD(vShufResult_lo
, C
<int32_t>({ 2, 6, 0, 0, 3, 7, 0, 0 })), v128Ty
);
2286 vi128ZW_hi
= BITCAST(PERMD(vShufResult_hi
, C
<int32_t>({ 2, 6, 0, 0, 3, 7, 0, 0 })), v128Ty
);
2289 // init denormalize variables if needed
2290 Instruction::CastOps fpCast
;
2291 Value
*conversionFactor
;
2293 switch (conversionType
)
2295 case CONVERT_NORMALIZED
:
2296 fpCast
= Instruction::CastOps::SIToFP
;
2297 conversionFactor
= VIMMED1((float)(1.0 / 127.0));
2299 case CONVERT_SSCALED
:
2300 fpCast
= Instruction::CastOps::SIToFP
;
2301 conversionFactor
= VIMMED1((float)(1.0));
2303 case CONVERT_USCALED
:
2304 SWR_INVALID("Type should not be sign extended!");
2305 conversionFactor
= nullptr;
2308 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2309 conversionFactor
= nullptr;
2313 // sign extend all enabled components. If we have a fill vVertexElements, output to current simdvertex
2314 for (uint32_t i
= 0; i
< 4; i
++)
2316 if (isComponentEnabled(compMask
, i
))
2318 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2320 // if x or z, extract 128bits from lane 0, else for y or w, extract from lane 1
2321 uint32_t lane
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2322 // if x or y, use vi128XY permute result, else use vi128ZW
2323 Value
*selectedPermute_lo
= (i
< 2) ? vi128XY_lo
: vi128ZW_lo
;
2324 Value
*selectedPermute_hi
= (i
< 2) ? vi128XY_hi
: vi128ZW_hi
;
2327 Value
*temp_lo
= PMOVSXBD(BITCAST(VEXTRACT(selectedPermute_lo
, C(lane
)), v16x8Ty
));
2328 Value
*temp_hi
= PMOVSXBD(BITCAST(VEXTRACT(selectedPermute_hi
, C(lane
)), v16x8Ty
));
2330 // denormalize if needed
2331 if (conversionType
!= CONVERT_NONE
)
2333 temp_lo
= FMUL(CAST(fpCast
, temp_lo
, mSimdFP32Ty
), conversionFactor
);
2334 temp_hi
= FMUL(CAST(fpCast
, temp_hi
, mSimdFP32Ty
), conversionFactor
);
2337 vVertexElements
[currentVertexElement
] = VUNDEF2_F();
2338 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_lo
, 0);
2339 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_hi
, 1);
2341 currentVertexElement
+= 1;
2345 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector2(compCtrl
[i
]);
2348 if (currentVertexElement
> 3)
2350 StoreVertexElements2(pVtxOut
, outputElt
++, 4, vVertexElements
);
2351 // reset to the next vVertexElement to output
2352 currentVertexElement
= 0;
2358 else if ((extendType
== Instruction::CastOps::ZExt
) || (extendType
== Instruction::CastOps::UIToFP
))
2360 // init denormalize variables if needed
2361 Instruction::CastOps fpCast
;
2362 Value
*conversionFactor
;
2364 switch (conversionType
)
2366 case CONVERT_NORMALIZED
:
2367 fpCast
= Instruction::CastOps::UIToFP
;
2368 conversionFactor
= VIMMED1((float)(1.0 / 255.0));
2370 case CONVERT_USCALED
:
2371 fpCast
= Instruction::CastOps::UIToFP
;
2372 conversionFactor
= VIMMED1((float)(1.0));
2374 case CONVERT_SSCALED
:
2375 SWR_INVALID("Type should not be zero extended!");
2376 conversionFactor
= nullptr;
2379 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2380 conversionFactor
= nullptr;
2384 // shuffle enabled components into lower byte of each 32bit lane, 0 extending to 32 bits
2385 for (uint32_t i
= 0; i
< 4; i
++)
2387 if (isComponentEnabled(compMask
, i
))
2389 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2391 // pshufb masks for each component
2397 vConstMask
= C
<char>({ 0, -1, -1, -1, 4, -1, -1, -1, 8, -1, -1, -1, 12, -1, -1, -1,
2398 0, -1, -1, -1, 4, -1, -1, -1, 8, -1, -1, -1, 12, -1, -1, -1 });
2402 vConstMask
= C
<char>({ 1, -1, -1, -1, 5, -1, -1, -1, 9, -1, -1, -1, 13, -1, -1, -1,
2403 1, -1, -1, -1, 5, -1, -1, -1, 9, -1, -1, -1, 13, -1, -1, -1 });
2407 vConstMask
= C
<char>({ 2, -1, -1, -1, 6, -1, -1, -1, 10, -1, -1, -1, 14, -1, -1, -1,
2408 2, -1, -1, -1, 6, -1, -1, -1, 10, -1, -1, -1, 14, -1, -1, -1 });
2412 vConstMask
= C
<char>({ 3, -1, -1, -1, 7, -1, -1, -1, 11, -1, -1, -1, 15, -1, -1, -1,
2413 3, -1, -1, -1, 7, -1, -1, -1, 11, -1, -1, -1, 15, -1, -1, -1 });
2416 vConstMask
= nullptr;
2420 Value
*vGatherResult_lo
= EXTRACT2_I(vGatherResult
, 0);
2421 Value
*vGatherResult_hi
= EXTRACT2_I(vGatherResult
, 1);
2423 Value
*temp_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
), vGatherTy
);
2424 Value
*temp_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
), vGatherTy
);
2426 // after pshufb for x channel
2427 // 256i - 0 1 2 3 4 5 6 7
2428 // x000 x000 x000 x000 x000 x000 x000 x000
2430 // denormalize if needed
2431 if (conversionType
!= CONVERT_NONE
)
2433 temp_lo
= FMUL(CAST(fpCast
, temp_lo
, mSimdFP32Ty
), conversionFactor
);
2434 temp_hi
= FMUL(CAST(fpCast
, temp_hi
, mSimdFP32Ty
), conversionFactor
);
2437 vVertexElements
[currentVertexElement
] = VUNDEF2_F();
2438 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_lo
, 0);
2439 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_hi
, 1);
2441 currentVertexElement
+= 1;
2445 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector2(compCtrl
[i
]);
2448 if (currentVertexElement
> 3)
2450 StoreVertexElements2(pVtxOut
, outputElt
++, 4, vVertexElements
);
2451 // reset to the next vVertexElement to output
2452 currentVertexElement
= 0;
2459 SWR_INVALID("Unsupported conversion type");
2464 //////////////////////////////////////////////////////////////////////////
2465 /// @brief Takes a SIMD of gathered 16bpc verts, zero or sign extends,
2466 /// denormalizes if needed, converts to F32 if needed, and positions in
2467 // the proper SIMD rows to be output to the simdvertex structure
2468 /// @param args: (tuple of args, listed below)
2469 /// @param vGatherResult[2] - array of gathered 16bpc vertices, 4 per index
2470 /// @param pVtxOut - base pointer to output simdvertex struct
2471 /// @param extendType - sign extend or zero extend
2472 /// @param bNormalized - do we need to denormalize?
2473 /// @param currentVertexElement - reference to the current vVertexElement
2474 /// @param outputElt - reference to the current offset from simdvertex we're o
2475 /// @param compMask - component packing mask
2476 /// @param compCtrl - component control val
2477 /// @param vVertexElements[4] - vertex components to output
2478 #if USE_SIMD16_SHADERS
2479 void FetchJit::Shuffle16bpcGather(Shuffle16bpcArgs
&args
, bool useVertexID2
)
2481 void FetchJit::Shuffle16bpcGather(Shuffle16bpcArgs
&args
)
2484 // Unpack tuple args
2485 Value
* (&vGatherResult
)[2] = std::get
<0>(args
);
2486 Value
* pVtxOut
= std::get
<1>(args
);
2487 const Instruction::CastOps extendType
= std::get
<2>(args
);
2488 const ConversionType conversionType
= std::get
<3>(args
);
2489 uint32_t ¤tVertexElement
= std::get
<4>(args
);
2490 uint32_t &outputElt
= std::get
<5>(args
);
2491 const ComponentEnable compMask
= std::get
<6>(args
);
2492 const ComponentControl(&compCtrl
)[4] = std::get
<7>(args
);
2493 Value
* (&vVertexElements
)[4] = std::get
<8>(args
);
2496 Type
* vGatherTy
= VectorType::get(IntegerType::getInt32Ty(JM()->mContext
), mVWidth
);
2497 Type
* v32x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 4); // vwidth is units of 32 bits
2499 // have to do extra work for sign extending
2500 if ((extendType
== Instruction::CastOps::SExt
) || (extendType
== Instruction::CastOps::SIToFP
)||
2501 (extendType
== Instruction::CastOps::FPExt
))
2503 // is this PP float?
2504 bool bFP
= (extendType
== Instruction::CastOps::FPExt
) ? true : false;
2506 Type
* v8x16Ty
= VectorType::get(mInt16Ty
, 8); // 8x16bit in a 128bit lane
2507 Type
* v128bitTy
= VectorType::get(IntegerType::getIntNTy(JM()->mContext
, 128), mVWidth
/ 4); // vwidth is units of 32 bits
2510 Value
* vConstMask
= C
<char>({0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15,
2511 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15});
2512 Value
* vi128XY
= nullptr;
2513 if(isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1)){
2514 Value
* vShufResult
= BITCAST(PSHUFB(BITCAST(vGatherResult
[0], v32x8Ty
), vConstMask
), vGatherTy
);
2515 // after pshufb: group components together in each 128bit lane
2516 // 256i - 0 1 2 3 4 5 6 7
2517 // xxxx xxxx yyyy yyyy xxxx xxxx yyyy yyyy
2519 vi128XY
= BITCAST(PERMD(vShufResult
, C
<int32_t>({0, 1, 4, 5, 2, 3, 6, 7})), v128bitTy
);
2520 // after PERMD: move and pack xy components into each 128bit lane
2521 // 256i - 0 1 2 3 4 5 6 7
2522 // xxxx xxxx xxxx xxxx yyyy yyyy yyyy yyyy
2525 // do the same for zw components
2526 Value
* vi128ZW
= nullptr;
2527 if(isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3)){
2528 Value
* vShufResult
= BITCAST(PSHUFB(BITCAST(vGatherResult
[1], v32x8Ty
), vConstMask
), vGatherTy
);
2529 vi128ZW
= BITCAST(PERMD(vShufResult
, C
<int32_t>({0, 1, 4, 5, 2, 3, 6, 7})), v128bitTy
);
2532 // init denormalize variables if needed
2533 Instruction::CastOps IntToFpCast
;
2534 Value
* conversionFactor
;
2536 switch (conversionType
)
2538 case CONVERT_NORMALIZED
:
2539 IntToFpCast
= Instruction::CastOps::SIToFP
;
2540 conversionFactor
= VIMMED1((float)(1.0 / 32767.0));
2542 case CONVERT_SSCALED
:
2543 IntToFpCast
= Instruction::CastOps::SIToFP
;
2544 conversionFactor
= VIMMED1((float)(1.0));
2546 case CONVERT_USCALED
:
2547 SWR_INVALID("Type should not be sign extended!");
2548 conversionFactor
= nullptr;
2551 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2552 conversionFactor
= nullptr;
2556 // sign extend all enabled components. If we have a fill vVertexElements, output to current simdvertex
2557 for (uint32_t i
= 0; i
< 4; i
++)
2559 if (isComponentEnabled(compMask
, i
))
2561 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2563 // if x or z, extract 128bits from lane 0, else for y or w, extract from lane 1
2564 uint32_t lane
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2565 // if x or y, use vi128XY permute result, else use vi128ZW
2566 Value
* selectedPermute
= (i
< 2) ? vi128XY
: vi128ZW
;
2569 // extract 128 bit lanes to sign extend each component
2570 vVertexElements
[currentVertexElement
] = CVTPH2PS(BITCAST(VEXTRACT(selectedPermute
, C(lane
)), v8x16Ty
));
2573 // extract 128 bit lanes to sign extend each component
2574 vVertexElements
[currentVertexElement
] = PMOVSXWD(BITCAST(VEXTRACT(selectedPermute
, C(lane
)), v8x16Ty
));
2576 // denormalize if needed
2577 if (conversionType
!= CONVERT_NONE
) {
2578 vVertexElements
[currentVertexElement
] = FMUL(CAST(IntToFpCast
, vVertexElements
[currentVertexElement
], mSimdFP32Ty
), conversionFactor
);
2581 currentVertexElement
++;
2585 #if USE_SIMD16_SHADERS
2586 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
2588 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
2592 if (currentVertexElement
> 3)
2594 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
2595 // reset to the next vVertexElement to output
2596 currentVertexElement
= 0;
2602 else if ((extendType
== Instruction::CastOps::ZExt
) || (extendType
== Instruction::CastOps::UIToFP
))
2604 // pshufb masks for each component
2605 Value
* vConstMask
[2];
2606 if(isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 2)){
2608 vConstMask
[0] = C
<char>({0, 1, -1, -1, 4, 5, -1, -1, 8, 9, -1, -1, 12, 13, -1, -1,
2609 0, 1, -1, -1, 4, 5, -1, -1, 8, 9, -1, -1, 12, 13, -1, -1, });
2612 if(isComponentEnabled(compMask
, 1) || isComponentEnabled(compMask
, 3)){
2614 vConstMask
[1] = C
<char>({2, 3, -1, -1, 6, 7, -1, -1, 10, 11, -1, -1, 14, 15, -1, -1,
2615 2, 3, -1, -1, 6, 7, -1, -1, 10, 11, -1, -1, 14, 15, -1, -1});
2618 // init denormalize variables if needed
2619 Instruction::CastOps fpCast
;
2620 Value
* conversionFactor
;
2622 switch (conversionType
)
2624 case CONVERT_NORMALIZED
:
2625 fpCast
= Instruction::CastOps::UIToFP
;
2626 conversionFactor
= VIMMED1((float)(1.0 / 65535.0));
2628 case CONVERT_USCALED
:
2629 fpCast
= Instruction::CastOps::UIToFP
;
2630 conversionFactor
= VIMMED1((float)(1.0f
));
2632 case CONVERT_SSCALED
:
2633 SWR_INVALID("Type should not be zero extended!");
2634 conversionFactor
= nullptr;
2637 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2638 conversionFactor
= nullptr;
2642 // shuffle enabled components into lower word of each 32bit lane, 0 extending to 32 bits
2643 for (uint32_t i
= 0; i
< 4; i
++)
2645 if (isComponentEnabled(compMask
, i
))
2647 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2649 // select correct constMask for x/z or y/w pshufb
2650 uint32_t selectedMask
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2651 // if x or y, use vi128XY permute result, else use vi128ZW
2652 uint32_t selectedGather
= (i
< 2) ? 0 : 1;
2654 vVertexElements
[currentVertexElement
] = BITCAST(PSHUFB(BITCAST(vGatherResult
[selectedGather
], v32x8Ty
), vConstMask
[selectedMask
]), vGatherTy
);
2655 // after pshufb mask for x channel; z uses the same shuffle from the second gather
2656 // 256i - 0 1 2 3 4 5 6 7
2657 // xx00 xx00 xx00 xx00 xx00 xx00 xx00 xx00
2659 // denormalize if needed
2660 if (conversionType
!= CONVERT_NONE
)
2662 vVertexElements
[currentVertexElement
] = FMUL(CAST(fpCast
, vVertexElements
[currentVertexElement
], mSimdFP32Ty
), conversionFactor
);
2664 currentVertexElement
++;
2668 #if USE_SIMD16_SHADERS
2669 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
], useVertexID2
);
2671 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector(compCtrl
[i
]);
2675 if (currentVertexElement
> 3)
2677 StoreVertexElements(pVtxOut
, outputElt
++, 4, vVertexElements
);
2678 // reset to the next vVertexElement to output
2679 currentVertexElement
= 0;
2686 SWR_INVALID("Unsupported conversion type");
2690 #if USE_SIMD16_BUILDER
2691 void FetchJit::Shuffle16bpcGather2(Shuffle16bpcArgs
&args
)
2693 // Unpack tuple args
2694 Value
* (&vGatherResult
)[2] = std::get
<0>(args
);
2695 Value
* pVtxOut
= std::get
<1>(args
);
2696 const Instruction::CastOps extendType
= std::get
<2>(args
);
2697 const ConversionType conversionType
= std::get
<3>(args
);
2698 uint32_t ¤tVertexElement
= std::get
<4>(args
);
2699 uint32_t &outputElt
= std::get
<5>(args
);
2700 const ComponentEnable compMask
= std::get
<6>(args
);
2701 const ComponentControl(&compCtrl
)[4] = std::get
<7>(args
);
2702 Value
* (&vVertexElements
)[4] = std::get
<8>(args
);
2705 Type
*vGatherTy
= VectorType::get(IntegerType::getInt32Ty(JM()->mContext
), mVWidth
);
2706 Type
*v32x8Ty
= VectorType::get(mInt8Ty
, mVWidth
* 4); // vwidth is units of 32 bits
2708 // have to do extra work for sign extending
2709 if ((extendType
== Instruction::CastOps::SExt
) || (extendType
== Instruction::CastOps::SIToFP
) || (extendType
== Instruction::CastOps::FPExt
))
2711 // is this PP float?
2712 bool bFP
= (extendType
== Instruction::CastOps::FPExt
) ? true : false;
2714 Type
*v8x16Ty
= VectorType::get(mInt16Ty
, 8); // 8x16bit in a 128bit lane
2715 Type
*v128bitTy
= VectorType::get(IntegerType::getIntNTy(JM()->mContext
, 128), mVWidth
/ 4); // vwidth is units of 32 bits
2718 Value
*vConstMask
= C
<char>({ 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15,
2719 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15 });
2720 Value
*vi128XY
= nullptr;
2721 Value
*vi128XY_lo
= nullptr;
2722 Value
*vi128XY_hi
= nullptr;
2723 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 1))
2725 // SIMD16 PSHUFB isnt part of AVX-512F, so split into SIMD8 for the sake of KNL, for now..
2727 Value
*vGatherResult_lo
= EXTRACT2_I(vGatherResult
[0], 0);
2728 Value
*vGatherResult_hi
= EXTRACT2_I(vGatherResult
[0], 1);
2730 Value
*vShufResult_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
), vGatherTy
);
2731 Value
*vShufResult_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
), vGatherTy
);
2733 // after pshufb: group components together in each 128bit lane
2734 // 256i - 0 1 2 3 4 5 6 7
2735 // xxxx xxxx yyyy yyyy xxxx xxxx yyyy yyyy
2737 vi128XY_lo
= BITCAST(PERMD(vShufResult_lo
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2738 vi128XY_hi
= BITCAST(PERMD(vShufResult_hi
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2740 // after PERMD: move and pack xy components into each 128bit lane
2741 // 256i - 0 1 2 3 4 5 6 7
2742 // xxxx xxxx xxxx xxxx yyyy yyyy yyyy yyyy
2745 vi128XY
= VUNDEF2_I();
2746 vi128XY
= INSERT2_I(vi128XY
, vi128XY_lo
, 0);
2747 vi128XY
= INSERT2_I(vi128XY
, vi128XY_hi
, 1);
2751 // do the same for zw components
2752 Value
*vi128ZW
= nullptr;
2753 Value
*vi128ZW_lo
= nullptr;
2754 Value
*vi128ZW_hi
= nullptr;
2755 if (isComponentEnabled(compMask
, 2) || isComponentEnabled(compMask
, 3))
2757 Value
*vGatherResult_lo
= EXTRACT2_I(vGatherResult
[1], 0);
2758 Value
*vGatherResult_hi
= EXTRACT2_I(vGatherResult
[1], 1);
2760 Value
*vShufResult_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
), vGatherTy
);
2761 Value
*vShufResult_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
), vGatherTy
);
2763 vi128ZW_lo
= BITCAST(PERMD(vShufResult_lo
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2764 vi128ZW_hi
= BITCAST(PERMD(vShufResult_hi
, C
<int32_t>({ 0, 1, 4, 5, 2, 3, 6, 7 })), v128bitTy
);
2767 vi128ZW
= VUNDEF2_I();
2768 vi128ZW
= INSERT2_I(vi128ZW
, vi128ZW_lo
, 0);
2769 vi128ZW
= INSERT2_I(vi128ZW
, vi128ZW_hi
, 1);
2773 // init denormalize variables if needed
2774 Instruction::CastOps IntToFpCast
;
2775 Value
*conversionFactor
;
2777 switch (conversionType
)
2779 case CONVERT_NORMALIZED
:
2780 IntToFpCast
= Instruction::CastOps::SIToFP
;
2781 conversionFactor
= VIMMED1((float)(1.0 / 32767.0));
2783 case CONVERT_SSCALED
:
2784 IntToFpCast
= Instruction::CastOps::SIToFP
;
2785 conversionFactor
= VIMMED1((float)(1.0));
2787 case CONVERT_USCALED
:
2788 SWR_INVALID("Type should not be sign extended!");
2789 conversionFactor
= nullptr;
2792 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2793 conversionFactor
= nullptr;
2797 // sign extend all enabled components. If we have a fill vVertexElements, output to current simdvertex
2798 for (uint32_t i
= 0; i
< 4; i
++)
2800 if (isComponentEnabled(compMask
, i
))
2802 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2804 // if x or z, extract 128bits from lane 0, else for y or w, extract from lane 1
2805 uint32_t lane
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2806 // if x or y, use vi128XY permute result, else use vi128ZW
2807 Value
*selectedPermute_lo
= (i
< 2) ? vi128XY_lo
: vi128ZW_lo
;
2808 Value
*selectedPermute_hi
= (i
< 2) ? vi128XY_hi
: vi128ZW_hi
;
2812 // extract 128 bit lanes to sign extend each component
2813 Value
*temp_lo
= CVTPH2PS(BITCAST(VEXTRACT(selectedPermute_lo
, C(lane
)), v8x16Ty
));
2814 Value
*temp_hi
= CVTPH2PS(BITCAST(VEXTRACT(selectedPermute_hi
, C(lane
)), v8x16Ty
));
2816 vVertexElements
[currentVertexElement
] = VUNDEF2_F();
2817 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_lo
, 0);
2818 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_hi
, 1);
2822 // extract 128 bit lanes to sign extend each component
2823 Value
*temp_lo
= PMOVSXWD(BITCAST(VEXTRACT(selectedPermute_lo
, C(lane
)), v8x16Ty
));
2824 Value
*temp_hi
= PMOVSXWD(BITCAST(VEXTRACT(selectedPermute_hi
, C(lane
)), v8x16Ty
));
2826 // denormalize if needed
2827 if (conversionType
!= CONVERT_NONE
)
2829 temp_lo
= FMUL(CAST(IntToFpCast
, temp_lo
, mSimdFP32Ty
), conversionFactor
);
2830 temp_hi
= FMUL(CAST(IntToFpCast
, temp_hi
, mSimdFP32Ty
), conversionFactor
);
2833 vVertexElements
[currentVertexElement
] = VUNDEF2_F();
2834 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_lo
, 0);
2835 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_hi
, 1);
2838 currentVertexElement
+= 1;
2842 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector2(compCtrl
[i
]);
2845 if (currentVertexElement
> 3)
2847 StoreVertexElements2(pVtxOut
, outputElt
++, 4, vVertexElements
);
2848 // reset to the next vVertexElement to output
2849 currentVertexElement
= 0;
2855 else if ((extendType
== Instruction::CastOps::ZExt
) || (extendType
== Instruction::CastOps::UIToFP
))
2857 // pshufb masks for each component
2858 Value
*vConstMask
[2];
2860 if (isComponentEnabled(compMask
, 0) || isComponentEnabled(compMask
, 2))
2863 vConstMask
[0] = C
<char>({ 0, 1, -1, -1, 4, 5, -1, -1, 8, 9, -1, -1, 12, 13, -1, -1,
2864 0, 1, -1, -1, 4, 5, -1, -1, 8, 9, -1, -1, 12, 13, -1, -1, });
2867 if (isComponentEnabled(compMask
, 1) || isComponentEnabled(compMask
, 3))
2870 vConstMask
[1] = C
<char>({ 2, 3, -1, -1, 6, 7, -1, -1, 10, 11, -1, -1, 14, 15, -1, -1,
2871 2, 3, -1, -1, 6, 7, -1, -1, 10, 11, -1, -1, 14, 15, -1, -1 });
2874 // init denormalize variables if needed
2875 Instruction::CastOps fpCast
;
2876 Value
* conversionFactor
;
2878 switch (conversionType
)
2880 case CONVERT_NORMALIZED
:
2881 fpCast
= Instruction::CastOps::UIToFP
;
2882 conversionFactor
= VIMMED1((float)(1.0 / 65535.0));
2884 case CONVERT_USCALED
:
2885 fpCast
= Instruction::CastOps::UIToFP
;
2886 conversionFactor
= VIMMED1((float)(1.0f
));
2888 case CONVERT_SSCALED
:
2889 SWR_INVALID("Type should not be zero extended!");
2890 conversionFactor
= nullptr;
2893 SWR_ASSERT(conversionType
== CONVERT_NONE
);
2894 conversionFactor
= nullptr;
2898 // shuffle enabled components into lower word of each 32bit lane, 0 extending to 32 bits
2899 for (uint32_t i
= 0; i
< 4; i
++)
2901 if (isComponentEnabled(compMask
, i
))
2903 if (compCtrl
[i
] == ComponentControl::StoreSrc
)
2905 // select correct constMask for x/z or y/w pshufb
2906 uint32_t selectedMask
= ((i
== 0) || (i
== 2)) ? 0 : 1;
2907 // if x or y, use vi128XY permute result, else use vi128ZW
2908 uint32_t selectedGather
= (i
< 2) ? 0 : 1;
2910 // SIMD16 PSHUFB isnt part of AVX-512F, so split into SIMD8 for the sake of KNL, for now..
2912 Value
*vGatherResult_lo
= EXTRACT2_I(vGatherResult
[selectedGather
], 0);
2913 Value
*vGatherResult_hi
= EXTRACT2_I(vGatherResult
[selectedGather
], 1);
2915 Value
*temp_lo
= BITCAST(PSHUFB(BITCAST(vGatherResult_lo
, v32x8Ty
), vConstMask
[selectedMask
]), vGatherTy
);
2916 Value
*temp_hi
= BITCAST(PSHUFB(BITCAST(vGatherResult_hi
, v32x8Ty
), vConstMask
[selectedMask
]), vGatherTy
);
2918 // after pshufb mask for x channel; z uses the same shuffle from the second gather
2919 // 256i - 0 1 2 3 4 5 6 7
2920 // xx00 xx00 xx00 xx00 xx00 xx00 xx00 xx00
2922 // denormalize if needed
2923 if (conversionType
!= CONVERT_NONE
)
2925 temp_lo
= FMUL(CAST(fpCast
, temp_lo
, mSimdFP32Ty
), conversionFactor
);
2926 temp_hi
= FMUL(CAST(fpCast
, temp_hi
, mSimdFP32Ty
), conversionFactor
);
2929 vVertexElements
[currentVertexElement
] = VUNDEF2_F();
2930 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_lo
, 0);
2931 vVertexElements
[currentVertexElement
] = INSERT2_F(vVertexElements
[currentVertexElement
], temp_hi
, 1);
2933 currentVertexElement
+= 1;
2937 vVertexElements
[currentVertexElement
++] = GenerateCompCtrlVector2(compCtrl
[i
]);
2940 if (currentVertexElement
> 3)
2942 StoreVertexElements2(pVtxOut
, outputElt
++, 4, vVertexElements
);
2943 // reset to the next vVertexElement to output
2944 currentVertexElement
= 0;
2951 SWR_INVALID("Unsupported conversion type");
2956 //////////////////////////////////////////////////////////////////////////
2957 /// @brief Output a simdvertex worth of elements to the current outputElt
2958 /// @param pVtxOut - base address of VIN output struct
2959 /// @param outputElt - simdvertex offset in VIN to write to
2960 /// @param numEltsToStore - number of simdvertex rows to write out
2961 /// @param vVertexElements - LLVM Value*[] simdvertex to write out
2962 void FetchJit::StoreVertexElements(Value
* pVtxOut
, const uint32_t outputElt
, const uint32_t numEltsToStore
, Value
* (&vVertexElements
)[4])
2964 SWR_ASSERT(numEltsToStore
<= 4, "Invalid element count.");
2966 for(uint32_t c
= 0; c
< numEltsToStore
; ++c
)
2968 // STORE expects FP32 x vWidth type, just bitcast if needed
2969 if(!vVertexElements
[c
]->getType()->getScalarType()->isFloatTy())
2971 #if FETCH_DUMP_VERTEX
2972 PRINT("vVertexElements[%d]: 0x%x\n", {C(c
), vVertexElements
[c
]});
2974 vVertexElements
[c
] = BITCAST(vVertexElements
[c
], mSimdFP32Ty
);
2976 #if FETCH_DUMP_VERTEX
2979 PRINT("vVertexElements[%d]: %f\n", {C(c
), vVertexElements
[c
]});
2982 // outputElt * 4 = offsetting by the size of a simdvertex
2983 // + c offsets to a 32bit x vWidth row within the current vertex
2984 #if USE_SIMD16_SHADERS
2985 Value
* dest
= GEP(pVtxOut
, C(outputElt
* 8 + c
* 2), "destGEP");
2987 Value
* dest
= GEP(pVtxOut
, C(outputElt
* 4 + c
), "destGEP");
2989 STORE(vVertexElements
[c
], dest
);
2993 #if USE_SIMD16_BUILDER
2994 void FetchJit::StoreVertexElements2(Value
* pVtxOut
, const uint32_t outputElt
, const uint32_t numEltsToStore
, Value
* (&vVertexElements
)[4])
2996 SWR_ASSERT(numEltsToStore
<= 4, "Invalid element count.");
2998 for (uint32_t c
= 0; c
< numEltsToStore
; ++c
)
3000 // STORE expects FP32 x vWidth type, just bitcast if needed
3001 if (!vVertexElements
[c
]->getType()->getScalarType()->isFloatTy())
3003 #if FETCH_DUMP_VERTEX
3004 PRINT("vVertexElements[%d]: 0x%x\n", { C(c
), vVertexElements
[c
] });
3006 vVertexElements
[c
] = BITCAST(vVertexElements
[c
], mSimd2FP32Ty
);
3008 #if FETCH_DUMP_VERTEX
3011 PRINT("vVertexElements[%d]: %f\n", { C(c
), vVertexElements
[c
] });
3014 // outputElt * 4 = offsetting by the size of a simdvertex
3015 // + c offsets to a 32bit x vWidth row within the current vertex
3016 Value
* dest
= GEP(pVtxOut
, C(outputElt
* 4 + c
), "destGEP");
3017 STORE(vVertexElements
[c
], dest
);
3022 //////////////////////////////////////////////////////////////////////////
3023 /// @brief Generates a constant vector of values based on the
3024 /// ComponentControl value
3025 /// @param ctrl - ComponentControl value
3026 #if USE_SIMD16_SHADERS
3027 Value
* FetchJit::GenerateCompCtrlVector(const ComponentControl ctrl
, bool useVertexID2
)
3029 Value
* FetchJit::GenerateCompCtrlVector(const ComponentControl ctrl
)
3034 case NoStore
: return VUNDEF_I();
3035 case Store0
: return VIMMED1(0);
3036 case Store1Fp
: return VIMMED1(1.0f
);
3037 case Store1Int
: return VIMMED1(1);
3040 #if USE_SIMD16_SHADERS
3044 pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID2
})), mSimdFP32Ty
);
3048 pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID
})), mSimdFP32Ty
);
3051 Value
* pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID
})), mSimdFP32Ty
);
3053 return VBROADCAST(pId
);
3055 case StoreInstanceId
:
3057 Value
* pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_CurInstance
})), mFP32Ty
);
3058 return VBROADCAST(pId
);
3061 default: SWR_INVALID("Invalid component control"); return VUNDEF_I();
3065 #if USE_SIMD16_BUILDER
3066 Value
* FetchJit::GenerateCompCtrlVector2(const ComponentControl ctrl
)
3070 case NoStore
: return VUNDEF2_I();
3071 case Store0
: return VIMMED2_1(0);
3072 case Store1Fp
: return VIMMED2_1(1.0f
);
3073 case Store1Int
: return VIMMED2_1(1);
3076 Value
* pId
= VUNDEF2_F();
3078 Value
* pId_lo
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID
})), mSimdFP32Ty
);
3079 Value
* pId_hi
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_VertexID2
})), mSimdFP32Ty
);
3081 pId
= INSERT2_F(pId
, pId_lo
, 0);
3082 pId
= INSERT2_F(pId
, pId_hi
, 1);
3084 return VBROADCAST2(pId
);
3086 case StoreInstanceId
:
3088 Value
* pId
= BITCAST(LOAD(GEP(mpFetchInfo
, { 0, SWR_FETCH_CONTEXT_CurInstance
})), mFP32Ty
);
3089 return VBROADCAST2(pId
);
3092 default: SWR_INVALID("Invalid component control"); return VUNDEF2_I();
3097 //////////////////////////////////////////////////////////////////////////
3098 /// @brief Returns the enable mask for the specified component.
3099 /// @param enableMask - enable bits
3100 /// @param component - component to check if enabled.
3101 bool isComponentEnabled(ComponentEnable enableMask
, uint8_t component
)
3106 case 0: return (enableMask
& ComponentEnable::X
);
3108 case 1: return (enableMask
& ComponentEnable::Y
);
3110 case 2: return (enableMask
& ComponentEnable::Z
);
3112 case 3: return (enableMask
& ComponentEnable::W
);
3114 default: return false;
3119 //////////////////////////////////////////////////////////////////////////
3120 /// @brief JITs from fetch shader IR
3121 /// @param hJitMgr - JitManager handle
3122 /// @param func - LLVM function IR
3123 /// @return PFN_FETCH_FUNC - pointer to fetch code
3124 PFN_FETCH_FUNC
JitFetchFunc(HANDLE hJitMgr
, const HANDLE hFunc
)
3126 const llvm::Function
* func
= (const llvm::Function
*)hFunc
;
3127 JitManager
* pJitMgr
= reinterpret_cast<JitManager
*>(hJitMgr
);
3128 PFN_FETCH_FUNC pfnFetch
;
3130 pfnFetch
= (PFN_FETCH_FUNC
)(pJitMgr
->mpExec
->getFunctionAddress(func
->getName().str()));
3131 // MCJIT finalizes modules the first time you JIT code from them. After finalized, you cannot add new IR to the module
3132 pJitMgr
->mIsModuleFinalized
= true;
3134 #if defined(KNOB_SWRC_TRACING)
3136 const char *funcName
= func
->getName().data();
3137 sprintf(fName
, "%s.bin", funcName
);
3138 FILE *fd
= fopen(fName
, "wb");
3139 fwrite((void *)pfnFetch
, 1, 2048, fd
);
3143 pJitMgr
->DumpAsm(const_cast<llvm::Function
*>(func
), "final");
3148 //////////////////////////////////////////////////////////////////////////
3149 /// @brief JIT compiles fetch shader
3150 /// @param hJitMgr - JitManager handle
3151 /// @param state - fetch state to build function from
3152 extern "C" PFN_FETCH_FUNC JITCALL
JitCompileFetch(HANDLE hJitMgr
, const FETCH_COMPILE_STATE
& state
)
3154 JitManager
* pJitMgr
= reinterpret_cast<JitManager
*>(hJitMgr
);
3156 pJitMgr
->SetupNewModule();
3158 FetchJit
theJit(pJitMgr
);
3159 HANDLE hFunc
= theJit
.Create(state
);
3161 return JitFetchFunc(hJitMgr
, hFunc
);