Skip to content

Commit e17feea

Browse files
committed
Load 3D texture function added
1 parent b7286bc commit e17feea

File tree

11 files changed

+122
-42
lines changed

11 files changed

+122
-42
lines changed

include/engine/core/materials/hair_strand.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,14 @@ class HairStrandMaterial2 : public HairStrandMaterial
215215

216216
enum Textures
217217
{
218-
M = 0,
219-
N1 = 1,
220-
N2 = 2,
218+
219+
N1 = 0,
220+
N2 = 1,
221+
GI = 2,
222+
MGI = 3,
221223
};
222224

223-
std::unordered_map<int, ITexture*> m_textures{{M, nullptr}, {N1, nullptr}, {N2, nullptr}};
225+
std::unordered_map<int, ITexture*> m_textures{{N1, nullptr}, {N2, nullptr}, {GI, nullptr}, {MGI, nullptr}};
224226
virtual Graphics::MaterialUniforms get_uniforms() const;
225227

226228
virtual inline std::unordered_map<int, ITexture*> get_textures() const {
@@ -240,22 +242,27 @@ class HairStrandMaterial2 : public HairStrandMaterial
240242
TextureSettings settings{};
241243
settings.useMipmaps = false;
242244
settings.adressMode = TextureAdressModeType::EDGE_CLAMP;
243-
m_textures[M] = new Texture(settings);
244245
m_textures[N1] = new Texture(settings);
245246
m_textures[N2] = new Texture(settings);
246-
Tools::Loaders::load_texture(
247-
m_textures[M], ENGINE_RESOURCES_PATH "textures/m.png", TextureFormatType::COLOR_FORMAT, false);
247+
m_textures[GI] = new Texture(settings);
248+
m_textures[MGI] = new Texture(settings);
248249
Tools::Loaders::load_texture(
249250
m_textures[N1], ENGINE_RESOURCES_PATH "textures/N_TT_R.png", TextureFormatType::COLOR_FORMAT, false);
250251
Tools::Loaders::load_texture(
251252
m_textures[N2], ENGINE_RESOURCES_PATH "textures/N_TRT.png", TextureFormatType::COLOR_FORMAT, false);
252-
m_textures[M]->set_format(RGBA_8U);
253+
Tools::Loaders::load_3D_texture(
254+
m_textures[GI], ENGINE_RESOURCES_PATH "textures/GI.png");
255+
Tools::Loaders::load_texture(
256+
m_textures[MGI], ENGINE_RESOURCES_PATH "textures/M_GI.png", TextureFormatType::COLOR_FORMAT, false);
253257
m_textures[N1]->set_format(RGBA_8U);
254258
m_textures[N2]->set_format(RGBA_8U);
255-
m_textureBindingState[M] = false;
256-
m_textureBindingState[N1] = false;
257-
m_textureBindingState[N2] = false;
258-
m_isDirty = true;
259+
m_textures[GI]->set_format(RGBA_8U);
260+
m_textures[MGI]->set_format(RGBA_8U);
261+
m_textureBindingState[N1] = false;
262+
m_textureBindingState[N2] = false;
263+
m_textureBindingState[GI] = false;
264+
m_textureBindingState[MGI] = false;
265+
m_isDirty = true;
259266
}
260267
};
261268
} // namespace Core

include/engine/core/textures/texture.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ class ITexture
9393
inline Extent3D get_size() const {
9494
return m_image.extent;
9595
}
96+
inline void set_size(Extent3D s) {
97+
m_image.extent = s;
98+
}
9699

