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 "PixelRoutine.hpp"
19#include "Reactor/Reactor.hpp"
20
21namespace sw
22{
23 enum SamplerMethod
24 {
25 Implicit, // Compute gradients (pixel shader only).
26 Bias, // Compute gradients and add provided bias.
27 Lod, // Use provided LOD.
28 Grad, // Use provided gradients.
29 Fetch, // Use provided integer coordinates.
30 Base // Sample base level.
31 };
32
33 enum SamplerOption
34 {
35 None,
36 Offset // Offset sample location by provided integer coordinates.
37 };
38
39 struct SamplerFunction
40 {
41 SamplerFunction(SamplerMethod method, SamplerOption option = None) : method(method), option(option) {}
42 operator SamplerMethod() { return method; }
43
44 const SamplerMethod method;
45 const SamplerOption option;
46 };
47
48 class SamplerCore
49 {
50 public:
51 SamplerCore(Pointer<Byte> &constants, const Sampler::State &state);
52
53 Vector4s sampleTexture(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Float4 &bias, Vector4f &dsx, Vector4f &dsy);
54 Vector4f sampleTexture(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Float4 &bias, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function);
55 static Vector4f textureSize(Pointer<Byte> &mipmap, Float4 &lod);
56
57 private:
58 Vector4s sampleTexture(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Float4 &bias, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function, bool fixed12);
59
60 void border(Short4 &mask, Float4 &coordinates);
61 void border(Int4 &mask, Float4 &coordinates);
62 Short4 offsetSample(Short4 &uvw, Pointer<Byte> &mipmap, int halfOffset, bool wrap, int count, Float &lod);
63 Vector4s sampleFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Int face[4], SamplerFunction function);
64 Vector4s sampleAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Int face[4], bool secondLOD, SamplerFunction function);
65 Vector4s sampleQuad(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, Float &lod, Int face[4], bool secondLOD, SamplerFunction function);
66 Vector4s sampleQuad2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, Float &lod, Int face[4], bool secondLOD, SamplerFunction function);
67 Vector4s sample3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, Float &lod, bool secondLOD, SamplerFunction function);
68 Vector4f sampleFloatFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &offset, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Int face[4], SamplerFunction function);
69 Vector4f sampleFloatAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &offset, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Int face[4], bool secondLOD, SamplerFunction function);
70 Vector4f sampleFloat(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &offset, Float &lod, Int face[4], bool secondLOD, SamplerFunction function);
71 Vector4f sampleFloat2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &offset, Float &lod, Int face[4], bool secondLOD, SamplerFunction function);
72 Vector4f sampleFloat3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Vector4f &offset, Float &lod, bool secondLOD, SamplerFunction function);
73 Float log2sqrt(Float lod);
74 Float log2(Float lod);
75 void computeLod(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &u, Float4 &v, const Float &lodBias, Vector4f &dsx, Vector4f &dsy, SamplerFunction function);
76 void computeLodCube(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, const Float &lodBias, Vector4f &dsx, Vector4f &dsy, Float4 &M, SamplerFunction function);
77 void computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, const Float &lodBias, Vector4f &dsx, Vector4f &dsy, SamplerFunction function);
78 void cubeFace(Int face[4], Float4 &U, Float4 &V, Float4 &x, Float4 &y, Float4 &z, Float4 &M);
79 Short4 applyOffset(Short4 &uvw, Float4 &offset, const Int4 &whd, AddressingMode mode);
80 void computeIndices(UInt index[4], Short4 uuuu, Short4 vvvv, Short4 wwww, Vector4f &offset, const Pointer<Byte> &mipmap, SamplerFunction function);
81 void computeIndices(UInt index[4], Int4& uuuu, Int4& vvvv, Int4& wwww, const Pointer<Byte> &mipmap, SamplerFunction function);
82 Vector4s sampleTexel(Short4 &u, Short4 &v, Short4 &s, Vector4f &offset, Pointer<Byte> &mipmap, Pointer<Byte> buffer[4], SamplerFunction function);
83 Vector4s sampleTexel(UInt index[4], Pointer<Byte> buffer[4]);
84 Vector4f sampleTexel(Int4 &u, Int4 &v, Int4 &s, Float4 &z, Pointer<Byte> &mipmap, Pointer<Byte> buffer[4], SamplerFunction function);
85 void selectMipmap(Pointer<Byte> &texture, Pointer<Byte> buffer[4], Pointer<Byte> &mipmap, Float &lod, Int face[4], bool secondLOD);
86 Short4 address(Float4 &uw, AddressingMode addressingMode, Pointer<Byte>& mipmap);
87 void address(Float4 &uw, Int4& xyz0, Int4& xyz1, Float4& f, Pointer<Byte>& mipmap, Float4 &texOffset, Int4 &filter, int whd, AddressingMode addressingMode, SamplerFunction function);
88 Int4 computeFilterOffset(Float &lod);
89
90 void convertFixed12(Short4 &ci, Float4 &cf);
91 void convertFixed12(Vector4s &cs, Vector4f &cf);
92 void convertSigned12(Float4 &cf, Short4 &ci);
93 void convertSigned15(Float4 &cf, Short4 &ci);
94 void convertUnsigned16(Float4 &cf, Short4 &ci);
95 void sRGBtoLinear16_8_16(Short4 &c);
96 void sRGBtoLinear16_6_16(Short4 &c);
97 void sRGBtoLinear16_5_16(Short4 &c);
98
99 bool hasFloatTexture() const;
100 bool hasUnnormalizedIntegerTexture() const;
101 bool hasUnsignedTextureComponent(int component) const;
102 int textureComponentCount() const;
103 bool hasThirdCoordinate() const;
104 bool has16bitTextureFormat() const;
105 bool has8bitTextureComponents() const;
106 bool has16bitTextureComponents() const;
107 bool has32bitIntegerTextureComponents() const;
108 bool hasYuvFormat() const;
109 bool isRGBComponent(int component) const;
110
111 Pointer<Byte> &constants;
112 const Sampler::State &state;
113 };
114}
115
116#endif // sw_SamplerCore_hpp
117