Skip to content

Commit

Permalink
Load 3D texture function added
Browse files Browse the repository at this point in the history
  • Loading branch information
AEspinosaDev committed Nov 18, 2024
1 parent b7286bc commit e17feea
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 42 deletions.
31 changes: 19 additions & 12 deletions include/engine/core/materials/hair_strand.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ class HairStrandMaterial2 : public HairStrandMaterial

enum Textures
{
M = 0,
N1 = 1,
N2 = 2,

N1 = 0,
N2 = 1,
GI = 2,
MGI = 3,
};

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

virtual inline std::unordered_map<int, ITexture*> get_textures() const {
Expand All @@ -240,22 +242,27 @@ class HairStrandMaterial2 : public HairStrandMaterial
TextureSettings settings{};
settings.useMipmaps = false;
settings.adressMode = TextureAdressModeType::EDGE_CLAMP;
m_textures[M] = new Texture(settings);
m_textures[N1] = new Texture(settings);
m_textures[N2] = new Texture(settings);
Tools::Loaders::load_texture(
m_textures[M], ENGINE_RESOURCES_PATH "textures/m.png", TextureFormatType::COLOR_FORMAT, false);
m_textures[GI] = new Texture(settings);
m_textures[MGI] = new Texture(settings);
Tools::Loaders::load_texture(
m_textures[N1], ENGINE_RESOURCES_PATH "textures/N_TT_R.png", TextureFormatType::COLOR_FORMAT, false);
Tools::Loaders::load_texture(
m_textures[N2], ENGINE_RESOURCES_PATH "textures/N_TRT.png", TextureFormatType::COLOR_FORMAT, false);
m_textures[M]->set_format(RGBA_8U);
Tools::Loaders::load_3D_texture(
m_textures[GI], ENGINE_RESOURCES_PATH "textures/GI.png");
Tools::Loaders::load_texture(
m_textures[MGI], ENGINE_RESOURCES_PATH "textures/M_GI.png", TextureFormatType::COLOR_FORMAT, false);
m_textures[N1]->set_format(RGBA_8U);
m_textures[N2]->set_format(RGBA_8U);
m_textureBindingState[M] = false;
m_textureBindingState[N1] = false;
m_textureBindingState[N2] = false;
m_isDirty = true;
m_textures[GI]->set_format(RGBA_8U);
m_textures[MGI]->set_format(RGBA_8U);
m_textureBindingState[N1] = false;
m_textureBindingState[N2] = false;
m_textureBindingState[GI] = false;
m_textureBindingState[MGI] = false;
m_isDirty = true;
}
};
} // namespace Core
Expand Down
3 changes: 3 additions & 0 deletions include/engine/core/textures/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class ITexture
inline Extent3D get_size() const {
return m_image.extent;
}
inline void set_size(Extent3D s) {
m_image.extent = s;
}