97100
inline void set_use_mipmaps(bool op) {
98101
m_settings.useMipmaps = op;

include/engine/graphics/utilities/initializers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ VkImageCreateInfo image_create_info(VkFormat format,
5757
uint32_t mipLevels = 1,
5858
VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT,
5959
uint32_t layers = 1,
60+
VkImageType type = VK_IMAGE_TYPE_2D,
6061
VkImageCreateFlags flags = {});
6162
VkImageViewCreateInfo imageview_create_info(VkFormat format,
6263
VkImage image,

include/engine/tools/loaders.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,18 @@ Load .png file.
6161
void load_PNG(Core::Texture* const texture,
6262
const std::string fileName,
6363
TextureFormatType textureFormat = TextureFormatType::COLOR_FORMAT);
64-
6564
/*
6665
Load .hrd
6766
*/
6867
void load_HDRi(Core::TextureHDR* const texture, const std::string fileName);
68+
/*
69+
Load texture as 3D image. It will require and image with all the layers defined. The larger of their extent properties
70+
will be used for computing the depth if no depthy input is given. PNG or JPEG available.
71+
*/
72+
void load_3D_texture(Core::ITexture* const texture,
73+
const std::string fileName,
74+
uint16_t depth = 0,
75+
TextureFormatType textureFormat = TextureFormatType::COLOR_FORMAT);
6976

7077
void compute_tangents_gram_smidt(std::vector<Graphics::Vertex>& vertices, const std::vector<uint32_t>& indices);
7178

resources/shaders/forward/hair_strand2.glsl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ layout(set = 1, binding = 1) uniform MaterialUniforms {
153153
bool occlusion;
154154
} material;
155155

156-
layout(set = 2, binding = 0) uniform sampler2D mTex;
157-
layout(set = 2, binding = 1) uniform sampler2D nTex1;
158-
layout(set = 2, binding = 2) uniform sampler2D nTex2;
156+
layout(set = 2, binding = 0) uniform sampler2D nTex1;
157+
layout(set = 2, binding = 1) uniform sampler2D nTex2;
158+
layout(set = 2, binding = 2) uniform sampler3D GITex;
159+
// layout(set = 2, binding = 3) uniform sampler2D mGITex;
159160

160161
MarschnerLookupBSDF bsdf;
161162

@@ -198,9 +199,9 @@ vec3 computeAmbient(vec3 n) {
198199
normalize(-g_pos),
199200
texture(irradianceMap, rotatedNormal).rgb*scene.ambientIntensity,
200201
bsdf,
201-
mTex,
202202
nTex1,
203203
nTex2,
204+
GITex,
204205
material.r,
205206
false, //Take oput transmitance
206207
material.trt);
@@ -233,9 +234,9 @@ void main() {
233234
normalize(-g_pos),
234235
scene.lights[i].color * scene.lights[i].intensity,
235236
bsdf,
236-
mTex,
237237
nTex1,
238238
nTex2,
239+
GITex,
239240
material.r,
240241
material.tt,
241242
material.trt);

resources/shaders/scripts/BRDFs/marschner_LUT_BSDF.glsl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define SCALE_N_TRT 0.25
2222

2323
#define GLOBAL_SCALE 5.0
24+
#define DENSITY 0.7
2425

2526

2627
struct MarschnerLookupBSDF{
@@ -49,9 +50,9 @@ vec3 evalMarschnerLookupBSDF(
4950
vec3 v, //View vector
5051
vec3 irradiance,
5152
MarschnerLookupBSDF bsdf,
52-
sampler2D texM,
5353
sampler2D texN,
5454
sampler2D texNTRT,
55+
sampler3D texGI,
5556
bool r,
5657
bool tt,
5758
bool trt)
@@ -86,8 +87,8 @@ vec3 evalMarschnerLookupBSDF(
8687
//////////////////////////////////////////////////////////////////////////
8788

8889
// N
89-
vec2 index1 = vec2( phiH * ONE_OVER_PI, 1.0-ix_th );
90-
vec2 index2 = vec2( phiTRT * ONE_OVER_PI, 1.0-ix_th );
90+
vec2 index1 = vec2( phiH * ONE_OVER_PI, 1-ix_th );
91+
vec2 index2 = vec2( phiH * ONE_OVER_PI, 1-ix_th );
9192

9293
vec4 N = texture(texN, index1);
9394
float NR = SCALE_N_R * N.a;
@@ -109,6 +110,17 @@ vec3 evalMarschnerLookupBSDF(
109110
//////////////////////////////////////////////////////////////////////////
110111
// Local Scattering
111112
//////////////////////////////////////////////////////////////////////////
113+
float ix_thH = thH * ONE_OVER_PI * 0.5 + 0.5;
114+
vec3 ix_spread = sqrt(vec3(0.5)) * ONE_OVER_PI*0.5;
115+
116+
vec3 gi;
117+
gi.r = DENSITY * texture( texGI, vec3( ix_spread.r, ix_thH, 1-ix_th ) ).r;
118+
gi.g = DENSITY * texture( texGI, vec3( ix_spread.g, ix_thH, 1-ix_th ) ).g;
119+
gi.b = DENSITY * texture( texGI, vec3( ix_spread.b, ix_thH, 1-ix_th ) ).b;
120+
121+
//specular += gi;
122+
123+
// specular *= directFraction;
112124

113125

114126
return (specular) * irradiance * GLOBAL_SCALE;

resources/textures/GI.png

25.5 KB
Loading

resources/textures/M_GI.png

2.11 KB
Loading

src/graphics/device.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,20 @@ Image Device::create_image(Extent3D extent, ImageConfig config, bool useMipmaps,
159159
useMipmaps ? static_cast<uint32_t>(std::floor(std::log2(std::max(extent.width, extent.height)))) + 1 : 1;
160160
img.layers = config.viewType == TextureType::TEXTURE_CUBE ? CUBEMAP_FACES : config.layers;
161161

162+
VkImageType imageType = VK_IMAGE_TYPE_2D;
163+
if (config.viewType == TextureType::TEXTURE_3D)
164+
imageType = VK_IMAGE_TYPE_3D;
165+
if (config.viewType == TextureType::TEXTURE_1D || config.viewType == TextureType::TEXTURE_1D_ARRAY)
166+
imageType = VK_IMAGE_TYPE_1D;
167+
162168
VkImageCreateInfo img_info =
163169
Init::image_create_info(Translator::get(config.format),
164170
config.usageFlags,
165171
extent,
166172
img.mipLevels,
167173
static_cast<VkSampleCountFlagBits>(config.samples),
168174
img.layers,
175+
imageType,
169176
config.viewType == TextureType::TEXTURE_CUBE ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0);
170177

171178
VK_CHECK(vmaCreateImage(m_allocator, &img_info, &img_allocinfo, &img.handle, &img.allocation, nullptr));
@@ -501,7 +508,7 @@ void Device::upload_texture_image(Image& img,
501508
config.usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
502509
config.samples = 1;
503510
config.aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
504-
img = create_image(img.extent, config, mipmapping);
511+
img = create_image(img.extent, config, mipmapping);
505512
img.create_view(config);
506513

507514
VkDeviceSize imageSize = img.extent.width * img.extent.height * img.extent.depth * bytesPerPixel;
@@ -759,10 +766,10 @@ void Device::wait() {
759766
VK_CHECK(vkDeviceWaitIdle(m_handle));
760767
}
761768

762-
void Device::init_imgui(void* windowHandle,
763-
WindowingSystem windowingSystem,
764-
VulkanRenderPass renderPass,
765-
uint16_t samples) {
769+
void Device::init_imgui(void* windowHandle,
770+
WindowingSystem windowingSystem,
771+
VulkanRenderPass renderPass,
772+
uint16_t samples) {
766773

767774
m_guiPool = create_descriptor_pool(1000,
768775
1000,

src/graphics/utilities/initializers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,15 @@ VkImageCreateInfo Init::image_create_info(VkFormat format,
280280
uint32_t mipLevels,
281281
VkSampleCountFlagBits samples,
282282
uint32_t layers,
283+
VkImageType type,
283284
VkImageCreateFlags flags) {
284285

285286
VkImageCreateInfo info = {};
286287
info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
287288
info.pNext = nullptr;
288289
info.flags = flags;
289290

290-
info.imageType = VK_IMAGE_TYPE_2D;
291+
info.imageType = type;
291292

292293
info.format = format;
293294
info.extent = extent;

0 commit comments

Comments
 (0)