Commit 2e40a573 by Nicolas Capens Committed by Nicolas Capens

Refactor sampling code

- Sort the coordinates and operands in the same order as for the SPIR-V instructions. - Renamed 'q' to 'dRef'. - Used integer type for offset operands. - Renamed 'addressingModeY' to 'addressingModeA'. Note this is only used by cube array coordinates. 2D array coordinates use 'addressingModeW' at the moment. - Removed array layer coordinate parameter from 3D sampling. - Added 'dRef' operand for 3D sampling. Bug: b/162315264 Change-Id: Ia24aa22a57ba5507aa4c8cf617f49f18ccca59c5 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/47097 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Kokoro-Result: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com>
parent d0b7d1e3
...@@ -97,7 +97,7 @@ struct Sampler ...@@ -97,7 +97,7 @@ struct Sampler
AddressingMode addressingModeU; AddressingMode addressingModeU;
AddressingMode addressingModeV; AddressingMode addressingModeV;
AddressingMode addressingModeW; AddressingMode addressingModeW;
AddressingMode addressingModeY; AddressingMode addressingModeA;
MipmapType mipmapFilter; MipmapType mipmapFilter;
VkComponentMapping swizzle; VkComponentMapping swizzle;
int gatherComponent; int gatherComponent;
......
...@@ -58,36 +58,36 @@ class SamplerCore ...@@ -58,36 +58,36 @@ class SamplerCore
public: public:
SamplerCore(Pointer<Byte> &constants, const Sampler &state); SamplerCore(Pointer<Byte> &constants, const Sampler &state);
Vector4f sampleTexture(Pointer<Byte> &texture, Float4 uvw[4], Float4 &q, Float &&lodOrBias, Float4 &dsx, Float4 &dsy, Vector4f &offset, Int4 &sampleId, SamplerFunction function); Vector4f sampleTexture(Pointer<Byte> &texture, Float4 uvwa[4], Float4 &q, Float &&lodOrBias, Float4 &dsx, Float4 &dsy, Vector4i &offset, Int4 &sample, SamplerFunction function);
private: private:
Short4 offsetSample(Short4 &uvw, Pointer<Byte> &mipmap, int halfOffset, bool wrap, int count, Float &lod); Short4 offsetSample(Short4 &uvw, Pointer<Byte> &mipmap, int halfOffset, bool wrap, int count, Float &lod);
Vector4s sampleFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, SamplerFunction function); Vector4s sampleFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &cubeArrayCoord, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, SamplerFunction function);
Vector4s sampleAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, bool secondLOD, SamplerFunction function); Vector4s sampleAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &cubeArrayCoord, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, bool secondLOD, SamplerFunction function);
Vector4s sampleQuad(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, bool secondLOD, SamplerFunction function); Vector4s sampleQuad(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &cubeArrayCoord, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD, SamplerFunction function);
Vector4s sampleQuad2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, bool secondLOD, SamplerFunction function); Vector4s sampleQuad2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &cubeArrayCoord, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD, SamplerFunction function);
Vector4s sample3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, bool secondLOD, SamplerFunction function); Vector4s sample3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD, SamplerFunction function);
Vector4f sampleFloatFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, SamplerFunction function); Vector4f sampleFloatFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &cubeArrayCoord, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, SamplerFunction function);
Vector4f sampleFloatAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, bool secondLOD, SamplerFunction function); Vector4f sampleFloatAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &cubeArrayCoord, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, bool secondLOD, SamplerFunction function);
Vector4f sampleFloat(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, bool secondLOD, SamplerFunction function); Vector4f sampleFloat(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &cubeArrayCoord, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD, SamplerFunction function);
Vector4f sampleFloat2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, bool secondLOD, SamplerFunction function); Vector4f sampleFloat2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &cubeArrayCoord, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD, SamplerFunction function);
Vector4f sampleFloat3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, const Float4 &cubeArrayCoord, const Int4 &sampleId, Float &lod, bool secondLOD, SamplerFunction function); Vector4f sampleFloat3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD, SamplerFunction function);
Float log2sqrt(Float lod); Float log2sqrt(Float lod);
Float log2(Float lod); Float log2(Float lod);
void computeLod(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &u, Float4 &v, Float4 &dsx, Float4 &dsy, SamplerFunction function); void computeLod(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &u, Float4 &v, Float4 &dsx, Float4 &dsy, SamplerFunction function);
void computeLodCube(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, Float4 &dsx, Float4 &dsy, Float4 &M, SamplerFunction function); void computeLodCube(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, Float4 &dsx, Float4 &dsy, Float4 &M, SamplerFunction function);
void computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, Float4 &dsx, Float4 &dsy, SamplerFunction function); void computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, Float4 &dsx, Float4 &dsy, SamplerFunction function);
Int4 cubeFace(Float4 &U, Float4 &V, Float4 &x, Float4 &y, Float4 &z, Float4 &M); Int4 cubeFace(Float4 &U, Float4 &V, Float4 &x, Float4 &y, Float4 &z, Float4 &M);
Short4 applyOffset(Short4 &uvw, Float4 &offset, const Int4 &whd, AddressingMode mode); Short4 applyOffset(Short4 &uvw, Int4 &offset, const Int4 &whd, AddressingMode mode);
void computeIndices(UInt index[4], Short4 uuuu, Short4 vvvv, Short4 wwww, Vector4f &offset, const Pointer<Byte> &mipmap, const Short4 &cubeArrayId, const Int4 &sampleId, SamplerFunction function); void computeIndices(UInt index[4], Short4 uuuu, Short4 vvvv, Short4 wwww, const Short4 &cubeArrayLayer, Vector4i &offset, const Int4 &sample, const Pointer<Byte> &mipmap, SamplerFunction function);
void computeIndices(UInt index[4], Int4 uuuu, Int4 vvvv, Int4 wwww, Int4 valid, const Pointer<Byte> &mipmap, const Int4 &cubeArrayId, const Int4 &sampleId, SamplerFunction function); void computeIndices(UInt index[4], Int4 uuuu, Int4 vvvv, Int4 wwww, const Int4 &cubeArrayLayer, const Int4 &sample, Int4 valid, const Pointer<Byte> &mipmap, SamplerFunction function);
Vector4s sampleTexel(Short4 &u, Short4 &v, Short4 &s, Vector4f &offset, Pointer<Byte> &mipmap, const Short4 &cubeArrayId, const Int4 &sampleId, Pointer<Byte> buffer, SamplerFunction function); Vector4s sampleTexel(Short4 &u, Short4 &v, Short4 &w, const Short4 &cubeArrayLayer, Vector4i &offset, const Int4 &sample, Pointer<Byte> &mipmap, Pointer<Byte> buffer, SamplerFunction function);
Vector4s sampleTexel(UInt index[4], Pointer<Byte> buffer); Vector4s sampleTexel(UInt index[4], Pointer<Byte> buffer);
Vector4f sampleTexel(Int4 &u, Int4 &v, Int4 &s, Float4 &z, Pointer<Byte> &mipmap, const Int4 &cubeArrayId, const Int4 &sampleId, Pointer<Byte> buffer, SamplerFunction function); Vector4f sampleTexel(Int4 &u, Int4 &v, Int4 &w, const Int4 &cubeArrayLayer, Float4 &dRef, const Int4 &sample, Pointer<Byte> &mipmap, Pointer<Byte> buffer, SamplerFunction function);
Vector4f replaceBorderTexel(const Vector4f &c, Int4 valid); Vector4f replaceBorderTexel(const Vector4f &c, Int4 valid);
void selectMipmap(const Pointer<Byte> &texture, Pointer<Byte> &mipmap, Pointer<Byte> &buffer, const Float &lod, bool secondLOD); void selectMipmap(const Pointer<Byte> &texture, Pointer<Byte> &mipmap, Pointer<Byte> &buffer, const Float &lod, bool secondLOD);
Short4 address(const Float4 &uw, AddressingMode addressingMode, Pointer<Byte> &mipmap); Short4 address(const Float4 &uw, AddressingMode addressingMode, Pointer<Byte> &mipmap);
void address(const Float4 &uw, Int4 &xyz0, Int4 &xyz1, Float4 &f, Pointer<Byte> &mipmap, Float4 &texOffset, Int4 &filter, int whd, AddressingMode addressingMode, SamplerFunction function); void address(const Float4 &uw, Int4 &xyz0, Int4 &xyz1, Float4 &f, Pointer<Byte> &mipmap, Int4 &offset, Int4 &filter, int whd, AddressingMode addressingMode, SamplerFunction function);
Int4 computeFilterOffset(Float &lod); Int4 computeFilterOffset(Float &lod);
void convertSigned15(Float4 &cf, Short4 &ci); void convertSigned15(Float4 &cf, Short4 &ci);
......
...@@ -107,6 +107,49 @@ Float4 &Vector4f::operator[](int i) ...@@ -107,6 +107,49 @@ Float4 &Vector4f::operator[](int i)
return x; return x;
} }
Vector4i::Vector4i()
{
}
Vector4i::Vector4i(int x, int y, int z, int w)
{
this->x = Int4(x);
this->y = Int4(y);
this->z = Int4(z);
this->w = Int4(w);
}
Vector4i::Vector4i(const Vector4i &rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
w = rhs.w;
}
Vector4i &Vector4i::operator=(const Vector4i &rhs)
{
x = rhs.x;
y = rhs.y;
z = rhs.z;
w = rhs.w;
return *this;
}
Int4 &Vector4i::operator[](int i)
{
switch(i)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3: return w;
}
return x;
}
Float4 exponential2(RValue<Float4> x, bool pp) Float4 exponential2(RValue<Float4> x, bool pp)
{ {
// This implementation is based on 2^(i + f) = 2^i * 2^f, // This implementation is based on 2^(i + f) = 2^i * 2^f,
......
...@@ -59,6 +59,22 @@ public: ...@@ -59,6 +59,22 @@ public:
Float4 w; Float4 w;
}; };
class Vector4i
{
public:
Vector4i();
Vector4i(int x, int y, int z, int w);
Vector4i(const Vector4i &rhs);
Int4 &operator[](int i);
Vector4i &operator=(const Vector4i &rhs);
Int4 x;
Int4 y;
Int4 z;
Int4 w;
};
enum class OutOfBoundsBehavior enum class OutOfBoundsBehavior
{ {
Nullify, // Loads become zero, stores are elided. Nullify, // Loads become zero, stores are elided.
......
...@@ -511,12 +511,12 @@ public: ...@@ -511,12 +511,12 @@ public:
uint32_t gatherComponent : 2; uint32_t gatherComponent : 2;
// Parameters are passed to the sampling routine in this order: // Parameters are passed to the sampling routine in this order:
uint32_t coordinates : 3; // 1-4 (does not contain projection component) uint32_t coordinates : 3; // 1-4 (does not contain projection component)
// uint32_t dref : 1; // Indicated by Variant::ProjDref|Dref /* uint32_t dref : 1; */ // Indicated by Variant::ProjDref|Dref
// uint32_t lodOrBias : 1; // Indicated by SamplerMethod::Lod|Bias|Fetch /* uint32_t lodOrBias : 1; */ // Indicated by SamplerMethod::Lod|Bias|Fetch
uint32_t grad : 2; // 0-3 components (for each of dx / dy) uint32_t grad : 2; // 0-3 components (for each of dx / dy)
uint32_t offset : 2; // 0-3 components uint32_t offset : 2; // 0-3 components
uint32_t sample : 1; // 0-1 scalar integer uint32_t sample : 1; // 0-1 scalar integer
}; };
uint32_t parameters; uint32_t parameters;
......
...@@ -51,7 +51,7 @@ SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::Sampl ...@@ -51,7 +51,7 @@ SpirvShader::ImageSampler *SpirvShader::getImageSampler(uint32_t inst, vk::Sampl
samplerState.addressingModeU = convertAddressingMode(0, sampler, type); samplerState.addressingModeU = convertAddressingMode(0, sampler, type);
samplerState.addressingModeV = convertAddressingMode(1, sampler, type); samplerState.addressingModeV = convertAddressingMode(1, sampler, type);
samplerState.addressingModeW = convertAddressingMode(2, sampler, type); samplerState.addressingModeW = convertAddressingMode(2, sampler, type);
samplerState.addressingModeY = convertAddressingMode(3, sampler, type); samplerState.addressingModeA = convertAddressingMode(3, sampler, type);
samplerState.mipmapFilter = convertMipmapMode(sampler); samplerState.mipmapFilter = convertMipmapMode(sampler);
samplerState.swizzle = imageDescriptor->swizzle; samplerState.swizzle = imageDescriptor->swizzle;
...@@ -104,24 +104,24 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in ...@@ -104,24 +104,24 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in
Pointer<SIMD::Float> out = function.Arg<2>(); Pointer<SIMD::Float> out = function.Arg<2>();
Pointer<Byte> constants = function.Arg<3>(); Pointer<Byte> constants = function.Arg<3>();
SIMD::Float uvw[4] = { 0, 0, 0, 0 }; SIMD::Float uvwa[4] = { 0, 0, 0, 0 };
SIMD::Float q = 0; SIMD::Float dRef = 0;
SIMD::Float lodOrBias = 0; // Explicit level-of-detail, or bias added to the implicit level-of-detail (depending on samplerMethod). SIMD::Float lodOrBias = 0; // Explicit level-of-detail, or bias added to the implicit level-of-detail (depending on samplerMethod).
Vector4f dsx = { 0, 0, 0, 0 }; Vector4f dsx = { 0, 0, 0, 0 };
Vector4f dsy = { 0, 0, 0, 0 }; Vector4f dsy = { 0, 0, 0, 0 };
Vector4f offset = { 0, 0, 0, 0 }; Vector4i offset = { 0, 0, 0, 0 };
SIMD::Int sampleId = 0; SIMD::Int sampleId = 0;
SamplerFunction samplerFunction = instruction.getSamplerFunction(); SamplerFunction samplerFunction = instruction.getSamplerFunction();
uint32_t i = 0; uint32_t i = 0;
for(; i < instruction.coordinates; i++) for(; i < instruction.coordinates; i++)
{ {
uvw[i] = in[i]; uvwa[i] = in[i];
} }
if(instruction.isDref()) if(instruction.isDref())
{ {
q = in[i]; dRef = in[i];
i++; i++;
} }
...@@ -129,12 +129,12 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in ...@@ -129,12 +129,12 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in
// Implement optimized 1D sampling. // Implement optimized 1D sampling.
if(samplerState.textureType == VK_IMAGE_VIEW_TYPE_1D) if(samplerState.textureType == VK_IMAGE_VIEW_TYPE_1D)
{ {
uvw[1] = SIMD::Float(0); uvwa[1] = SIMD::Float(0);
} }
else if(samplerState.textureType == VK_IMAGE_VIEW_TYPE_1D_ARRAY) else if(samplerState.textureType == VK_IMAGE_VIEW_TYPE_1D_ARRAY)
{ {
uvw[1] = SIMD::Float(0); uvwa[1] = SIMD::Float(0);
uvw[2] = in[1]; // Move 1D layer coordinate to 2D layer coordinate index. uvwa[2] = in[1]; // Move 1D layer coordinate to 2D layer coordinate index.
} }
if(instruction.samplerMethod == Lod || instruction.samplerMethod == Bias || instruction.samplerMethod == Fetch) if(instruction.samplerMethod == Lod || instruction.samplerMethod == Bias || instruction.samplerMethod == Fetch)
...@@ -157,7 +157,7 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in ...@@ -157,7 +157,7 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in
for(uint32_t j = 0; j < instruction.offset; j++, i++) for(uint32_t j = 0; j < instruction.offset; j++, i++)
{ {
offset[j] = in[i]; offset[j] = As<SIMD::Int>(in[i]);
} }
if(instruction.sample) if(instruction.sample)
...@@ -193,7 +193,7 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in ...@@ -193,7 +193,7 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in
dPdy.y = Float(0.0f); dPdy.y = Float(0.0f);
} }
Vector4f sample = s.sampleTexture(texture, uvw, q, lod[i], dPdx, dPdy, offset, sampleId, samplerFunction); Vector4f sample = s.sampleTexture(texture, uvwa, dRef, lod[i], dPdx, dPdy, offset, sampleId, samplerFunction);
Pointer<Float> rgba = out; Pointer<Float> rgba = out;
rgba[0 * SIMD::Width + i] = Pointer<Float>(&sample.x)[i]; rgba[0 * SIMD::Width + i] = Pointer<Float>(&sample.x)[i];
...@@ -204,7 +204,7 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in ...@@ -204,7 +204,7 @@ std::shared_ptr<rr::Routine> SpirvShader::emitSamplerRoutine(ImageInstruction in
} }
else else
{ {
Vector4f sample = s.sampleTexture(texture, uvw, q, lodOrBias.x, (dsx.x), (dsy.x), offset, sampleId, samplerFunction); Vector4f sample = s.sampleTexture(texture, uvwa, dRef, lodOrBias.x, (dsx.x), (dsy.x), offset, sampleId, samplerFunction);
Pointer<SIMD::Float> rgba = out; Pointer<SIMD::Float> rgba = out;
rgba[0] = sample.x; rgba[0] = sample.x;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment