Compare commits

..
9 Commits
Author SHA1 Message Date
jinchao c9f617f1cc 设置缺省环境光照亮度 2025-12-06 16:14:36 +08:00
jinchao b0964dfe72 加入标准测试模型Monkey 2025-12-06 16:12:18 +08:00
jinchao 5d68961188 使用phong材质 2025-12-04 23:35:21 +08:00
jinchao 656ce63d55 在FragementShader的begin函数中准备输入的顶点数据偏移 2025-12-04 23:09:04 +08:00
jinchao 0cd230b577 PixelShader更名为FragementShader 2025-12-04 22:06:55 +08:00
jinchao 3c0e177905 重构材质系统 2025-12-03 23:38:08 +08:00
jinchao bf62257884 新建一个DeviceBuf时,必需指定内存大小 2025-11-19 16:32:31 +08:00
jinchao a0beb9eac7 11.15 2025-11-15 16:27:19 +08:00
jinchao 8db6f7b115 增加材质和纹理 2025-10-26 20:07:32 +08:00
81 changed files with 2221 additions and 948 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,6 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.3.47",
"generator":"Khronos glTF Blender I/O v5.0.21",
"version":"2.0"
},
"extensionsUsed":[
@@ -53,7 +53,7 @@
"translation":[
4.076245307922363,
5.903861999511719,
-1.0054539442062378
2.520627498626709
]
},
{
@@ -66,9 +66,9 @@
0.8937962055206299
],
"translation":[
4.200888156890869,
2.8001465797424316,
3.949000835418701
3.8443074226379395,
2.5564608573913574,
3.6128811836242676
]
},
{
@@ -88,6 +88,12 @@
"type":"perspective"
}
],
"materials":[
{
"doubleSided":true,
"name":"Material.002"
}
],
"meshes":[
{
"name":"Suzanne",
@@ -96,9 +102,11 @@
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
"TEXCOORD_0":2,
"TANGENT":3
},
"indices":3
"indices":4,
"material":0
}
]
}
@@ -107,67 +115,79 @@
{
"bufferView":0,
"componentType":5126,
"count":1966,
"count":3410,
"max":[
1.3671875,
0.984375,
0.8515625
1.325934886932373,
0.9392362236976624,
0.8223199844360352
],
"min":[
-1.3671875,
-0.984375,
-0.8515625
-1.325934886932373,
-0.9704862236976624,
-0.7782661318778992
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":1966,
"count":3410,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":1966,
"count":3410,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5126,
"count":3410,
"type":"VEC4"
},
{
"bufferView":4,
"componentType":5123,
"count":2904,
"count":11808,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":23592,
"byteLength":40920,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":23592,
"byteOffset":23592,
"byteLength":40920,
"byteOffset":40920,
"target":34962
},
{
"buffer":0,
"byteLength":15728,
"byteOffset":47184,
"byteLength":27280,
"byteOffset":81840,
"target":34962
},
{
"buffer":0,
"byteLength":5808,
"byteOffset":62912,
"byteLength":54560,
"byteOffset":109120,
"target":34962
},
{
"buffer":0,
"byteLength":23616,
"byteOffset":163680,
"target":34963
}
],
"buffers":[
{
"byteLength":68720,
"byteLength":187296,
"uri":"monkey.bin"
}
]
+1
View File
@@ -4,6 +4,7 @@
add_subdirectory(core)
add_subdirectory(utils)
add_subdirectory(console)
add_subdirectory(shaders)
#################
+21 -2
View File
@@ -11,8 +11,8 @@
#include "ui/ui_main.h"
#include "adapter/adapter_texture.h"
#include "device/dvc_render_target.h"
#include <scene/dvc_scene.h>
#include "device/dvc_device.h"
#include "scene/dvc_scene.h"
int32_t g_screenWidth = 2048;
int32_t g_screenHeight = 1024;
@@ -24,7 +24,12 @@ 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;
int32_t g_mousePosX = 0;
int32_t g_mousePosY = 0;
davinci::fVector4 g_mouseColor = davinci::fVector4::BLACK;
int32_t g_debugMaterialID = 0;
//-------------------------------------------------------------------------------------
//int main(int argc, char* argv[])
@@ -159,6 +164,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();
+60 -6
View File
@@ -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"
@@ -16,13 +17,22 @@
#include "scene/dvc_scene_node.h"
#include "scene/component/dvc_light_component.h"
#include "../adapter/adapter_texture.h"
#include "standard/dvu_standard_material.h"
#include "asset/dvc_material.h"
SDL_Window* main_window;
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;
extern int32_t g_debugMaterialID;
typedef std::map<std::string, davinci::MaterialPtr> DebugMaterialMap;
DebugMaterialMap g_debugMaterialMap;
bool initMainUI(SDL_Window* window, SDL_GLContext gl_context, const char* glsl_version)
{
@@ -72,10 +82,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,33 +110,76 @@ 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"};
const char* aaItem[] = { "none", "MSAA_2X2", "MSAA_4X4"};
ImGui::Combo("AntiAliasing", (int*)(&g_aaTech), aaItem, IM_ARRAYSIZE(aaItem));
const char* materialItems[] = {
"none",
"classic:phong",
"debug:solid_color",
"debug:base_color_texture",
"debug:normal_texture",
"debug:texture_coordinates_0",
"debug:geometry_normal"
};
ImGui::Combo("DebugMaterial", (int*)(&g_debugMaterialID), materialItems, IM_ARRAYSIZE(materialItems));
if (ImGui::Button("Render"))
{
davinci::RenderPipe pipe;
davinci::RenderPipe pipe(g_device);
davinci::UniformBuffer& uniformBuffer = pipe.getUniformBuffer();
uniformBuffer.updateStandardAttribute(g_scene);
//find Light node
uniformBuffer.setAttribute("ambient_light:color", davinci::fVector3(0.1f, 0.1f, 0.1f));
davinci::SceneNodeConstPtr lightNode = g_scene.getNodeWithComponent<davinci::LightComponent>();
if (lightNode)
{
uniformBuffer.setAttribute("light0:position", lightNode->getWorldPosition());
uniformBuffer.setAttribute("light0:color", davinci::fVector3::WHITE);
}
pipe.setAntiAliasingTech(g_aaTech);
if (g_debugMaterialID == 0) {
pipe.setDebugMaterial(nullptr);
}
else
{
DebugMaterialMap::iterator it = g_debugMaterialMap.find(materialItems[g_debugMaterialID]);
if(it==g_debugMaterialMap.end())
{
davinci::MaterialPtr debugMaterial = davinci_utils::create_standard_material(&g_device, materialItems[g_debugMaterialID]);
if (debugMaterial)
{
debugMaterial->setupMaterial();
g_debugMaterialMap[materialItems[g_debugMaterialID]] = debugMaterial;
pipe.setDebugMaterial(debugMaterial);
}
}
else
{
pipe.setDebugMaterial(it->second);
}
}
g_scene.buildRenderableQueue(pipe);
g_renderTarget.clear();
pipe.process(g_renderTarget);
davinci_utils::dump_render_target("d:\\rendertarget", g_renderTarget);
//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
+10
View File
@@ -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})
+39
View File
@@ -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
+121
View File
@@ -0,0 +1,121 @@
#pragma once
#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 "math/dvc_matrix3.h"
#include "math/dvc_matrix4.h"
#include "dvc_texture.h"
DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API Material
{
public:
typedef std::variant<int32_t, float32_t, fVector2, fVector3, fVector4, fMatrix3, fMatrix4, TextureInfo> Value;
struct Param
{
enum ParamType
{
PF_Int32,
PT_Float,
PT_Float2,
PT_Float3,
PT_Float4,
PT_Matrix3,
PT_Matrix4,
PT_Texture,
};
std::string name;
ParamType type;
Value value;
Param() = delete;
Param(int32_t v) : type(PF_Int32), value(v) { }
Param(float32_t v) : type(PT_Float), value(v) { }
Param(const fVector2& v) : type(PT_Float2), value(v) {}
Param(const fVector3& v) : type(PT_Float3), value(v) {}
Param(const fVector4& v) : type(PT_Float4), value(v) {}
Param(const fMatrix3& v) : type(PT_Matrix3), value(v) {}
Param(const fMatrix4& v) : type(PT_Matrix4), value(v) {}
Param(const TextureInfo& v) : type(PT_Texture), value(v) {}
};
typedef std::map<std::string, Param> ParamMap;
public:
template<typename T>
bool setParam(const char* name, const T& value)
{
ParamMap::iterator it = m_params.find(name);
if(it==m_params.end() || (!std::holds_alternative<T>(it->second.value)))
{
return false;
}
it->second.value = value;
return true;
}
template<typename T>
bool getParam(const char* name, T& value) const
{
ParamMap::const_iterator it = m_params.find(name);
if(it==m_params.end() || (!std::holds_alternative<T>(it->second.value)))
{
return false;
}
value = std::get<T>(it->second.value);
return true;
}
public:
int32_t getID(void) const { return m_id; }
VertexShaderInterface* getVertexShader() const {
return m_vertexShader;
}
FragementShaderInterface* getFragementShader() const {
return m_fragementShader;
}
virtual bool setupMaterial();
virtual bool pickupParam(const davinci::MaterialConstPtr otherMaterial) {
return true;
}
protected:
void setVertexShader(const char* szShaderFile, const char* vertexShaderName);
void setFragementShader(const char* szShaderFile, const char* fragementShaderName);
template<typename T>
bool registerParam(const char* name, const T& defaultValue) {
if (m_params.find(name) != m_params.end()) return false;
m_params.insert(std::make_pair(name, Param(defaultValue)));
return true;
}
protected:
int32_t m_id;
std::string m_name;
VertexShaderInterface* m_vertexShader = nullptr;
FragementShaderInterface* m_fragementShader = nullptr;
std::string m_vertexshaderFile;
std::string m_vertexShaderName;
std::string m_fragementShaderFile;
std::string m_fragementShaderName;
ParamMap m_params;
public:
Material(int32_t id, const char* name);
~Material();
};
DV_CORE_END_NAMESPACE
+5 -1
View File
@@ -14,12 +14,15 @@ public:
PrimitiveType type;
PrimitiveAttributes attributes;
DeviceBufferAccessorConstPtr indices;
MaterialPtr material;
};
typedef std::shared_ptr<Primitive> PrimitivePtr;
typedef std::shared_ptr<const Primitive> PrimitiveConstPtr;
typedef std::vector<PrimitivePtr> 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();
};
+37
View File
@@ -0,0 +1,37 @@
#pragma once
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector4.h"
#include "math/dvc_matrix3.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();
};
struct TextureInfo
{
TextureConstPtr texture;
int32_t texCoord = 0;
fMatrix3 uvTransform;
};
DV_CORE_END_NAMESPACE
+3 -2
View File
@@ -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();
};
@@ -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();
};
+4 -1
View File
@@ -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();
};
+75
View File
@@ -0,0 +1,75 @@
#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);
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;
template<typename T>
MaterialPtr newMaterialAsset(const char* name) {
MaterialPtr materialPtr = std::make_shared<T>(nextID(), name);
m_materialAssetMap.insert(std::make_pair(materialPtr->getID(), materialPtr));
return materialPtr;
}
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<int32_t, DeviceBufferPtr> m_deviceBufferMap;
//device buffer view
std::map<int32_t, DeviceBufferViewPtr> m_bufferViewMap;
//device buffer accessor
std::map<int32_t, DeviceBufferAccessorPtr> m_bufferAccessorMap;
//mesh asset resources
std::map<int32_t, MeshPtr> m_meshAssetMap;
//image asset resources
std::map<int32_t, ImagePtr> m_imageAssetMap;
//texture asset resources
std::map<int32_t, TexturePtr> m_textureAssetMap;
//material asset resources
std::map<int32_t, MaterialPtr> m_materialAssetMap;
public:
Device();
~Device();
};
DV_CORE_END_NAMESPACE
+3 -3
View File
@@ -22,9 +22,9 @@ public:
bool addElement(const std::string& name, VertexElementType elementType,
ComponentType componentType, DataType dataType);
const Element* getElement(size_t index) const;
const Element* getElement(VertexElementType elementType) const;
const Element* getElement(const std::string& name) const;
const Element* getElementByIndex(size_t index) const;
const Element* getElementByType(VertexElementType elementType) const;
const Element* getElementByName(const std::string& name) const;
//total byte size of each vertex
size_t vertexByteSize(void) const { return m_vertexSize; }
+13 -10
View File
@@ -46,7 +46,8 @@ enum VertexElementType
{
VET_UNKNOWN,
VET_POSITION,
VET_POSITION_MODEL, VET_POSITION_WORLD, VET_POSITION_VIEW, VET_POSITION_NDC,
VET_NORMAL,
VET_TANGENT,
VET_BINORMAL,
@@ -89,17 +90,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
+15
View File
@@ -36,8 +36,22 @@ class Mesh;
typedef std::shared_ptr<Mesh> MeshPtr;
typedef std::shared_ptr<const Mesh> MeshConstPtr;
class Image;
typedef std::shared_ptr<Image> ImagePtr;
typedef std::shared_ptr<const Image> ImageConstPtr;
class Texture;
typedef std::shared_ptr<Texture> TexturePtr;
typedef std::shared_ptr<const Texture> TextureConstPtr;
class Material;
typedef std::shared_ptr<Material> MaterialPtr;
typedef std::shared_ptr<const Material> MaterialConstPtr;
class Scene;
class Device;
class SceneNode;
typedef std::shared_ptr<SceneNode> SceneNodePtr;
typedef std::shared_ptr<const SceneNode> SceneNodeConstPtr;
@@ -68,6 +82,7 @@ typedef std::shared_ptr<const Renderable> RenderableConstPtr;
typedef std::vector<RenderableConstPtr> RenderableArray;
class VertexShaderInterface;
class FragementShaderInterface;
class UniformBuffer;
+1
View File
@@ -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);
+39
View File
@@ -94,6 +94,45 @@ public:
template<typename U>
friend TMatrix3<U> operator * (U fScalar, const TMatrix3<U>& 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<T>& 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<T>& 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
+5
View File
@@ -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;
+24 -2
View File
@@ -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,26 +11,47 @@ 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;
}
void setAntiAliasingTech(AntiAliasingTech aaTech) {
m_aaTech = aaTech;
}
MaterialPtr getDebugMaterial() const {
return m_debugMaterial;
}
void setDebugMaterial(MaterialPtr material) {
m_debugMaterial = material;
}
void pushRenderable(RenderableConstPtr renderable);
void process(RenderTarget& renderTarget);
protected:
AntiAliasingTech m_aaTech;
Device& m_device;
AntiAliasingTech m_aaTech = AntiAliasingTech::AA_None;
UniformBuffer m_uniformBuffer;
RenderableArray m_renderableQueue;
MaterialPtr m_debugMaterial;
public:
RenderPipe();
RenderPipe(Device& device);
~RenderPipe();
};
@@ -17,7 +17,7 @@ public:
PrimitiveType primitiveType;
PrimitiveAttributes vertexData;
DeviceBufferAccessorConstPtr indices;
VertexShaderInterface* vs;
MaterialConstPtr material;
};
typedef std::vector<Node> NodeArray;
@@ -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<Node> NodeArray;
public:
void process(UniformBuffer& uniformBuffer, const InputAssemberStep::NodeArray& assembedNodeArray);
void process(RenderPipe& pipe, const InputAssemberStep::NodeArray& assembedNodeArray);
public:
NodeArray m_nodes;
@@ -20,7 +20,7 @@ public:
typedef std::vector<Fragement> 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;
-34
View File
@@ -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<DeviceBufferPtr> m_deviceBufferArray;
std::vector<DeviceBufferViewPtr> m_bufferViewArray;
std::vector<DeviceBufferAccessorPtr> m_bufferAccessorArray;
//asset resources
std::vector<MeshPtr> m_meshArray;
public:
Scene();
~Scene();
@@ -0,0 +1,37 @@
#pragma once
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector4.h"
#include "asset/dvc_material.h"
DV_CORE_BEGIN_NAMESPACE
class shader
{
public:
template<typename T>
static inline int32_t step(const T& edge, const T& x) {
return (x >= edge) ? 1 : 0;
}
template<typename T>
static inline T max(const T& a, const T& b) {
return (a > b) ? a : b;
}
template<typename T>
static inline T pow(const T& base, const T& exponent) {
return std::pow(base, exponent);
}
template<typename T>
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 TextureInfo& texturePtr, const fVector2& uv, const fVector4& defaultValue);
};
DV_CORE_END_NAMESPACE
@@ -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,27 +16,30 @@ public:
virtual const VertexDesc& getOutputVertexDesc() const = 0;
typedef std::function<DeviceBufferAccessorConstPtr(VertexElementType)> SetInputAttributeFunc;
virtual void prepare(const UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) = 0;
virtual void begin(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) = 0;
virtual void process(size_t index, void* outputVertex, fVector3& vertexWorldPos) = 0;
virtual void process(size_t index, float32_t* outputVertex) = 0;
virtual void end() = 0;
};
class PixelShaderInterface
class FragementShaderInterface
{
public:
virtual const char* getName(void) const = 0;
typedef std::function<size_t(const std::string&)> SetInputAttributeFunc;
virtual void prepare(const UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) = 0;
virtual bool begin(const RenderPipe* pipe, MaterialConstPtr material, const VertexDesc& veretexDesc) = 0;
virtual void process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) = 0;
virtual void process(const float32_t* pixelData, fVector4& color) = 0;
virtual void end() = 0;
};
class ShaderFileInterface
{
public:
virtual VertexShaderInterface* getVertexShader(const char* szName) = 0;
virtual PixelShaderInterface* getPixelShader(const char* szName) = 0;
virtual FragementShaderInterface* getFragementShader(const char* szName) = 0;
};
typedef ShaderFileInterface* (*ShaderFileInterfaceFunc)();
+79
View File
@@ -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
+55
View File
@@ -0,0 +1,55 @@
#include "asset/dvc_material.h"
#include "shader/dvc_shader_interface.h"
DV_CORE_BEGIN_NAMESPACE
Material::Material(int32_t id, const char* name)
: m_id(id)
, m_name(name)
{
}
Material::~Material()
{
}
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::setupMaterial()
{
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_fragementShader = shaderInterface->getFragementShader(m_fragementShaderName.c_str());
if (!m_fragementShader)
{
return false;
}
return true;
}
DV_CORE_END_NAMESPACE
+3 -2
View File
@@ -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)
{
}
+33
View File
@@ -0,0 +1,33 @@
#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;
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);
}
DV_CORE_END_NAMESPACE
+3 -8
View File
@@ -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);
@@ -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)
@@ -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)
+148
View File
@@ -0,0 +1,148 @@
#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<DeviceBuffer>(nextID(), length);
m_deviceBufferMap.insert(std::make_pair(deviceBuffer->getID(), deviceBuffer));
return deviceBuffer;
}
DeviceBufferConstPtr Device::getDeviceBuffer(int32_t id) const
{
std::map<int32_t, DeviceBufferPtr>::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<DeviceBufferView>(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<int32_t, DeviceBufferViewPtr>::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<DeviceBufferAccessor>(
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<int32_t, DeviceBufferAccessorPtr>::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<davinci::Mesh>(nextID(), name);
m_meshAssetMap.insert(std::make_pair(meshPtr->getID(), meshPtr));
return meshPtr;
}
MeshPtr Device::getMesh(int32_t id) const
{
std::map<int32_t, MeshPtr>::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<Image>(nextID());
m_imageAssetMap.insert(std::make_pair(imagePtr->getID(), imagePtr));
return imagePtr;
}
ImagePtr Device::getImage(int32_t id) const
{
std::map<int32_t, ImagePtr>::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<Texture>(nextID());
m_textureAssetMap.insert(std::make_pair(texturePtr->getID(), texturePtr));
return texturePtr;
}
TexturePtr Device::getTexture(int32_t id) const
{
std::map<int32_t, TexturePtr>::const_iterator it = m_textureAssetMap.find(id);
if(it==m_textureAssetMap.end())
{
return nullptr;
}
return it->second;
}
MaterialPtr Device::getMaterial(int32_t id) const
{
std::map<int32_t, MaterialPtr>::const_iterator it = m_materialAssetMap.find(id);
if(it==m_materialAssetMap.end())
{
return nullptr;
}
return it->second;
}
DV_CORE_END_NAMESPACE
@@ -75,7 +75,7 @@ bool VertexDesc::addElement(const std::string& name, VertexElementType elementTy
return true;
}
const VertexDesc::Element* VertexDesc::getElement(size_t index) const
const VertexDesc::Element* VertexDesc::getElementByIndex(size_t index) const
{
if (index >= m_elements.size())
{
@@ -84,24 +84,24 @@ const VertexDesc::Element* VertexDesc::getElement(size_t index) const
return &(m_elements[index]);
}
const VertexDesc::Element* VertexDesc::getElement(VertexElementType type) const
const VertexDesc::Element* VertexDesc::getElementByType(VertexElementType type) const
{
auto it = m_elementTypeMap.find(type);
if (it == m_elementTypeMap.end())
{
return nullptr;
}
return getElement(it->second);
return getElementByIndex(it->second);
}
const VertexDesc::Element* VertexDesc::getElement(const std::string& name) const
const VertexDesc::Element* VertexDesc::getElementByName(const std::string& name) const
{
auto it = m_nameMap.find(name);
if (it == m_nameMap.end())
{
return nullptr;
}
return getElement(it->second);
return getElementByIndex(it->second);
}
DV_CORE_END_NAMESPACE
+29
View File
@@ -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;
+5
View File
@@ -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);
+5 -3
View File
@@ -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;
@@ -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 = pipe.getDebugMaterial() ? pipe.getDebugMaterial()->getVertexShader(): inputNode.material->getVertexShader();
vs->begin(&pipe,
[inputNode](VertexElementType type) -> DeviceBufferAccessorConstPtr
{
InputAssemberStep::PrimitiveAttributes::const_iterator it = inputNode.vertexData.find(type);
@@ -40,11 +42,13 @@ void VertexShaderStep::process(UniformBuffer& uniformBuffer, const InputAssember
);
size_t vertexCounts = inputNode.indices->getCount();
size_t outputVertexByteSize = vs->getOutputVertexDesc().vertexByteSize();
Node outputNode;
outputNode.material = inputNode.material;
outputNode.vertexCounts = vertexCounts;
outputNode.vertexDesc = standardVS->getOutputVertexDesc();
outputNode.vertexBuf = std::make_shared<DeviceBuffer>(outputNode.vertexDesc.vertexByteSize() * vertexCounts);
outputNode.vertexDesc = vs->getOutputVertexDesc();
outputNode.vertexBuf = device.newDeviceBuffer(outputVertexByteSize * vertexCounts);
outputNode.depth.resize(vertexCounts);
outputNode.invZ.resize(vertexCounts);
@@ -52,22 +56,25 @@ void VertexShaderStep::process(UniformBuffer& uniformBuffer, const InputAssember
{
uint16_t index = *((const uint16_t*)(inputNode.indices->getElement(i)));
fVector3 vertexWorldPos;
void* outputVertexMemory = outputNode.vertexBuf->ptr(i* outputNode.vertexDesc.vertexByteSize());
standardVS->process(index, outputVertexMemory, vertexWorldPos);
uint8_t* outputVertexMemory = (uint8_t*)(outputNode.vertexBuf->ptr(i * outputVertexByteSize));
vs->process(index, (float32_t*)outputVertexMemory);
//TODO: clip
//TODO: div-zero
fVector3 cameraSpacePos = (*cameraView) * vertexWorldPos;
outputNode.depth[i] = (std::fabsf(cameraSpacePos.z) - cameraNear) / (cameraFar - cameraNear);
float32_t cameraSpaceZ = cameraSpacePos.z;
outputNode.invZ[i] = 1.f/std::fabsf(cameraSpacePos.z);
size_t ndcPosOffset = outputNode.vertexDesc.getElementByType(VET_POSITION_NDC)->offset;
const fVector3* vertexNDCPos = (const fVector3*)(outputVertexMemory+ndcPosOffset);
outputNode.depth[i] = (std::fabsf(vertexNDCPos->z) - cameraNear) / (cameraFar - cameraNear);
float32_t cameraSpaceZ = vertexNDCPos->z;
outputNode.invZ[i] = 1.f/std::fabsf(vertexNDCPos->z);
}
m_nodes.push_back(outputNode);
vs->end();
}
}
@@ -3,13 +3,15 @@
#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");
Device& device = pipe.getDevice();
UniformBuffer& uniformBuffer = pipe.getUniformBuffer();
fMatrix4 view_trans =
fMatrix4::makeTrans(canvasWidth / 2.f, canvasHeight / 2.f, 0.f) *
@@ -17,13 +19,26 @@ void RasterizerStep::process(const VertexShaderStep::NodeArray& vsNodeArray, Ant
m_startOffsetArray.resize(canvasWidth* canvasHeight, -1);
MaterialPtr debugMaterial = pipe.getDebugMaterial();
VertexShaderStep::NodeArray::const_iterator it, end = vsNodeArray.end();
for (it = vsNodeArray.begin(); it != end; ++it)
{
const VertexShaderStep::Node& node = *it;
if (debugMaterial)
{
debugMaterial->pickupParam(node.material);
}
FragementShaderInterface* fragementShader = debugMaterial ? debugMaterial->getFragementShader() : node.material->getFragementShader();
if (!(fragementShader->begin(&pipe, debugMaterial ? debugMaterial : node.material, node.vertexDesc)))
{
//TODO: shader fallback
continue;
}
size_t vertexSize = node.vertexDesc.vertexByteSize();
size_t POS_offset = node.vertexDesc.getElement(VET_POSITION)->offset;
size_t POS_offset = node.vertexDesc.getElementByType(VET_POSITION_NDC)->offset;
for (size_t i = 0; i < node.vertexCounts; i += 3)
{
@@ -40,7 +55,7 @@ void RasterizerStep::process(const VertexShaderStep::NodeArray& vsNodeArray, Ant
Rasterizer::drawTriangleLarrabee(aaTech,
canvasWidth, canvasHeight, pos0.xy(), pos1.xy(), pos2.xy(),
[this, canvasWidth, pixelShader, vertex0, vertex1, vertex2, invZ, vertexDepth, &node]
[this, canvasWidth, fragementShader, vertex0, vertex1, vertex2, invZ, vertexDepth, &node]
(const std::pair<int32_t, int32_t>& dot, const fVector3& percent, uint32_t mask)
{
fVector3 percentCorrected(percent);
@@ -59,7 +74,7 @@ void RasterizerStep::process(const VertexShaderStep::NodeArray& vsNodeArray, Ant
}
fVector4 color;
pixelShader->process(node.vertexDesc, &(pixelData[0]), color);
fragementShader->process(pixelData.data(), color);
float32_t depth = MathUtil::lerp3(vertexDepth[0], vertexDepth[1], vertexDepth[2], percentCorrected);
@@ -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;
}
-76
View File
@@ -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<DeviceBuffer>();
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<DeviceBufferView>(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<DeviceBufferAccessor>(
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)
@@ -0,0 +1,15 @@
#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 TextureInfo& textureInfo, const fVector2& uv, const fVector4& defaultValue)
{
if (!(textureInfo.texture)) return defaultValue;
fVector3 uvTransform = textureInfo.uvTransform * fVector3(uv, 1);
return textureInfo.texture->sample(uvTransform.xy());
}
DV_CORE_END_NAMESPACE
+1
View File
@@ -37,6 +37,7 @@
#include <unordered_map>
#include <functional>
#include <algorithm>
#include <variant>
#ifdef DV_SYS_WINDOWS
#define snprintf _snprintf
+4
View File
@@ -0,0 +1,4 @@
#################
# sub dictionary
#################
add_subdirectory(standard)
+36
View File
@@ -0,0 +1,36 @@
add_library(dv_standard_shaders SHARED
dll_main.cpp
vs_standard.h
vs_standard.cpp
fs_debug_solid_color.h
fs_debug_solid_color.cpp
fs_debug_show_texture.h
fs_debug_show_texture.cpp
fs_debug_texture_coordinates.h
fs_debug_texture_coordinates.cpp
fs_debug_geometry_normal.h
fs_debug_geometry_normal.cpp
fs_classic_phong.h
fs_classic_phong.cpp
fs_pbr_gltf.h
fs_pbr_gltf.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 $<TARGET_FILE:dv_standard_shaders> ${CMAKE_BINARY_DIR}/source/console/$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>/)
+88
View File
@@ -0,0 +1,88 @@
#include "dv_config.h"
#include "shader/dvc_shader_interface.h"
#include "vs_standard.h"
#include "fs_debug_solid_color.h"
#include "fs_debug_show_texture.h"
#include "fs_debug_texture_coordinates.h"
#include "fs_debug_geometry_normal.h"
#include "fs_classic_phong.h"
#include "fs_pbr_gltf.h"
class StandardShaderInterface : public davinci::ShaderFileInterface
{
public:
virtual davinci::VertexShaderInterface* getVertexShader(const char* szName) override
{
auto it = m_vertexShaderMap.find(szName);
if (it != m_vertexShaderMap.end()) return it->second;
return nullptr;
}
virtual davinci::FragementShaderInterface* getFragementShader(const char* szName) override
{
auto it = m_fragementShaderMap.find(szName);
if (it != m_fragementShaderMap.end()) return it->second;
return nullptr;
}
public:
StandardShaderInterface()
{
registerVertexShader(new StandardVertexShader(false , false , false));
registerVertexShader(new StandardVertexShader(false , false , true));
registerVertexShader(new StandardVertexShader(false , true , false));
registerVertexShader(new StandardVertexShader(false , true , true));
registerVertexShader(new StandardVertexShader(true , false , false));
registerVertexShader(new StandardVertexShader(true , false , true));
registerVertexShader(new StandardVertexShader(true , true , false));
registerVertexShader(new StandardVertexShader(true , true , true));
registerFragementShader(new DebugFragementShader_SolidColor());
registerFragementShader(new DebugFragementShader_ShowTexture());
registerFragementShader(new DebugFragementShader_TextureCoordinates());
registerFragementShader(new DebugFragementShader_GeometryNormal());
registerFragementShader(new ClassicFragementShader_Phong());
registerFragementShader(new PbrFragementShader_GLTF());
}
~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::FragementShaderInterface* fragementShader)
{
m_fragementShaderMap.insert(std::make_pair(fragementShader->getName(), fragementShader));
}
private:
std::map<std::string, davinci::VertexShaderInterface*> m_vertexShaderMap;
std::map<std::string, davinci::FragementShaderInterface*> m_fragementShaderMap;
};
extern "C"
{
__declspec(dllexport) davinci::ShaderFileInterface* ShaderFileInterface()
{
static StandardShaderInterface theInterface;
return &theInterface;
}
}
@@ -0,0 +1,106 @@
#include "fs_classic_phong.h"
#include "asset/dvc_material.h"
#include "pipe/dvc_render_pipe.h"
#include "shader/dvc_shader_functions.h"
using namespace davinci;
const char* ClassicFragementShader_Phong::getName(void) const
{
return "classic:phong";
}
bool ClassicFragementShader_Phong::begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material, const davinci::VertexDesc& veretexDesc)
{
m_pipe = pipe;
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
if(!(material->getParam("u_materialColor", m_materialColor)))
{
m_materialColor = davinci::fVector4::WHITE;
}
if(!(material->getParam("u_specularShininess", m_specularShininess)))
{
m_specularShininess = 64.0f;
}
if (!(material->getParam("u_specularStrength", m_specularStrength)))
{
m_specularStrength = 5.0f;
}
if (const davinci::fVector3* attribute = (davinci::fVector3*)(uniformBuffer.getAttribute("ambient_light:color")))
{
m_ambientLightColor = *attribute;
}
else
{
m_ambientLightColor = davinci::fVector3(0.2f, 0.2f, 0.2f);
}
if (const davinci::fVector3* attribute = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:position")))
{
m_light0Position = *attribute;
}
else return false;
if (const davinci::fVector3* attribute = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:color")))
{
m_light0Color = *attribute;
}
else return false;
if (const davinci::fVector3* attribute = (davinci::fVector3*)(uniformBuffer.getAttribute("camera:eye_pos")))
{
m_cameraPosition = *attribute;
}
else return false;
if (const VertexDesc::Element* element = veretexDesc.getElementByType(davinci::VET_POSITION_WORLD))
{
m_vWorldPosOffset = element->offset / sizeof(float32_t);
}
else return false;
if (const VertexDesc::Element* element = veretexDesc.getElementByType(davinci::VET_NORMAL))
{
m_vNormalOffset = element->offset / sizeof(float32_t);
}
else return false;
return true;
}
void ClassicFragementShader_Phong::process(const float32_t* pixelData, davinci::fVector4& color)
{
fVector3 v_worldpos = *((const fVector3*)(pixelData + m_vWorldPosOffset));
fVector3 v_normal = *((const fVector3*)(pixelData + m_vNormalOffset));
fVector3 toLight = m_light0Position - v_worldpos;
toLight.normalise();
fVector3 toCamera = m_cameraPosition - v_worldpos;
toCamera.normalise();
float32_t ndotl = toLight.dotProduct(v_normal);
fVector3 vR = 2 * ndotl*v_normal - toLight;
float32_t rdotv = vR.dotProduct(toCamera);
fVector3 colAmbient = m_ambientLightColor * m_materialColor.xyz();
fVector3 colDiffuse = shader::max(0.0f, ndotl) * m_light0Color * m_materialColor.xyz();
float32_t specular = shader::step(0.f, ndotl) * m_specularStrength * shader::pow(shader::max(0.0f, rdotv), m_specularShininess);
fVector3 colSpecular = specular * m_light0Color;
fVector3 final = fVector3::BLACK;
final += colAmbient;
final += colDiffuse;
final += colSpecular;
color = fVector4(final.saturate(), 1.0f);
}
void ClassicFragementShader_Phong::end()
{
}
@@ -0,0 +1,28 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class ClassicFragementShader_Phong : public davinci::FragementShaderInterface
{
public:
virtual const char* getName(void) const;
virtual bool begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material, const davinci::VertexDesc& veretexDesc) override;
virtual void process(const float32_t* pixelData, davinci::fVector4& color) override;
virtual void end() override;
private:
const davinci::RenderPipe* m_pipe = nullptr;
davinci::fVector4 m_materialColor = davinci::fVector4::WHITE;
float32_t m_specularShininess = 64.0f;
float32_t m_specularStrength = 5.0f;
davinci::fVector3 m_ambientLightColor = davinci::fVector3(0.2f, 0.2f, 0.2f);
davinci::fVector3 m_light0Position;
davinci::fVector3 m_light0Color;
davinci::fVector3 m_cameraPosition;
size_t m_vWorldPosOffset;
size_t m_vNormalOffset;
};
@@ -0,0 +1,43 @@
#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"
using namespace davinci;
const char* DebugFragementShader_GeometryNormal::getName(void) const
{
return "debug:geometry_normal";
}
bool DebugFragementShader_GeometryNormal::begin(const RenderPipe* pipe, MaterialConstPtr material, const VertexDesc& veretexDesc)
{
m_pipe = pipe;
if (const VertexDesc::Element* element = veretexDesc.getElementByType(VET_NORMAL))
{
m_vNormalOffset = element->offset / sizeof(float32_t);
}
else return false;
return true;
}
void DebugFragementShader_GeometryNormal::process(const float32_t* pixelData, fVector4& color)
{
const UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
fVector3 v_normal = *((const fVector3*)(pixelData + m_vNormalOffset));
fVector3 normalColor = v_normal;
normalColor.normalise();
normalColor = normalColor * 0.5f + 0.5f;
color = fVector4(normalColor, 1);
}
void DebugFragementShader_GeometryNormal::end()
{
}
@@ -0,0 +1,18 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class DebugFragementShader_GeometryNormal : public davinci::FragementShaderInterface
{
public:
virtual const char* getName(void) const;
virtual bool begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material, const davinci::VertexDesc& veretexDesc) override;
virtual void process(const float32_t* pixelData, davinci::fVector4& color) override;
virtual void end() override;
private:
const davinci::RenderPipe* m_pipe = nullptr;
size_t m_vNormalOffset;
};
@@ -0,0 +1,44 @@
#include "fs_debug_show_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"
using namespace davinci;
const char* DebugFragementShader_ShowTexture::getName(void) const
{
return "debug:show_texture";
}
bool DebugFragementShader_ShowTexture::begin(const RenderPipe* pipe, MaterialConstPtr material, const VertexDesc& veretexDesc)
{
m_pipe = pipe;
if (!(material->getParam("u_textureInfo", m_textureInfo)))
{
return false;
}
if (const VertexDesc::Element* element = veretexDesc.getElementByType(static_cast<VertexElementType>(VET_TEXCOORD_0+m_textureInfo.texCoord)))
{
m_vTextureCoordinateOffset = element->offset / sizeof(float32_t);
}
else return false;
return true;
}
void DebugFragementShader_ShowTexture::process(const float32_t* pixelData, fVector4& color)
{
const fVector2* v_TextureCoordinate = (const fVector2*)(pixelData + m_vTextureCoordinateOffset);
fVector4 textureColor = shader::texture2D(*m_pipe, m_textureInfo, (*v_TextureCoordinate), fVector4::WHITE);
color = textureColor;
}
void DebugFragementShader_ShowTexture::end()
{
m_textureInfo.texture.reset();
}
@@ -0,0 +1,21 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
#include "asset/dvc_material.h"
class DebugFragementShader_ShowTexture : public davinci::FragementShaderInterface
{
public:
virtual const char* getName(void) const;
virtual bool begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material, const davinci::VertexDesc& veretexDesc) override;
virtual void process(const float32_t* pixelData, davinci::fVector4& color) override;
virtual void end() override;
private:
const davinci::RenderPipe* m_pipe = nullptr;
davinci::TextureInfo m_textureInfo;
size_t m_vTextureCoordinateOffset;
};
@@ -0,0 +1,27 @@
#include "fs_debug_solid_color.h"
#include "asset/dvc_material.h"
using namespace davinci;
const char* DebugFragementShader_SolidColor::getName(void) const
{
return "debug:solid_color";
}
bool DebugFragementShader_SolidColor::begin(const RenderPipe* pipe, MaterialConstPtr material, const VertexDesc& veretexDesc)
{
if (!(material->getParam("u_solidColor", m_solidColor)))
{
m_solidColor = davinci::fVector4::WHITE;
}
return true;
}
void DebugFragementShader_SolidColor::process(const float32_t* pixelData, fVector4& color)
{
color = m_solidColor;
}
void DebugFragementShader_SolidColor::end()
{
}
@@ -0,0 +1,17 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class DebugFragementShader_SolidColor : public davinci::FragementShaderInterface
{
public:
virtual const char* getName(void) const;
virtual bool begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material, const davinci::VertexDesc& veretexDesc) override;
virtual void process(const float32_t* pixelData, davinci::fVector4& color) override;
virtual void end() override;
private:
davinci::fVector4 m_solidColor = davinci::fVector4::WHITE;
};
@@ -0,0 +1,46 @@
#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"
using namespace davinci;
const char* DebugFragementShader_TextureCoordinates::getName(void) const
{
return "debug:texture_coordinates";
}
bool DebugFragementShader_TextureCoordinates::begin(const RenderPipe* pipe, MaterialConstPtr material, const VertexDesc& veretexDesc)
{
m_pipe = pipe;
if (!(material->getParam("u_texCoord", m_texCoord)))
{
m_texCoord = 0;
}
if (const VertexDesc::Element* element = veretexDesc.getElementByType(static_cast<VertexElementType>(VET_TEXCOORD_0+m_texCoord)))
{
m_vTextureCoordinateOffset = element->offset / sizeof(float32_t);
}
else return false;
return true;
}
void DebugFragementShader_TextureCoordinates::process(const float32_t* pixelData, fVector4& color)
{
const fVector2* v_uv = (const fVector2*)(pixelData + m_vTextureCoordinateOffset);
fVector2 uv = *v_uv;
float uv_x = uv.x - std::floor(uv.x);
float uv_y = uv.y - std::floor(uv.y);
color = fVector4(uv_x, uv_y, 0, 1);
}
void DebugFragementShader_TextureCoordinates::end()
{
}
@@ -0,0 +1,20 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class DebugFragementShader_TextureCoordinates : public davinci::FragementShaderInterface
{
public:
virtual const char* getName(void) const;
virtual bool begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material, const davinci::VertexDesc& veretexDesc) override;
virtual void process(const float32_t* pixelData, davinci::fVector4& color) override;
virtual void end() override;
private:
const davinci::RenderPipe* m_pipe = nullptr;
int32_t m_texCoord=0;
size_t m_vTextureCoordinateOffset;
};
+26
View File
@@ -0,0 +1,26 @@
#include "fs_pbr_gltf.h"
#include "pipe/dvc_render_pipe.h"
using namespace davinci;
const char* PbrFragementShader_GLTF::getName(void) const
{
return "pbr:gltf";
}
bool PbrFragementShader_GLTF::begin(const RenderPipe* pipe, MaterialConstPtr material, const VertexDesc& veretexDesc)
{
m_pipe = pipe;
const UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
return true;
}
void PbrFragementShader_GLTF::process(const float32_t* pixelData, fVector4& color)
{
color = fVector4::BLUE;
}
void PbrFragementShader_GLTF::end()
{
}
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class PbrFragementShader_GLTF : public davinci::FragementShaderInterface
{
public:
virtual const char* getName(void) const;
virtual bool begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material, const davinci::VertexDesc& veretexDesc) override;
virtual void process(const float32_t* pixelData, davinci::fVector4& color) override;
virtual void end() override;
private:
const davinci::RenderPipe* m_pipe = nullptr;
davinci::MaterialConstPtr m_material;
};
+123
View File
@@ -0,0 +1,123 @@
#include "vs_standard.h"
#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"
StandardVertexShader::StandardVertexShader(bool withWorldPos, bool withNormal, bool withTexCoord0)
: m_withWorldPos(withWorldPos)
, m_withNormal(withNormal)
, m_withTexCoord0(withTexCoord0)
{
m_name = "standard";
m_inputAccessors[davinci::VET_POSITION_MODEL] = nullptr;
m_outputVertexDesc.addElement("v_ndc_pos", davinci::VET_POSITION_NDC, davinci::CT_Float32, davinci::DT_Vec3);
if (withWorldPos)
{
m_outputVertexDesc.addElement("v_world_pos", davinci::VET_POSITION_WORLD, davinci::CT_Float32, davinci::DT_Vec3);
}
m_name += withWorldPos ? "+worldpos" : "-worldpos";
if (withNormal)
{
m_inputAccessors[davinci::VET_NORMAL] = nullptr;
m_outputVertexDesc.addElement("v_normal", davinci::VET_NORMAL, davinci::CT_Float32, davinci::DT_Vec3);
}
m_name += withNormal ? "+normal" : "-normal";
if (withTexCoord0)
{
m_inputAccessors[davinci::VET_TEXCOORD_0] = nullptr;
m_outputVertexDesc.addElement("v_uv", davinci::VET_TEXCOORD_0, davinci::CT_Float32, davinci::DT_Vec2);
}
m_name += withTexCoord0 ? "+uv0" : "-uv0";
}
StandardVertexShader::~StandardVertexShader()
{
}
void StandardVertexShader::begin(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc)
{
m_pipe = pipe;
for (auto& pair : m_inputAccessors)
{
pair.second = setInputAttributeFunc(pair.first);
}
assert(getInputAccessor(davinci::VET_POSITION_MODEL));
assert((!m_withNormal) || getInputAccessor(davinci::VET_NORMAL));
assert((!m_withTexCoord0) || getInputAccessor(davinci::VET_TEXCOORD_0));
}
void StandardVertexShader::process(size_t index, float32_t* outputVertex)
{
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 = getInputAccessor(davinci::VET_POSITION_MODEL);
davinci::DeviceBufferAccessorConstPtr inputNormal = m_withNormal ? getInputAccessor(davinci::VET_NORMAL) : nullptr;
davinci::DeviceBufferAccessorConstPtr inputTexcoord0 = m_withTexCoord0 ? getInputAccessor(davinci::VET_TEXCOORD_0) : nullptr;
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(inputTexcoord0 ? (const float32_t*)(inputTexcoord0->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;
}
davinci::fVector3 vertexWorldPos = mat_world * a_position;
//output NDC position
{
davinci::fVector3 vertexNDCPos = mat_camera_view_proj * vertexWorldPos;
memcpy((char*)outputVertex + m_outputVertexDesc.getElementByType(davinci::VET_POSITION_NDC)->offset, &vertexNDCPos, sizeof(davinci::fVector3));
}
//output world position
if (m_withWorldPos)
{
memcpy((char*)outputVertex + m_outputVertexDesc.getElementByType(davinci::VET_POSITION_WORLD)->offset, &vertexWorldPos, sizeof(davinci::fVector3));
}
//output normal
if (m_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.getElementByType(davinci::VET_NORMAL)->offset, &v_normal, sizeof(davinci::fVector3));
}
//output uv0
if (m_withTexCoord0)
{
memcpy((char*)outputVertex + m_outputVertexDesc.getElementByType(davinci::VET_TEXCOORD_0)->offset, &a_uv, sizeof(float32_t) * 2);
}
}
void StandardVertexShader::end()
{
}
+51
View File
@@ -0,0 +1,51 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class StandardVertexShader : public davinci::VertexShaderInterface
{
public:
virtual const char* getName(void) const {
return m_name.c_str();
}
virtual const davinci::VertexDesc& getOutputVertexDesc() const {
return m_outputVertexDesc;
}
virtual size_t getOutputVertexByteSize(void) const {
return m_outputVertexDesc.vertexByteSize();
}
virtual void begin(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc);
virtual void process(size_t index, float32_t* outputVertex);
virtual void end();
protected:
davinci::DeviceBufferAccessorConstPtr getInputAccessor(davinci::VertexElementType elementType) const {
auto it = m_inputAccessors.find(elementType);
if (it != m_inputAccessors.end()) {
return it->second;
}
return nullptr;
}
private:
const davinci::RenderPipe* m_pipe = nullptr;
std::string m_name;
bool m_withWorldPos = false;
bool m_withNormal = false;
bool m_withTexCoord0 = false;
typedef std::map<davinci::VertexElementType, davinci::DeviceBufferAccessorConstPtr> InputAccessorMap;
InputAccessorMap m_inputAccessors;
davinci::VertexDesc m_outputVertexDesc;
public:
StandardVertexShader(bool withWorldPos, bool withNormal, bool withTexCoord0);
virtual ~StandardVertexShader();
};
+15
View File
@@ -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
+3 -2
View File
@@ -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
@@ -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
+12 -5
View File
@@ -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;
+283 -46
View File
@@ -8,11 +8,20 @@
#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 "standard/dvu_standard_material.h"
#include <tiny_gltf.h>
#include <string>
@@ -94,6 +103,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)
{
@@ -110,7 +138,7 @@ davinci::VertexElementType _getElementTypeFromGltf(const std::string& gltfElemen
if (gltfElementType == "POSITION")
{
return davinci::VET_POSITION;
return davinci::VET_POSITION_MODEL;
}
else if(gltfElementType == "NORMAL")
{
@@ -132,7 +160,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,9 +174,8 @@ davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedInteg
}
davinci::DataType dataType = inpuptBufferAccesser->getDataType();
davinci::DeviceBufferPtr newBuffer = scene.newDeviceBuffer();
size_t bufferSize = elementCounts * davinci::MathUtil::ComponentSize(davinci::ComponentType::CT_Float32) * davinci::MathUtil::DataTypeSize(dataType);
newBuffer->init(bufferSize);
davinci::DeviceBufferPtr newBuffer = device.newDeviceBuffer(bufferSize);
if (dataType == davinci::DataType::DT_Scalar)
{
@@ -254,12 +282,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();
@@ -267,7 +296,7 @@ davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccesso
switch (vetType)
{
case davinci::VET_POSITION:
case davinci::VET_POSITION_MODEL:
case davinci::VET_NORMAL:
case davinci::VET_BINORMAL:
if (componentType == davinci::ComponentType::CT_Float32 && dataType == davinci::DataType::DT_Vec3)
@@ -299,7 +328,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 +349,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 +387,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 +408,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 +472,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<gltfModel.meshes.size(); i++)
{
const tinygltf::Mesh& gltfMesh = gltfModel.meshes[i];
tinygltf::Mesh& gltfMesh = gltfModel.meshes[i];
davinci::MeshPtr mesh = std::make_shared<davinci::Mesh>(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; j<gltfMesh.primitives.size(); j++)
{
@@ -459,21 +496,28 @@ bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene)
davinci::Mesh::PrimitivePtr primitive = mesh->newPrimitive();
primitive->type = _getPrimitiveTypeFromGltf(gltfPrimitive.mode);
//apply attributes
std::map<std::string, int>::const_iterator it(gltfPrimitive.attributes.begin());
std::map<std::string, int>::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 +526,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 +545,177 @@ 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;
}
else
{
davinci::MaterialPtr materialPtr = create_standard_material(&device, "classic:phong");
materialPtr->setParam("u_materialColor", davinci::fVector4::WHITE);
materialPtr->setParam("u_specularShininess", 32.f);
materialPtr->setupMaterial();
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<double>& 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]);
}
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++)
{
tinygltf::Material& gltfMaterial = gltfModel.materials[i];
//create new material
davinci::MaterialPtr materialPtr = create_standard_material(&device, "pbr:gltf");
//apply matellic roughness info
const tinygltf::PbrMetallicRoughness& gltfPBRMetllicRoughness = gltfMaterial.pbrMetallicRoughness;
//base color texture
const tinygltf::TextureInfo& gltfBaseColorTextureInfo = gltfPBRMetllicRoughness.baseColorTexture;
davinci::TextureInfo baseColorTextureInfo = davinci::TextureInfo{
_getTexturePtr(gltfModel, device, gltfBaseColorTextureInfo.index),
gltfBaseColorTextureInfo.texCoord,
_getTextureTransform(gltfBaseColorTextureInfo.extensions)
};
materialPtr->setParam("u_baseColorTextureInfo", baseColorTextureInfo);
//matallic & roughness texture
const tinygltf::TextureInfo& gltfMetalicRoughnessTextureInfo = gltfPBRMetllicRoughness.metallicRoughnessTexture;
davinci::TextureInfo metalicRoughnessTextureInfo = davinci::TextureInfo{
_getTexturePtr(gltfModel, device, gltfMetalicRoughnessTextureInfo.index),
gltfMetalicRoughnessTextureInfo.texCoord,
_getTextureTransform(gltfMetalicRoughnessTextureInfo.extensions),
};
materialPtr->setParam("u_metalicRoughnessTextureInfo", baseColorTextureInfo);
materialPtr->setParam("u_metalicFactor", static_cast<float32_t>(gltfPBRMetllicRoughness.metallicFactor));
materialPtr->setParam("u_roughnessFactor", static_cast<float32_t>(gltfPBRMetllicRoughness.roughnessFactor));
//normal texture
const tinygltf::NormalTextureInfo& gltfNormalTextureInfo = gltfMaterial.normalTexture;
davinci::TextureInfo normalTextureInfo = davinci::TextureInfo{
_getTexturePtr(gltfModel, device, gltfNormalTextureInfo.index),
gltfNormalTextureInfo.texCoord,
_getTextureTransform(gltfNormalTextureInfo.extensions),
};
materialPtr->setParam("u_normalTextureInfo", normalTextureInfo);
materialPtr->setParam("u_normalTextureScale", static_cast<float32_t>(gltfMaterial.normalTexture.scale));
//load shader
if (!(materialPtr->setupMaterial()))
{
return false;
}
gltfMaterial.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)materialPtr->getID())));
}
return true;
}
@@ -558,8 +777,7 @@ bool _loadLightExternsion(const tinygltf::Model& gltfModel, std::vector<davinci:
return true;
}
bool _loadSceneNodes(const tinygltf::Model& gltfModel, davinci::Scene& scene, const std::vector<davinci::LightComponent::LightDefine>& lightsArray)
bool _loadSceneNodes(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene, const std::vector<davinci::LightComponent::LightDefine>& lightsArray)
{
std::vector<std::pair<bool, davinci::SceneNodePtr>> sceneNodeArray;
@@ -599,7 +817,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<davinci::MeshComponent>(sceneNode);
@@ -707,7 +928,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 +939,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 +976,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 +1019,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 +1035,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 +1154,7 @@ bool export_mesh_to_obj(const davinci::Scene& scene)
fclose(fp);
}
#endif
return true;
}
@@ -0,0 +1,174 @@
#include "standard/dvu_standard_material.h"
#include "device/dvc_device.h"
#include "asset/dvc_material.h"
DV_UTILS_BEGIN_NAMESPACE
class PbrGltfMaterial : public davinci::Material
{
public:
PbrGltfMaterial(int32_t id, const char* name) : davinci::Material(id, name)
{
setVertexShader("dv_standard_shaders.dll", "standard+worldpos+normal+uv0");
setFragementShader("dv_standard_shaders.dll", "pbr:gltf");
registerParam<davinci::TextureInfo>("u_baseColorTextureInfo", {});
registerParam<davinci::TextureInfo>("u_metalicRoughnessTextureInfo", {});
registerParam<float32_t>("u_metalicFactor", 1.f);
registerParam<float32_t>("u_roughnessFactor", 1.f);
registerParam<davinci::TextureInfo>("u_normalTextureInfo", {});
registerParam<float32_t>("u_normalTextureScale", 1.f);
}
};
class PhongMaterial : public davinci::Material
{
public:
PhongMaterial(int32_t id, const char* name) : davinci::Material(id, name)
{
setVertexShader("dv_standard_shaders.dll", "standard+worldpos+normal-uv0");
setFragementShader("dv_standard_shaders.dll", "classic:phong");
registerParam<davinci::fVector4>("u_materialColor", davinci::fVector4::GRAY);
registerParam<float32_t>("u_specularShininess", 64.f);
registerParam<float32_t>("u_specularStrength", 5.f);
}
virtual bool pickupParam(const davinci::MaterialConstPtr orginalMaterial) override
{
return true;
}
};
class SolidColorMaterial : public davinci::Material
{
public:
SolidColorMaterial(int32_t id, const char* name) : davinci::Material(id, name)
{
setVertexShader("dv_standard_shaders.dll", "standard-worldpos-normal-uv0");
setFragementShader("dv_standard_shaders.dll", "debug:solid_color");
registerParam<davinci::fVector4>("u_solidColor", davinci::fVector4::WHITE);
}
virtual bool pickupParam(const davinci::MaterialConstPtr orginalMaterial) override
{
return true;
}
};
class BaseColorTextureMaterial : public davinci::Material
{
public:
BaseColorTextureMaterial(int32_t id, const char* name) : davinci::Material(id, name)
{
setVertexShader("dv_standard_shaders.dll", "standard+worldpos-normal+uv0");
setFragementShader("dv_standard_shaders.dll", "debug:show_texture");
registerParam<davinci::TextureInfo>("u_textureInfo", {});
}
virtual bool pickupParam(const davinci::MaterialConstPtr orginalMaterial) override
{
davinci::TextureInfo baseColorTextureInfo;
if (!(orginalMaterial->getParam("u_baseColorTextureInfo", baseColorTextureInfo)))
{
return false;
}
this->setParam("u_textureInfo", baseColorTextureInfo);
return true;
}
};
class NormalTextureMaterial : public davinci::Material
{
public:
NormalTextureMaterial(int32_t id, const char* name) : davinci::Material(id, name)
{
setVertexShader("dv_standard_shaders.dll", "standard+worldpos-normal+uv0");
setFragementShader("dv_standard_shaders.dll", "debug:show_texture");
registerParam<davinci::TextureInfo>("u_textureInfo", {});
}
virtual bool pickupParam(const davinci::MaterialConstPtr orginalMaterial) override
{
davinci::TextureInfo normalTextureInfo;
if (!(orginalMaterial->getParam("u_normalTextureInfo", normalTextureInfo)))
{
return false;
}
this->setParam("u_textureInfo", normalTextureInfo);
return true;
}
};
class TextureCoordinates0Material : public davinci::Material
{
public:
TextureCoordinates0Material(int32_t id, const char* name) : davinci::Material(id, name)
{
setVertexShader("dv_standard_shaders.dll", "standard+worldpos-normal+uv0");
setFragementShader("dv_standard_shaders.dll", "debug:texture_coordinates");
registerParam<int32_t>("u_texCoord", 0);
}
virtual bool pickupParam(const davinci::MaterialConstPtr orginalMaterial) override
{
return true;
}
};
class GeometryNormalMaterial : public davinci::Material
{
public:
GeometryNormalMaterial(int32_t id, const char* name) : davinci::Material(id, name)
{
setVertexShader("dv_standard_shaders.dll", "standard+worldpos+normal-uv0");
setFragementShader("dv_standard_shaders.dll", "debug:geometry_normal");
}
virtual bool pickupParam(const davinci::MaterialConstPtr orginalMaterial) override
{
return true;
}
};
davinci::MaterialPtr create_standard_material(davinci::Device* device, const char* name)
{
if(strcmp(name, "pbr:gltf")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset<PbrGltfMaterial>(name);
return materialPtr;
}
else if(strcmp(name, "classic:phong")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset<PhongMaterial>(name);
return materialPtr;
}
else if(strcmp(name, "debug:solid_color")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset<SolidColorMaterial>(name);
return materialPtr;
}
else if (strcmp(name, "debug:base_color_texture")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset<BaseColorTextureMaterial>(name);
return materialPtr;
}
else if (strcmp(name, "debug:normal_texture")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset<NormalTextureMaterial>(name);
return materialPtr;
}
else if (strcmp(name, "debug:texture_coordinates_0")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset<TextureCoordinates0Material>(name);
return materialPtr;
}
else if (strcmp(name, "debug:geometry_normal")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset<GeometryNormalMaterial>(name);
return materialPtr;
}
return nullptr;
}
DV_UTILS_END_NAMESPACE
-1
View File
@@ -2,5 +2,4 @@ set(GTest_DIR $ENV{LIBRARIES_SDK_ROOT}/gtest/lib/cmake/GTest)
find_package(GTest CONFIG REQUIRED)
add_subdirectory(unit)
add_subdirectory(shader)
add_subdirectory(rasterization)
-28
View File
@@ -1,28 +0,0 @@
add_library(TestShader SHARED
dll_main.cpp
test_vertex_shader.h
test_vertex_shader.cpp
test_vertex_shader.osl
test_pixel_shader.h
test_pixel_shader.cpp
test_pixel_shader.osl
)
add_definitions(-DDV_SHADER_EXPORTS)
target_include_directories(TestShader
PRIVATE
${DV_AUTO_INCLUDE_PATH}
${DV_CORE_INCLUDE_PATH}
)
target_link_libraries(TestShader
dv_core
)
#################
# copy runtime dll(for debug)
#################
add_custom_command(
TARGET TestShader POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:TestShader> ${CMAKE_BINARY_DIR}/source/console/$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>)
-49
View File
@@ -1,49 +0,0 @@
#include "dv_config.h"
#include "shader/dvc_shader_interface.h"
#include "test_vertex_shader.h"
#include "test_pixel_shader.h"
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)
{
static TestVertexShader testVertexShader;
return &testVertexShader;
}
return nullptr;
}
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)
{
return &testPixelShader;
}
return nullptr;
}
};
extern "C"
{
__declspec(dllexport) davinci::ShaderFileInterface* ShaderFileInterface()
{
static TestShaderInterface theInterface;
return &theInterface;
}
}
-109
View File
@@ -1,109 +0,0 @@
OpenShadingLanguage 1.00
# Compiled by oslc 1.13.12.0
# options:
shader phong_shader
param point v_position 0 0 0 %read{2147483647,-1} %write{0,0} %initexpr
param vector v_normal 0 1 0 %read{3,3} %write{2147483647,-1}
param vector v_toCamera 0 0 0 %read{2,2} %write{2147483647,-1}
param vector v_toLight 0 0 0 %read{1,1} %write{2147483647,-1}
oparam color resultRGB 0 0 0 %read{2147483647,-1} %write{27,27}
local vector vL %read{4,14} %write{1,1}
local vector vV %read{8,8} %write{2,2}
local vector vN %read{4,18} %write{3,3}
local vector vR %read{2147483647,-1} %write{7,7}
local vector vH %read{18,18} %write{9,9}
local color u_ambientColor %read{13,13} %write{10,10}
local color u_matColor %read{13,23} %write{11,11}
local color u_lightColor %read{17,24} %write{12,12}
local color colAmbient %read{25,25} %write{13,13}
local float ndotl %read{16,20} %write{15,15}
local color colDiff %read{25,25} %write{17,17}
local float spec %read{21,21} %write{19,19}
local color colSpec %read{26,26} %write{24,24}
local color final %read{27,27} %write{26,26}
const vector $const1 0 0 0 %read{0,0} %write{2147483647,-1}
temp float $tmp1 %read{5,5} %write{4,4}
temp float $tmp2 %read{6,6} %write{5,5}
const float $const3 2 %read{5,5} %write{2147483647,-1}
temp vector $tmp3 %read{7,7} %write{6,6}
temp vector $tmp4 %read{9,9} %write{8,8}
temp int $tmp5 %read{2147483647,-1} %write{10,10}
const string $const4 "env:ambient_color" %read{10,10} %write{2147483647,-1}
temp int $tmp6 %read{2147483647,-1} %write{11,11}
const string $const5 "self:mat_color" %read{11,11} %write{2147483647,-1}
temp int $tmp7 %read{2147483647,-1} %write{12,12}
const string $const6 "light0:color" %read{12,12} %write{2147483647,-1}
temp float $tmp8 %read{15,15} %write{14,14}
const float $const8 0 %read{15,20} %write{2147483647,-1}
temp color $tmp9 %read{17,17} %write{16,16}
temp float $tmp10 %read{19,19} %write{18,18}
temp float $tmp11 %read{22,22} %write{20,20}
temp float $tmp12 %read{22,22} %write{21,21}
const float $const10 64 %read{21,21} %write{2147483647,-1}
temp float $tmp13 %read{23,23} %write{22,22}
temp color $tmp14 %read{24,24} %write{23,23}
temp color $tmp15 %read{26,26} %write{25,25}
const vector $const11 0.454545438 0.454545438 0.454545438 %read{27,27} %write{2147483647,-1}
code v_position
# test_pixel_shader.osl:4
# point v_position = vector(0,0,0),
assign v_position $const1 %filename{"test_pixel_shader.osl"} %line{4} %argrw{"wr"}
code ___main___
# test_pixel_shader.osl:11
# vector vL = normalize(v_toLight);
normalize vL v_toLight %filename{"test_pixel_shader.osl"} %line{11} %argrw{"wr"}
# test_pixel_shader.osl:12
# vector vV = normalize(v_toCamera);
normalize vV v_toCamera %line{12} %argrw{"wr"}
# test_pixel_shader.osl:13
# vector vN = normalize(v_normal);
normalize vN v_normal %line{13} %argrw{"wr"}
# test_pixel_shader.osl:14
# vector vR = 2 * dot(vL, vN)*vN - vL;
dot $tmp1 vL vN %line{14} %argrw{"wrr"}
mul $tmp2 $const3 $tmp1 %argrw{"wrr"}
mul $tmp3 $tmp2 vN %argrw{"wrr"}
sub vR $tmp3 vL %argrw{"wrr"}
# test_pixel_shader.osl:15
# vector vH = normalize(vL + vV);
add $tmp4 vL vV %line{15} %argrw{"wrr"}
normalize vH $tmp4 %argrw{"wr"}
# test_pixel_shader.osl:18
# getattribute("env:ambient_color", u_ambientColor);
getattribute $tmp5 $const4 u_ambientColor %line{18} %argrw{"wrw"}
# test_pixel_shader.osl:21
# getattribute("self:mat_color", u_matColor);
getattribute $tmp6 $const5 u_matColor %line{21} %argrw{"wrw"}
# test_pixel_shader.osl:24
# getattribute("light0:color", u_lightColor);
getattribute $tmp7 $const6 u_lightColor %line{24} %argrw{"wrw"}
# test_pixel_shader.osl:26
# color colAmbient = u_ambientColor*u_matColor;
mul colAmbient u_ambientColor u_matColor %line{26} %argrw{"wrr"}
# test_pixel_shader.osl:28
# float ndotl = max(dot(vN, vL), 0);
dot $tmp8 vN vL %line{28} %argrw{"wrr"}
max ndotl $tmp8 $const8 %argrw{"wrr"}
# test_pixel_shader.osl:29
# color colDiff = ndotl * u_matColor * u_lightColor;
mul $tmp9 ndotl u_matColor %line{29} %argrw{"wrr"}
mul colDiff $tmp9 u_lightColor %argrw{"wrr"}
# test_pixel_shader.osl:31
# float spec = max(dot(vN, vH), 0);
dot $tmp10 vN vH %line{31} %argrw{"wrr"}
max spec $tmp10 $const8 %argrw{"wrr"}
# test_pixel_shader.osl:32
# color colSpec = step(0, ndotl) * pow(spec, 64) * u_matColor * u_lightColor;
step $tmp11 $const8 ndotl %line{32} %argrw{"wrr"}
pow $tmp12 spec $const10 %argrw{"wrr"}
mul $tmp13 $tmp11 $tmp12 %argrw{"wrr"}
mul $tmp14 $tmp13 u_matColor %argrw{"wrr"}
mul colSpec $tmp14 u_lightColor %argrw{"wrr"}
# test_pixel_shader.osl:34
# color final = colAmbient + colDiff + colSpec;
add $tmp15 colAmbient colDiff %line{34} %argrw{"wrr"}
add final $tmp15 colSpec %argrw{"wrr"}
# test_pixel_shader.osl:37
# resultRGB = pow(final, vector(1.0/2.2));
pow resultRGB final $const11 %line{37} %argrw{"wrr"}
end
-64
View File
@@ -1,64 +0,0 @@
OpenShadingLanguage 1.00
# Compiled by oslc 1.13.12.0
# options:
shader standard_shader
param point a_position 0 0 0 %meta{string,attribute,"POSITION"} %read{5,5} %write{0,0} %initexpr
param vector a_normal 0 1 0 %meta{string,attribute,"NORMAL"} %read{7,7} %write{2147483647,-1}
param float[2] a_uv 0 0 %meta{string,attribute,"TEXCOORD_0"} %read{8,8} %write{2147483647,-1}
oparam point v_position 0 0 0 %read{9,11} %write{6,6}
oparam vector v_normal 0 1 0 %read{2147483647,-1} %write{7,7}
oparam float[2] v_uv 0 0 %read{2147483647,-1} %write{8,8}
oparam vector v_toCamera 0 0 0 %read{2147483647,-1} %write{10,10}
oparam vector v_toLight 0 0 0 %read{2147483647,-1} %write{12,12}
local matrix mat_world %read{5,5} %write{1,1}
local matrix mat_camera_view_proj %read{6,6} %write{2,2}
local point camera_position %read{9,9} %write{3,3}
local point light0_position %read{11,11} %write{4,4}
const vector $const1 0 0 0 %read{0,0} %write{2147483647,-1}
temp int $tmp1 %read{2147483647,-1} %write{1,1}
const string $const2 "self:world_transform" %read{1,1} %write{2147483647,-1}
temp int $tmp2 %read{2147483647,-1} %write{2,2}
const string $const3 "camera:view_proj" %read{2,2} %write{2147483647,-1}
temp int $tmp3 %read{2147483647,-1} %write{3,3}
const string $const4 "camera:position" %read{3,3} %write{2147483647,-1}
temp int $tmp4 %read{2147483647,-1} %write{4,4}
const string $const5 "light0:position" %read{4,4} %write{2147483647,-1}
temp point $tmp5 %read{6,6} %write{5,5}
temp vector $tmp6 %read{10,10} %write{9,9}
temp vector $tmp7 %read{12,12} %write{11,11}
code a_position
# test_vertex_shader.osl:6
# point a_position = vector(0, 0, 0) [[ string attribute="POSITION"]],
assign a_position $const1 %filename{"test_vertex_shader.osl"} %line{6} %argrw{"wr"}
code ___main___
# test_vertex_shader.osl:19
# getattribute("self:world_transform", mat_world);
getattribute $tmp1 $const2 mat_world %filename{"test_vertex_shader.osl"} %line{19} %argrw{"wrw"}
# test_vertex_shader.osl:25
# getattribute("camera:view_proj", mat_camera_view_proj);
getattribute $tmp2 $const3 mat_camera_view_proj %line{25} %argrw{"wrw"}
# test_vertex_shader.osl:28
# getattribute("camera:position", camera_position);
getattribute $tmp3 $const4 camera_position %line{28} %argrw{"wrw"}
# test_vertex_shader.osl:31
# getattribute("light0:position", light0_position);
getattribute $tmp4 $const5 light0_position %line{31} %argrw{"wrw"}
# test_vertex_shader.osl:34
# v_position = transform(mat_camera_view_proj, transform(mat_world, a_position));
transform $tmp5 mat_world a_position %line{34} %argrw{"wrr"}
transform v_position mat_camera_view_proj $tmp5 %argrw{"wrr"}
# test_vertex_shader.osl:35
# v_normal = a_normal;
assign v_normal a_normal %line{35} %argrw{"wr"}
# test_vertex_shader.osl:36
# v_uv = a_uv;
arraycopy v_uv a_uv %line{36} %argrw{"wr"}
# test_vertex_shader.osl:38
# v_toCamera = normalize(camera_position-v_position);
sub $tmp6 camera_position v_position %line{38} %argrw{"wrr"}
normalize v_toCamera $tmp6 %argrw{"wr"}
# test_vertex_shader.osl:39
# v_toLight = normalize(light0_position-v_position);
sub $tmp7 light0_position v_position %line{39} %argrw{"wrr"}
normalize v_toLight $tmp7 %argrw{"wr"}
end
-92
View File
@@ -1,92 +0,0 @@
#include "test_pixel_shader.h"
const char* TestPixelShader::getName(void) const
{
return "test";
}
void TestPixelShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc)
{
}
void TestPixelShader::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::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::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 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 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 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 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)));
//vector vL = normalize(v_toLight);
davinci::fVector3 vL = *v_toLight;
vL.normalise();
//vector vV = normalize(v_toCamera);
davinci::fVector3 vV = *v_toCamera;
vV.normalise();
//vector vN = normalize(v_normal);
davinci::fVector3 vN = *v_normal;
vN.normalise();
//vector vR = 2 * dot(vL, vN)*vN - vL;
davinci::fVector3 vR = 2 * vL.dotProduct(vN) * vN - vL;
//vector vH = normalize(vL + vV);
davinci::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;
//float ndotl = max(dot(vN, vL), 0);
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;
//float spec = max(dot(vN, vH), 0);
float32_t spec = std::max(vN.dotProduct(vH), 0.0f);
//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;
//color final = colAmbient + colDiff + colSpec;
davinci::fVector3 final = colAmbient +colDiff +colSpec;
//davinci::fVector3 final = *v_color0;
//color = davinci::fVector4::RED;
color = davinci::fVector4(final.saturate(), 1.0f);// davinci::fVector4::RED;
}
const char* SolidColorPixelShader::getName(void) const
{
return "solid";
}
void SolidColorPixelShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc)
{
}
void SolidColorPixelShader::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
{
color = davinci::fVector4::RED;
}
-26
View File
@@ -1,26 +0,0 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class TestPixelShader : public davinci::PixelShaderInterface
{
public:
virtual const char* getName(void) const;
virtual void prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
};
class SolidColorPixelShader : public davinci::PixelShaderInterface
{
public:
virtual const char* getName(void) const;
virtual void prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
};
-38
View File
@@ -1,38 +0,0 @@
shader
phong_shader(
point v_position = vector(0,0,0),
vector v_normal = vector(0,1,0),
vector v_toCamera = vector(0,0,0),
vector v_toLight = vector(0,0,0),
output color resultRGB = 0
)
{
vector vL = normalize(v_toLight);
vector vV = normalize(v_toCamera);
vector vN = normalize(v_normal);
vector vR = 2 * dot(vL, vN)*vN - vL;
vector vH = normalize(vL + vV);
color u_ambientColor;
getattribute("env:ambient_color", u_ambientColor);
color u_matColor;
getattribute("self:mat_color", u_matColor);
color u_lightColor;
getattribute("light0:color", u_lightColor);
color colAmbient = u_ambientColor*u_matColor;
float ndotl = max(dot(vN, vL), 0);
color colDiff = ndotl * u_matColor * u_lightColor;
float spec = max(dot(vN, vH), 0);
color colSpec = step(0, ndotl) * pow(spec, 64) * u_matColor * u_lightColor;
color final = colAmbient + colDiff + colSpec;
//gamma
resultRGB = pow(final, vector(1.0/2.2));
}
-182
View File
@@ -1,182 +0,0 @@
#include "test_vertex_shader.h"
#include "device/dvc_buffer_accessor.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector3.h"
#include "math/dvc_matrix4.h"
#include "device/dvc_uniform_buffer.h"
TestVertexShader::TestVertexShader()
{
//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);
//output vector v_normal = vector(0,1,0) [[ string attribute="NORMAL"]],
m_outputVertexDesc.addElement("v_normal", davinci::VET_NORMAL, davinci::CT_Float32, davinci::DT_Vec3);
//output float v_uv[2] = {0, 0} [[ string attribute="TEXCOORD_0"]],
m_outputVertexDesc.addElement("v_uv", davinci::VET_TEXCOORD_0, davinci::CT_Float32, davinci::DT_Vec2);
//output vector v_toCamera = vector(0, 0, 0),
m_outputVertexDesc.addElement("v_toCamera", davinci::VET_UNKNOWN, davinci::CT_Float32, davinci::DT_Vec3);
//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);
}
TestVertexShader::~TestVertexShader()
{
}
const char* TestVertexShader::getName(void) const
{
return "test";
}
size_t TestVertexShader::getOutputVertexByteSize(void) const
{
return m_outputVertexDesc.vertexByteSize();
}
const davinci::VertexDesc& TestVertexShader::getOutputVertexDesc() const
{
return m_outputVertexDesc;
}
void TestVertexShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc)
{
m_uniformBuffer = uniformBuffer;
//point a_position = vector(0, 0, 0) [[ string attribute="POSITION"]],
m_POSITION = setInputAttributeFunc(davinci::VET_POSITION);
//vector a_normal = vector(0, 1, 0) [[ string attribute="NORMAL"]],
m_NORMAL = setInputAttributeFunc(davinci::VET_NORMAL);
//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);
}
void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos)
{
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 };
davinci::fVector3 a_position(m_POSITION ? (const float32_t*)(m_POSITION->getElement(index)) : DEFAULT_POSITION);
davinci::fVector3 a_normal(m_NORMAL ? (const float32_t*)(m_NORMAL->getElement(index)) : DEFAULT_NORMAL);
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::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY;
if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(m_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")))
{
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")))
{
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")))
{
light0_position = *p_light0_position;
}
davinci::fVector3 temp1 = mat_world * a_position;
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")))
{
matNormal = *p_mat_normal;
}
davinci::fVector3 v_normal = (matNormal * davinci::fVector4(a_normal, 0)).xyz().normalise();
davinci::fVector3 v_color_0 = a_color_0;
davinci::fVector3 v_to_camera = (camera_position - temp1).normalise();
davinci::fVector3 v_to_light = (light0_position - temp1).normalise();
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));
vertexWorldPos = mat_world * a_position;
}
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);
}
StandardVertexShader::~StandardVertexShader()
{
}
const char* StandardVertexShader::getName(void) const
{
return "standard";
}
size_t StandardVertexShader::getOutputVertexByteSize(void) const
{
return m_outputVertexDesc.vertexByteSize();
}
const davinci::VertexDesc& StandardVertexShader::getOutputVertexDesc() const
{
return m_outputVertexDesc;
}
void StandardVertexShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc)
{
m_uniformBuffer = uniformBuffer;
//point a_position = vector(0, 0, 0) [[ string attribute="POSITION"]],
m_POSITION = setInputAttributeFunc(davinci::VET_POSITION);
}
void StandardVertexShader::process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos)
{
static const float DEFAULT_POSITION[] = {0.f, 0.f, 0.f};
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")))
{
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")))
{
mat_camera_view_proj = *p_mat_camera_view_proj;
}
davinci::fVector3 temp1 = mat_world * a_position;
davinci::fVector3 v_position = mat_camera_view_proj * temp1;
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(0)->offset, &v_position, sizeof(davinci::fVector3));
vertexWorldPos = mat_world * a_position;
}
-52
View File
@@ -1,52 +0,0 @@
#pragma once
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "shader/dvc_shader_interface.h"
#include "device/dvc_vertex_desc.h"
class TestVertexShader : 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::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc);
virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos);
private:
const davinci::UniformBuffer* m_uniformBuffer;
davinci::DeviceBufferAccessorConstPtr m_POSITION;
davinci::DeviceBufferAccessorConstPtr m_NORMAL;
davinci::DeviceBufferAccessorConstPtr m_TEXCOORD_0;
davinci::DeviceBufferAccessorConstPtr m_COLOR_0;
davinci::VertexDesc m_outputVertexDesc;
public:
TestVertexShader();
virtual ~TestVertexShader();
};
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::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc);
virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos);
private:
const davinci::UniformBuffer* m_uniformBuffer = nullptr;
davinci::DeviceBufferAccessorConstPtr m_POSITION;
davinci::VertexDesc m_outputVertexDesc;
public:
StandardVertexShader();
virtual ~StandardVertexShader();
};
-40
View File
@@ -1,40 +0,0 @@
//
shader
standard_shader(
point a_position = vector(0, 0, 0) [[ string attribute="POSITION"]],
vector a_normal = vector(0, 1, 0) [[ string attribute="NORMAL"]],
float a_uv[2] = {0,0} [[ string attribute="TEXCOORD_0"]],
output point v_position = point(0, 0, 0) [[ string attribute="POSITION"]],
output vector v_normal = vector(0,1,0) [[ string attribute="NORMAL"]],
output float v_uv[2] = {0, 0} [[ string attribute="TEXCOORD_0"]],
output vector v_toCamera = vector(0, 0, 0),
output vector v_toLight = vector(0, 0, 0)
)
{
matrix mat_world;
getattribute("self:world_transform", mat_world);
//matrix mat_normal;
//getattribute("self:normal_transform", mat_normal);
matrix mat_camera_view_proj;
getattribute("camera:view_proj", mat_camera_view_proj);
point camera_position;
getattribute("camera:eye_pos", camera_position);
point light0_position;
getattribute("light0:position", light0_position);
v_position = transform(mat_camera_view_proj, transform(mat_world, a_position));
v_normal = a_normal;
v_uv = a_uv;
v_toCamera = normalize(camera_position-v_position);
v_toLight = normalize(light0_position-v_position);
}