From a0beb9eac7908d60459d7ff284e7ca290206012b Mon Sep 17 00:00:00 2001 From: thejinchao Date: Sat, 15 Nov 2025 16:27:19 +0800 Subject: [PATCH] 11.15 --- source/CMakeLists.txt | 1 + source/console/dv_console_main.cpp | 17 ++ source/console/ui/ui_main.cpp | 7 + source/core/include/asset/dvc_material.h | 34 ++-- source/core/include/asset/dvc_texture.h | 1 + source/core/include/math/dvc_matrix3.h | 39 ++++ .../include/shader/dvc_shader_functions.h | 3 +- .../include/shader/dvc_shader_interface.h | 3 +- source/core/source/asset/dvc_material.cpp | 67 ++++++- source/core/source/asset/dvc_texture.cpp | 8 +- .../pipe/dvc_render_step2_rasterizer.cpp | 17 +- .../source/shader/dvc_shader_functions.cpp | 13 +- source/shaders/CMakeLists.txt | 4 + source/shaders/standard/CMakeLists.txt | 34 ++++ source/shaders/standard/dll_main.cpp | 82 ++++++++ .../shaders/standard/fs_debug_base_color.cpp | 29 +++ source/shaders/standard/fs_debug_base_color.h | 19 ++ .../standard/fs_debug_geometry_normal.cpp | 30 +++ .../standard/fs_debug_geometry_normal.h | 19 ++ .../standard/fs_debug_normal_texture.cpp | 29 +++ .../standard/fs_debug_normal_texture.h | 19 ++ .../shaders/standard/fs_debug_solid_color.cpp | 16 ++ .../shaders/standard/fs_debug_solid_color.h | 15 ++ .../standard/fs_debug_texture_coordinates.cpp | 31 +++ .../standard/fs_debug_texture_coordinates.h | 19 ++ source/shaders/standard/vs_standard.h | 41 ++++ source/shaders/standard/vs_standard.inc | 181 ++++++++++++++++++ source/utils/CMakeLists.txt | 15 ++ .../include/standard/dvu_standard_material.h | 11 ++ source/utils/source/scene/dvu_load_gltf.cpp | 83 ++++++-- .../source/standard/dvu_standard_material.cpp | 40 ++++ test/shader/dll_main.cpp | 14 +- test/shader/test_pixel_shader.cpp | 53 +---- test/shader/test_pixel_shader.h | 12 +- test/shader/test_vertex_shader.cpp | 8 +- 35 files changed, 886 insertions(+), 128 deletions(-) create mode 100644 source/shaders/CMakeLists.txt create mode 100644 source/shaders/standard/CMakeLists.txt create mode 100644 source/shaders/standard/dll_main.cpp create mode 100644 source/shaders/standard/fs_debug_base_color.cpp create mode 100644 source/shaders/standard/fs_debug_base_color.h create mode 100644 source/shaders/standard/fs_debug_geometry_normal.cpp create mode 100644 source/shaders/standard/fs_debug_geometry_normal.h create mode 100644 source/shaders/standard/fs_debug_normal_texture.cpp create mode 100644 source/shaders/standard/fs_debug_normal_texture.h create mode 100644 source/shaders/standard/fs_debug_solid_color.cpp create mode 100644 source/shaders/standard/fs_debug_solid_color.h create mode 100644 source/shaders/standard/fs_debug_texture_coordinates.cpp create mode 100644 source/shaders/standard/fs_debug_texture_coordinates.h create mode 100644 source/shaders/standard/vs_standard.h create mode 100644 source/shaders/standard/vs_standard.inc create mode 100644 source/utils/include/standard/dvu_standard_material.h create mode 100644 source/utils/source/standard/dvu_standard_material.cpp diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index e8cbac5..b3a1f32 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -4,6 +4,7 @@ add_subdirectory(core) add_subdirectory(utils) add_subdirectory(console) +add_subdirectory(shaders) ################# diff --git a/source/console/dv_console_main.cpp b/source/console/dv_console_main.cpp index f052b53..9332424 100644 --- a/source/console/dv_console_main.cpp +++ b/source/console/dv_console_main.cpp @@ -26,6 +26,9 @@ int32_t g_canvasWidth = 1024; int32_t g_canvasHeight = 512; davinci::Device g_device; davinci::Scene g_scene; +int32_t g_mousePosX = 0; +int32_t g_mousePosY = 0; +davinci::fVector4 g_mouseColor = davinci::fVector4::BLACK; //------------------------------------------------------------------------------------- //int main(int argc, char* argv[]) @@ -160,6 +163,20 @@ SDL_AppResult SDL_AppIterate(void* appstate) g_adapterTexture.updateTexture(g_canvasWidth, 0, g_renderTarget.getDepthBuffer()); g_adapterTexture.draw(); + float mousePosX, mousePosY; + SDL_GetMouseState(&mousePosX, &mousePosY); + + g_mousePosX = (int32_t)(mousePosX) % g_canvasWidth; + g_mousePosY = g_screenHeight - (int32_t)(mousePosY); + if(g_mousePosX>=0 && g_mousePosX < g_canvasWidth && g_mousePosY >=0 && g_mousePosY < g_canvasHeight) + { + g_mouseColor = g_renderTarget.getColorBuffer().getPixel(g_mousePosX, g_mousePosY); + } + else + { + g_mouseColor = davinci::fVector4::BLACK; + } + //draw ui drawMainUI(); diff --git a/source/console/ui/ui_main.cpp b/source/console/ui/ui_main.cpp index 74816e4..16c7b4e 100644 --- a/source/console/ui/ui_main.cpp +++ b/source/console/ui/ui_main.cpp @@ -25,6 +25,9 @@ extern davinci::Device g_device; extern davinci::Scene g_scene; extern davinci::RenderTarget g_renderTarget; extern davinci::AntiAliasingTech g_aaTech; +extern int32_t g_mousePosX; +extern int32_t g_mousePosY; +extern davinci::fVector4 g_mouseColor; bool initMainUI(SDL_Window* window, SDL_GLContext gl_context, const char* glsl_version) { @@ -129,6 +132,10 @@ void drawMainUI() davinci_utils::dump_render_target("d:\\rendertarget", g_renderTarget); } + ImGui::Separator(); + ImVec2 mousePos = ImGui::GetMousePos(); + ImGui::Text("MousePosition: %d,%d", g_mousePosX, g_mousePosY); + ImGui::Text("MouseColor: %.3f,%.3f,%.3f,%.3f", g_mouseColor.x, g_mouseColor.y, g_mouseColor.z, g_mouseColor.w); ImGui::End(); // Rendering diff --git a/source/core/include/asset/dvc_material.h b/source/core/include/asset/dvc_material.h index 0a1b271..c7d8c49 100644 --- a/source/core/include/asset/dvc_material.h +++ b/source/core/include/asset/dvc_material.h @@ -3,6 +3,7 @@ #include "dvc_config.h" #include "dvc_pre_declare.h" #include "math/dvc_vector4.h" +#include "math/dvc_matrix3.h" DV_CORE_BEGIN_NAMESPACE @@ -13,16 +14,14 @@ public: { TextureConstPtr texture; int32_t texCoord = 0; + fMatrix3 uvTransform; }; public: int32_t getID(void) const { return m_id; } - void setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fVector4& baseColorfactor) { - m_baseColorTexture.texture = texture; - m_baseColorTexture.texCoord = texCoord; - m_baseColorFactor = baseColorfactor; - } + void setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform, + const fVector4& baseColorfactor); const TextureInfo& getBaseColorTexture() const { return m_baseColorTexture; } @@ -31,12 +30,8 @@ public: return m_baseColorFactor; } - void setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, float32_t metallicFactor=1.f, float32_t roughnessFactor=1.f ) { - m_metallicRoughnessTexture.texture = texture; - m_metallicRoughnessTexture.texCoord = texCoord; - m_metallicFactor = metallicFactor; - m_roughnessFactor = roughnessFactor; - } + void setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform, + float32_t metallicFactor = 1, float32_t roughnessFactor = 1); const TextureInfo& getMetallicRoughnessTexture() const { return m_metallicRoughnessTexture; @@ -50,11 +45,8 @@ public: return m_roughnessFactor; } - void setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, float32_t scale=1.f) { - m_normalTexture.texture = texture; - m_normalTexture.texCoord = texCoord; - m_normalScale = scale; - } + void setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform, + float32_t scale = 1); const TextureInfo& getNormalTexture() const { return m_normalTexture; @@ -64,6 +56,9 @@ public: return m_normalScale; } + void setVertexShader(const char* szShaderFile, const char* vertexShaderName); + void setFragementShader(const char* szShaderFile, const char* fragementShaderName); + VertexShaderInterface* getVertexShader() const { return m_vertexShader; } @@ -72,6 +67,7 @@ public: return m_pixelShader; } + public: bool loadShader(); @@ -105,6 +101,12 @@ protected: VertexShaderInterface* m_vertexShader = nullptr; PixelShaderInterface* m_pixelShader = nullptr; + std::string m_vertexshaderFile; + std::string m_vertexShaderName; + + std::string m_fragementShaderFile; + std::string m_fragementShaderName; + public: Material(int32_t id); ~Material(); diff --git a/source/core/include/asset/dvc_texture.h b/source/core/include/asset/dvc_texture.h index af882e6..66752df 100644 --- a/source/core/include/asset/dvc_texture.h +++ b/source/core/include/asset/dvc_texture.h @@ -4,6 +4,7 @@ #include "dvc_pre_declare.h" #include "math/dvc_vector2.h" #include "math/dvc_vector4.h" +#include "math/dvc_matrix3.h" DV_CORE_BEGIN_NAMESPACE diff --git a/source/core/include/math/dvc_matrix3.h b/source/core/include/math/dvc_matrix3.h index a226e8b..b5ffc4c 100644 --- a/source/core/include/math/dvc_matrix3.h +++ b/source/core/include/math/dvc_matrix3.h @@ -94,6 +94,45 @@ public: template friend TMatrix3 operator * (U fScalar, const TMatrix3& rkMatrix); +public: + static inline TMatrix3 makeTrans(T x, T y) { + return TMatrix3( + 1, 0, x, + 0, 1, y, + 0, 0, 1); + } + + static inline TMatrix3 makeTrans(const TVector2& pos) { + return TMatrix3( + 1, 0, pos.x, + 0, 1, pos.y, + 0, 0, 1); + } + + static inline TMatrix3 makeScale(T x, T y) { + return TMatrix3( + x, 0, 0, + 0, y, 0, + 0, 0, 1); + } + + static inline TMatrix3 makeScale(const TVector2& scale) { + return TMatrix3( + scale.x, 0, 0, + 0, scale.y, 0, + 0, 0, 1); + } + + //2D rotate, left multiplication + static inline TMatrix3 makeRotate(T angle) { + T s = sin(angle); + T c = cos(angle); + return TMatrix3( + c, -s, 0, + s, c, 0, + 0, 0, 1); + } + public: // Utilities diff --git a/source/core/include/shader/dvc_shader_functions.h b/source/core/include/shader/dvc_shader_functions.h index 94e9839..5c2bc8c 100644 --- a/source/core/include/shader/dvc_shader_functions.h +++ b/source/core/include/shader/dvc_shader_functions.h @@ -4,6 +4,7 @@ #include "dvc_pre_declare.h" #include "math/dvc_vector2.h" #include "math/dvc_vector4.h" +#include "asset/dvc_material.h" DV_CORE_BEGIN_NAMESPACE @@ -30,7 +31,7 @@ public: return (a < b) ? a : b; } - static DV_CORE_API fVector4 texture2D(const RenderPipe& pipe, const char* textName, const fVector2& uv, const fVector4& defaultValue); + static DV_CORE_API fVector4 texture2D(const RenderPipe& pipe, const Material::TextureInfo& texturePtr, const fVector2& uv, const fVector4& defaultValue); }; DV_CORE_END_NAMESPACE diff --git a/source/core/include/shader/dvc_shader_interface.h b/source/core/include/shader/dvc_shader_interface.h index 67c624c..90ac5c1 100644 --- a/source/core/include/shader/dvc_shader_interface.h +++ b/source/core/include/shader/dvc_shader_interface.h @@ -26,8 +26,7 @@ class PixelShaderInterface public: virtual const char* getName(void) const = 0; - typedef std::function SetInputAttributeFunc; - virtual void prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) = 0; + virtual void prepare(const RenderPipe* pipe, MaterialConstPtr material) = 0; virtual void process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) = 0; }; diff --git a/source/core/source/asset/dvc_material.cpp b/source/core/source/asset/dvc_material.cpp index ea2075c..7e29f54 100644 --- a/source/core/source/asset/dvc_material.cpp +++ b/source/core/source/asset/dvc_material.cpp @@ -12,12 +12,73 @@ Material::~Material() { } +void Material::setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform, + const fVector4& baseColorfactor) +{ + m_baseColorTexture.texture = texture; + m_baseColorTexture.texCoord = texCoord; + m_baseColorTexture.uvTransform = uvTransform; + + m_baseColorFactor = baseColorfactor; +} + +void Material::setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform, + float32_t metallicFactor, float32_t roughnessFactor) +{ + m_metallicRoughnessTexture.texture = texture; + m_metallicRoughnessTexture.texCoord = texCoord; + m_metallicRoughnessTexture.uvTransform = uvTransform; + + m_metallicFactor = metallicFactor; + m_roughnessFactor = roughnessFactor; +} + +void Material::setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform, + float32_t scale) +{ + m_normalTexture.texture = texture; + m_normalTexture.texCoord = texCoord; + m_normalTexture.uvTransform = uvTransform; + + m_normalScale = scale; +} + +void Material::setVertexShader(const char* szShaderFile, const char* vertexShaderName) +{ + m_vertexshaderFile = szShaderFile; + m_vertexShaderName = vertexShaderName; +} + +void Material::setFragementShader(const char* szShaderFile, const char* fragementShaderName) +{ + m_fragementShaderFile = szShaderFile; + m_fragementShaderName = fragementShaderName; +} + bool Material::loadShader() { - ShaderFileInterface* shaderInterface = davinci::LoadShaderFile("TestShader.dll"); - m_vertexShader = shaderInterface->getVertexShader("test"); - m_pixelShader = shaderInterface->getPixelShader("test"); + ShaderFileInterface* shaderInterface = davinci::LoadShaderFile(m_vertexshaderFile.c_str()); + if (shaderInterface == nullptr) + { + return false; + } + m_vertexShader = shaderInterface->getVertexShader(m_vertexShaderName.c_str()); + if (!m_vertexShader) + { + return false; + } + shaderInterface = davinci::LoadShaderFile(m_fragementShaderFile.c_str()); + if (shaderInterface == nullptr) + { + return false; + } + + m_pixelShader = shaderInterface->getPixelShader(m_fragementShaderName.c_str()); + if (!m_pixelShader) + { + return false; + } return true; } diff --git a/source/core/source/asset/dvc_texture.cpp b/source/core/source/asset/dvc_texture.cpp index 0362249..3a5ae04 100644 --- a/source/core/source/asset/dvc_texture.cpp +++ b/source/core/source/asset/dvc_texture.cpp @@ -20,8 +20,12 @@ fVector4 Texture::sample(const fVector2& uv) const fVector4 color = fVector4::ZERO; if (!m_image) return color; - int32_t u = uv.x * m_image->getWidth(); - int32_t v = uv.y * m_image->getHeight(); + float uv_x = uv.x - std::floor(uv.x); + float uv_y = uv.y - std::floor(uv.y); + + + int32_t u = uv_x * m_image->getWidth(); + int32_t v = uv_y * m_image->getHeight(); return m_image->getPixel(u, v); } diff --git a/source/core/source/pipe/dvc_render_step2_rasterizer.cpp b/source/core/source/pipe/dvc_render_step2_rasterizer.cpp index 42af3a7..dab19f3 100644 --- a/source/core/source/pipe/dvc_render_step2_rasterizer.cpp +++ b/source/core/source/pipe/dvc_render_step2_rasterizer.cpp @@ -26,18 +26,8 @@ void RasterizerStep::process(RenderPipe& pipe, const VertexShaderStep::NodeArray { const VertexShaderStep::Node& node = *it; - if (node.material->getBaseColorTexture().texture) - { - uniformBuffer.setAttribute("material:pbr:base_color_texture", node.material->getBaseColorTexture().texture->getID()); - } - - if (node.material->getNormalTexture().texture) - { - uniformBuffer.setAttribute("material:pbr:normal_texture", node.material->getNormalTexture().texture->getID()); - } - PixelShaderInterface* pixelShader = node.material->getPixelShader(); - pixelShader->prepare(&pipe, nullptr); + pixelShader->prepare(&pipe, node.material); size_t vertexSize = node.vertexDesc.vertexByteSize(); size_t POS_offset = node.vertexDesc.getElement(VET_POSITION)->offset; @@ -75,9 +65,14 @@ void RasterizerStep::process(RenderPipe& pipe, const VertexShaderStep::NodeArray pixelData[j] = MathUtil::lerp3(vertex0[j], vertex1[j], vertex2[j], percentCorrected); } + if(dot.first==570 && dot.second==405 ) + { + int a = 0; + } fVector4 color; pixelShader->process(node.vertexDesc, &(pixelData[0]), color); + float32_t depth = MathUtil::lerp3(vertexDepth[0], vertexDepth[1], vertexDepth[2], percentCorrected); int32_t x = dot.first; diff --git a/source/core/source/shader/dvc_shader_functions.cpp b/source/core/source/shader/dvc_shader_functions.cpp index 75006e4..b17de00 100644 --- a/source/core/source/shader/dvc_shader_functions.cpp +++ b/source/core/source/shader/dvc_shader_functions.cpp @@ -4,15 +4,12 @@ DV_CORE_BEGIN_NAMESPACE -fVector4 shader::texture2D(const RenderPipe& pipe, const char* textName, const fVector2& uv, const fVector4& defaultValue) +fVector4 shader::texture2D(const RenderPipe& pipe, const Material::TextureInfo& textureInfo, const fVector2& uv, const fVector4& defaultValue) { - davinci::fVector4 textureColor = defaultValue; - const int32_t* textureID = (const int32_t* )(pipe.getUniformBuffer().getAttribute(textName)); - if (textureID != nullptr) - { - textureColor = pipe.getDevice().getTexture(*textureID)->sample(uv); - } - return textureColor; + if (!(textureInfo.texture)) return defaultValue; + fVector3 uvTransform = textureInfo.uvTransform * fVector3(uv, 1); + return textureInfo.texture->sample(uvTransform.xy()); } + DV_CORE_END_NAMESPACE diff --git a/source/shaders/CMakeLists.txt b/source/shaders/CMakeLists.txt new file mode 100644 index 0000000..e0a0f66 --- /dev/null +++ b/source/shaders/CMakeLists.txt @@ -0,0 +1,4 @@ +################# +# sub dictionary +################# +add_subdirectory(standard) diff --git a/source/shaders/standard/CMakeLists.txt b/source/shaders/standard/CMakeLists.txt new file mode 100644 index 0000000..7c48174 --- /dev/null +++ b/source/shaders/standard/CMakeLists.txt @@ -0,0 +1,34 @@ +add_library(dv_standard_shaders SHARED + dll_main.cpp + vs_standard.h + vs_standard.inc + fs_debug_solid_color.h + fs_debug_solid_color.cpp + fs_debug_base_color.h + fs_debug_base_color.cpp + fs_debug_normal_texture.h + fs_debug_normal_texture.cpp + fs_debug_texture_coordinates.h + fs_debug_texture_coordinates.cpp + fs_debug_geometry_normal.h + fs_debug_geometry_normal.cpp +) + +add_definitions(-DDV_SHADER_EXPORTS) + +target_include_directories(dv_standard_shaders +PRIVATE + ${DV_AUTO_INCLUDE_PATH} + ${DV_CORE_INCLUDE_PATH} +) + +target_link_libraries(dv_standard_shaders + dv_core +) + +################# +# copy runtime dll(for debug) +################# +add_custom_command( + TARGET dv_standard_shaders POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ ${CMAKE_BINARY_DIR}/source/console/$<$:Debug>$<$:Release>/) diff --git a/source/shaders/standard/dll_main.cpp b/source/shaders/standard/dll_main.cpp new file mode 100644 index 0000000..3d00317 --- /dev/null +++ b/source/shaders/standard/dll_main.cpp @@ -0,0 +1,82 @@ +#include "dv_config.h" +#include "shader/dvc_shader_interface.h" + +#include "vs_standard.h" +#include "fs_debug_solid_color.h" +#include "fs_debug_base_color.h" +#include "fs_debug_normal_texture.h" +#include "fs_debug_texture_coordinates.h" +#include "fs_debug_geometry_normal.h" + +class StandardShaderInterface : public davinci::ShaderFileInterface +{ +public: + virtual davinci::VertexShaderInterface* getVertexShader(const char* szName) + { + auto it = m_vertexShaderMap.find(szName); + if (it != m_vertexShaderMap.end()) return it->second; + return nullptr; + } + + virtual davinci::PixelShaderInterface* getPixelShader(const char* szName) + { + auto it = m_fragementShaderMap.find(szName); + if (it != m_fragementShaderMap.end()) return it->second; + return nullptr; + } + +public: + StandardShaderInterface() + { + registerVertexShader(new StandardVertexShader()); + registerVertexShader(new StandardVertexShader()); + registerVertexShader(new StandardVertexShader()); + registerVertexShader(new StandardVertexShader()); + + registerFragementShader(new DebugFragementShader_SolidColor()); + registerFragementShader(new DebugFragementShader_BaseColor()); + registerFragementShader(new DebugFragementShader_NormalTexture()); + registerFragementShader(new DebugFragementShader_TextureCoordinates()); + registerFragementShader(new DebugFragementShader_GeometryNormal()); + } + + ~StandardShaderInterface() + { + for (auto it = m_vertexShaderMap.begin(); it != m_vertexShaderMap.end(); ++it) + { + delete it->second; + } + m_vertexShaderMap.clear(); + + for (auto it = m_fragementShaderMap.begin(); it != m_fragementShaderMap.end(); ++it) + { + delete it->second; + } + m_fragementShaderMap.clear(); + } + +private: + void registerVertexShader(davinci::VertexShaderInterface* vertexShader) + { + m_vertexShaderMap.insert(std::make_pair(vertexShader->getName(), vertexShader)); + } + void registerFragementShader(davinci::PixelShaderInterface* fragementShader) + { + m_fragementShaderMap.insert(std::make_pair(fragementShader->getName(), fragementShader)); + } + +private: + std::map m_vertexShaderMap; + std::map m_fragementShaderMap; +}; + +extern "C" +{ + +__declspec(dllexport) davinci::ShaderFileInterface* ShaderFileInterface() +{ + static StandardShaderInterface theInterface; + return &theInterface; +} + +} diff --git a/source/shaders/standard/fs_debug_base_color.cpp b/source/shaders/standard/fs_debug_base_color.cpp new file mode 100644 index 0000000..62504d8 --- /dev/null +++ b/source/shaders/standard/fs_debug_base_color.cpp @@ -0,0 +1,29 @@ +#include "fs_debug_base_color.h" +#include "device/dvc_device.h" +#include "device/dvc_uniform_buffer.h" +#include "asset/dvc_texture.h" +#include "shader/dvc_shader_functions.h" +#include "pipe/dvc_render_pipe.h" + +const char* DebugFragementShader_BaseColor::getName(void) const +{ + return "debug:base_color"; +} + +void DebugFragementShader_BaseColor::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material) +{ + m_pipe = pipe; + m_material = material; +} + +void DebugFragementShader_BaseColor::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color) +{ + const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); + + const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv"); + const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t))); + + davinci::fVector4 u_baseColor = davinci::shader::texture2D(*m_pipe, m_material->getBaseColorTexture(), (*v_uv), davinci::fVector4::WHITE); + + color = davinci::fVector4(u_baseColor.xyz(), 1); +} diff --git a/source/shaders/standard/fs_debug_base_color.h b/source/shaders/standard/fs_debug_base_color.h new file mode 100644 index 0000000..ad534a4 --- /dev/null +++ b/source/shaders/standard/fs_debug_base_color.h @@ -0,0 +1,19 @@ +#pragma once + +#include "dvc_config.h" +#include "shader/dvc_shader_interface.h" + +class DebugFragementShader_BaseColor : public davinci::PixelShaderInterface +{ +public: + virtual const char* getName(void) const; + + virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material); + + virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, + davinci::fVector4& color); + +private: + const davinci::RenderPipe* m_pipe = nullptr; + davinci::MaterialConstPtr m_material; +}; diff --git a/source/shaders/standard/fs_debug_geometry_normal.cpp b/source/shaders/standard/fs_debug_geometry_normal.cpp new file mode 100644 index 0000000..386d5af --- /dev/null +++ b/source/shaders/standard/fs_debug_geometry_normal.cpp @@ -0,0 +1,30 @@ +#include "fs_debug_geometry_normal.h" +#include "device/dvc_device.h" +#include "device/dvc_uniform_buffer.h" +#include "asset/dvc_texture.h" +#include "shader/dvc_shader_functions.h" +#include "pipe/dvc_render_pipe.h" + +const char* DebugFragementShader_GeometryNormal::getName(void) const +{ + return "debug:geometry_normal"; +} + +void DebugFragementShader_GeometryNormal::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material) +{ + m_pipe = pipe; + m_material = material; +} + +void DebugFragementShader_GeometryNormal::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color) +{ + const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); + + const davinci::VertexDesc::Element* v_normal_Element = veretexDesc.getElement("v_normal"); + const davinci::fVector3* v_normal = (const davinci::fVector3*)(pixelData + (v_normal_Element->offset / sizeof(float32_t))); + + davinci::fVector3 normalColor = *v_normal; + normalColor.normalise(); + + color = davinci::fVector4(normalColor, 1); +} diff --git a/source/shaders/standard/fs_debug_geometry_normal.h b/source/shaders/standard/fs_debug_geometry_normal.h new file mode 100644 index 0000000..c12777c --- /dev/null +++ b/source/shaders/standard/fs_debug_geometry_normal.h @@ -0,0 +1,19 @@ +#pragma once + +#include "dvc_config.h" +#include "shader/dvc_shader_interface.h" + +class DebugFragementShader_GeometryNormal : public davinci::PixelShaderInterface +{ +public: + virtual const char* getName(void) const; + + virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material); + + virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, + davinci::fVector4& color); + +private: + const davinci::RenderPipe* m_pipe = nullptr; + davinci::MaterialConstPtr m_material; +}; diff --git a/source/shaders/standard/fs_debug_normal_texture.cpp b/source/shaders/standard/fs_debug_normal_texture.cpp new file mode 100644 index 0000000..23caa46 --- /dev/null +++ b/source/shaders/standard/fs_debug_normal_texture.cpp @@ -0,0 +1,29 @@ +#include "fs_debug_normal_texture.h" +#include "device/dvc_device.h" +#include "device/dvc_uniform_buffer.h" +#include "asset/dvc_texture.h" +#include "shader/dvc_shader_functions.h" +#include "pipe/dvc_render_pipe.h" + +const char* DebugFragementShader_NormalTexture::getName(void) const +{ + return "debug:normal_texture"; +} + +void DebugFragementShader_NormalTexture::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material) +{ + m_pipe = pipe; + m_material = material; +} + +void DebugFragementShader_NormalTexture::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color) +{ + const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); + + const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv"); + const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t))); + + davinci::fVector4 u_baseColor = davinci::shader::texture2D(*m_pipe, m_material->getNormalTexture(), (*v_uv), davinci::fVector4::WHITE); + + color = davinci::fVector4(u_baseColor.xyz(), 1); +} diff --git a/source/shaders/standard/fs_debug_normal_texture.h b/source/shaders/standard/fs_debug_normal_texture.h new file mode 100644 index 0000000..b75c106 --- /dev/null +++ b/source/shaders/standard/fs_debug_normal_texture.h @@ -0,0 +1,19 @@ +#pragma once + +#include "dvc_config.h" +#include "shader/dvc_shader_interface.h" + +class DebugFragementShader_NormalTexture : public davinci::PixelShaderInterface +{ +public: + virtual const char* getName(void) const; + + virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material); + + virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, + davinci::fVector4& color); + +private: + const davinci::RenderPipe* m_pipe = nullptr; + davinci::MaterialConstPtr m_material; +}; diff --git a/source/shaders/standard/fs_debug_solid_color.cpp b/source/shaders/standard/fs_debug_solid_color.cpp new file mode 100644 index 0000000..87b4bd9 --- /dev/null +++ b/source/shaders/standard/fs_debug_solid_color.cpp @@ -0,0 +1,16 @@ +#include "fs_debug_solid_color.h" + +const char* DebugFragementShader_SolidColor::getName(void) const +{ + return "debug:solid_color"; +} + +void DebugFragementShader_SolidColor::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material) +{ + +} + +void DebugFragementShader_SolidColor::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color) +{ + color = davinci::fVector4::RED; +} diff --git a/source/shaders/standard/fs_debug_solid_color.h b/source/shaders/standard/fs_debug_solid_color.h new file mode 100644 index 0000000..b27dd58 --- /dev/null +++ b/source/shaders/standard/fs_debug_solid_color.h @@ -0,0 +1,15 @@ +#pragma once + +#include "dvc_config.h" +#include "shader/dvc_shader_interface.h" + +class DebugFragementShader_SolidColor : public davinci::PixelShaderInterface +{ +public: + virtual const char* getName(void) const; + + virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material); + + virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, + davinci::fVector4& color); +}; diff --git a/source/shaders/standard/fs_debug_texture_coordinates.cpp b/source/shaders/standard/fs_debug_texture_coordinates.cpp new file mode 100644 index 0000000..c160451 --- /dev/null +++ b/source/shaders/standard/fs_debug_texture_coordinates.cpp @@ -0,0 +1,31 @@ +#include "fs_debug_texture_coordinates.h" +#include "device/dvc_device.h" +#include "device/dvc_uniform_buffer.h" +#include "asset/dvc_texture.h" +#include "shader/dvc_shader_functions.h" +#include "pipe/dvc_render_pipe.h" + +const char* DebugFragementShader_TextureCoordinates::getName(void) const +{ + return "debug:texture_coordinates"; +} + +void DebugFragementShader_TextureCoordinates::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material) +{ + m_pipe = pipe; + m_material = material; +} + +void DebugFragementShader_TextureCoordinates::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color) +{ + const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); + + const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv"); + const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t))); + + davinci::fVector2 uv = *v_uv; + float uv_x = uv.x - std::floor(uv.x); + float uv_y = uv.y - std::floor(uv.y); + + color = davinci::fVector4(uv_x, uv_y, 0, 1); +} diff --git a/source/shaders/standard/fs_debug_texture_coordinates.h b/source/shaders/standard/fs_debug_texture_coordinates.h new file mode 100644 index 0000000..e2433c9 --- /dev/null +++ b/source/shaders/standard/fs_debug_texture_coordinates.h @@ -0,0 +1,19 @@ +#pragma once + +#include "dvc_config.h" +#include "shader/dvc_shader_interface.h" + +class DebugFragementShader_TextureCoordinates : public davinci::PixelShaderInterface +{ +public: + virtual const char* getName(void) const; + + virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material); + + virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, + davinci::fVector4& color); + +private: + const davinci::RenderPipe* m_pipe = nullptr; + davinci::MaterialConstPtr m_material; +}; diff --git a/source/shaders/standard/vs_standard.h b/source/shaders/standard/vs_standard.h new file mode 100644 index 0000000..01a4088 --- /dev/null +++ b/source/shaders/standard/vs_standard.h @@ -0,0 +1,41 @@ +#pragma once + +#include "dvc_config.h" +#include "shader/dvc_shader_interface.h" + + +template +class StandardVertexShader : public davinci::VertexShaderInterface +{ +public: + virtual const char* getName(void) const; + virtual const davinci::VertexDesc& getOutputVertexDesc() const; + virtual size_t getOutputVertexByteSize(void) const; + virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc); + virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos); + +private: + const davinci::RenderPipe* m_pipe = nullptr; + davinci::VertexDesc m_outputVertexDesc; + + using InputBufferMembers = decltype(std::tuple_cat( + std::tuple(), + std::conditional_t, std::tuple<>>{}, + std::conditional_t, std::tuple<>>{} + )); + InputBufferMembers m_inputAccessors; + + static constexpr int32_t iPOSITION() { return 0; }; + static constexpr int32_t iNORMAL() { return 1; }; + static constexpr int32_t iUV() { return WithNormal+1; }; + + //davinci::DeviceBufferAccessorConstPtr& getPositionAccessor(); + //davinci::DeviceBufferAccessorConstPtr& getNormalAccessor(); + //davinci::DeviceBufferAccessorConstPtr& getUVAccessor(); + +public: + StandardVertexShader(); + virtual ~StandardVertexShader(); +}; + +#include "vs_standard.inc" diff --git a/source/shaders/standard/vs_standard.inc b/source/shaders/standard/vs_standard.inc new file mode 100644 index 0000000..22454da --- /dev/null +++ b/source/shaders/standard/vs_standard.inc @@ -0,0 +1,181 @@ +#include "device/dvc_buffer_accessor.h" +#include "math/dvc_vector2.h" +#include "math/dvc_vector3.h" +#include "math/dvc_matrix4.h" +#include "pipe/dvc_render_pipe.h" +#include "device/dvc_uniform_buffer.h" + +template +StandardVertexShader::StandardVertexShader() +{ + m_outputVertexDesc.addElement("v_position", davinci::VET_POSITION, davinci::CT_Float32, davinci::DT_Vec3); + + if constexpr(WithNormal) + m_outputVertexDesc.addElement("v_normal", davinci::VET_NORMAL, davinci::CT_Float32, davinci::DT_Vec3); + + if constexpr(WithUV) + m_outputVertexDesc.addElement("v_uv", davinci::VET_TEXCOORD_0, davinci::CT_Float32, davinci::DT_Vec2); +} + +template +StandardVertexShader::~StandardVertexShader() +{ + +} + +template +struct CompileTimeString { + std::array data{}; + std::size_t size = 0; + + constexpr void append(const char* str, std::size_t len) { + for (std::size_t i = 0; i < len; ++i) { + data[size++] = str[i]; + } + } + + constexpr void append(const char* str) { + while (*str) { + data[size++] = *str++; + } + } + + constexpr const char* c_str() const { return data.data(); } + constexpr std::string_view view() const { return {data.data(), size}; } +}; + +template +constexpr auto buildNameString() +{ + constexpr const char* prefix = "standard"; + constexpr const char* normalString = WithNormal ? "+normal" : "-normal"; + constexpr const char* uvString = WithUV ? "+uv" : "-uv"; + + constexpr std::size_t total_len = std::char_traits::length(prefix) + + std::char_traits::length(normalString) + + std::char_traits::length(uvString); + + CompileTimeString result; + result.append(prefix); + result.append(normalString); + result.append(uvString); + result.append(""); // null terminator + return result; +} + +template +const char* StandardVertexShader::getName(void) const +{ + static constexpr auto strName = buildNameString(); + const char* p = strName.c_str(); + return p; +} + +template +const davinci::VertexDesc& StandardVertexShader::getOutputVertexDesc() const +{ + return m_outputVertexDesc; +} + +template +size_t StandardVertexShader::getOutputVertexByteSize(void) const +{ + return m_outputVertexDesc.vertexByteSize(); +} + +template +void StandardVertexShader::prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) +{ + m_pipe = pipe; + + std::get(m_inputAccessors) = setInputAttributeFunc(davinci::VET_POSITION); + + if constexpr(WithNormal) + std::get(m_inputAccessors) = setInputAttributeFunc(davinci::VET_NORMAL); + + if constexpr(WithUV) + std::get(m_inputAccessors) = setInputAttributeFunc(davinci::VET_TEXCOORD_0); + +} +/* +template +davinci::DeviceBufferAccessorConstPtr& StandardVertexShader::getPositionAccessor() +{ + return std::get<0>(m_inputAccessors); +} + +template +davinci::DeviceBufferAccessorConstPtr& StandardVertexShader::getNormalAccessor() +{ + if constexpr(WithNormal) + return std::get<1>(m_inputAccessors); + else + return nullptr; +} + +template +davinci::DeviceBufferAccessorConstPtr& StandardVertexShader::getUVAccessor() +{ + if constexpr(WithUV) + return std::get(m_inputAccessors); + else + return nullptr; +} +*/ +template +void StandardVertexShader::process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos) +{ + static const float DEFAULT_POSITION[] = {0.f, 0.f, 0.f}; + static const float32_t DEFAULT_NORMAL[] = { 0.f, 0.f, 0.f }; + static const float32_t DEFAULT_TEXCOORD0[] = { 0.f, 0.f}; + + const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); + + davinci::DeviceBufferAccessorConstPtr inputPosition = std::get<0>(m_inputAccessors); + + davinci::DeviceBufferAccessorConstPtr inputNormal = nullptr; + if constexpr(WithNormal) + inputNormal = std::get(m_inputAccessors); + + davinci::DeviceBufferAccessorConstPtr inputUV = nullptr; + if constexpr(WithUV) + inputUV = std::get(m_inputAccessors); + + davinci::fVector3 a_position(inputPosition ? (const float32_t*)(inputPosition->getElement(index)) : DEFAULT_POSITION); + davinci::fVector3 a_normal(inputNormal ? (const float32_t*)(inputNormal->getElement(index)) : DEFAULT_NORMAL); + davinci::fVector2 a_uv(inputUV ? (const float32_t*)(inputUV->getElement(index)) : DEFAULT_TEXCOORD0); + + davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY; + if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform"))) + { + mat_world = *p_mat_world; + } + + davinci::fMatrix4 mat_camera_view_proj = davinci::fMatrix4::IDENTITY; + if (davinci::fMatrix4* p_mat_camera_view_proj = (davinci::fMatrix4*)(uniformBuffer.getAttribute("camera:view_proj_mat"))) + { + mat_camera_view_proj = *p_mat_camera_view_proj; + } + + vertexWorldPos = mat_world * a_position; + davinci::fVector3 v_position = mat_camera_view_proj * vertexWorldPos; + + int32_t outputOffset=0; + memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &v_position, sizeof(davinci::fVector3)); + + if constexpr(WithNormal) + { + davinci::fMatrix4 matNormal = davinci::fMatrix4::IDENTITY; + if (davinci::fMatrix4* p_mat_normal = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform_inverse_trans"))) + { + matNormal = *p_mat_normal; + } + davinci::fVector3 v_normal = (matNormal * davinci::fVector4(a_normal, 0)).xyz().normalise(); + memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &v_normal, sizeof(davinci::fVector3)); + } + + if constexpr(WithUV) + { + memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &a_uv, sizeof(float32_t) * 2); + } +} diff --git a/source/utils/CMakeLists.txt b/source/utils/CMakeLists.txt index 379df84..8d3f9de 100644 --- a/source/utils/CMakeLists.txt +++ b/source/utils/CMakeLists.txt @@ -43,6 +43,19 @@ set(DV_UTILS_CAMERA_SOURCE_FILES ) source_group("source/camera" FILES ${DV_UTILS_CAMERA_SOURCE_FILES}) +################# +# standard asserts +################# +set(DV_UTILS_STANDARD_INCLUDE_FILES + include/standard/dvu_standard_material.h +) +source_group("include/standard" FILES ${DV_UTILS_STANDARD_INCLUDE_FILES}) + +set(DV_UTILS_STANDARD_SOURCE_FILES + source/standard/dvu_standard_material.cpp +) +source_group("source/standard" FILES ${DV_UTILS_STANDARD_SOURCE_FILES}) + include_directories( ${DV_AUTO_INCLUDE_PATH} ${DV_CORE_INCLUDE_PATH} @@ -58,6 +71,8 @@ add_library(dv_utils SHARED ${DV_UTILS_BUFFER_SOURCE_FILES} ${DV_UTILS_CAMERA_INCLUDE_FILES} ${DV_UTILS_CAMERA_SOURCE_FILES} + ${DV_UTILS_STANDARD_INCLUDE_FILES} + ${DV_UTILS_STANDARD_SOURCE_FILES} ) target_link_libraries(dv_utils diff --git a/source/utils/include/standard/dvu_standard_material.h b/source/utils/include/standard/dvu_standard_material.h new file mode 100644 index 0000000..02d64c5 --- /dev/null +++ b/source/utils/include/standard/dvu_standard_material.h @@ -0,0 +1,11 @@ +#pragma once + +#include "dvu_config.h" +#include "dvc_config.h" +#include "dvc_pre_declare.h" + +DV_UTILS_BEGIN_NAMESPACE + +davinci::MaterialPtr DV_UTILS_API create_standard_material(davinci::Device* device, const char* name); + +DV_UTILS_END_NAMESPACE diff --git a/source/utils/source/scene/dvu_load_gltf.cpp b/source/utils/source/scene/dvu_load_gltf.cpp index 21ae519..1dcfc0e 100644 --- a/source/utils/source/scene/dvu_load_gltf.cpp +++ b/source/utils/source/scene/dvu_load_gltf.cpp @@ -20,6 +20,8 @@ #include "buffer/dvu_dump_buffer.h" +#include "standard/dvu_standard_material.h" + #include #include @@ -614,6 +616,49 @@ davinci::fVector4 _getVector4FromDoubleArray(const std::vector& doubleAr return davinci::fVector4((float32_t)doubleArray[0], (float32_t)doubleArray[1], (float32_t)doubleArray[2], (float32_t)doubleArray[3]); } +davinci::fMatrix3 _getTextureTransform(const tinygltf::ExtensionMap& gltfTextureInfoExternsionMap) +{ + davinci::fMatrix3 textureTransform = davinci::fMatrix3::IDENTITY; + + auto transformExternsions = gltfTextureInfoExternsionMap.find("KHR_texture_transform"); + if (transformExternsions != gltfTextureInfoExternsionMap.end()) + { + davinci::fVector2 scale = davinci::fVector2::ONE; + const tinygltf::Value& scaleArray = transformExternsions->second.Get("scale"); + if (scaleArray.IsArray() && scaleArray.ArrayLen()>=2 && + scaleArray.Get(0).IsNumber() && scaleArray.Get(1).IsNumber()) + { + scale = davinci::fVector2( + (float32_t)(scaleArray.Get(0).GetNumberAsDouble()), + (float32_t)(scaleArray.Get(1).GetNumberAsDouble()) + ); + } + + float32_t rotate = 0.f; + const tinygltf::Value& rotationValue = transformExternsions->second.Get("rotation"); + if (rotationValue.IsNumber()) + { + rotate = (float32_t)rotationValue.GetNumberAsDouble(); + } + + davinci::fVector2 offset = davinci::fVector2::ZERO; + const tinygltf::Value& offsetArray = transformExternsions->second.Get("offset"); + if (offsetArray.IsArray() && offsetArray.ArrayLen()>=2 && + offsetArray.Get(0).IsNumber() && offsetArray.Get(1).IsNumber()) + { + offset = davinci::fVector2( + (float32_t)(offsetArray.Get(0).GetNumberAsDouble()), + (float32_t)(offsetArray.Get(1).GetNumberAsDouble()) + ); + } + + textureTransform = davinci::fMatrix3::makeTrans(offset) * + davinci::fMatrix3::makeRotate(rotate) * davinci::fMatrix3::makeScale(scale); + } + + return textureTransform; +} + bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene) { for (size_t i = 0; i < gltfModel.materials.size(); i++) @@ -621,26 +666,36 @@ bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci: tinygltf::Material& gltfMaterial = gltfModel.materials[i]; //create new material - davinci::MaterialPtr materialPtr = device.newMaterialAsset(); + davinci::MaterialPtr materialPtr = create_standard_material(&device, "debug:geometry_normal"); //apply matellic roughness info - const tinygltf::PbrMetallicRoughness& pbrMetllicRoughness = gltfMaterial.pbrMetallicRoughness; + const tinygltf::PbrMetallicRoughness& gltfPBRMetllicRoughness = gltfMaterial.pbrMetallicRoughness; + + //base color texture + const tinygltf::TextureInfo& gltfBaseColorTextureInfo = gltfPBRMetllicRoughness.baseColorTexture; materialPtr->setBaseColorTexture( - _getTexturePtr(gltfModel, device, pbrMetllicRoughness.baseColorTexture.index), - pbrMetllicRoughness.baseColorTexture.texCoord, - _getVector4FromDoubleArray(pbrMetllicRoughness.baseColorFactor) - ); - materialPtr->setMetallicRoughnessTexture( - _getTexturePtr(gltfModel, device, pbrMetllicRoughness.metallicRoughnessTexture.index), - pbrMetllicRoughness.metallicRoughnessTexture.texCoord, - pbrMetllicRoughness.metallicFactor, - pbrMetllicRoughness.roughnessFactor + _getTexturePtr(gltfModel, device, gltfBaseColorTextureInfo.index), + gltfBaseColorTextureInfo.texCoord, + _getTextureTransform(gltfBaseColorTextureInfo.extensions), + _getVector4FromDoubleArray(gltfPBRMetllicRoughness.baseColorFactor) ); - //set normal texture info + //matallic & roughness texture + const tinygltf::TextureInfo& gltfMetalicRoughnessTextureInfo = gltfPBRMetllicRoughness.metallicRoughnessTexture; + materialPtr->setMetallicRoughnessTexture( + _getTexturePtr(gltfModel, device, gltfMetalicRoughnessTextureInfo.index), + gltfMetalicRoughnessTextureInfo.texCoord, + _getTextureTransform(gltfMetalicRoughnessTextureInfo.extensions), + gltfPBRMetllicRoughness.metallicFactor, + gltfPBRMetllicRoughness.roughnessFactor + ); + + //normal texture + const tinygltf::NormalTextureInfo& gltfNormalTextureInfo = gltfMaterial.normalTexture; materialPtr->setNormalTexture( - _getTexturePtr(gltfModel, device, gltfMaterial.normalTexture.index), - gltfMaterial.normalTexture.texCoord, + _getTexturePtr(gltfModel, device, gltfNormalTextureInfo.index), + gltfNormalTextureInfo.texCoord, + _getTextureTransform(gltfNormalTextureInfo.extensions), gltfMaterial.normalTexture.scale ); diff --git a/source/utils/source/standard/dvu_standard_material.cpp b/source/utils/source/standard/dvu_standard_material.cpp new file mode 100644 index 0000000..b84b20c --- /dev/null +++ b/source/utils/source/standard/dvu_standard_material.cpp @@ -0,0 +1,40 @@ +#include "standard/dvu_standard_material.h" +#include "device/dvc_device.h" +#include "asset/dvc_material.h" + +DV_UTILS_BEGIN_NAMESPACE + +davinci::MaterialPtr create_standard_material(davinci::Device* device, const char* name) +{ + if (strcmp(name, "debug:base_color")==0) + { + davinci::MaterialPtr materialPtr = device->newMaterialAsset(); + materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv"); + materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:base_color"); + return materialPtr; + } + else if (strcmp(name, "debug:normal_texture")==0) + { + davinci::MaterialPtr materialPtr = device->newMaterialAsset(); + materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv"); + materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:normal_texture"); + return materialPtr; + } + else if (strcmp(name, "debug:texture_coordinates")==0) + { + davinci::MaterialPtr materialPtr = device->newMaterialAsset(); + materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv"); + materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:texture_coordinates"); + return materialPtr; + } + else if (strcmp(name, "debug:geometry_normal")==0) + { + davinci::MaterialPtr materialPtr = device->newMaterialAsset(); + materialPtr->setVertexShader("dv_standard_shaders.dll", "standard+normal-uv"); + materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:geometry_normal"); + return materialPtr; + } + return nullptr; +} + +DV_UTILS_END_NAMESPACE diff --git a/test/shader/dll_main.cpp b/test/shader/dll_main.cpp index f0f0347..72a28d7 100644 --- a/test/shader/dll_main.cpp +++ b/test/shader/dll_main.cpp @@ -9,12 +9,7 @@ class TestShaderInterface : public davinci::ShaderFileInterface public: virtual davinci::VertexShaderInterface* getVertexShader(const char* szName) { - if (strcmp(szName, "standard") == 0) - { - static StandardVertexShader standardVertexShader; - return &standardVertexShader; - } - else if (strcmp(szName, "test") == 0) + if (strcmp(szName, "test") == 0) { static TestVertexShader testVertexShader; return &testVertexShader; @@ -23,13 +18,8 @@ public: } virtual davinci::PixelShaderInterface* getPixelShader(const char* szName) { - static SolidColorPixelShader solidColorPixelShader; static TestPixelShader testPixelShader; - if(strcmp(szName, solidColorPixelShader.getName()) == 0) - { - return &solidColorPixelShader; - } - else if (strcmp(szName, testPixelShader.getName()) == 0) + if (strcmp(szName, testPixelShader.getName()) == 0) { return &testPixelShader; } diff --git a/test/shader/test_pixel_shader.cpp b/test/shader/test_pixel_shader.cpp index 82404d9..7fb8ba3 100644 --- a/test/shader/test_pixel_shader.cpp +++ b/test/shader/test_pixel_shader.cpp @@ -12,7 +12,7 @@ const char* TestPixelShader::getName(void) const return "test"; } -void TestPixelShader::prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) +void TestPixelShader::prepare(const RenderPipe* pipe, davinci::MaterialConstPtr material) { m_pipe = pipe; } @@ -68,76 +68,41 @@ void TestPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pi fVector3 u_lightColor = fVector3(1.0, 1.0, 1.0); - fVector4 u_textureColor = shader::texture2D(*m_pipe, "material:pbr:base_color_texture", *v_uv, fVector4::WHITE); + fVector4 u_textureColor;// = shader::texture2D(*m_pipe, "material:pbr:base_color_texture", *v_uv, fVector4::WHITE); const fVector4 defaultNormal = fVector4(0.5f, 0.5f, 1.0f, 1.0f); - fVector4 u_normalColor = shader::texture2D(*m_pipe, "material:pbr:normal_texture", *v_uv, defaultNormal); + fVector4 u_normalColor;// = shader::texture2D(*m_pipe, "material:pbr:normal_texture", *v_uv, defaultNormal); //---------------- - fVector3 vN = fVector3::UNIT_Z;// u_normalColor.xyz() * 2.0f - fVector3::ONE; + fVector3 vN = u_normalColor.xyz() * 2.0f - fVector3::ONE; vN.normalise(); - //vector vL = normalize(v_toLight); fVector3 vL =vLight; vL.normalise(); - //vector vV = normalize(v_toCamera); fVector3 vV = *v_toCamera; vV.normalise(); float ndotl = vL.dotProduct(vN); - - //vector vR = 2 * dot(vL, vN)*vN - vL; fVector3 vR = 2 * ndotl * vN - vL; float rdotv = vR.dotProduct(vV); - //vector vH = normalize(vL + vV); - //fVector3 vH = vL + vV; - //vH.normalise(); - - - //color colAmbient = u_ambientColor*u_matColor; fVector3 colAmbient = u_ambientColor * u_matColor; - - //float ndotl = max(dot(vN, vL), 0); - //float32_t ndotl = std::max(vN.dotProduct(vL), 0.0f); - //color colDiff = ndotl * u_matColor * u_lightColor; fVector3 colDiff = shader::max(0.0f, ndotl) * u_matColor* u_lightColor; - - //float32_t spec = std::max(vR.dotProduct(vV), 0.0f)*1.0; - //color colSpec = step(0, ndotl) * pow(spec, 64) * u_matColor * u_lightColor; - const float _m = 1.0f; float spec = shader::step(0.0f, ndotl) * shader::pow(shader::max(0.0f, rdotv), 32.f); fVector3 colSpec = spec * u_matColor* u_lightColor; - //color final = colAmbient + colDiff + colSpec; - fVector3 final = fVector3::BLACK;// (colAmbient + colDiff + colSpec)* u_textureColor.xyz(); - - final += colAmbient; + fVector3 final = fVector3::BLACK; + //final += colAmbient; final += colDiff; - final += colSpec; - - final *= u_textureColor.xyz(); - //fVector3 final = (colAmbient +colDiff +colSpec)*u_textureColor.xyz(); + //final += colSpec; + //final *= u_textureColor.xyz(); + //final = u_normalColor.xyz(); //color = fVector4::RED; color = fVector4(final.saturate(), 1.0f);// fVector4::RED; } -const char* SolidColorPixelShader::getName(void) const -{ - return "solid"; -} - -void SolidColorPixelShader::prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) -{ - -} - -void SolidColorPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) -{ - color = fVector4::RED; -} diff --git a/test/shader/test_pixel_shader.h b/test/shader/test_pixel_shader.h index ac2d44e..de1aa49 100644 --- a/test/shader/test_pixel_shader.h +++ b/test/shader/test_pixel_shader.h @@ -8,7 +8,7 @@ class TestPixelShader : public davinci::PixelShaderInterface public: virtual const char* getName(void) const; - virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc); + virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material); virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color); @@ -16,13 +16,3 @@ private: const davinci::RenderPipe* m_pipe = nullptr; }; -class SolidColorPixelShader : public davinci::PixelShaderInterface -{ -public: - virtual const char* getName(void) const; - - virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc); - - virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, - davinci::fVector4& color); -}; diff --git a/test/shader/test_vertex_shader.cpp b/test/shader/test_vertex_shader.cpp index ce9a3b3..a93fe51 100644 --- a/test/shader/test_vertex_shader.cpp +++ b/test/shader/test_vertex_shader.cpp @@ -63,7 +63,7 @@ void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVecto static const float32_t DEFAULT_POSITION[] = {0.f, 0.f, 0.f}; static const float32_t DEFAULT_NORMAL[] = { 0.f, 0.f, 0.f }; static const float32_t DEFAULT_TEXCOORD0[] = { 0.f, 0.f}; - static const float32_t DEFAULT_TANGENT[] = { 1.f, 0.f, 0.f }; + static const float32_t DEFAULT_TANGENT[] = { 1.f, 0.f, 0.f, 1.f }; const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); @@ -73,7 +73,7 @@ void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVecto davinci::fVector2 a_uv(m_TEXCOORD_0 ? (const float32_t*)(m_TEXCOORD_0->getElement(index)) : DEFAULT_TEXCOORD0); - davinci::fVector3 a_tangent(m_TANGENT ? (const float32_t*)(m_TANGENT->getElement(index)) : DEFAULT_TANGENT); + davinci::fVector4 a_tangent(m_TANGENT ? (const float32_t*)(m_TANGENT->getElement(index)) : DEFAULT_TANGENT); davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY; if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform"))) @@ -108,8 +108,8 @@ void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVecto matNormal = *p_mat_normal; } davinci::fVector3 v_normal = (matNormal * davinci::fVector4(a_normal, 0)).xyz().normalise(); - davinci::fVector3 v_tangent = (matNormal * davinci::fVector4(a_tangent, 0)).xyz().normalise(); - davinci::fVector3 v_binormal = v_normal.crossProduct(v_tangent).normalise(); + davinci::fVector3 v_tangent = (matNormal * davinci::fVector4(a_tangent.xyz(), 0)).xyz().normalise(); + davinci::fVector3 v_binormal = v_normal.crossProduct(v_tangent).normalise();// *a_tangent.w; davinci::fMatrix3 tbnMatrix = davinci::fMatrix3( v_tangent.x, v_binormal.x, v_normal.x,