From 8db6f7b1153d66b8ea0678cd2223145745884d3d Mon Sep 17 00:00:00 2001 From: thejinchao Date: Sun, 26 Oct 2025 20:07:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9D=90=E8=B4=A8=E5=92=8C?= =?UTF-8?q?=E7=BA=B9=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/console/dv_console_main.cpp | 5 +- source/console/ui/ui_main.cpp | 9 +- source/core/CMakeLists.txt | 10 + source/core/include/asset/dvc_image.h | 39 +++ source/core/include/asset/dvc_material.h | 113 ++++++++ source/core/include/asset/dvc_mesh.h | 6 +- source/core/include/asset/dvc_texture.h | 28 ++ source/core/include/device/dvc_buffer.h | 5 +- .../core/include/device/dvc_buffer_accessor.h | 5 +- source/core/include/device/dvc_buffer_view.h | 5 +- source/core/include/device/dvc_device.h | 69 +++++ source/core/include/dvc_config.h | 20 +- source/core/include/dvc_pre_declare.h | 15 + source/core/include/math/dvc_math_util.h | 1 + source/core/include/math/dvc_vector4.h | 5 + source/core/include/pipe/dvc_render_pipe.h | 15 +- .../pipe/dvc_render_step0_input_assember.h | 2 +- .../pipe/dvc_render_step1_vertex_shader.h | 3 +- .../pipe/dvc_render_step2_rasterizer.h | 2 +- source/core/include/scene/dvc_scene.h | 34 --- .../include/shader/dvc_shader_functions.h | 36 +++ .../include/shader/dvc_shader_interface.h | 5 +- source/core/source/asset/dvc_image.cpp | 79 ++++++ source/core/source/asset/dvc_material.cpp | 24 ++ source/core/source/asset/dvc_mesh.cpp | 5 +- source/core/source/asset/dvc_texture.cpp | 29 ++ source/core/source/device/dvc_buffer.cpp | 11 +- .../source/device/dvc_buffer_accessor.cpp | 5 +- source/core/source/device/dvc_buffer_view.cpp | 5 +- source/core/source/device/dvc_device.cpp | 155 +++++++++++ source/core/source/math/dvc_math_util.cpp | 29 ++ source/core/source/math/dvc_vector4.cpp | 5 + source/core/source/pipe/dvc_render_pipe.cpp | 8 +- .../pipe/dvc_render_step1_vertex_shader.cpp | 17 +- .../pipe/dvc_render_step2_rasterizer.cpp | 23 +- .../source/pipe/dvc_renderable_entity.cpp | 1 + source/core/source/scene/dvc_scene.cpp | 76 ------ .../source/shader/dvc_shader_functions.cpp | 18 ++ source/utils/include/scene/dvu_load_gltf.h | 5 +- .../utils/source/buffer/dvu_dump_buffer.cpp | 17 +- source/utils/source/scene/dvu_load_gltf.cpp | 258 +++++++++++++++--- test/shader/test_pixel_shader.cpp | 133 ++++++--- test/shader/test_pixel_shader.h | 6 +- test/shader/test_vertex_shader.cpp | 53 ++-- test/shader/test_vertex_shader.h | 11 +- 45 files changed, 1124 insertions(+), 281 deletions(-) create mode 100644 source/core/include/asset/dvc_image.h create mode 100644 source/core/include/asset/dvc_material.h create mode 100644 source/core/include/asset/dvc_texture.h create mode 100644 source/core/include/device/dvc_device.h create mode 100644 source/core/include/shader/dvc_shader_functions.h create mode 100644 source/core/source/asset/dvc_image.cpp create mode 100644 source/core/source/asset/dvc_material.cpp create mode 100644 source/core/source/asset/dvc_texture.cpp create mode 100644 source/core/source/device/dvc_device.cpp create mode 100644 source/core/source/shader/dvc_shader_functions.cpp diff --git a/source/console/dv_console_main.cpp b/source/console/dv_console_main.cpp index 3787a7e..f052b53 100644 --- a/source/console/dv_console_main.cpp +++ b/source/console/dv_console_main.cpp @@ -11,8 +11,8 @@ #include "ui/ui_main.h" #include "adapter/adapter_texture.h" #include "device/dvc_render_target.h" - -#include +#include "device/dvc_device.h" +#include "scene/dvc_scene.h" int32_t g_screenWidth = 2048; int32_t g_screenHeight = 1024; @@ -24,6 +24,7 @@ davinci::AntiAliasingTech g_aaTech = davinci::AntiAliasingTech::AA_None; int32_t g_canvasWidth = 1024; int32_t g_canvasHeight = 512; +davinci::Device g_device; davinci::Scene g_scene; //------------------------------------------------------------------------------------- diff --git a/source/console/ui/ui_main.cpp b/source/console/ui/ui_main.cpp index 7e38e8c..74816e4 100644 --- a/source/console/ui/ui_main.cpp +++ b/source/console/ui/ui_main.cpp @@ -9,6 +9,7 @@ #include "scene/dvu_load_gltf.h" #include "pipe/dvc_render_pipe.h" +#include "device/dvc_device.h" #include "device/dvc_render_target.h" #include "shader/dvc_shader_interface.h" #include "buffer/dvu_dump_buffer.h" @@ -20,6 +21,7 @@ SDL_Window* main_window; +extern davinci::Device g_device; extern davinci::Scene g_scene; extern davinci::RenderTarget g_renderTarget; extern davinci::AntiAliasingTech g_aaTech; @@ -72,10 +74,11 @@ void OpenFileDialogFileCallback(void* userdata, const char* const* filelist, int { if (filelist == nullptr || *filelist == nullptr) return; + g_device.clear(); g_scene.clear(); const char* szFileName = *filelist; - if (!davinci_utils::load_scene_from_gltf(szFileName, g_scene)) + if (!davinci_utils::load_scene_from_gltf(szFileName, g_device, g_scene)) { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Open glf scene file error", main_window); } @@ -99,13 +102,13 @@ void drawMainUI() } if (ImGui::Button("Export")) { - davinci_utils::export_mesh_to_obj(g_scene); + davinci_utils::export_mesh_to_obj(g_device, g_scene); } const char* aaItem[] = { "None", "MSAA_2X2", "MSAA_4X4"}; ImGui::Combo("AntiAliasing", (int*)(&g_aaTech), aaItem, IM_ARRAYSIZE(aaItem)); if (ImGui::Button("Render")) { - davinci::RenderPipe pipe; + davinci::RenderPipe pipe(g_device); davinci::UniformBuffer& uniformBuffer = pipe.getUniformBuffer(); uniformBuffer.updateStandardAttribute(g_scene); diff --git a/source/core/CMakeLists.txt b/source/core/CMakeLists.txt index 5e2fb71..2f26638 100644 --- a/source/core/CMakeLists.txt +++ b/source/core/CMakeLists.txt @@ -69,11 +69,17 @@ source_group("source/scene/component" FILES ${DV_CORE_SCENE_COMPONENT_SOURCE_FIL ################# set(DV_CORE_ASSET_INCLUDE_FILES include/asset/dvc_mesh.h + include/asset/dvc_image.h + include/asset/dvc_texture.h + include/asset/dvc_material.h ) source_group("include/asset" FILES ${DV_CORE_ASSET_INCLUDE_FILES}) set(DV_CORE_ASSET_SOURCE_FILES source/asset/dvc_mesh.cpp + source/asset/dvc_image.cpp + source/asset/dvc_texture.cpp + source/asset/dvc_material.cpp ) source_group("source/asset" FILES ${DV_CORE_ASSET_SOURCE_FILES}) @@ -81,6 +87,7 @@ source_group("source/asset" FILES ${DV_CORE_ASSET_SOURCE_FILES}) # device ################# set(DV_CORE_DEVICE_INCLUDE_FILES + include/device/dvc_device.h include/device/dvc_buffer.h include/device/dvc_buffer_view.h include/device/dvc_buffer_accessor.h @@ -92,6 +99,7 @@ set(DV_CORE_DEVICE_INCLUDE_FILES source_group("include/device" FILES ${DV_CORE_DEVICE_INCLUDE_FILES}) set(DV_CORE_DEVICE_SOURCE_FILES + source/device/dvc_device.cpp source/device/dvc_buffer.cpp source/device/dvc_buffer_view.cpp source/device/dvc_buffer_accessor.cpp @@ -145,11 +153,13 @@ source_group("source/rasterizer" FILES ${DV_CORE_RASTERIZER_SOURCE_FILES}) ################# set(DV_CORE_SHADER_INCLUDE_FILES include/shader/dvc_shader_interface.h + include/shader/dvc_shader_functions.h ) source_group("include/shader" FILES ${DV_CORE_SHADER_INCLUDE_FILES}) set(DV_CORE_SHADER_SOURCE_FILES source/shader/dvc_shader_interface.cpp + source/shader/dvc_shader_functions.cpp ) source_group("source/shader" FILES ${DV_CORE_SHADER_SOURCE_FILES}) diff --git a/source/core/include/asset/dvc_image.h b/source/core/include/asset/dvc_image.h new file mode 100644 index 0000000..ca3f722 --- /dev/null +++ b/source/core/include/asset/dvc_image.h @@ -0,0 +1,39 @@ +#pragma once + +#include "dvc_config.h" +#include "dvc_pre_declare.h" +#include "math/dvc_vector2.h" +#include "math/dvc_vector4.h" + +DV_CORE_BEGIN_NAMESPACE + +class DV_CORE_API Image +{ +public: + int32_t getID(void) const { return m_id; } + int32_t getWidth(void) const { return m_width; } + int32_t getHeight(void) const { return m_height; } + PixelFormat getPixelFormat(void) const { return m_pixelFormat; } + DeviceBufferConstPtr getPixelBuffer(void) const { return m_pixelBuffer; } + + fVector4 getPixel(int32_t u, int32_t v) const; + + void clear(); + + bool initWithPixelData(Device& device, int32_t width, int32_t height, + PixelFormat pixelFormat, const uint8_t* data, size_t dataSize); + +protected: + int32_t m_id; + int32_t m_width; + int32_t m_height; + PixelFormat m_pixelFormat; + size_t m_pixelByteSize; + DeviceBufferPtr m_pixelBuffer; + +public: + Image(int32_t id); + ~Image(); +}; + +DV_CORE_END_NAMESPACE diff --git a/source/core/include/asset/dvc_material.h b/source/core/include/asset/dvc_material.h new file mode 100644 index 0000000..0a1b271 --- /dev/null +++ b/source/core/include/asset/dvc_material.h @@ -0,0 +1,113 @@ +#pragma once + +#include "dvc_config.h" +#include "dvc_pre_declare.h" +#include "math/dvc_vector4.h" + +DV_CORE_BEGIN_NAMESPACE + +class DV_CORE_API Material +{ +public: + struct TextureInfo + { + TextureConstPtr texture; + int32_t texCoord = 0; + }; + +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; + } + const TextureInfo& getBaseColorTexture() const { + return m_baseColorTexture; + } + + const fVector4& getBaseColorFactor() const { + 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; + } + + const TextureInfo& getMetallicRoughnessTexture() const { + return m_metallicRoughnessTexture; + } + + float32_t getMetallicFactor() const { + return m_metallicFactor; + } + + float32_t getRoughnessFactor() const { + 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; + } + + const TextureInfo& getNormalTexture() const { + return m_normalTexture; + } + + float32_t getNormalScale() const { + return m_normalScale; + } + + VertexShaderInterface* getVertexShader() const { + return m_vertexShader; + } + + PixelShaderInterface* getPixelShader() const { + return m_pixelShader; + } + +public: + bool loadShader(); + +protected: + int32_t m_id; + + /*The base color texture. + * The first three components (RGB) MUST be encoded with the sRGB transfer function. + */ + TextureInfo m_baseColorTexture; + fVector4 m_baseColorFactor = fVector4::ONE; + + /*The metallic - roughness texture. + * The metalness values are sampled from the B channel. + * The roughness values are sampled from the G channel. + */ + TextureInfo m_metallicRoughnessTexture; + float32_t m_metallicFactor = 1.0f; + float32_t m_roughnessFactor = 1.0f; + + /*The normal texture. + */ + TextureInfo m_normalTexture; + /*The scalar parameter applied to each normal vector of the texture. + * This value scales the normal vector in X and Y directions using the formula: + * scaledNormal = (normalize * 2.0 - 1.0) * vec3(, , 1.0). + */ + float32_t m_normalScale = 1.0f; + +protected: + VertexShaderInterface* m_vertexShader = nullptr; + PixelShaderInterface* m_pixelShader = nullptr; + +public: + Material(int32_t id); + ~Material(); +}; + +DV_CORE_END_NAMESPACE diff --git a/source/core/include/asset/dvc_mesh.h b/source/core/include/asset/dvc_mesh.h index aa2018c..410bca5 100644 --- a/source/core/include/asset/dvc_mesh.h +++ b/source/core/include/asset/dvc_mesh.h @@ -14,12 +14,15 @@ public: PrimitiveType type; PrimitiveAttributes attributes; DeviceBufferAccessorConstPtr indices; + MaterialPtr material; }; typedef std::shared_ptr PrimitivePtr; typedef std::shared_ptr PrimitiveConstPtr; typedef std::vector PrimitiveArray; public: + int32_t getID(void) const { return m_id; } + const std::string& getName(void) const { return m_name; } @@ -31,11 +34,12 @@ public: PrimitiveConstPtr getPrimitive(int32_t index) const; private: + int32_t m_id; std::string m_name; PrimitiveArray m_primitiveArray; public: - Mesh(const std::string& name); + Mesh(int32_t id, const std::string& name); ~Mesh(); }; diff --git a/source/core/include/asset/dvc_texture.h b/source/core/include/asset/dvc_texture.h new file mode 100644 index 0000000..af882e6 --- /dev/null +++ b/source/core/include/asset/dvc_texture.h @@ -0,0 +1,28 @@ +#pragma once + +#include "dvc_config.h" +#include "dvc_pre_declare.h" +#include "math/dvc_vector2.h" +#include "math/dvc_vector4.h" + +DV_CORE_BEGIN_NAMESPACE + +class DV_CORE_API Texture +{ +public: + int32_t getID(void) const { return m_id; } + ImagePtr getImage(void) const { return m_image; } + + void setImage(const ImagePtr& image) { m_image = image; } + + fVector4 sample(const fVector2& uv) const; + +private: + int32_t m_id; + ImagePtr m_image; + +public: + Texture(int32_t id); + ~Texture(); +}; +DV_CORE_END_NAMESPACE diff --git a/source/core/include/device/dvc_buffer.h b/source/core/include/device/dvc_buffer.h index 7627cf5..9fefae1 100644 --- a/source/core/include/device/dvc_buffer.h +++ b/source/core/include/device/dvc_buffer.h @@ -10,6 +10,7 @@ public: void init(size_t _length); void clear(void); + int32_t getID(void) const { return m_id; } size_t length(void) const { return m_length; } const uint8_t* ptr(size_t offseet) const { @@ -20,12 +21,12 @@ public: return m_ptr + offseet; } private: + int32_t m_id; uint8_t* m_ptr; size_t m_length; public: - DeviceBuffer(); - DeviceBuffer(size_t _length); + DeviceBuffer(int32_t id, size_t _length); ~DeviceBuffer(); }; diff --git a/source/core/include/device/dvc_buffer_accessor.h b/source/core/include/device/dvc_buffer_accessor.h index 2bbd48c..62f4dbb 100644 --- a/source/core/include/device/dvc_buffer_accessor.h +++ b/source/core/include/device/dvc_buffer_accessor.h @@ -7,6 +7,8 @@ DV_CORE_BEGIN_NAMESPACE class DV_CORE_API DeviceBufferAccessor { public: + int32_t getID(void) const { return m_id; } + const DeviceBufferViewConstPtr getBufferView(void) const { return m_bufferView; } @@ -39,6 +41,7 @@ public: private: + int32_t m_id; DeviceBufferViewConstPtr m_bufferView; size_t m_offset; size_t m_count; @@ -48,7 +51,7 @@ private: size_t m_elementSize; public: - DeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset, size_t count, ComponentType componentType, DataType dataType); + DeviceBufferAccessor(int32_t id, DeviceBufferViewConstPtr bufferView, size_t offset, size_t count, ComponentType componentType, DataType dataType); ~DeviceBufferAccessor(); }; diff --git a/source/core/include/device/dvc_buffer_view.h b/source/core/include/device/dvc_buffer_view.h index 11ef25a..d1a0b7d 100644 --- a/source/core/include/device/dvc_buffer_view.h +++ b/source/core/include/device/dvc_buffer_view.h @@ -8,6 +8,8 @@ DV_CORE_BEGIN_NAMESPACE class DV_CORE_API DeviceBufferView { public: + int32_t getID(void) const { return m_id; } + const DeviceBufferConstPtr getBuffer(void) const { return m_buffer; } @@ -25,13 +27,14 @@ public: } private: + int32_t m_id; DeviceBufferConstPtr m_buffer; size_t m_offset; size_t m_length; size_t m_stride; public: - DeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride); + DeviceBufferView(int32_t id, DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride); ~DeviceBufferView(); }; diff --git a/source/core/include/device/dvc_device.h b/source/core/include/device/dvc_device.h new file mode 100644 index 0000000..bab7c0f --- /dev/null +++ b/source/core/include/device/dvc_device.h @@ -0,0 +1,69 @@ +#pragma once + +#include "dvc_config.h" +#include "dvc_pre_declare.h" + +DV_CORE_BEGIN_NAMESPACE + +class DV_CORE_API Device +{ +public: + void clear(); + +public: + DeviceBufferPtr newDeviceBuffer(size_t length=0); + DeviceBufferConstPtr getDeviceBuffer(int32_t id) const; + + DeviceBufferViewPtr newDeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride); + DeviceBufferViewPtr getDeviceBufferView(int32_t id) const; + + DeviceBufferAccessorPtr newDeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset, + size_t count, ComponentType componentType, DataType dataType); + DeviceBufferAccessorPtr getDeviceBufferAccessor(int32_t id) const; + + MeshPtr newMeshAsset(const std::string& name); + MeshPtr getMesh(int32_t id) const; + + ImagePtr newImageAsset(); + ImagePtr getImage(int32_t id) const; + + TexturePtr newTextureAsset(); + TexturePtr getTexture(int32_t id) const; + + MaterialPtr newMaterialAsset(); + MaterialPtr getMaterial(int32_t id) const; + +private: + int32_t nextID() { + if (++m_currentID < 0) m_currentID = 0; + return m_currentID; + } +private: + int32_t m_currentID = 0; + + //device buffer + std::map m_deviceBufferMap; + + //device buffer view + std::map m_bufferViewMap; + + //device buffer accessor + std::map m_bufferAccessorMap; + + //mesh asset resources + std::map m_meshAssetMap; + + //image asset resources + std::map m_imageAssetMap; + + //texture asset resources + std::map m_textureAssetMap; + + //material asset resources + std::map m_materialAssetMap; + +public: + Device(); + ~Device(); +}; +DV_CORE_END_NAMESPACE diff --git a/source/core/include/dvc_config.h b/source/core/include/dvc_config.h index 0707023..f68921a 100644 --- a/source/core/include/dvc_config.h +++ b/source/core/include/dvc_config.h @@ -89,17 +89,19 @@ enum PrimitiveType enum PixelFormat { - //96-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue - PF_FLOAT32_RGB, + PF_UNKNOWN, - /// 128-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue, 32 bits (float) for alpha - PF_FLOAT32_RGBA, - - // 32-bit pixel format, 32 bits (float) for red - PF_FLOAT32_R, - - /// 132-bit pixel format, 8 bits (float) for red, 8 bits (float) for green, 8 bits (float) for blue, 8 bits (float) for alpha + PF_UINT8_ALPHA, + PF_UINT8_RGB, PF_UINT8_RGBA, + + PF_UINT16_ALPHA, + PF_UINT16_RGB, + PF_UINT16_RGBA, + + PF_FLOAT32_ALPHA, + PF_FLOAT32_RGB, + PF_FLOAT32_RGBA, }; enum AntiAliasingTech diff --git a/source/core/include/dvc_pre_declare.h b/source/core/include/dvc_pre_declare.h index 917a7ec..2c22c78 100644 --- a/source/core/include/dvc_pre_declare.h +++ b/source/core/include/dvc_pre_declare.h @@ -36,8 +36,22 @@ class Mesh; typedef std::shared_ptr MeshPtr; typedef std::shared_ptr MeshConstPtr; +class Image; +typedef std::shared_ptr ImagePtr; +typedef std::shared_ptr ImageConstPtr; + +class Texture; +typedef std::shared_ptr TexturePtr; +typedef std::shared_ptr TextureConstPtr; + +class Material; +typedef std::shared_ptr MaterialPtr; +typedef std::shared_ptr MaterialConstPtr; + class Scene; +class Device; + class SceneNode; typedef std::shared_ptr SceneNodePtr; typedef std::shared_ptr SceneNodeConstPtr; @@ -68,6 +82,7 @@ typedef std::shared_ptr RenderableConstPtr; typedef std::vector RenderableArray; class VertexShaderInterface; +class PixelShaderInterface; class UniformBuffer; diff --git a/source/core/include/math/dvc_math_util.h b/source/core/include/math/dvc_math_util.h index 13f1518..d2eb776 100644 --- a/source/core/include/math/dvc_math_util.h +++ b/source/core/include/math/dvc_math_util.h @@ -10,6 +10,7 @@ class MathUtil public: static DV_CORE_API size_t ComponentSize(ComponentType ct); static DV_CORE_API size_t DataTypeSize(DataType dt); + static DV_CORE_API size_t PixelSize(PixelFormat pixelFormat); //count the number of bits set in v static DV_CORE_API uint32_t popcount(uint8_t v); diff --git a/source/core/include/math/dvc_vector4.h b/source/core/include/math/dvc_vector4.h index ba23873..a526cd2 100644 --- a/source/core/include/math/dvc_vector4.h +++ b/source/core/include/math/dvc_vector4.h @@ -235,6 +235,11 @@ public: public: // special points static DV_CORE_API const TVector4 ZERO; + static DV_CORE_API const TVector4 ONE; + static DV_CORE_API const TVector4 UNIT_X; + static DV_CORE_API const TVector4 UNIT_Y; + static DV_CORE_API const TVector4 UNIT_Z; + static DV_CORE_API const TVector4 UNIT_W; static DV_CORE_API const TVector4 WHITE; static DV_CORE_API const TVector4 BLACK; static DV_CORE_API const TVector4 RED; diff --git a/source/core/include/pipe/dvc_render_pipe.h b/source/core/include/pipe/dvc_render_pipe.h index 80e298f..88d8c3c 100644 --- a/source/core/include/pipe/dvc_render_pipe.h +++ b/source/core/include/pipe/dvc_render_pipe.h @@ -1,6 +1,7 @@ #pragma once #include "dvc_config.h" +#include "device/dvc_device.h" #include "scene/dvc_scene.h" #include "device/dvc_uniform_buffer.h" #include "device/dvc_render_target.h" @@ -10,9 +11,18 @@ DV_CORE_BEGIN_NAMESPACE class DV_CORE_API RenderPipe { public: + Device& getDevice() { + return m_device; + } + const Device& getDevice() const { + return m_device; + } UniformBuffer& getUniformBuffer() { return m_uniformBuffer; } + const UniformBuffer& getUniformBuffer() const { + return m_uniformBuffer; + } AntiAliasingTech getAntiAliasingTech() const { return m_aaTech; } @@ -24,12 +34,13 @@ public: void process(RenderTarget& renderTarget); protected: - AntiAliasingTech m_aaTech; + Device& m_device; + AntiAliasingTech m_aaTech = AntiAliasingTech::AA_None; UniformBuffer m_uniformBuffer; RenderableArray m_renderableQueue; public: - RenderPipe(); + RenderPipe(Device& device); ~RenderPipe(); }; diff --git a/source/core/include/pipe/dvc_render_step0_input_assember.h b/source/core/include/pipe/dvc_render_step0_input_assember.h index 2b7d28e..afd7805 100644 --- a/source/core/include/pipe/dvc_render_step0_input_assember.h +++ b/source/core/include/pipe/dvc_render_step0_input_assember.h @@ -17,7 +17,7 @@ public: PrimitiveType primitiveType; PrimitiveAttributes vertexData; DeviceBufferAccessorConstPtr indices; - VertexShaderInterface* vs; + MaterialConstPtr material; }; typedef std::vector NodeArray; diff --git a/source/core/include/pipe/dvc_render_step1_vertex_shader.h b/source/core/include/pipe/dvc_render_step1_vertex_shader.h index ee174ce..a228884 100644 --- a/source/core/include/pipe/dvc_render_step1_vertex_shader.h +++ b/source/core/include/pipe/dvc_render_step1_vertex_shader.h @@ -12,6 +12,7 @@ class VertexShaderStep public: struct Node { + MaterialConstPtr material; DeviceBufferPtr vertexBuf; VertexDesc vertexDesc; size_t vertexCounts; @@ -21,7 +22,7 @@ public: typedef std::vector NodeArray; public: - void process(UniformBuffer& uniformBuffer, const InputAssemberStep::NodeArray& assembedNodeArray); + void process(RenderPipe& pipe, const InputAssemberStep::NodeArray& assembedNodeArray); public: NodeArray m_nodes; diff --git a/source/core/include/pipe/dvc_render_step2_rasterizer.h b/source/core/include/pipe/dvc_render_step2_rasterizer.h index 3fe0c1a..8fddefb 100644 --- a/source/core/include/pipe/dvc_render_step2_rasterizer.h +++ b/source/core/include/pipe/dvc_render_step2_rasterizer.h @@ -20,7 +20,7 @@ public: typedef std::vector FragementArray; public: - void process(const VertexShaderStep::NodeArray& vsNodeArray, AntiAliasingTech aaTech, int32_t canvasWidth, int32_t canvasHeight); + void process(RenderPipe& pipe, const VertexShaderStep::NodeArray& vsNodeArray, AntiAliasingTech aaTech, int32_t canvasWidth, int32_t canvasHeight); public: StartOffsetArray m_startOffsetArray; diff --git a/source/core/include/scene/dvc_scene.h b/source/core/include/scene/dvc_scene.h index 7131aad..0ffff45 100644 --- a/source/core/include/scene/dvc_scene.h +++ b/source/core/include/scene/dvc_scene.h @@ -11,32 +11,6 @@ public: void init(void); void clear(void); -public: - DeviceBufferPtr newDeviceBuffer(void); - int32_t getDeviceBufferCounts(void) const { - return (int32_t)m_deviceBufferArray.size(); - } - DeviceBufferConstPtr getDeviceBuffer(int32_t index) const; - - DeviceBufferViewPtr newDeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride); - int32_t getDeviceBufferViewCounts(void) const { - return (int32_t)m_bufferViewArray.size(); - } - DeviceBufferViewPtr getDeviceBufferView(int32_t index) const; - - DeviceBufferAccessorPtr newDeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset, - size_t count, ComponentType componentType, DataType dataType); - int32_t getDeviceBufferAccessorCounts(void) const { - return (int32_t)m_bufferAccessorArray.size(); - } - DeviceBufferAccessorPtr getDeviceBufferAccessor(int32_t index) const; - - int32_t pushNewMeshAsset(MeshPtr mesh); - int32_t getMeshCounts(void) const { - return (int32_t)m_meshArray.size(); - } - MeshConstPtr getMesh(int32_t index) const; - public: SceneNodePtr newSceneNode(const std::string& name); SceneNodePtr getRootSceneNode() const; @@ -80,14 +54,6 @@ public: protected: SceneNodePtr m_rootSceneNode; - //device resources - std::vector m_deviceBufferArray; - std::vector m_bufferViewArray; - std::vector m_bufferAccessorArray; - - //asset resources - std::vector m_meshArray; - public: Scene(); ~Scene(); diff --git a/source/core/include/shader/dvc_shader_functions.h b/source/core/include/shader/dvc_shader_functions.h new file mode 100644 index 0000000..94e9839 --- /dev/null +++ b/source/core/include/shader/dvc_shader_functions.h @@ -0,0 +1,36 @@ +#pragma once + +#include "dvc_config.h" +#include "dvc_pre_declare.h" +#include "math/dvc_vector2.h" +#include "math/dvc_vector4.h" + +DV_CORE_BEGIN_NAMESPACE + +class shader +{ +public: + template + static inline int32_t step(const T& edge, const T& x) { + return (x >= edge) ? 1 : 0; + } + + template + static inline T max(const T& a, const T& b) { + return (a > b) ? a : b; + } + + template + static inline T pow(const T& base, const T& exponent) { + return std::pow(base, exponent); + } + + template + static inline T min(const T& a, const T& b) { + return (a < b) ? a : b; + } + + static DV_CORE_API fVector4 texture2D(const RenderPipe& pipe, const char* textName, 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 a254dfb..67c624c 100644 --- a/source/core/include/shader/dvc_shader_interface.h +++ b/source/core/include/shader/dvc_shader_interface.h @@ -2,6 +2,7 @@ #include "dvc_config.h" #include "dvc_pre_declare.h" +#include "math/dvc_vector2.h" #include "math/dvc_vector3.h" #include "math/dvc_vector4.h" #include "device/dvc_vertex_desc.h" @@ -15,7 +16,7 @@ public: virtual const VertexDesc& getOutputVertexDesc() const = 0; typedef std::function SetInputAttributeFunc; - virtual void prepare(const UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) = 0; + virtual void prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) = 0; virtual void process(size_t index, void* outputVertex, fVector3& vertexWorldPos) = 0; }; @@ -26,7 +27,7 @@ public: virtual const char* getName(void) const = 0; typedef std::function SetInputAttributeFunc; - virtual void prepare(const UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) = 0; + virtual void prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) = 0; virtual void process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) = 0; }; diff --git a/source/core/source/asset/dvc_image.cpp b/source/core/source/asset/dvc_image.cpp new file mode 100644 index 0000000..35ba6b1 --- /dev/null +++ b/source/core/source/asset/dvc_image.cpp @@ -0,0 +1,79 @@ +#include "asset/dvc_image.h" +#include "math/dvc_math_util.h" +#include "device/dvc_device.h" +#include "device/dvc_buffer.h" + +DV_CORE_BEGIN_NAMESPACE + +Image::Image(int32_t id) + : m_id(id) +{ +} + +Image::~Image() +{ +} + +void Image::clear() +{ + m_width = 0; + m_height = 0; + m_pixelFormat = PF_UNKNOWN; + m_pixelByteSize = 0; + m_pixelBuffer = nullptr; +} + +fVector4 Image::getPixel(int32_t u, int32_t v) const +{ + if(u<0 || u>=m_width || v<0 || v>=m_height) + { + return fVector4::ZERO; + } + + const uint8_t* p = (const uint8_t*)(m_pixelBuffer->ptr(0)) + (v * m_width + u) * m_pixelByteSize; + switch (m_pixelFormat) + { + case PF_UINT8_ALPHA: + return fVector4(0, 0, 0, p[0] / 255.0f); + case PF_UINT8_RGB: + return fVector4(p[0] / 255.0f, p[1] / 255.0f, p[2] / 255.0f, 1.0f); + case PF_UINT8_RGBA: + return fVector4(p[0] / 255.0f, p[1] / 255.0f, p[2] / 255.0f, p[3] / 255.0f); + case PF_FLOAT32_ALPHA: + return fVector4(0, 0, 0, *((const float*)p)); + case PF_FLOAT32_RGB: + { + const float* fp = (const float*)p; + return fVector4(fp[0], fp[1], fp[2], 1.0f); + } + case PF_FLOAT32_RGBA: + { + const float* fp = (const float*)p; + return fVector4(fp[0], fp[1], fp[2], fp[3]); + } + default: + return fVector4::ZERO; + } +} + +bool Image::initWithPixelData(Device& device, int32_t width, int32_t height, + PixelFormat pixelFormat, const uint8_t* data, size_t dataSize) +{ + clear(); + + size_t pixelByteSize = MathUtil::PixelSize(pixelFormat); + if (dataSize != pixelByteSize*width*height) + { + return false; + } + + m_width = width; + m_height = height; + m_pixelFormat = pixelFormat; + m_pixelByteSize = pixelByteSize; + m_pixelBuffer = device.newDeviceBuffer(dataSize); + memcpy(m_pixelBuffer->ptr(0), data, dataSize); + return true; +} + +DV_CORE_END_NAMESPACE diff --git a/source/core/source/asset/dvc_material.cpp b/source/core/source/asset/dvc_material.cpp new file mode 100644 index 0000000..ea2075c --- /dev/null +++ b/source/core/source/asset/dvc_material.cpp @@ -0,0 +1,24 @@ +#include "asset/dvc_material.h" +#include "shader/dvc_shader_interface.h" + +DV_CORE_BEGIN_NAMESPACE + +Material::Material(int32_t id) + : m_id(id) +{ +} + +Material::~Material() +{ +} + +bool Material::loadShader() +{ + ShaderFileInterface* shaderInterface = davinci::LoadShaderFile("TestShader.dll"); + m_vertexShader = shaderInterface->getVertexShader("test"); + m_pixelShader = shaderInterface->getPixelShader("test"); + + return true; +} + +DV_CORE_END_NAMESPACE diff --git a/source/core/source/asset/dvc_mesh.cpp b/source/core/source/asset/dvc_mesh.cpp index 14ffefc..08bb6d2 100644 --- a/source/core/source/asset/dvc_mesh.cpp +++ b/source/core/source/asset/dvc_mesh.cpp @@ -2,8 +2,9 @@ DV_CORE_BEGIN_NAMESPACE -Mesh::Mesh(const std::string& name) - : m_name(name) +Mesh::Mesh(int32_t id, const std::string& name) + : m_id(id) + , m_name(name) { } diff --git a/source/core/source/asset/dvc_texture.cpp b/source/core/source/asset/dvc_texture.cpp new file mode 100644 index 0000000..0362249 --- /dev/null +++ b/source/core/source/asset/dvc_texture.cpp @@ -0,0 +1,29 @@ +#include "asset/dvc_texture.h" +#include "math/dvc_math_util.h" +#include "device/dvc_device.h" +#include "device/dvc_buffer.h" +#include "asset/dvc_image.h" + +DV_CORE_BEGIN_NAMESPACE + +Texture::Texture(int32_t id) + : m_id(id) +{ +} + +Texture::~Texture() +{ +} + +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(); + + return m_image->getPixel(u, v); +} + +DV_CORE_END_NAMESPACE diff --git a/source/core/source/device/dvc_buffer.cpp b/source/core/source/device/dvc_buffer.cpp index f75a0d8..3632e2e 100644 --- a/source/core/source/device/dvc_buffer.cpp +++ b/source/core/source/device/dvc_buffer.cpp @@ -2,14 +2,9 @@ DV_CORE_BEGIN_NAMESPACE -DeviceBuffer::DeviceBuffer() - : m_ptr(nullptr) - , m_length(0) -{ -} - -DeviceBuffer::DeviceBuffer(size_t _length) - : m_ptr(nullptr) +DeviceBuffer::DeviceBuffer(int32_t id, size_t _length) + : m_id(id) + , m_ptr(nullptr) , m_length(0) { init(_length); diff --git a/source/core/source/device/dvc_buffer_accessor.cpp b/source/core/source/device/dvc_buffer_accessor.cpp index 09dcc47..2a25632 100644 --- a/source/core/source/device/dvc_buffer_accessor.cpp +++ b/source/core/source/device/dvc_buffer_accessor.cpp @@ -3,8 +3,9 @@ DV_CORE_BEGIN_NAMESPACE -DeviceBufferAccessor::DeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset, size_t count, ComponentType componentType, DataType dataType) - : m_bufferView(bufferView) +DeviceBufferAccessor::DeviceBufferAccessor(int32_t id, DeviceBufferViewConstPtr bufferView, size_t offset, size_t count, ComponentType componentType, DataType dataType) + : m_id(id) + , m_bufferView(bufferView) , m_offset(offset) , m_count(count) , m_componentType(componentType) diff --git a/source/core/source/device/dvc_buffer_view.cpp b/source/core/source/device/dvc_buffer_view.cpp index a736893..bf4c6bd 100644 --- a/source/core/source/device/dvc_buffer_view.cpp +++ b/source/core/source/device/dvc_buffer_view.cpp @@ -2,8 +2,9 @@ DV_CORE_BEGIN_NAMESPACE -DeviceBufferView::DeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride) - : m_buffer(buffer) +DeviceBufferView::DeviceBufferView(int32_t id, DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride) + : m_id(id) + , m_buffer(buffer) , m_offset(offset) , m_length(length) , m_stride(stride) diff --git a/source/core/source/device/dvc_device.cpp b/source/core/source/device/dvc_device.cpp new file mode 100644 index 0000000..0ac5c85 --- /dev/null +++ b/source/core/source/device/dvc_device.cpp @@ -0,0 +1,155 @@ +#include "device/dvc_device.h" +#include "device/dvc_buffer.h" +#include "device/dvc_buffer_view.h" +#include "device/dvc_buffer_accessor.h" +#include "asset/dvc_mesh.h" +#include "asset/dvc_image.h" +#include "asset/dvc_texture.h" +#include "asset/dvc_material.h" + +DV_CORE_BEGIN_NAMESPACE + +Device::Device() +{ +} + +Device::~Device() +{ + clear(); +} + +void Device::clear(void) +{ + //clear assets + m_meshAssetMap.clear(); + m_imageAssetMap.clear(); + + //clear device resources + m_bufferAccessorMap.clear(); + m_bufferViewMap.clear(); + m_deviceBufferMap.clear(); +} + +DeviceBufferPtr Device::newDeviceBuffer(size_t length) +{ + DeviceBufferPtr deviceBuffer = std::make_shared(nextID(), length); + + m_deviceBufferMap.insert(std::make_pair(deviceBuffer->getID(), deviceBuffer)); + return deviceBuffer; +} + +DeviceBufferConstPtr Device::getDeviceBuffer(int32_t id) const +{ + std::map::const_iterator it = m_deviceBufferMap.find(id); + if(it==m_deviceBufferMap.end()) + { + return nullptr; + } + return it->second; +} + +DeviceBufferViewPtr Device::newDeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride) +{ + DeviceBufferViewPtr deviceBufferView = std::make_shared(nextID(), buffer, offset, length, stride); + m_bufferViewMap.insert(std::make_pair(deviceBufferView->getID(), deviceBufferView)); + return deviceBufferView; +} + +DeviceBufferViewPtr Device::getDeviceBufferView(int32_t id) const +{ + std::map::const_iterator it = m_bufferViewMap.find(id); + if(it==m_bufferViewMap.end()) + { + return nullptr; + } + return it->second; +} + +DeviceBufferAccessorPtr Device::newDeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset, + size_t count, ComponentType componentType, DataType dataType) +{ + DeviceBufferAccessorPtr deviceBufferAccessor = std::make_shared( + nextID(), bufferView, offset, count, componentType, dataType); + m_bufferAccessorMap.insert(std::make_pair(deviceBufferAccessor->getID(), deviceBufferAccessor)); + return deviceBufferAccessor; +} + +DeviceBufferAccessorPtr Device::getDeviceBufferAccessor(int32_t id) const +{ + std::map::const_iterator it = m_bufferAccessorMap.find(id); + if(it==m_bufferAccessorMap.end()) + { + return nullptr; + } + return it->second; +} + +MeshPtr Device::newMeshAsset(const std::string& name) +{ + MeshPtr meshPtr = std::make_shared(nextID(), name); + m_meshAssetMap.insert(std::make_pair(meshPtr->getID(), meshPtr)); + return meshPtr; +} + +MeshPtr Device::getMesh(int32_t id) const +{ + std::map::const_iterator it = m_meshAssetMap.find(id); + if(it==m_meshAssetMap.end()) + { + return nullptr; + } + return it->second; +} + +ImagePtr Device::newImageAsset() +{ + ImagePtr imagePtr = std::make_shared(nextID()); + m_imageAssetMap.insert(std::make_pair(imagePtr->getID(), imagePtr)); + return imagePtr; +} + +ImagePtr Device::getImage(int32_t id) const +{ + std::map::const_iterator it = m_imageAssetMap.find(id); + if(it==m_imageAssetMap.end()) + { + return nullptr; + } + return it->second; +} + +TexturePtr Device::newTextureAsset() +{ + TexturePtr texturePtr = std::make_shared(nextID()); + m_textureAssetMap.insert(std::make_pair(texturePtr->getID(), texturePtr)); + return texturePtr; +} + +TexturePtr Device::getTexture(int32_t id) const +{ + std::map::const_iterator it = m_textureAssetMap.find(id); + if(it==m_textureAssetMap.end()) + { + return nullptr; + } + return it->second; +} + +MaterialPtr Device::newMaterialAsset() +{ + MaterialPtr materialPtr = std::make_shared(nextID()); + m_materialAssetMap.insert(std::make_pair(materialPtr->getID(), materialPtr)); + return materialPtr; +} + +MaterialPtr Device::getMaterial(int32_t id) const +{ + std::map::const_iterator it = m_materialAssetMap.find(id); + if(it==m_materialAssetMap.end()) + { + return nullptr; + } + return it->second; +} + +DV_CORE_END_NAMESPACE diff --git a/source/core/source/math/dvc_math_util.cpp b/source/core/source/math/dvc_math_util.cpp index 73e7af0..06cc4e5 100644 --- a/source/core/source/math/dvc_math_util.cpp +++ b/source/core/source/math/dvc_math_util.cpp @@ -89,6 +89,35 @@ size_t MathUtil::DataTypeSize(DataType dt) return 0; } +size_t MathUtil::PixelSize(PixelFormat pixelFormat) +{ + switch (pixelFormat) + { + case PixelFormat::PF_UINT8_ALPHA: + return 1; + case PixelFormat::PF_UINT8_RGB: + return 3; + case PixelFormat::PF_UINT8_RGBA: + return 4; + case PixelFormat::PF_UINT16_ALPHA: + return 2; + case PixelFormat::PF_UINT16_RGB: + return 6; + case PixelFormat::PF_UINT16_RGBA: + return 8; + case PixelFormat::PF_FLOAT32_ALPHA: + return 4; + case PixelFormat::PF_FLOAT32_RGB: + return 12; + case PixelFormat::PF_FLOAT32_RGBA: + return 16; + default: + break; + } + assert(false); + return 0; +} + float MathUtil::unitRandom(void) { return float(rand()) / RAND_MAX; diff --git a/source/core/source/math/dvc_vector4.cpp b/source/core/source/math/dvc_vector4.cpp index 3642a11..72c117b 100644 --- a/source/core/source/math/dvc_vector4.cpp +++ b/source/core/source/math/dvc_vector4.cpp @@ -4,6 +4,11 @@ DV_CORE_BEGIN_NAMESPACE const fVector4 fVector4::ZERO(0, 0, 0, 0); +const fVector4 fVector4::ONE(1, 1, 1, 1); +const fVector4 fVector4::UNIT_X(1, 0, 0, 0); +const fVector4 fVector4::UNIT_Y(0, 1, 0, 0); +const fVector4 fVector4::UNIT_Z(0, 0, 1, 0); +const fVector4 fVector4::UNIT_W(0, 0, 0, 1); const fVector4 fVector4::WHITE(1, 1, 1, 1); const fVector4 fVector4::BLACK(0, 0, 0, 1); const fVector4 fVector4::RED(1, 0, 0, 1); diff --git a/source/core/source/pipe/dvc_render_pipe.cpp b/source/core/source/pipe/dvc_render_pipe.cpp index 15fa0c2..b2e27e3 100644 --- a/source/core/source/pipe/dvc_render_pipe.cpp +++ b/source/core/source/pipe/dvc_render_pipe.cpp @@ -8,7 +8,9 @@ DV_CORE_BEGIN_NAMESPACE -RenderPipe::RenderPipe() +RenderPipe::RenderPipe(Device& device) + : m_device(device) + , m_aaTech(AntiAliasingTech::AA_None) { } @@ -31,11 +33,11 @@ void RenderPipe::process(RenderTarget& renderTarget) //Vertex shader VertexShaderStep VS; - VS.process(m_uniformBuffer, IA.m_nodes); + VS.process(*this, IA.m_nodes); //Pixel shader RasterizerStep rasterizerStep; - rasterizerStep.process(VS.m_nodes, m_aaTech, renderTarget.getWidth(), renderTarget.getHeight()); + rasterizerStep.process(*this, VS.m_nodes, m_aaTech, renderTarget.getWidth(), renderTarget.getHeight()); //Output Merge OutputMergerStep OM; diff --git a/source/core/source/pipe/dvc_render_step1_vertex_shader.cpp b/source/core/source/pipe/dvc_render_step1_vertex_shader.cpp index 94d8cc1..4b1ab0b 100644 --- a/source/core/source/pipe/dvc_render_step1_vertex_shader.cpp +++ b/source/core/source/pipe/dvc_render_step1_vertex_shader.cpp @@ -2,13 +2,14 @@ #include "shader/dvc_shader_interface.h" #include "device/dvc_buffer_accessor.h" #include "math/dvc_math_util.h" +#include "asset/dvc_material.h" DV_CORE_BEGIN_NAMESPACE -void VertexShaderStep::process(UniformBuffer& uniformBuffer, const InputAssemberStep::NodeArray& assembedNodeArray) +void VertexShaderStep::process(RenderPipe& pipe, const InputAssemberStep::NodeArray& assembedNodeArray) { - ShaderFileInterface* shaderInterface = davinci::LoadShaderFile("TestShader.dll"); - VertexShaderInterface* standardVS = shaderInterface->getVertexShader("test"); + Device& device = pipe.getDevice(); + UniformBuffer& uniformBuffer = pipe.getUniformBuffer(); const fVector3* eyePos = (const fVector3*)(uniformBuffer.getAttribute("camera:eye_pos")); const fMatrix4* cameraView = (const fMatrix4*)(uniformBuffer.getAttribute("camera:view_mat")); @@ -27,7 +28,8 @@ void VertexShaderStep::process(UniformBuffer& uniformBuffer, const InputAssember fMatrix4 worldInverseTrans = inputNode.transform.inverse().transpose(); uniformBuffer.setAttribute("self:world_transform_inverse_trans", worldInverseTrans); - standardVS->prepare(&uniformBuffer, + VertexShaderInterface* vs = inputNode.material->getVertexShader(); + vs->prepare(&pipe, [inputNode](VertexElementType type) -> DeviceBufferAccessorConstPtr { InputAssemberStep::PrimitiveAttributes::const_iterator it = inputNode.vertexData.find(type); @@ -42,9 +44,10 @@ void VertexShaderStep::process(UniformBuffer& uniformBuffer, const InputAssember size_t vertexCounts = inputNode.indices->getCount(); Node outputNode; + outputNode.material = inputNode.material; outputNode.vertexCounts = vertexCounts; - outputNode.vertexDesc = standardVS->getOutputVertexDesc(); - outputNode.vertexBuf = std::make_shared(outputNode.vertexDesc.vertexByteSize() * vertexCounts); + outputNode.vertexDesc = vs->getOutputVertexDesc(); + outputNode.vertexBuf = device.newDeviceBuffer(outputNode.vertexDesc.vertexByteSize() * vertexCounts); outputNode.depth.resize(vertexCounts); outputNode.invZ.resize(vertexCounts); @@ -54,7 +57,7 @@ void VertexShaderStep::process(UniformBuffer& uniformBuffer, const InputAssember fVector3 vertexWorldPos; void* outputVertexMemory = outputNode.vertexBuf->ptr(i* outputNode.vertexDesc.vertexByteSize()); - standardVS->process(index, outputVertexMemory, vertexWorldPos); + vs->process(index, outputVertexMemory, vertexWorldPos); //TODO: clip diff --git a/source/core/source/pipe/dvc_render_step2_rasterizer.cpp b/source/core/source/pipe/dvc_render_step2_rasterizer.cpp index d4cca21..42af3a7 100644 --- a/source/core/source/pipe/dvc_render_step2_rasterizer.cpp +++ b/source/core/source/pipe/dvc_render_step2_rasterizer.cpp @@ -3,13 +3,17 @@ #include "device/dvc_buffer.h" #include "rasterizer/dvc_rasterizer_triangle.h" #include "math/dvc_math_util.h" +#include "asset/dvc_material.h" +#include "asset/dvc_texture.h" DV_CORE_BEGIN_NAMESPACE -void RasterizerStep::process(const VertexShaderStep::NodeArray& vsNodeArray, AntiAliasingTech aaTech, int32_t canvasWidth, int32_t canvasHeight) +void RasterizerStep::process(RenderPipe& pipe, const VertexShaderStep::NodeArray& vsNodeArray, AntiAliasingTech aaTech, int32_t canvasWidth, int32_t canvasHeight) { - ShaderFileInterface* shaderInterface = davinci::LoadShaderFile("TestShader.dll"); - PixelShaderInterface* pixelShader = shaderInterface->getPixelShader("test"); + //ShaderFileInterface* shaderInterface = davinci::LoadShaderFile("TestShader.dll"); + //PixelShaderInterface* pixelShader = shaderInterface->getPixelShader("test"); + Device& device = pipe.getDevice(); + UniformBuffer& uniformBuffer = pipe.getUniformBuffer(); fMatrix4 view_trans = fMatrix4::makeTrans(canvasWidth / 2.f, canvasHeight / 2.f, 0.f) * @@ -22,6 +26,19 @@ void RasterizerStep::process(const VertexShaderStep::NodeArray& vsNodeArray, Ant { 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); + size_t vertexSize = node.vertexDesc.vertexByteSize(); size_t POS_offset = node.vertexDesc.getElement(VET_POSITION)->offset; diff --git a/source/core/source/pipe/dvc_renderable_entity.cpp b/source/core/source/pipe/dvc_renderable_entity.cpp index b990816..50edf14 100644 --- a/source/core/source/pipe/dvc_renderable_entity.cpp +++ b/source/core/source/pipe/dvc_renderable_entity.cpp @@ -15,6 +15,7 @@ bool RenderableEntity::buildAssemberNode(InputAssemberStep::Node& node) const node.transform = m_worldTransform; node.vertexData = m_primitive->attributes; node.indices = m_primitive->indices; + node.material = m_primitive->material; return true; } diff --git a/source/core/source/scene/dvc_scene.cpp b/source/core/source/scene/dvc_scene.cpp index 3bbe388..bf43795 100644 --- a/source/core/source/scene/dvc_scene.cpp +++ b/source/core/source/scene/dvc_scene.cpp @@ -1,8 +1,5 @@ #include "scene/dvc_scene.h" #include "scene/dvc_scene_node.h" -#include "device/dvc_buffer.h" -#include "device/dvc_buffer_view.h" -#include "device/dvc_buffer_accessor.h" #include "scene/component/dvc_camera_component.h" DV_CORE_BEGIN_NAMESPACE @@ -33,79 +30,6 @@ void Scene::clear(void) m_rootSceneNode->clear(); } m_rootSceneNode = nullptr; - - //clear assets - m_meshArray.clear(); - - //clear device resources - m_bufferAccessorArray.clear(); - m_bufferViewArray.clear(); - m_deviceBufferArray.clear(); -} - -DeviceBufferPtr Scene::newDeviceBuffer() -{ - DeviceBufferPtr deviceBuffer = std::make_shared(); - m_deviceBufferArray.push_back(deviceBuffer); - return deviceBuffer; -} - -DeviceBufferConstPtr Scene::getDeviceBuffer(int32_t index) const -{ - if (index < 0 || index >= m_deviceBufferArray.size()) - { - return nullptr; - } - return m_deviceBufferArray[index]; -} - -DeviceBufferViewPtr Scene::newDeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride) -{ - DeviceBufferViewPtr deviceBufferView = std::make_shared(buffer, offset, length, stride); - m_bufferViewArray.push_back(deviceBufferView); - return deviceBufferView; -} - -DeviceBufferViewPtr Scene::getDeviceBufferView(int32_t index) const -{ - if (index < 0 || index >= m_bufferViewArray.size()) - { - return nullptr; - } - return m_bufferViewArray[index]; -} - -DeviceBufferAccessorPtr Scene::newDeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset, - size_t count, ComponentType componentType, DataType dataType) -{ - DeviceBufferAccessorPtr deviceBufferAccessor = std::make_shared( - bufferView, offset, count, componentType, dataType); - m_bufferAccessorArray.push_back(deviceBufferAccessor); - return deviceBufferAccessor; -} - -DeviceBufferAccessorPtr Scene::getDeviceBufferAccessor(int32_t index) const -{ - if (index < 0 || index >= m_bufferAccessorArray.size()) - { - return nullptr; - } - return m_bufferAccessorArray[index]; -} - -int32_t Scene::pushNewMeshAsset(MeshPtr mesh) -{ - m_meshArray.push_back(mesh); - return (int32_t)(m_meshArray.size()); -} - -MeshConstPtr Scene::getMesh(int32_t index) const -{ - if (index < 0 || index >= m_meshArray.size()) - { - return nullptr; - } - return m_meshArray[index]; } SceneNodePtr Scene::newSceneNode(const std::string& name) diff --git a/source/core/source/shader/dvc_shader_functions.cpp b/source/core/source/shader/dvc_shader_functions.cpp new file mode 100644 index 0000000..75006e4 --- /dev/null +++ b/source/core/source/shader/dvc_shader_functions.cpp @@ -0,0 +1,18 @@ +#include "shader/dvc_shader_functions.h" +#include "pipe/dvc_render_pipe.h" +#include "asset/dvc_texture.h" + +DV_CORE_BEGIN_NAMESPACE + +fVector4 shader::texture2D(const RenderPipe& pipe, const char* textName, 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; +} + +DV_CORE_END_NAMESPACE diff --git a/source/utils/include/scene/dvu_load_gltf.h b/source/utils/include/scene/dvu_load_gltf.h index 99946a4..05256cf 100644 --- a/source/utils/include/scene/dvu_load_gltf.h +++ b/source/utils/include/scene/dvu_load_gltf.h @@ -1,12 +1,13 @@ #pragma once #include "dvu_config.h" +#include "device/dvc_device.h" #include "scene/dvc_scene.h" DV_UTILS_BEGIN_NAMESPACE -bool DV_UTILS_API load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene); +bool DV_UTILS_API load_scene_from_gltf(const char* szSceneFiles, davinci::Device& device, davinci::Scene& scene); -bool DV_UTILS_API export_mesh_to_obj(const davinci::Scene& scene); +bool DV_UTILS_API export_mesh_to_obj(const davinci::Device& device, const davinci::Scene& scene); DV_UTILS_END_NAMESPACE diff --git a/source/utils/source/buffer/dvu_dump_buffer.cpp b/source/utils/source/buffer/dvu_dump_buffer.cpp index 89863ff..c97ab62 100644 --- a/source/utils/source/buffer/dvu_dump_buffer.cpp +++ b/source/utils/source/buffer/dvu_dump_buffer.cpp @@ -17,8 +17,10 @@ bool dump_pixel_buffer(const char* filename, int32_t width, int32_t height, case davinci::PF_UINT8_RGBA: color_type = PNG_COLOR_TYPE_RGB_ALPHA; break; case davinci::PF_FLOAT32_RGB: + case davinci::PF_UINT8_RGB: color_type = PNG_COLOR_TYPE_RGB; break; - case davinci::PF_FLOAT32_R: + case davinci::PF_FLOAT32_ALPHA: + case davinci::PF_UINT8_ALPHA: color_type = PNG_COLOR_TYPE_GRAY; break; default: //not support yet! @@ -54,9 +56,14 @@ bool dump_pixel_buffer(const char* filename, int32_t width, int32_t height, png_write_info(png_ptr, info_ptr); //integer pixelformat - if (davinci::PF_UINT8_RGBA == pixelFormat) { - for (int32_t y = 0; y < height; y++) { - const uint32_t* p = (const uint32_t*)pixelData + (height - 1 - y) * width; + if (davinci::PF_UINT8_RGBA == pixelFormat || + davinci::PF_UINT8_RGB == pixelFormat || + davinci::PF_UINT8_ALPHA == pixelFormat) + { + size_t pixelSize = davinci::MathUtil::PixelSize(pixelFormat); + for (int32_t y = 0; y < height; y++) + { + const uint8_t* p = (const uint8_t*)pixelData + (height - 1 - y) * width * pixelSize; png_write_rows(png_ptr, (png_bytepp)&p, 1); } } @@ -165,7 +172,7 @@ bool dump_render_target(const char* baseFilename, const davinci::RenderTarget& r _snprintf(szFileName, 256, "%s_depth.png", baseFilename); dump_pixel_buffer(szFileName, renderTarget.getWidth(), renderTarget.getHeight(), - &(depth_data[0]), davinci::PF_FLOAT32_R + &(depth_data[0]), davinci::PF_FLOAT32_ALPHA ); return true; diff --git a/source/utils/source/scene/dvu_load_gltf.cpp b/source/utils/source/scene/dvu_load_gltf.cpp index c1dce5e..21ae519 100644 --- a/source/utils/source/scene/dvu_load_gltf.cpp +++ b/source/utils/source/scene/dvu_load_gltf.cpp @@ -8,11 +8,18 @@ #include "scene/component/dvc_mesh_component.h" #include "scene/component/dvc_camera_component.h" #include "scene/component/dvc_light_component.h" +#include "device/dvc_device.h" #include "device/dvc_buffer_accessor.h" #include "asset/dvc_mesh.h" +#include "asset/dvc_image.h" +#include "asset/dvc_texture.h" +#include "asset/dvc_material.h" + #include "math/dvc_vector3.h" #include "math/dvc_vector4.h" +#include "buffer/dvu_dump_buffer.h" + #include #include @@ -94,6 +101,25 @@ davinci::PrimitiveType _getPrimitiveTypeFromGltf(int gltfModelType) } } +davinci::PixelFormat _getPixelFormatFromGltf(int32_t gltfComponent, int32_t gltfPixelType) +{ + if (gltfPixelType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) + { + if (gltfComponent == 1) return davinci::PF_UINT8_ALPHA; + else if (gltfComponent == 3) return davinci::PF_UINT8_RGB; + else if (gltfComponent == 4) return davinci::PF_UINT8_RGBA; + else return davinci::PF_UNKNOWN; + } + else if (gltfPixelType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) + { + if (gltfComponent == 1) return davinci::PF_UINT16_ALPHA; + else if (gltfComponent == 3) return davinci::PF_UINT16_RGB; + else if (gltfComponent == 4) return davinci::PF_UINT16_RGBA; + else return davinci::PF_UNKNOWN; + } + else return davinci::PF_UNKNOWN; +} + //from "POSITION/NORMAL/..." to VET_*** davinci::VertexElementType _getElementTypeFromGltf(const std::string& gltfElementType) { @@ -132,7 +158,8 @@ davinci::VertexElementType _getElementTypeFromGltf(const std::string& gltfElemen return davinci::VET_UNKNOWN; } -davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedIntegerType(davinci::Scene& scene, +davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedIntegerType( + davinci::Device& device, davinci::Scene& scene, davinci::DeviceBufferAccessorPtr inpuptBufferAccesser) { size_t elementCounts = inpuptBufferAccesser->getCount(); @@ -145,7 +172,7 @@ davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedInteg } davinci::DataType dataType = inpuptBufferAccesser->getDataType(); - davinci::DeviceBufferPtr newBuffer = scene.newDeviceBuffer(); + davinci::DeviceBufferPtr newBuffer = device.newDeviceBuffer(); size_t bufferSize = elementCounts * davinci::MathUtil::ComponentSize(davinci::ComponentType::CT_Float32) * davinci::MathUtil::DataTypeSize(dataType); newBuffer->init(bufferSize); @@ -254,12 +281,13 @@ davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedInteg } ); } - davinci::DeviceBufferViewPtr newBufferViewer = scene.newDeviceBufferView(newBuffer, 0, bufferSize, 0); - return scene.newDeviceBufferAccessor(newBufferViewer, 0, elementCounts, davinci::ComponentType::CT_Float32, dataType); + davinci::DeviceBufferViewPtr newBufferViewer = device.newDeviceBufferView(newBuffer, 0, bufferSize, 0); + return device.newDeviceBufferAccessor(newBufferViewer, 0, elementCounts, davinci::ComponentType::CT_Float32, dataType); } davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccessor( - davinci::Scene& scene, davinci::VertexElementType vetType, davinci::DeviceBufferAccessorPtr inpuptBufferAccesser) + davinci::Device& device, davinci::Scene& scene, + davinci::VertexElementType vetType, davinci::DeviceBufferAccessorPtr inpuptBufferAccesser) { if (!inpuptBufferAccesser) return nullptr; davinci::ComponentType componentType = inpuptBufferAccesser->getComponentType(); @@ -299,7 +327,7 @@ davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccesso } else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16) { - return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser); + return _createFloat32BufferAccessorFromNormalizedIntegerType(device, scene, inpuptBufferAccesser); } } break; @@ -320,7 +348,7 @@ davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccesso } else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16) { - return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser); + return _createFloat32BufferAccessorFromNormalizedIntegerType(device, scene, inpuptBufferAccesser); } } break; @@ -358,7 +386,7 @@ davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccesso } else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16) { - return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser); + return _createFloat32BufferAccessorFromNormalizedIntegerType(device, scene, inpuptBufferAccesser); } } break; @@ -379,47 +407,53 @@ bool _isSupportedIndicesType(const davinci::DeviceBufferAccessorConstPtr accesse return componentType == davinci::ComponentType::CT_Uint16 && dataType == davinci::DataType::DT_Scalar; } -bool _loadDeviceBuffer(const tinygltf::Model& gltfModel, davinci::Scene& scene) +bool _loadDeviceBuffer(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene) { for (size_t i = 0; i < gltfModel.buffers.size(); i++) { - const tinygltf::Buffer& gltfBuffer = gltfModel.buffers[i]; + tinygltf::Buffer& gltfBuffer = gltfModel.buffers[i]; - davinci::DeviceBufferPtr deviceBufferPtr = scene.newDeviceBuffer(); - deviceBufferPtr->init(gltfBuffer.data.size()); + davinci::DeviceBufferPtr deviceBufferPtr = device.newDeviceBuffer(gltfBuffer.data.size()); //fill data memcpy(deviceBufferPtr->ptr(0), gltfBuffer.data.data(), gltfBuffer.data.size()); + + //set davinci id + gltfBuffer.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)deviceBufferPtr->getID()))); } return true; } -bool _loadBufferView(const tinygltf::Model& gltfModel, davinci::Scene& scene) +bool _loadBufferView(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene) { for (size_t i = 0; i < gltfModel.bufferViews.size(); i++) { - const tinygltf::BufferView& gltfBufferView = gltfModel.bufferViews[i]; - int32_t bufferIndex = gltfBufferView.buffer; - davinci::DeviceBufferConstPtr deviceBufferPtr = scene.getDeviceBuffer(bufferIndex); + tinygltf::BufferView& gltfBufferView = gltfModel.bufferViews[i]; + tinygltf::Buffer& gltfBuffer = gltfModel.buffers[gltfBufferView.buffer]; + int32_t bufferID = gltfBuffer.extensions["DAVINCI_ID"].GetNumberAsInt(); + + davinci::DeviceBufferConstPtr deviceBufferPtr = device.getDeviceBuffer(bufferID); if (deviceBufferPtr == nullptr) { return false; } - davinci::DeviceBufferViewPtr bufferViewPtr = scene.newDeviceBufferView( + davinci::DeviceBufferViewPtr bufferViewPtr = device.newDeviceBufferView( deviceBufferPtr, gltfBufferView.byteOffset, gltfBufferView.byteLength, gltfBufferView.byteStride); + gltfBufferView.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)bufferViewPtr->getID()))); } return true; } -bool _loadBufferAccessor(const tinygltf::Model& gltfModel, davinci::Scene& scene) +bool _loadBufferAccessor(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene) { for (size_t i = 0; i < gltfModel.accessors.size(); i++) { - const tinygltf::Accessor& gltfAccessor = gltfModel.accessors[i]; + tinygltf::Accessor& gltfAccessor = gltfModel.accessors[i]; + tinygltf::BufferView& gltfBufferView = gltfModel.bufferViews[gltfAccessor.bufferView]; + int32_t bufferViewID = gltfBufferView.extensions["DAVINCI_ID"].GetNumberAsInt(); - int32_t bufferViewIndex = gltfAccessor.bufferView; - davinci::DeviceBufferViewPtr deviceBufferView = scene.getDeviceBufferView(bufferViewIndex); + davinci::DeviceBufferViewPtr deviceBufferView = device.getDeviceBufferView(bufferViewID); if (deviceBufferView==nullptr) { return false; @@ -437,20 +471,22 @@ bool _loadBufferAccessor(const tinygltf::Model& gltfModel, davinci::Scene& scene return false; } - davinci::DeviceBufferAccessorPtr bufferAccessor = scene.newDeviceBufferAccessor( + davinci::DeviceBufferAccessorPtr bufferAccessor = device.newDeviceBufferAccessor( deviceBufferView, gltfAccessor.byteOffset, gltfAccessor.count, componentType, dataType); + gltfAccessor.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)bufferAccessor->getID()))); } return true; } -bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene) +bool _loadMesh(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene) { for (size_t i=0; i(gltfMesh.name); + davinci::MeshPtr mesh = device.newMeshAsset(gltfMesh.name); + gltfMesh.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)mesh->getID()))); for (size_t j=0; jnewPrimitive(); primitive->type = _getPrimitiveTypeFromGltf(gltfPrimitive.mode); + //apply attributes std::map::const_iterator it(gltfPrimitive.attributes.begin()); std::map::const_iterator itEnd(gltfPrimitive.attributes.end()); for (; it != itEnd; it++) { davinci::VertexElementType vetype = _getElementTypeFromGltf(it->first); + int32_t gltfAccessIndex = it->second; + if (gltfAccessIndex < 0 || gltfAccessIndex >= gltfModel.accessors.size()) + { + return false; + } + tinygltf::Accessor& gltfAccessor = gltfModel.accessors[gltfAccessIndex]; + int32_t accessorID = gltfAccessor.extensions["DAVINCI_ID"].GetNumberAsInt(); - int32_t gltfAccessor = it->second; - davinci::DeviceBufferAccessorPtr deviceBufferAccessor = scene.getDeviceBufferAccessor(gltfAccessor); + davinci::DeviceBufferAccessorPtr deviceBufferAccessor = device.getDeviceBufferAccessor(accessorID); if (deviceBufferAccessor ==nullptr) { return false; } //convert to supported primitive buffer(float32) - deviceBufferAccessor = _createSupportedPrimitiveAttributeBufferAccessor(scene, vetype, deviceBufferAccessor); + deviceBufferAccessor = _createSupportedPrimitiveAttributeBufferAccessor(device, scene, vetype, deviceBufferAccessor); if (deviceBufferAccessor == nullptr) { return false; @@ -482,7 +525,14 @@ bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene) primitive->attributes.insert(std::make_pair(vetype, deviceBufferAccessor)); } - davinci::DeviceBufferAccessorPtr indicesBufferAccessor = scene.getDeviceBufferAccessor(gltfPrimitive.indices); + //apply indices + if (gltfPrimitive.indices < 0 || gltfPrimitive.indices >= gltfModel.accessors.size()) + { + return false; + } + tinygltf::Accessor& gltfIndicesAccessor = gltfModel.accessors[gltfPrimitive.indices]; + int32_t accessorID = gltfIndicesAccessor.extensions["DAVINCI_ID"].GetNumberAsInt(); + davinci::DeviceBufferAccessorPtr indicesBufferAccessor = device.getDeviceBufferAccessor(accessorID); if (indicesBufferAccessor == nullptr) { return false; @@ -494,9 +544,113 @@ bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene) } primitive->indices = indicesBufferAccessor; + + //apply material + if (gltfPrimitive.material >= 0 && gltfPrimitive.material < gltfModel.materials.size()) + { + tinygltf::Material& gltfMaterial = gltfModel.materials[gltfPrimitive.material]; + int32_t materialID = gltfMaterial.extensions["DAVINCI_ID"].GetNumberAsInt(); + davinci::MaterialPtr materialPtr = device.getMaterial(materialID); + if (materialPtr == nullptr) + { + return false; + } + primitive->material = materialPtr; + } + } + } + return true; +} + +bool _loadTexture(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene) +{ + for (size_t i = 0; i < gltfModel.images.size(); i++) + { + tinygltf::Image& gltfImage = gltfModel.images[i]; + + davinci::ImagePtr imagePtr = device.newImageAsset(); + imagePtr->initWithPixelData(device, gltfImage.width, gltfImage.height, + _getPixelFormatFromGltf(gltfImage.component, gltfImage.pixel_type), gltfImage.image.data(), gltfImage.image.size()); + + gltfImage.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)imagePtr->getID()))); + } + + for (size_t i = 0; i < gltfModel.textures.size(); i++) + { + tinygltf::Texture& gltfTexture = gltfModel.textures[i]; + if (gltfTexture.source < 0 || gltfTexture.source >= (int)gltfModel.images.size()) + { + continue; + } + tinygltf::Image& gltfImage = gltfModel.images[gltfTexture.source]; + if (gltfImage.width <= 0 || gltfImage.height <= 0 || gltfImage.component <= 0 || gltfImage.component > 4) + { + continue; } - scene.pushNewMeshAsset(mesh); + int32_t imageID = gltfImage.extensions["DAVINCI_ID"].GetNumberAsInt(); + davinci::ImagePtr imagePtr = device.getImage(imageID); + + davinci::TexturePtr texturePtr = device.newTextureAsset(); + texturePtr->setImage(imagePtr); + + gltfTexture.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)texturePtr->getID()))); + } + return true; +} + +davinci::TexturePtr _getTexturePtr(tinygltf::Model& gltfModel, davinci::Device& device, int gltfTextureIndex) +{ + if (gltfTextureIndex < 0 || gltfTextureIndex >= gltfModel.textures.size()) return nullptr; + tinygltf::Texture& gltfTexture = gltfModel.textures[gltfTextureIndex]; + + int32_t textureID = gltfTexture.extensions["DAVINCI_ID"].GetNumberAsInt(); + return device.getTexture(textureID); +} + +davinci::fVector4 _getVector4FromDoubleArray(const std::vector& doubleArray) +{ + if (doubleArray.size() != 4) return davinci::fVector4::ONE; + return davinci::fVector4((float32_t)doubleArray[0], (float32_t)doubleArray[1], (float32_t)doubleArray[2], (float32_t)doubleArray[3]); +} + +bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene) +{ + for (size_t i = 0; i < gltfModel.materials.size(); i++) + { + tinygltf::Material& gltfMaterial = gltfModel.materials[i]; + + //create new material + davinci::MaterialPtr materialPtr = device.newMaterialAsset(); + + //apply matellic roughness info + const tinygltf::PbrMetallicRoughness& pbrMetllicRoughness = gltfMaterial.pbrMetallicRoughness; + 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 + ); + + //set normal texture info + materialPtr->setNormalTexture( + _getTexturePtr(gltfModel, device, gltfMaterial.normalTexture.index), + gltfMaterial.normalTexture.texCoord, + gltfMaterial.normalTexture.scale + ); + + //load shader + if (!(materialPtr->loadShader())) + { + return false; + } + + gltfMaterial.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)materialPtr->getID()))); } return true; } @@ -558,8 +712,7 @@ bool _loadLightExternsion(const tinygltf::Model& gltfModel, std::vector& lightsArray) +bool _loadSceneNodes(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene, const std::vector& lightsArray) { std::vector> sceneNodeArray; @@ -599,7 +752,10 @@ bool _loadSceneNodes(const tinygltf::Model& gltfModel, davinci::Scene& scene, co //add mesh component if (gltfNode.mesh != -1) { - davinci::MeshConstPtr meshPtr = scene.getMesh(gltfNode.mesh); + tinygltf::Mesh& gltfMesh = gltfModel.meshes[gltfNode.mesh]; + int32_t meshID = gltfMesh.extensions["DAVINCI_ID"].GetNumberAsInt(); + + davinci::MeshConstPtr meshPtr = device.getMesh(meshID); if (meshPtr) { davinci::MeshComponentPtr meshComponet = std::make_shared(sceneNode); @@ -707,7 +863,7 @@ bool _loadSceneNodes(const tinygltf::Model& gltfModel, davinci::Scene& scene, co return true; } -bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene) +bool load_scene_from_gltf(const char* szSceneFiles, davinci::Device& device, davinci::Scene& scene) { std::filesystem::path sceneFilePath = std::filesystem::path(szSceneFiles); @@ -718,7 +874,10 @@ bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene) } tinygltf::Model gltfModel; + tinygltf::TinyGLTF gltf_ctx; + gltf_ctx.SetPreserveImageChannels(true); + std::string err; std::string warn; bool ret = false; @@ -752,25 +911,37 @@ bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene) //dump to davinci scene //load device buffer - if (!_loadDeviceBuffer(gltfModel, scene)) + if (!_loadDeviceBuffer(gltfModel, device, scene)) { return false; } //load buffer view - if (!_loadBufferView(gltfModel, scene)) + if (!_loadBufferView(gltfModel, device, scene)) { return false; } //load buffer accessor - if (!_loadBufferAccessor(gltfModel, scene)) + if (!_loadBufferAccessor(gltfModel, device, scene)) + { + return false; + } + + //load texture + if (!_loadTexture(gltfModel, device, scene)) + { + return false; + } + + //load material + if (!_loadMaterial(gltfModel, device, scene)) { return false; } //load mesh - if (!_loadMesh(gltfModel, scene)) + if (!_loadMesh(gltfModel, device, scene)) { return false; } @@ -783,7 +954,7 @@ bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene) } //load scene nodes - if (!_loadSceneNodes(gltfModel, scene, lightDefine)) + if (!_loadSceneNodes(gltfModel, device, scene, lightDefine)) { return false; } @@ -799,13 +970,13 @@ bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene) } -bool export_mesh_to_obj(const davinci::Scene& scene) +bool export_mesh_to_obj(const davinci::Device& device, const davinci::Scene& scene) { - - int32_t meshCounts = scene.getMeshCounts(); +#if 0 + int32_t meshCounts = device.getMeshCounts(); for (int32_t meshIndex = 0; meshIndex < meshCounts; meshIndex++) { - davinci::MeshConstPtr mesh = scene.getMesh(meshIndex); + davinci::MeshConstPtr mesh = device.getMesh(meshIndex); const std::string& meshName = mesh->getName(); if (meshName.empty()) { @@ -918,6 +1089,7 @@ bool export_mesh_to_obj(const davinci::Scene& scene) fclose(fp); } +#endif return true; } diff --git a/test/shader/test_pixel_shader.cpp b/test/shader/test_pixel_shader.cpp index be534ab..82404d9 100644 --- a/test/shader/test_pixel_shader.cpp +++ b/test/shader/test_pixel_shader.cpp @@ -1,79 +1,130 @@ #include "test_pixel_shader.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" + +using namespace davinci; const char* TestPixelShader::getName(void) const { return "test"; } -void TestPixelShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) +void TestPixelShader::prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) { - + m_pipe = pipe; } -void TestPixelShader::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color) +void TestPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) { - //const davinci::VertexDesc::Element* v_position_Element = veretexDesc.getElement("v_position"); - //const davinci::fVector3* v_position = (const davinci::fVector3*)(pixelData + (v_position_Element->offset / sizeof(float32_t))); + 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))); + const VertexDesc::Element* v_normal_Element = veretexDesc.getElement("v_normal"); + const fVector3* v_normal = (const fVector3*)(pixelData + (v_normal_Element->offset / sizeof(float32_t))); - //const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv"); - //const float32_t* v_uv = (const float32_t*)(pixelData + (v_uv_Element->offset / sizeof(float32_t))); + const VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv"); + const fVector2* v_uv = (const fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t))); - const davinci::VertexDesc::Element* v_toCamera_Element = veretexDesc.getElement("v_toCamera"); - const davinci::fVector3* v_toCamera = (const davinci::fVector3*)(pixelData + (v_toCamera_Element->offset / sizeof(float32_t))); + const VertexDesc::Element* v_toCamera_Element = veretexDesc.getElement("v_toCamera"); + const fVector3* v_toCamera = (const fVector3*)(pixelData + (v_toCamera_Element->offset / sizeof(float32_t))); - const davinci::VertexDesc::Element* v_toLight_Element = veretexDesc.getElement("v_toLight"); - const davinci::fVector3* v_toLight = (const davinci::fVector3*)(pixelData + (v_toLight_Element->offset / sizeof(float32_t))); + const VertexDesc::Element* v_toLight_Element = veretexDesc.getElement("v_toLight"); + const fVector3* v_toLight = (const fVector3*)(pixelData + (v_toLight_Element->offset / sizeof(float32_t))); - const davinci::VertexDesc::Element* v_color0_Element = veretexDesc.getElement("v_color0"); - const davinci::fVector3* v_color0 = (const davinci::fVector3*)(pixelData + (v_color0_Element->offset / sizeof(float32_t))); + const VertexDesc::Element* v_tangent_Element = veretexDesc.getElement("v_tangent"); + const fVector3* v_tangent = (const fVector3*)(pixelData + (v_tangent_Element->offset / sizeof(float32_t))); + + const VertexDesc::Element* v_binormal_Element = veretexDesc.getElement("v_binormal"); + const fVector3* v_binormal = (const fVector3*)(pixelData + (v_binormal_Element->offset / sizeof(float32_t))); + + const VertexDesc::Element* v_wposition_Element = veretexDesc.getElement("v_wposition"); + const fVector3* v_wposition = (const fVector3*)(pixelData + (v_wposition_Element->offset / sizeof(float32_t))); + + fMatrix3 tbnMatrix = fMatrix3( + v_tangent->x, v_binormal->x, v_normal->x, + v_tangent->y, v_binormal->y, v_normal->y, + v_tangent->z, v_binormal->z, v_normal->z); + + davinci::fVector3 light0_position = davinci::fVector3::ZERO; + if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:position"))) + { + light0_position = *p_light0_position; + } + + //davinci::fVector3 camera_position = davinci::fVector3::ZERO; + //if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(uniformBuffer.getAttribute("camera:eye_pos"))) + //{ + // camera_position = *p_camera_position; + //} + + fVector3 lp = light0_position - *v_wposition; + fVector3 vLight = lp.normalise() * tbnMatrix; + + fVector3 u_ambientColor = fVector3(0.2, 0.2, 0.2); + + fVector3 u_matColor = fVector3(1.0, 1.0, 1.0); + + 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); + + 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); + + //---------------- + fVector3 vN = fVector3::UNIT_Z;// u_normalColor.xyz() * 2.0f - fVector3::ONE; + vN.normalise(); //vector vL = normalize(v_toLight); - davinci::fVector3 vL = *v_toLight; + fVector3 vL =vLight; vL.normalise(); //vector vV = normalize(v_toCamera); - davinci::fVector3 vV = *v_toCamera; + fVector3 vV = *v_toCamera; vV.normalise(); - //vector vN = normalize(v_normal); - davinci::fVector3 vN = *v_normal; - vN.normalise(); + float ndotl = vL.dotProduct(vN); //vector vR = 2 * dot(vL, vN)*vN - vL; - davinci::fVector3 vR = 2 * vL.dotProduct(vN) * vN - vL; + fVector3 vR = 2 * ndotl * vN - vL; + + float rdotv = vR.dotProduct(vV); //vector vH = normalize(vL + vV); - davinci::fVector3 vH = vL + vV; - vH.normalise(); + //fVector3 vH = vL + vV; + //vH.normalise(); - davinci::fVector3 u_ambientColor = davinci::fVector3(0.2, 0.2, 0.2); - - davinci::fVector3 u_matColor = davinci::fVector3(0.7, 0.7, 0.7); - - davinci::fVector3 u_lightColor = davinci::fVector3(1.0, 1.0, 1.0); //color colAmbient = u_ambientColor*u_matColor; - davinci::fVector3 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); + //float32_t ndotl = std::max(vN.dotProduct(vL), 0.0f); //color colDiff = ndotl * u_matColor * u_lightColor; - davinci::fVector3 colDiff = ndotl * u_matColor * u_lightColor; + fVector3 colDiff = shader::max(0.0f, ndotl) * u_matColor* u_lightColor; - //float spec = max(dot(vN, vH), 0); - float32_t spec = std::max(vN.dotProduct(vH), 0.0f); + + //float32_t spec = std::max(vR.dotProduct(vV), 0.0f)*1.0; //color colSpec = step(0, ndotl) * pow(spec, 64) * u_matColor * u_lightColor; - davinci::fVector3 colSpec = (ndotl < 0.f ? 0.0 : 1.0) * std::powf(spec, 64.f) * 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; - davinci::fVector3 final = colAmbient +colDiff +colSpec; - //davinci::fVector3 final = *v_color0; + fVector3 final = fVector3::BLACK;// (colAmbient + colDiff + colSpec)* u_textureColor.xyz(); - //color = davinci::fVector4::RED; - color = davinci::fVector4(final.saturate(), 1.0f);// davinci::fVector4::RED; + final += colAmbient; + final += colDiff; + final += colSpec; + + final *= u_textureColor.xyz(); + //fVector3 final = (colAmbient +colDiff +colSpec)*u_textureColor.xyz(); + + //color = fVector4::RED; + color = fVector4(final.saturate(), 1.0f);// fVector4::RED; } const char* SolidColorPixelShader::getName(void) const @@ -81,12 +132,12 @@ const char* SolidColorPixelShader::getName(void) const return "solid"; } -void SolidColorPixelShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) +void SolidColorPixelShader::prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) { } -void SolidColorPixelShader::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color) +void SolidColorPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) { - color = davinci::fVector4::RED; + color = fVector4::RED; } diff --git a/test/shader/test_pixel_shader.h b/test/shader/test_pixel_shader.h index aad5cfa..ac2d44e 100644 --- a/test/shader/test_pixel_shader.h +++ b/test/shader/test_pixel_shader.h @@ -8,10 +8,12 @@ class TestPixelShader : public davinci::PixelShaderInterface public: virtual const char* getName(void) const; - virtual void prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc); + virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc); virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color); +private: + const davinci::RenderPipe* m_pipe = nullptr; }; class SolidColorPixelShader : public davinci::PixelShaderInterface @@ -19,7 +21,7 @@ class SolidColorPixelShader : public davinci::PixelShaderInterface public: virtual const char* getName(void) const; - virtual void prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc); + 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 0ba675b..ce9a3b3 100644 --- a/test/shader/test_vertex_shader.cpp +++ b/test/shader/test_vertex_shader.cpp @@ -3,6 +3,7 @@ #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" TestVertexShader::TestVertexShader() @@ -18,7 +19,9 @@ TestVertexShader::TestVertexShader() //output vector v_toLight = vector(0,0,0) m_outputVertexDesc.addElement("v_toLight", davinci::VET_UNKNOWN, davinci::CT_Float32, davinci::DT_Vec3); - m_outputVertexDesc.addElement("v_color0", davinci::VET_UNKNOWN, davinci::CT_Float32, davinci::DT_Vec3); + m_outputVertexDesc.addElement("v_tangent", davinci::VET_TANGENT, davinci::CT_Float32, davinci::DT_Vec3); + m_outputVertexDesc.addElement("v_binormal", davinci::VET_BINORMAL, davinci::CT_Float32, davinci::DT_Vec3); + m_outputVertexDesc.addElement("v_wposition", davinci::VET_UNKNOWN, davinci::CT_Float32, davinci::DT_Vec3); } TestVertexShader::~TestVertexShader() @@ -41,9 +44,9 @@ const davinci::VertexDesc& TestVertexShader::getOutputVertexDesc() const return m_outputVertexDesc; } -void TestVertexShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) +void TestVertexShader::prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) { - m_uniformBuffer = uniformBuffer; + m_pipe = pipe; //point a_position = vector(0, 0, 0) [[ string attribute="POSITION"]], m_POSITION = setInputAttributeFunc(davinci::VET_POSITION); @@ -52,7 +55,7 @@ void TestVertexShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetI //float a_uv[2] = {0,0} [[ string attribute="TEXCOORD_0"]], m_TEXCOORD_0 = setInputAttributeFunc(davinci::VET_TEXCOORD_0); // - m_COLOR_0 = setInputAttributeFunc(davinci::VET_COLOR_0); + m_TANGENT = setInputAttributeFunc(davinci::VET_TANGENT); } void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos) @@ -60,7 +63,9 @@ 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_COLOR0[] = { 0.f, 0.f, 0.f }; + static const float32_t DEFAULT_TANGENT[] = { 1.f, 0.f, 0.f }; + + const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); davinci::fVector3 a_position(m_POSITION ? (const float32_t*)(m_POSITION->getElement(index)) : DEFAULT_POSITION); @@ -68,28 +73,28 @@ 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_color_0(m_COLOR_0 ? (const float32_t*)(m_COLOR_0->getElement(index)) : DEFAULT_COLOR0); + davinci::fVector3 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*)(m_uniformBuffer->getAttribute("self:world_transform"))) + 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*)(m_uniformBuffer->getAttribute("camera:view_proj_mat"))) + 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; } davinci::fVector3 camera_position = davinci::fVector3::ZERO; - if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(m_uniformBuffer->getAttribute("camera:eye_pos"))) + if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(uniformBuffer.getAttribute("camera:eye_pos"))) { camera_position = *p_camera_position; } davinci::fVector3 light0_position = davinci::fVector3::ZERO; - if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(m_uniformBuffer->getAttribute("light0:position"))) + if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:position"))) { light0_position = *p_light0_position; } @@ -98,23 +103,30 @@ void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVecto davinci::fVector3 v_position = mat_camera_view_proj * temp1; davinci::fMatrix4 matNormal = davinci::fMatrix4::IDENTITY; - if (davinci::fMatrix4* p_mat_normal = (davinci::fMatrix4*)(m_uniformBuffer->getAttribute("self:world_transform_inverse_trans"))) + 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(); + 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_color_0 = a_color_0; + davinci::fMatrix3 tbnMatrix = davinci::fMatrix3( + v_tangent.x, v_binormal.x, v_normal.x, + v_tangent.y, v_binormal.y, v_normal.y, + v_tangent.z, v_binormal.z, v_normal.z); - davinci::fVector3 v_to_camera = (camera_position - temp1).normalise(); - davinci::fVector3 v_to_light = (light0_position - temp1).normalise(); + davinci::fVector3 v_to_camera = (camera_position - temp1).normalise() * tbnMatrix; + davinci::fVector3 v_to_light = (light0_position - temp1).normalise() * tbnMatrix; memcpy((char*)outputVertex + m_outputVertexDesc.getElement(0)->offset, &v_position, sizeof(davinci::fVector3)); memcpy((char*)outputVertex + m_outputVertexDesc.getElement(1)->offset, &v_normal, sizeof(davinci::fVector3)); memcpy((char*)outputVertex + m_outputVertexDesc.getElement(2)->offset, &a_uv, sizeof(float32_t) * 2); memcpy((char*)outputVertex + m_outputVertexDesc.getElement(3)->offset, &v_to_camera, sizeof(davinci::fVector3)); memcpy((char*)outputVertex + m_outputVertexDesc.getElement(4)->offset, &v_to_light, sizeof(davinci::fVector3)); - memcpy((char*)outputVertex + m_outputVertexDesc.getElement(5)->offset, &v_color_0, sizeof(davinci::fVector3)); + memcpy((char*)outputVertex + m_outputVertexDesc.getElement(5)->offset, &v_tangent, sizeof(davinci::fVector3)); + memcpy((char*)outputVertex + m_outputVertexDesc.getElement(6)->offset, &v_binormal, sizeof(davinci::fVector3)); + memcpy((char*)outputVertex + m_outputVertexDesc.getElement(7)->offset, &temp1, sizeof(davinci::fVector3)); vertexWorldPos = mat_world * a_position; } @@ -122,7 +134,6 @@ void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVecto StandardVertexShader::StandardVertexShader() { - m_uniformBuffer = nullptr; //output point v_position = point(0, 0, 0) [[ string attribute="POSITION"]], m_outputVertexDesc.addElement("v_position", davinci::VET_POSITION, davinci::CT_Float32, davinci::DT_Vec3); } @@ -147,9 +158,9 @@ const davinci::VertexDesc& StandardVertexShader::getOutputVertexDesc() const return m_outputVertexDesc; } -void StandardVertexShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) +void StandardVertexShader::prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) { - m_uniformBuffer = uniformBuffer; + m_pipe = pipe; //point a_position = vector(0, 0, 0) [[ string attribute="POSITION"]], m_POSITION = setInputAttributeFunc(davinci::VET_POSITION); @@ -159,16 +170,18 @@ void StandardVertexShader::process(size_t index, void* outputVertex, davinci::fV { static const float DEFAULT_POSITION[] = {0.f, 0.f, 0.f}; + const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer(); + davinci::fVector3 a_position(m_POSITION ? (const float32_t*)(m_POSITION->getElement(index)) : DEFAULT_POSITION); davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY; - if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(m_uniformBuffer->getAttribute("self:world_transform"))) + 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*)(m_uniformBuffer->getAttribute("camera:view_proj_mat"))) + 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; } diff --git a/test/shader/test_vertex_shader.h b/test/shader/test_vertex_shader.h index 4867943..c044704 100644 --- a/test/shader/test_vertex_shader.h +++ b/test/shader/test_vertex_shader.h @@ -13,16 +13,17 @@ public: virtual const davinci::VertexDesc& getOutputVertexDesc() const; virtual size_t getOutputVertexByteSize(void) const; - virtual void prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc); + virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc); virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos); private: - const davinci::UniformBuffer* m_uniformBuffer; + const davinci::RenderPipe* m_pipe = nullptr; + davinci::DeviceBufferAccessorConstPtr m_POSITION; davinci::DeviceBufferAccessorConstPtr m_NORMAL; davinci::DeviceBufferAccessorConstPtr m_TEXCOORD_0; - davinci::DeviceBufferAccessorConstPtr m_COLOR_0; + davinci::DeviceBufferAccessorConstPtr m_TANGENT; davinci::VertexDesc m_outputVertexDesc; @@ -37,11 +38,11 @@ public: virtual const char* getName(void) const; virtual const davinci::VertexDesc& getOutputVertexDesc() const; virtual size_t getOutputVertexByteSize(void) const; - virtual void prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc); + virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc); virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos); private: - const davinci::UniformBuffer* m_uniformBuffer = nullptr; + const davinci::RenderPipe* m_pipe = nullptr; davinci::DeviceBufferAccessorConstPtr m_POSITION; davinci::VertexDesc m_outputVertexDesc;