MCPcopy Create free account
hub / github.com/TheRealMJP/DeferredTexturing / SampleTexture2D

Function SampleTexture2D

SampleFramework12/v1.00/Graphics/Textures.h:80–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78// == Texture Sampling Functions ==================================================================
79
80template<typename T> static DirectX::XMVECTOR SampleTexture2D(Float2 uv, uint32 arraySlice, const Array<T>& texels,
81 uint32 texWidth, uint32 texHeight, uint32 numSlices)
82{
83 Float2 texSize = Float2(float(texWidth), float(texHeight));
84 Float2 halfTexelSize(0.5f / texSize.x, 0.5f / texSize.y);
85 Float2 samplePos = Frac(uv - halfTexelSize);
86 if(samplePos.x < 0.0f)
87 samplePos.x = 1.0f + samplePos.x;
88 if(samplePos.y < 0.0f)
89 samplePos.y = 1.0f + samplePos.y;
90 samplePos *= texSize;
91 uint32 samplePosX = std::min(uint32(samplePos.x), texWidth - 1);
92 uint32 samplePosY = std::min(uint32(samplePos.y), texHeight - 1);
93 uint32 samplePosXNext = std::min(samplePosX + 1, texWidth - 1);
94 uint32 samplePosYNext = std::min(samplePosY + 1, texHeight - 1);
95
96 Float2 lerpAmts = Float2(Frac(samplePos.x), Frac(samplePos.y));
97
98 numSlices = std::max<uint32>(numSlices, 1);
99 const uint32 sliceOffset = std::min(arraySlice, numSlices) * texWidth * texHeight;
100
101 DirectX::XMVECTOR samples[4];
102 samples[0] = texels[sliceOffset + samplePosY * texWidth + samplePosX].ToSIMD();
103 samples[1] = texels[sliceOffset + samplePosY * texWidth + samplePosXNext].ToSIMD();
104 samples[2] = texels[sliceOffset + samplePosYNext * texWidth + samplePosX].ToSIMD();
105 samples[3] = texels[sliceOffset + samplePosYNext * texWidth + samplePosXNext].ToSIMD();
106
107 // lerp between the shadow values to calculate our light amount
108 return DirectX::XMVectorLerp(DirectX::XMVectorLerp(samples[0], samples[1], lerpAmts.x),
109 DirectX::XMVectorLerp(samples[2], samples[3], lerpAmts.x), lerpAmts.y);
110}
111
112template<typename T> static DirectX::XMVECTOR SampleTexture2D(Float2 uv, uint32 arraySlice, const TextureData<T>& texData)
113{

Callers 1

SampleCubemapFunction · 0.85

Calls 3

Float2Class · 0.85
FracFunction · 0.85
ToSIMDMethod · 0.45

Tested by

no test coverage detected