Skip to content

Commit

Permalink
[SGE] Implemented SGE::Material::get<SGE::Texture>(); improvement in …
Browse files Browse the repository at this point in the history
…diagnostic messages upon invalid usage of material setters and getters
  • Loading branch information
ravi688 committed Dec 12, 2024
1 parent 44fb668 commit 42f80e5
Show file tree
Hide file tree
Showing 18 changed files with 575 additions and 137 deletions.
2 changes: 1 addition & 1 deletion dependencies/Common
2 changes: 1 addition & 1 deletion dependencies/GLSLCommon
32 changes: 24 additions & 8 deletions include/sge-cpp/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <sge/material.h> /* for material_t* */
#include <sge-cpp/Texture.hpp> // for SGE::Texture reference type

#include <string> /* for std::string */
#include <string_view> // std::string_view

namespace SGE
{
Expand All @@ -17,24 +17,40 @@ namespace SGE

public:
template<typename T>
void set(const std::string& name, T value)
void set(const std::string_view str, T value) noexcept
{
debug_log_error("Setter for this type is not defined");
static_assert(false, "Please specialize set<> for this type");
}
template<typename T>
T get(const std::string_view str) noexcept
{
static_assert(false, "Please specialize set<> for this type");
}
};


// Specializations for Setters

template<>
void Material::set<float>(const std::string& name, float value);
void Material::set<float>(const std::string_view str, float value) noexcept;

template<>
void Material::set<vec2_t>(const std::string& name, vec2_t value);
void Material::set<vec2_t>(const std::string_view str, vec2_t value) noexcept;

template<>
void Material::set<vec4_t>(const std::string& name, vec4_t value);
void Material::set<vec4_t>(const std::string_view str, vec4_t value) noexcept;

template<>
void Material::set<s32>(const std::string& name, s32 value);
void Material::set<s32>(const std::string_view str, s32 value) noexcept;

template<>
void Material::set<u32>(const std::string_view str, u32 value) noexcept;

template<>
void Material::set<Texture>(const std::string_view str, Texture texture) noexcept;

// Specializations for Getters

template<>
void Material::set<Texture>(const std::string& name, Texture texture);
Texture Material::get<Texture>(const std::string_view str) noexcept;
}
6 changes: 4 additions & 2 deletions include/sge/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,20 @@ typedef struct memory_allocator_t memory_allocator_t;
# define ELSE(x)
# define PARAM_IF_DEBUG(x) , x
# define debug_if(x) if(x)
# define debug_else else
# define debug_else(x) else { x }
# define debug_else_if(x) else debug_if(x)
#else
# define IF_DEBUG_MODE(x)
# define ELSE(x) x
# define PARAM_IF_DEBUG(x)
# define debug_if(x)
# define debug_else
# define debug_else(x)
# define debug_else_if(x)
#endif /* GLOBAL_DEBUG */

#define IF_DEBUG(x) IF_DEBUG_MODE(x)
#define DEBUG_IF(x) debug_if(x)
#define DEBUG_ELSE(x) debug_else(x)

static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE u32 max(u32 v1, u32 v2) { return (v1 > v2) ? v1 : v2; }

Expand Down
1 change: 1 addition & 0 deletions include/sge/internal/vulkan/vulkan_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ typedef struct vulkan_buffer_t
};
vulkan_buffer_traits_t traits;
};
IF_DEBUG (bool dbg_is_mapped;)
} vulkan_buffer_t;

typedef vulkan_buffer_t* vulkan_buffer_ptr_t;
Expand Down
10 changes: 6 additions & 4 deletions include/sge/internal/vulkan/vulkan_material.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ typedef struct vulkan_material_field_handle_t

typedef struct vulkan_uniform_resource_t
{
bool has_buffer;
vulkan_buffer_t buffer;
/* this is created internally in the vulkan_material_t class */
vulkan_buffer_t* buffer;
/* pointer to texture object is cached here so that it can be returned when vulkan_material_get_texture() is called */
vulkan_texture_t* texture;
u16 index; // index of this resource in the list of resouce descriptors of this material
} vulkan_uniform_resource_t;

