• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015-2017 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef SPIRV_CROSS_SAMPLER_HPP
19 #define SPIRV_CROSS_SAMPLER_HPP
20 
21 #include <vector>
22 
23 namespace spirv_cross
24 {
25 struct spirv_cross_sampler_2d
26 {
~spirv_cross_sampler_2dspirv_cross::spirv_cross_sampler_2d27 	inline virtual ~spirv_cross_sampler_2d()
28 	{
29 	}
30 };
31 
32 template <typename T>
33 struct sampler2DBase : spirv_cross_sampler_2d
34 {
sampler2DBasespirv_cross::sampler2DBase35 	sampler2DBase(const spirv_cross_sampler_info *info)
36 	{
37 		mips.insert(mips.end(), info->mipmaps, info->mipmaps + info->num_mipmaps);
38 		format = info->format;
39 		wrap_s = info->wrap_s;
40 		wrap_t = info->wrap_t;
41 		min_filter = info->min_filter;
42 		mag_filter = info->mag_filter;
43 		mip_filter = info->mip_filter;
44 	}
45 
samplespirv_cross::sampler2DBase46 	inline virtual T sample(glm::vec2 uv, float bias)
47 	{
48 		return sampleLod(uv, bias);
49 	}
50 
sampleLodspirv_cross::sampler2DBase51 	inline virtual T sampleLod(glm::vec2 uv, float lod)
52 	{
53 		if (mag_filter == SPIRV_CROSS_FILTER_NEAREST)
54 		{
55 			uv.x = wrap(uv.x, wrap_s, mips[0].width);
56 			uv.y = wrap(uv.y, wrap_t, mips[0].height);
57 			glm::vec2 uv_full = uv * glm::vec2(mips[0].width, mips[0].height);
58 
59 			int x = int(uv_full.x);
60 			int y = int(uv_full.y);
61 			return sample(x, y, 0);
62 		}
63 		else
64 		{
65 			return T(0, 0, 0, 1);
66 		}
67 	}
68 
wrapspirv_cross::sampler2DBase69 	inline float wrap(float v, spirv_cross_wrap wrap, unsigned size)
70 	{
71 		switch (wrap)
72 		{
73 		case SPIRV_CROSS_WRAP_REPEAT:
74 			return v - glm::floor(v);
75 		case SPIRV_CROSS_WRAP_CLAMP_TO_EDGE:
76 		{
77 			float half = 0.5f / size;
78 			return glm::clamp(v, half, 1.0f - half);
79 		}
80 
81 		default:
82 			return 0.0f;
83 		}
84 	}
85 
86 	std::vector<spirv_cross_miplevel> mips;
87 	spirv_cross_format format;
88 	spirv_cross_wrap wrap_s;
89 	spirv_cross_wrap wrap_t;
90 	spirv_cross_filter min_filter;
91 	spirv_cross_filter mag_filter;
92 	spirv_cross_mipfilter mip_filter;
93 };
94 
95 typedef sampler2DBase<glm::vec4> sampler2D;
96 typedef sampler2DBase<glm::ivec4> isampler2D;
97 typedef sampler2DBase<glm::uvec4> usampler2D;
98 
99 template <typename T>
texture(const sampler2DBase<T> & samp,const glm::vec2 & uv,float bias=0.0f)100 inline T texture(const sampler2DBase<T> &samp, const glm::vec2 &uv, float bias = 0.0f)
101 {
102 	return samp.sample(uv, bias);
103 }
104 }
105 
106 #endif
107