1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef sw_SamplerCore_hpp 16 #define sw_SamplerCore_hpp 17 18 #include "ShaderCore.hpp" 19 #include "Device/Sampler.hpp" 20 #include "Reactor/Print.hpp" 21 #include "Reactor/Reactor.hpp" 22 23 namespace sw { 24 25 using namespace rr; 26 27 enum SamplerMethod : uint32_t 28 { 29 Implicit, // Compute gradients (pixel shader only). 30 Bias, // Compute gradients and add provided bias. 31 Lod, // Use provided LOD. 32 Grad, // Use provided gradients. 33 Fetch, // Use provided integer coordinates. 34 Base, // Sample base level. 35 Query, // Return implicit LOD. 36 Gather, // Return one channel of each texel in footprint. 37 Read, // Read a texel from an image without a sampler. 38 Write, // Write a texel to an image without a sampler. 39 TexelPointer, // Form a pointer to a texel of an image. 40 SAMPLER_METHOD_LAST = TexelPointer, 41 }; 42 43 // TODO(b/129523279): Eliminate and use SpirvShader::ImageInstruction instead. 44 struct SamplerFunction 45 { SamplerFunctionsw::SamplerFunction46 SamplerFunction(SamplerMethod method, bool offset = false, bool sample = false) 47 : method(method) 48 , offset(offset) 49 , sample(sample) 50 {} 51 operator SamplerMethodsw::SamplerFunction52 operator SamplerMethod() const { return method; } 53 54 const SamplerMethod method; 55 const bool offset; 56 const bool sample; 57 }; 58 59 class SamplerCore 60 { 61 public: 62 SamplerCore(Pointer<Byte> &constants, const Sampler &state, SamplerFunction function); 63 64 SIMD::Float4 sampleTexture(Pointer<Byte> &texture, SIMD::Float uvwa[4], const SIMD::Float &dRef, const Float &lodOrBias, const SIMD::Float &dsx, const SIMD::Float &dsy, SIMD::Int offset[4], const SIMD::Int &sample); 65 66 private: 67 Vector4f sampleTexture128(Pointer<Byte> &texture, Float4 uvwa[4], const Float4 &dRef, const Float &lodOrBias, const Float4 &dsx, const Float4 &dsy, Vector4i &offset, const Int4 &sample); 68 69 Float4 applySwizzle(const Vector4f &c, VkComponentSwizzle swizzle, bool integer); 70 Short4 offsetSample(Short4 &uvw, Pointer<Byte> &mipmap, int halfOffset, bool wrap, int count, Float &lod); 71 Vector4s sampleFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta); 72 Vector4s sampleAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, bool secondLOD); 73 Vector4s sampleQuad(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD); 74 Vector4s sampleQuad2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD); 75 Vector4s sample3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD); 76 Vector4f sampleFloatFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, const Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta); 77 Vector4f sampleFloatAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, const Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, bool secondLOD); 78 Vector4f sampleFloat(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, const Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD); 79 Vector4f sampleFloat2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, const Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD); 80 Vector4f sampleFloat3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD); 81 void computeLod1D(Pointer<Byte> &texture, Float &lod, Float4 &u, const Float4 &dsx, const Float4 &dsy); 82 void computeLod2D(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &u, Float4 &v, const Float4 &dsx, const Float4 &dsy); 83 void computeLodCube(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, const Float4 &dsx, const Float4 &dsy, Float4 &M); 84 void computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, const Float4 &dsx, const Float4 &dsy); 85 Int4 cubeFace(Float4 &U, Float4 &V, Float4 &x, Float4 &y, Float4 &z, Float4 &M); 86 void applyOffset(Float4 &u, Float4 &v, Float4 &w, Vector4i &offset, Pointer<Byte> mipmap); 87 void computeIndices(UInt index[4], Short4 uuuu, Short4 vvvv, Short4 wwww, const Short4 &cubeArrayLayer, const Int4 &sample, const Pointer<Byte> &mipmap); 88 void computeIndices(UInt index[4], Int4 uuuu, Int4 vvvv, Int4 wwww, const Int4 &sample, Int4 valid, const Pointer<Byte> &mipmap); 89 void bilinearInterpolateFloat(Vector4f &output, const Short4 &uuuu0, const Short4 &vvvv0, Vector4f &c00, Vector4f &c01, Vector4f &c10, Vector4f &c11, const Pointer<Byte> &mipmap, bool interpolateComponent0, bool interpolateComponent1, bool interpolateComponent2, bool interpolateComponent3); 90 void bilinearInterpolate(Vector4s &output, const Short4 &uuuu0, const Short4 &vvvv0, Vector4s &c00, Vector4s &c01, Vector4s &c10, Vector4s &c11, const Pointer<Byte> &mipmap); 91 void sampleLumaTexel(Vector4f& output, Short4 &u, Short4 &v, Short4 &w, const Short4 &cubeArrayLayer, const Int4 &sample, Pointer<Byte> &lumaMipmap, Pointer<Byte> lumaBuffer); 92 void sampleChromaTexel(Vector4f& output, Short4 &u, Short4 &v, Short4 &w, const Short4 &cubeArrayLayer, const Int4 &sample, Pointer<Byte> &mipmapU, Pointer<Byte> bufferU, Pointer<Byte> &mipmapV, Pointer<Byte> bufferV); 93 Vector4s sampleTexel(Short4 &u, Short4 &v, Short4 &w, const Short4 &cubeArrayLayer, const Int4 &sample, Pointer<Byte> &mipmap, Pointer<Byte> buffer); 94 Vector4s sampleTexel(UInt index[4], Pointer<Byte> buffer); 95 Vector4f sampleTexel(Int4 &u, Int4 &v, Int4 &w, const Float4 &dRef, const Int4 &sample, Pointer<Byte> &mipmap, Pointer<Byte> buffer); 96 Vector4f replaceBorderTexel(const Vector4f &c, Int4 valid); 97 Pointer<Byte> selectMipmap(const Pointer<Byte> &texture, const Float &lod, bool secondLOD); 98 Short4 address(const Float4 &uvw, AddressingMode addressingMode); 99 Short4 computeLayerIndex16(const Float4 &a, Pointer<Byte> &mipmap); 100 void address(const Float4 &uvw, Int4 &xyz0, Int4 &xyz1, Float4 &f, Pointer<Byte> &mipmap, Int4 &filter, int whd, AddressingMode addressingMode); 101 Int4 computeLayerIndex(const Float4 &a, Pointer<Byte> &mipmap); 102 Int4 computeFilterOffset(Float &lod); 103 void sRGBtoLinearFF00(Short4 &c); 104 105 bool hasNormalizedFormat() const; 106 bool hasFloatTexture() const; 107 bool hasUnnormalizedIntegerTexture() const; 108 bool hasUnsignedTextureComponent(int component) const; 109 int textureComponentCount() const; 110 bool has16bitPackedTextureFormat() const; 111 bool has8bitTextureComponents() const; 112 bool has16bitTextureComponents() const; 113 bool has32bitIntegerTextureComponents() const; 114 bool isYcbcrFormat() const; 115 bool isRGBComponent(int component) const; 116 bool borderModeActive() const; 117 VkComponentSwizzle gatherSwizzle() const; 118 sw::float4 getComponentScale() const; 119 int getGatherComponent() const; 120 121 Pointer<Byte> &constants; 122 const Sampler &state; 123 const SamplerFunction function; 124 }; 125 126 } // namespace sw 127 128 #ifdef ENABLE_RR_PRINT 129 namespace rr { 130 131 template<> 132 struct PrintValue::Ty<sw::SamplerFunction> 133 { fmtrr::PrintValue::Ty134 static std::string fmt(const sw::SamplerFunction &v) 135 { 136 return std::string("SamplerFunction[") + 137 "method: " + std::to_string(v.method) + 138 ", offset: " + std::to_string(v.offset) + 139 ", sample: " + std::to_string(v.sample) + 140 "]"; 141 } 142 valrr::PrintValue::Ty143 static std::vector<rr::Value *> val(const sw::SamplerFunction &v) { return {}; } 144 }; 145 146 } // namespace rr 147 #endif // ENABLE_RR_PRINT 148 149 #endif // sw_SamplerCore_hpp 150