inline void set_use_mipmaps(bool op) {
m_settings.useMipmaps = op;
Expand Down
1 change: 1 addition & 0 deletions include/engine/graphics/utilities/initializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ VkImageCreateInfo image_create_info(VkFormat format,
uint32_t mipLevels = 1,
VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT,
uint32_t layers = 1,
VkImageType type = VK_IMAGE_TYPE_2D,
VkImageCreateFlags flags = {});
VkImageViewCreateInfo imageview_create_info(VkFormat format,
VkImage image,
Expand Down
9 changes: 8 additions & 1 deletion include/engine/tools/loaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,18 @@ Load .png file.
void load_PNG(Core::Texture* const texture,
const std::string fileName,
TextureFormatType textureFormat = TextureFormatType::COLOR_FORMAT);

/*
Load .hrd
*/
void load_HDRi(Core::TextureHDR* const texture, const std::string fileName);
/*
Load texture as 3D image. It will require and image with all the layers defined. The larger of their extent properties
will be used for computing the depth if no depthy input is given. PNG or JPEG available.
*/
void load_3D_texture(Core::ITexture* const texture,
const std::string fileName,
uint16_t depth = 0,
TextureFormatType textureFormat = TextureFormatType::COLOR_FORMAT);

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

Expand Down
11 changes: 6 additions & 5 deletions resources/shaders/forward/hair_strand2.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ layout(set = 1, binding = 1) uniform MaterialUniforms {
bool occlusion;
} material;

layout(set = 2, binding = 0) uniform sampler2D mTex;
layout(set = 2, binding = 1) uniform sampler2D nTex1;
layout(set = 2, binding = 2) uniform sampler2D nTex2;
layout(set = 2, binding = 0) uniform sampler2D nTex1;
layout(set = 2, binding = 1) uniform sampler2D nTex2;
layout(set = 2, binding = 2) uniform sampler3D GITex;
// layout(set = 2, binding = 3) uniform sampler2D mGITex;

MarschnerLookupBSDF bsdf;

Expand Down Expand Up @@ -198,9 +199,9 @@ vec3 computeAmbient(vec3 n) {
normalize(-g_pos),
texture(irradianceMap, rotatedNormal).rgb*scene.ambientIntensity,
bsdf,
mTex,
nTex1,
nTex2,
GITex,
material.r,
false, //Take oput transmitance
material.trt);
Expand Down Expand Up @@ -233,9 +234,9 @@ void main() {
normalize(-g_pos),
scene.lights[i].color * scene.lights[i].intensity,
bsdf,
mTex,
nTex1,
nTex2,
GITex,
material.r,
material.tt,
material.trt);
Expand Down
18 changes: 15 additions & 3 deletions resources/shaders/scripts/BRDFs/marschner_LUT_BSDF.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define SCALE_N_TRT 0.25

#define GLOBAL_SCALE 5.0
#define DENSITY 0.7


struct MarschnerLookupBSDF{
Expand Down Expand Up @@ -49,9 +50,9 @@ vec3 evalMarschnerLookupBSDF(
vec3 v, //View vector
vec3 irradiance,
MarschnerLookupBSDF bsdf,
sampler2D texM,
sampler2D texN,
sampler2D texNTRT,
sampler3D texGI,
bool r,
bool tt,
bool trt)
Expand Down Expand Up @@ -86,8 +87,8 @@ vec3 evalMarschnerLookupBSDF(
//////////////////////////////////////////////////////////////////////////

// N
vec2 index1 = vec2( phiH * ONE_OVER_PI, 1.0-ix_th );
vec2 index2 = vec2( phiTRT * ONE_OVER_PI, 1.0-ix_th );
vec2 index1 = vec2( phiH * ONE_OVER_PI, 1-ix_th );
vec2 index2 = vec2( phiH * ONE_OVER_PI, 1-ix_th );

vec4 N = texture(texN, index1);
float NR = SCALE_N_R * N.a;
Expand All @@ -109,6 +110,17 @@ vec3 evalMarschnerLookupBSDF(
//////////////////////////////////////////////////////////////////////////
// Local Scattering
//////////////////////////////////////////////////////////////////////////
float ix_thH = thH * ONE_OVER_PI * 0.5 + 0.5;
vec3 ix_spread = sqrt(vec3(0.5)) * ONE_OVER_PI*0.5;

vec3 gi;
gi.r = DENSITY * texture( texGI, vec3( ix_spread.r, ix_thH, 1-ix_th ) ).r;
gi.g = DENSITY * texture( texGI, vec3( ix_spread.g, ix_thH, 1-ix_th ) ).g;
gi.b = DENSITY * texture( texGI, vec3( ix_spread.b, ix_thH, 1-ix_th ) ).b;

//specular += gi;

// specular *= directFraction;


return (specular) * irradiance * GLOBAL_SCALE;
Expand Down
Binary file added resources/textures/GI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/textures/M_GI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 12 additions & 5 deletions src/graphics/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,20 @@ Image Device::create_image(Extent3D extent, ImageConfig config, bool useMipmaps,
useMipmaps ? static_cast<uint32_t>(std::floor(std::log2(std::max(extent.width, extent.height)))) + 1 : 1;
img.layers = config.viewType == TextureType::TEXTURE_CUBE ? CUBEMAP_FACES : config.layers;

VkImageType imageType = VK_IMAGE_TYPE_2D;
if (config.viewType == TextureType::TEXTURE_3D)
imageType = VK_IMAGE_TYPE_3D;
if (config.viewType == TextureType::TEXTURE_1D || config.viewType == TextureType::TEXTURE_1D_ARRAY)
imageType = VK_IMAGE_TYPE_1D;

VkImageCreateInfo img_info =
Init::image_create_info(Translator::get(config.format),
config.usageFlags,
extent,
img.mipLevels,
static_cast<VkSampleCountFlagBits>(config.samples),
img.layers,
imageType,
config.viewType == TextureType::TEXTURE_CUBE ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0);

VK_CHECK(vmaCreateImage(m_allocator, &img_info, &img_allocinfo, &img.handle, &img.allocation, nullptr));
Expand Down Expand Up @@ -501,7 +508,7 @@ void Device::upload_texture_image(Image& img,
config.usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
config.samples = 1;
config.aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
img = create_image(img.extent, config, mipmapping);
img = create_image(img.extent, config, mipmapping);
img.create_view(config);

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

void Device::init_imgui(void* windowHandle,
WindowingSystem windowingSystem,
VulkanRenderPass renderPass,
uint16_t samples) {
void Device::init_imgui(void* windowHandle,
WindowingSystem windowingSystem,
VulkanRenderPass renderPass,
uint16_t samples) {

m_guiPool = create_descriptor_pool(1000,
1000,
Expand Down
3 changes: 2 additions & 1 deletion src/graphics/utilities/initializers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,15 @@ VkImageCreateInfo Init::image_create_info(VkFormat format,
uint32_t mipLevels,
VkSampleCountFlagBits samples,
uint32_t layers,
VkImageType type,
VkImageCreateFlags flags) {

VkImageCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
info.pNext = nullptr;
info.flags = flags;

info.imageType = VK_IMAGE_TYPE_2D;
info.imageType = type;

info.format = format;
info.extent = extent;
Expand Down
71 changes: 56 additions & 15 deletions src/tools/loaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ void VKFW::Tools::Loaders::load_PNG(Core::Texture* const texture,
if (imgCache)
{
texture->set_image_cache(imgCache, {static_cast<unsigned int>(w), static_cast<unsigned int>(h), 1}, 4);
// Set automatically teh best format for each type.
// Set automatically teh optimal format for each type.
// User can override it after, I he need some other more specific format ...
switch (textureFormat)
{
Expand Down Expand Up @@ -777,9 +777,50 @@ void VKFW::Tools::Loaders::load_HDRi(Core::TextureHDR* const texture, const std:
DEBUG_LOG("HDRi Texture loaded successfully");
#endif // DEBUG
}

void VKFW::Tools::Loaders::load_3D_texture(Core::ITexture* const texture,
const std::string fileName,
uint16_t depth,
TextureFormatType textureFormat) {
int w, h, ch;
unsigned char* imgCache = nullptr;
imgCache = stbi_load(fileName.c_str(), &w, &h, &ch, STBI_rgb_alpha);
if (imgCache)
{
texture->set_type(TextureType::TEXTURE_3D);
int largerSide = w > h ? w : h;
int shorterSide = w > h ? h : w;
uint16_t finalDepth = depth == 0 ? largerSide / shorterSide : depth;
texture->set_image_cache(
imgCache,
{static_cast<unsigned int>(shorterSide), static_cast<unsigned int>(largerSide / finalDepth), finalDepth},
4);
// Set automatically the optimal format for each type.
// User can override it after, I he need some other more specific format ...
switch (textureFormat)
{
case TextureFormatType::COLOR_FORMAT:
texture->set_format(SRGBA_8);
break;
case TextureFormatType::NORMAL_FORMAT:
texture->set_format(RGBA_8U);
break;
case TextureFormatType::HDR_FORMAT:
texture->set_format(SRGBA_16F);
break;
}
} else
{
#ifndef NDEBUG
DEBUG_LOG("Failed to load texture PNG file" + fileName);
#endif
return;
};
#ifndef NDEBUG
DEBUG_LOG("PNG Texture loaded successfully");
#endif // DEBUG
}
void VKFW::Tools::Loaders::compute_tangents_gram_smidt(std::vector<Graphics::Vertex>& vertices,
const std::vector<uint32_t>& indices) {
const std::vector<uint32_t>& indices) {
if (!indices.empty())
for (size_t i = 0; i < indices.size(); i += 3)
{
Expand All @@ -788,12 +829,12 @@ void VKFW::Tools::Loaders::compute_tangents_gram_smidt(std::vector<Graphics::Ver
size_t i2 = indices[i + 2];

Vec3 tangent = Graphics::Utils::get_tangent_gram_smidt(vertices[i0].pos,
vertices[i1].pos,
vertices[i2].pos,
vertices[i0].texCoord,
vertices[i1].texCoord,
vertices[i2].texCoord,
vertices[i0].normal);
vertices[i1].pos,
vertices[i2].pos,
vertices[i0].texCoord,
vertices[i1].texCoord,
vertices[i2].texCoord,
vertices[i0].normal);

vertices[i0].tangent += tangent;
vertices[i1].tangent += tangent;
Expand All @@ -803,12 +844,12 @@ void VKFW::Tools::Loaders::compute_tangents_gram_smidt(std::vector<Graphics::Ver
for (size_t i = 0; i < vertices.size(); i += 3)
{
Vec3 tangent = Graphics::Utils::get_tangent_gram_smidt(vertices[i].pos,
vertices[i + 1].pos,
vertices[i + 2].pos,
vertices[i].texCoord,
vertices[i + 1].texCoord,
vertices[i + 2].texCoord,
vertices[i].normal);
vertices[i + 1].pos,
vertices[i + 2].pos,
vertices[i].texCoord,
vertices[i + 1].texCoord,
vertices[i + 2].texCoord,
vertices[i].normal);

vertices[i].tangent += tangent;
vertices[i + 1].tangent += tangent;
Expand Down

0 comments on commit e17feea

Please sign in to comment.