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 #ifdef None 24 # undef None // b/127920555 25 #endif 26 27 namespace sw { 28 29 using namespace rr; 30 31 enum SamplerMethod : uint32_t 32 { 33 Implicit, // Compute gradients (pixel shader only). 34 Bias, // Compute gradients and add provided bias. 35 Lod, // Use provided LOD. 36 Grad, // Use provided gradients. 37 Fetch, // Use provided integer coordinates. 38 Base, // Sample base level. 39 Query, // Return implicit LOD. 40 Gather, // Return one channel of each texel in footprint. 41 SAMPLER_METHOD_LAST = Gather, 42 }; 43 44 // TODO(b/129523279): Eliminate and use SpirvShader::ImageInstruction instead. 45 struct SamplerFunction 46 { SamplerFunctionsw::SamplerFunction47 SamplerFunction(SamplerMethod method, bool offset = false, bool sample = false) 48 : method(method) 49 , offset(offset) 50 , sample(sample) 51 {} 52 operator SamplerMethodsw::SamplerFunction53 operator SamplerMethod() { return method; } 54 55 const SamplerMethod method; 56 const bool offset; 57 const bool sample; 58 }; 59 60 class SamplerCore 61 { 62 public: 63 SamplerCore(Pointer<Byte> &constants, const Sampler &state); 64 65 Vector4f sampleTexture(Pointer<Byte> &texture, Pointer<Byte> &sampler, Float4 uvw[4], Float4 &q, Float &&lodOrBias, Float4 &dsx, Float4 &dsy, Vector4f &offset, Int4 &sampleId, SamplerFunction function); 66 67 private: 68 Short4 offsetSample(Short4 &uvw, Pointer<Byte> &mipmap, int halfOffset, bool wrap, int count, Float &lod); 69 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); 70 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); 71 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); 72 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); 73 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); 74 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); 75 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); 76 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); 77 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); 78 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); 79 Float log2sqrt(Float lod); 80 Float log2(Float lod); 81 void computeLod(Pointer<Byte> &texture, Pointer<Byte> &sampler, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &u, Float4 &v, Float4 &dsx, Float4 &dsy, SamplerFunction function); 82 void computeLodCube(Pointer<Byte> &texture, Pointer<Byte> &sampler, Float &lod, Float4 &u, Float4 &v, Float4 &w, Float4 &dsx, Float4 &dsy, Float4 &M, SamplerFunction function); 83 void computeLod3D(Pointer<Byte> &texture, Pointer<Byte> &sampler, Float &lod, Float4 &u, Float4 &v, Float4 &w, Float4 &dsx, Float4 &dsy, SamplerFunction function); 84 Int4 cubeFace(Float4 &U, Float4 &V, Float4 &x, Float4 &y, Float4 &z, Float4 &M); 85 Short4 applyOffset(Short4 &uvw, Float4 &offset, const Int4 &whd, AddressingMode mode); 86 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); 87 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); 88 Vector4s sampleTexel(Short4 &u, Short4 &v, Short4 &s, Vector4f &offset, Pointer<Byte> &mipmap, const Short4 &cubeArrayId, const Int4 &sampleId, Pointer<Byte> buffer, SamplerFunction function); 89 Vector4s sampleTexel(UInt index[4], Pointer<Byte> buffer); 90 Vector4f sampleTexel(Int4 &u, Int4 &v, Int4 &s, Float4 &z, Pointer<Byte> &mipmap, const Int4 &cubeArrayId, const Int4 &sampleId, Pointer<Byte> buffer, SamplerFunction function); 91 Vector4f replaceBorderTexel(const Vector4f &c, Int4 valid); 92 void selectMipmap(const Pointer<Byte> &texture, Pointer<Byte> &mipmap, Pointer<Byte> &buffer, const Float &lod, bool secondLOD); 93 Short4 address(const Float4 &uw, AddressingMode addressingMode, Pointer<Byte> &mipmap); 94 void address(const Float4 &uw, Int4 &xyz0, Int4 &xyz1, Float4 &f, Pointer<Byte> &mipmap, Float4 &texOffset, Int4 &filter, int whd, AddressingMode addressingMode, SamplerFunction function); 95 Int4 computeFilterOffset(Float &lod); 96 97 void convertSigned15(Float4 &cf, Short4 &ci); 98 void convertUnsigned16(Float4 &cf, Short4 &ci); 99 void sRGBtoLinearFF00(Short4 &c); 100 101 bool hasFloatTexture() const; 102 bool hasUnnormalizedIntegerTexture() const; 103 bool hasUnsignedTextureComponent(int component) const; 104 int textureComponentCount() const; 105 bool hasThirdCoordinate() const; 106 bool has16bitTextureFormat() const; 107 bool has8bitTextureComponents() const; 108 bool has16bitTextureComponents() const; 109 bool has32bitIntegerTextureComponents() const; 110 bool isYcbcrFormat() const; 111 bool isRGBComponent(int component) const; 112 bool borderModeActive() const; 113 bool isCube() const; 114 VkComponentSwizzle gatherSwizzle() const; 115 116 Pointer<Byte> &constants; 117 const Sampler &state; 118 }; 119 120 } // namespace sw 121 122 #ifdef ENABLE_RR_PRINT 123 namespace rr { 124 125 template<> 126 struct PrintValue::Ty<sw::SamplerFunction> 127 { fmtrr::PrintValue::Ty128 static std::string fmt(const sw::SamplerFunction &v) 129 { 130 return std::string("SamplerFunction[") + 131 "method: " + std::to_string(v.method) + 132 ", offset: " + std::to_string(v.offset) + 133 ", sample: " + std::to_string(v.sample) + 134 "]"; 135 } 136 valrr::PrintValue::Ty137 static std::vector<rr::Value *> val(const sw::SamplerFunction &v) { return {}; } 138 }; 139 140 } // namespace rr 141 #endif // ENABLE_RR_PRINT 142 143 #endif // sw_SamplerCore_hpp 144