Expand Down Expand Up @@ -124,7 +126,7 @@ SGE_API vec3_t vulkan_material_get_vec3H(vulkan_material_t* material, vulkan_mat
SGE_API vec4_t vulkan_material_get_vec4H(vulkan_material_t* material, vulkan_material_field_handle_t handle);
SGE_API mat2_t vulkan_material_get_mat2H(vulkan_material_t* material, vulkan_material_field_handle_t handle);
SGE_API mat4_t vulkan_material_get_mat4H(vulkan_material_t* material, vulkan_material_field_handle_t handle);
SGE_API vulkan_texture_t* vulkan_material_get_texture2dH(vulkan_material_t* material, vulkan_material_field_handle_t handle);
SGE_API vulkan_texture_t* vulkan_material_get_textureH(vulkan_material_t* material, vulkan_material_field_handle_t handle);

SGE_API float vulkan_material_get_float(vulkan_material_t* material, const char* name);
SGE_API int vulkan_material_get_int(vulkan_material_t* material, const char* name);
Expand All @@ -134,7 +136,7 @@ SGE_API vec3_t vulkan_material_get_vec3(vulkan_material_t* material, const char*
SGE_API vec4_t vulkan_material_get_vec4(vulkan_material_t* material, const char* name);
SGE_API mat2_t vulkan_material_get_mat2(vulkan_material_t* material, const char* name);
SGE_API mat4_t vulkan_material_get_mat4(vulkan_material_t* material, const char* name);
SGE_API vulkan_texture_t* vulkan_material_get_texture2d(vulkan_material_t* material, const char* name);
SGE_API vulkan_texture_t* vulkan_material_get_texture(vulkan_material_t* material, const char* name);

/* setters */
SGE_API void vulkan_material_set_push_floatH(vulkan_material_t* material, vulkan_material_field_handle_t handle, float value);
Expand Down
24 changes: 22 additions & 2 deletions include/sge/internal/vulkan/vulkan_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,34 @@ SGE_API void vulkan_texture_set_usage_stage(vulkan_texture_t* texture, vulkan_te
/* uploads the data to the GPU local memory */
SGE_API void vulkan_texture_upload_data(vulkan_texture_t* texture, u32 data_count, vulkan_texture_data_t* data);

static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE bool vulkan_texture_is_cube(vulkan_texture_t* texture)
{
return HAS_FLAG(texture->type, VULKAN_TEXTURE_TYPE_CUBE);
}

static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE bool vulkan_texture_is_3d(vulkan_texture_t* texture)
{
return (texture->width > 1) && (texture->height > 1) && (texture->depth > 1);
}

static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE bool vulkan_texture_is_2d(vulkan_texture_t* texture)
{
return texture->depth == 1;
}

static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE bool vulkan_texture_is_1d(vulkan_texture_t* texture)
{
return (texture->height == 1) && (texture->depth == 1);
}

static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE vulkan_image_view_t* vulkan_texture_get_image_views(vulkan_texture_t* texture)
{
return ((texture->type & VULKAN_TEXTURE_TYPE_CUBE) == 0) ? &texture->image_view : texture->image_views;
return vulkan_texture_is_cube(texture) ? texture->image_views : &texture->image_view;
}

static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE u32 vulkan_texture_get_image_view_count(vulkan_texture_t* texture)
{
return ((texture->type & VULKAN_TEXTURE_TYPE_CUBE) == 0) ? 1 : texture->image_view_count;
return vulkan_texture_is_cube(texture) ? texture->image_view_count : 1;
}
static CAN_BE_UNUSED_FUNCTION INLINE_IF_RELEASE_MODE vulkan_texture_usage_stage_t vulkan_texture_get_usage_stage(vulkan_texture_t* texture) { return texture->current_stage; }

Expand Down
4 changes: 2 additions & 2 deletions include/sge/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ SGE_API vec3_t material_get_vec3H(material_t* material, material_field_handle_t
SGE_API vec4_t material_get_vec4H(material_t* material, material_field_handle_t handle);
SGE_API mat2_t material_get_mat2H(material_t* material, material_field_handle_t handle);
SGE_API mat4_t material_get_mat4H(material_t* material, material_field_handle_t handle);
SGE_API texture_t* material_get_texture2dH(material_t* material, material_field_handle_t handle);
SGE_API texture_t* material_get_textureH(material_t* material, material_field_handle_t handle);

SGE_API float material_get_float(material_t* material, const char* name);
SGE_API int material_get_int(material_t* material, const char* name);
Expand All @@ -150,6 +150,6 @@ SGE_API vec3_t material_get_vec3(material_t* material, const char* name);
SGE_API vec4_t material_get_vec4(material_t* material, const char* name);
SGE_API mat2_t material_get_mat2(material_t* material, const char* name);
SGE_API mat4_t material_get_mat4(material_t* material, const char* name);
SGE_API texture_t* material_get_texture2d(material_t* material, const char* name);
SGE_API texture_t* material_get_texture(material_t* material, const char* name);

END_CPP_COMPATIBLE
2 changes: 1 addition & 1 deletion include/sge/pygen/shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/***This is computer generated file - Do not modify it***/

/* This is auto generated header file (by pygen/gen_shaders.py python script). Do not modify it directly.
* Time & Date (yy/mm/yyy) of Generation: 1h:48m:8s, 20/9/2024
* Time & Date (yy/mm/yyy) of Generation: 11h:6m:1s, 12/12/2024
*/

#pragma once
Expand Down
36 changes: 36 additions & 0 deletions include/sge/tests/material_get_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
***This is computer generated notice - Do not modify it***
VulkanRenderer (inclusive of its dependencies and subprojects
such as toolchains written by the same author) is a software to render
2D & 3D geometries by writing C/C++ code and shaders.
File: TID-48.case5.h is a part of VulkanRenderer
Copyright (C) 2021 - 2024 Author: Ravi Prakash Singh
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


#pragma once


#include <sge/test.h>

BEGIN_CPP_COMPATIBLE

TEST_DECLARE(MATERIAL_GET_SET);

END_CPP_COMPATIBLE
128 changes: 128 additions & 0 deletions shaders/test_shaders/material_get_set.v3dshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
***This is computer generated notice - Do not modify it***

VulkanRenderer (inclusive of its dependencies and subprojects
such as toolchains written by the same author) is a software to render
2D & 3D geometries by writing C/C++ code and shaders.

File: texture_sample.v3dshader is a part of VulkanRenderer

Copyright (C) 2021 - 2024 Author: Ravi Prakash Singh

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


#sl version 2023
#sb version 2023

Shader
{
Properties
{
[Stage(fragment)]
[Set(material_set, texture0)]
uniform sampler2D albedo;

[Stage(fragment, vertex)]
[Set(material_set, material_properties)]
uniform Parameters
{
vec4 color;
float testValue;
int testValue2;
vec3 testValue3;
} parameters;
}

Layout
{
[Rate(per_vertex)]
[MeshLayout(sge_optimal)]
[Attribute(position)]
vec3 position;

[Rate(per_vertex)]
[MeshLayout(sge_optimal)]
[Attribute(texcoord)]
vec2 texcoord;
}

RenderPass
{
SubPass
{
[NoParse]
GraphicsPipeline
{
colorBlend
{
attachment
{
blendenable = true
}
}

depthStencil
{
depthWriteEnable = true,
depthTestEnable = true
}
}

[NoParse]
GLSL
{
#stage vertex

#version 450

#include <v3d.h>

layout(SGE_UNIFORM_BUFFER_LAYOUT, set = CAMERA_SET, binding = CAMERA_PROPERTIES_BINDING) uniform CameraInfo cameraInfo;
layout(SGE_UNIFORM_BUFFER_LAYOUT, set = OBJECT_SET, binding = TRANSFORM_BINDING) uniform ObjectInfo objectInfo;

layout(location = POSITION_LOCATION) in vec3 position;
layout(location = TEXCOORD_LOCATION) in vec2 texcoord;

layout(location = 0) out vec2 _texcoord;

void main()
{
vec4 clipPos = cameraInfo.projection * cameraInfo.view * objectInfo.transform * vec4(position, 1);
clipPos.y = -clipPos.y;
gl_Position = clipPos;
_texcoord = texcoord;
}

#stage fragment

#version 450

#include <v3d.h>

layout(set = MATERIAL_SET, binding = TEXTURE_BINDING0) uniform sampler2D albedo;

layout(location = 0) in vec2 _texcoord;
layout(location = 0) out vec4 color;

void main()
{
color = texture(albedo, _texcoord).rgba;
}

}
}
}
}
Loading

0 comments on commit 42f80e5

Please sign in to comment.