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