重构材质系统

This commit is contained in:
2025-12-03 23:38:08 +08:00
parent bf62257884
commit 3c0e177905
47 changed files with 755 additions and 1151 deletions
+1
View File
@@ -29,6 +29,7 @@ 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[])
+43 -3
View File
@@ -17,7 +17,8 @@
#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;
@@ -28,6 +29,10 @@ 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)
{
@@ -107,8 +112,19 @@ void drawMainUI()
{
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",
"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(g_device);
@@ -125,12 +141,36 @@ void drawMainUI()
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();
+76 -70
View File
@@ -2,63 +2,79 @@
#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:
struct TextureInfo
typedef std::variant<int32_t, float32_t, fVector2, fVector3, fVector4, fMatrix3, fMatrix4, TextureInfo> Value;
struct Param
{
TextureConstPtr texture;
int32_t texCoord = 0;
fMatrix3 uvTransform;
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; }
void setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
const fVector4& baseColorfactor);
const TextureInfo& getBaseColorTexture() const {
return m_baseColorTexture;
}
const fVector4& getBaseColorFactor() const {
return m_baseColorFactor;
}
void setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
float32_t metallicFactor = 1, float32_t roughnessFactor = 1);
const TextureInfo& getMetallicRoughnessTexture() const {
return m_metallicRoughnessTexture;
}
float32_t getMetallicFactor() const {
return m_metallicFactor;
}
float32_t getRoughnessFactor() const {
return m_roughnessFactor;
}
void setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
float32_t scale = 1);
const TextureInfo& getNormalTexture() const {
return m_normalTexture;
}
float32_t getNormalScale() const {
return m_normalScale;
}
void setVertexShader(const char* szShaderFile, const char* vertexShaderName);
void setFragementShader(const char* szShaderFile, const char* fragementShaderName);
VertexShaderInterface* getVertexShader() const {
return m_vertexShader;
}
@@ -67,37 +83,26 @@ public:
return m_pixelShader;
}
virtual bool setupMaterial();
virtual bool pickupParam(const davinci::MaterialConstPtr otherMaterial) {
return true;
}
public:
bool loadShader();
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;
/*The base color texture.
* The first three components (RGB) MUST be encoded with the sRGB transfer function.
*/
TextureInfo m_baseColorTexture;
fVector4 m_baseColorFactor = fVector4::ONE;
/*The metallic - roughness texture.
* The metalness values are sampled from the B channel.
* The roughness values are sampled from the G channel.
*/
TextureInfo m_metallicRoughnessTexture;
float32_t m_metallicFactor = 1.0f;
float32_t m_roughnessFactor = 1.0f;
/*The normal texture.
*/
TextureInfo m_normalTexture;
/*The scalar parameter applied to each normal vector of the texture.
* This value scales the normal vector in X and Y directions using the formula:
* scaledNormal = (normalize<sampled normal texture value> * 2.0 - 1.0) * vec3(<normal scale>, <normal scale>, 1.0).
*/
float32_t m_normalScale = 1.0f;
protected:
VertexShaderInterface* m_vertexShader = nullptr;
PixelShaderInterface* m_pixelShader = nullptr;
@@ -107,8 +112,9 @@ protected:
std::string m_fragementShaderFile;
std::string m_fragementShaderName;
ParamMap m_params;
public:
Material(int32_t id);
Material(int32_t id, const char* name);
~Material();
};
+8
View File
@@ -26,4 +26,12 @@ public:
Texture(int32_t id);
~Texture();
};
struct TextureInfo
{
TextureConstPtr texture;
int32_t texCoord = 0;
fMatrix3 uvTransform;
};
DV_CORE_END_NAMESPACE
+7 -1
View File
@@ -30,7 +30,13 @@ public:
TexturePtr newTextureAsset();
TexturePtr getTexture(int32_t id) const;
MaterialPtr newMaterialAsset();
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:
+2 -1
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,
@@ -17,18 +17,28 @@ public:
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);
@@ -38,6 +48,7 @@ protected:
AntiAliasingTech m_aaTech = AntiAliasingTech::AA_None;
UniformBuffer m_uniformBuffer;
RenderableArray m_renderableQueue;
MaterialPtr m_debugMaterial;
public:
RenderPipe(Device& device);
@@ -31,7 +31,7 @@ public:
return (a < b) ? a : b;
}
static DV_CORE_API fVector4 texture2D(const RenderPipe& pipe, const Material::TextureInfo& texturePtr, const fVector2& uv, const fVector4& defaultValue);
static DV_CORE_API fVector4 texture2D(const RenderPipe& pipe, const TextureInfo& texturePtr, const fVector2& uv, const fVector4& defaultValue);
};
DV_CORE_END_NAMESPACE
@@ -16,9 +16,11 @@ public:
virtual const VertexDesc& getOutputVertexDesc() const = 0;
typedef std::function<DeviceBufferAccessorConstPtr(VertexElementType)> SetInputAttributeFunc;
virtual void prepare(const RenderPipe* pipe, 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
@@ -26,9 +28,11 @@ class PixelShaderInterface
public:
virtual const char* getName(void) const = 0;
virtual void prepare(const RenderPipe* pipe, MaterialConstPtr material) = 0;
virtual void begin(const RenderPipe* pipe, MaterialConstPtr material) = 0;
virtual void process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) = 0;
virtual void end() = 0;
};
class ShaderFileInterface
+3 -33
View File
@@ -3,8 +3,9 @@
DV_CORE_BEGIN_NAMESPACE
Material::Material(int32_t id)
Material::Material(int32_t id, const char* name)
: m_id(id)
, m_name(name)
{
}
@@ -12,37 +13,6 @@ Material::~Material()
{
}
void Material::setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
const fVector4& baseColorfactor)
{
m_baseColorTexture.texture = texture;
m_baseColorTexture.texCoord = texCoord;
m_baseColorTexture.uvTransform = uvTransform;
m_baseColorFactor = baseColorfactor;
}
void Material::setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
float32_t metallicFactor, float32_t roughnessFactor)
{
m_metallicRoughnessTexture.texture = texture;
m_metallicRoughnessTexture.texCoord = texCoord;
m_metallicRoughnessTexture.uvTransform = uvTransform;
m_metallicFactor = metallicFactor;
m_roughnessFactor = roughnessFactor;
}
void Material::setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, const fMatrix3& uvTransform,
float32_t scale)
{
m_normalTexture.texture = texture;
m_normalTexture.texCoord = texCoord;
m_normalTexture.uvTransform = uvTransform;
m_normalScale = scale;
}
void Material::setVertexShader(const char* szShaderFile, const char* vertexShaderName)
{
m_vertexshaderFile = szShaderFile;
@@ -55,7 +25,7 @@ void Material::setFragementShader(const char* szShaderFile, const char* fragemen
m_fragementShaderName = fragementShaderName;
}
bool Material::loadShader()
bool Material::setupMaterial()
{
ShaderFileInterface* shaderInterface = davinci::LoadShaderFile(m_vertexshaderFile.c_str());
if (shaderInterface == nullptr)
-7
View File
@@ -135,13 +135,6 @@ TexturePtr Device::getTexture(int32_t id) const
return it->second;
}
MaterialPtr Device::newMaterialAsset()
{
MaterialPtr materialPtr = std::make_shared<Material>(nextID());
m_materialAssetMap.insert(std::make_pair(materialPtr->getID(), materialPtr));
return materialPtr;
}
MaterialPtr Device::getMaterial(int32_t id) const
{
std::map<int32_t, MaterialPtr>::const_iterator it = m_materialAssetMap.find(id);
@@ -28,8 +28,8 @@ void VertexShaderStep::process(RenderPipe& pipe, const InputAssemberStep::NodeAr
fMatrix4 worldInverseTrans = inputNode.transform.inverse().transpose();
uniformBuffer.setAttribute("self:world_transform_inverse_trans", worldInverseTrans);
VertexShaderInterface* vs = inputNode.material->getVertexShader();
vs->prepare(&pipe,
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);
@@ -42,12 +42,13 @@ void VertexShaderStep::process(RenderPipe& pipe, const InputAssemberStep::NodeAr
);
size_t vertexCounts = inputNode.indices->getCount();
size_t outputVertexByteSize = vs->getOutputVertexDesc().vertexByteSize();
Node outputNode;
outputNode.material = inputNode.material;
outputNode.vertexCounts = vertexCounts;
outputNode.vertexDesc = vs->getOutputVertexDesc();
outputNode.vertexBuf = device.newDeviceBuffer(outputNode.vertexDesc.vertexByteSize() * vertexCounts);
outputNode.vertexBuf = device.newDeviceBuffer(outputVertexByteSize * vertexCounts);
outputNode.depth.resize(vertexCounts);
outputNode.invZ.resize(vertexCounts);
@@ -55,22 +56,25 @@ void VertexShaderStep::process(RenderPipe& pipe, const InputAssemberStep::NodeAr
{
uint16_t index = *((const uint16_t*)(inputNode.indices->getElement(i)));
fVector3 vertexWorldPos;
void* outputVertexMemory = outputNode.vertexBuf->ptr(i* outputNode.vertexDesc.vertexByteSize());
vs->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.getElement(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();
}
}
@@ -10,8 +10,6 @@ DV_CORE_BEGIN_NAMESPACE
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();
@@ -21,16 +19,22 @@ void RasterizerStep::process(RenderPipe& pipe, const VertexShaderStep::NodeArray
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;
PixelShaderInterface* pixelShader = node.material->getPixelShader();
pixelShader->prepare(&pipe, node.material);
if (debugMaterial)
{
debugMaterial->pickupParam(node.material);
}
PixelShaderInterface* pixelShader = debugMaterial ? debugMaterial->getPixelShader() : node.material->getPixelShader();
pixelShader->begin(&pipe, debugMaterial ?debugMaterial : node.material);
size_t vertexSize = node.vertexDesc.vertexByteSize();
size_t POS_offset = node.vertexDesc.getElement(VET_POSITION)->offset;
size_t POS_offset = node.vertexDesc.getElement(VET_POSITION_NDC)->offset;
for (size_t i = 0; i < node.vertexCounts; i += 3)
{
@@ -65,10 +69,10 @@ void RasterizerStep::process(RenderPipe& pipe, const VertexShaderStep::NodeArray
pixelData[j] = MathUtil::lerp3(vertex0[j], vertex1[j], vertex2[j], percentCorrected);
}
if(dot.first==570 && dot.second==405 )
{
int a = 0;
}
//if(dot.first==570 && dot.second==405 )
//{
// int a = 0;
//}
fVector4 color;
pixelShader->process(node.vertexDesc, &(pixelData[0]), color);
@@ -4,7 +4,7 @@
DV_CORE_BEGIN_NAMESPACE
fVector4 shader::texture2D(const RenderPipe& pipe, const Material::TextureInfo& textureInfo, const fVector2& uv, const fVector4& defaultValue)
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);
+1
View File
@@ -37,6 +37,7 @@
#include <unordered_map>
#include <functional>
#include <algorithm>
#include <variant>
#ifdef DV_SYS_WINDOWS
#define snprintf _snprintf
+7 -5
View File
@@ -1,17 +1,19 @@
add_library(dv_standard_shaders SHARED
dll_main.cpp
vs_standard.h
vs_standard.inc
vs_standard.cpp
fs_debug_solid_color.h
fs_debug_solid_color.cpp
fs_debug_base_color.h
fs_debug_base_color.cpp
fs_debug_normal_texture.h
fs_debug_normal_texture.cpp
fs_debug_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)
+14 -8
View File
@@ -3,10 +3,11 @@
#include "vs_standard.h"
#include "fs_debug_solid_color.h"
#include "fs_debug_base_color.h"
#include "fs_debug_normal_texture.h"
#include "fs_debug_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
{
@@ -28,16 +29,21 @@ public:
public:
StandardShaderInterface()
{
registerVertexShader(new StandardVertexShader<false , false>());
registerVertexShader(new StandardVertexShader<true , false>());
registerVertexShader(new StandardVertexShader<false , true>());
registerVertexShader(new StandardVertexShader<true , true>());
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_BaseColor());
registerFragementShader(new DebugFragementShader_NormalTexture());
registerFragementShader(new DebugFragementShader_ShowTexture());
registerFragementShader(new DebugFragementShader_TextureCoordinates());
registerFragementShader(new DebugFragementShader_GeometryNormal());
registerFragementShader(new ClassicFragementShader_Phong());
registerFragementShader(new PbrFragementShader_GLTF());
}
~StandardShaderInterface()
@@ -0,0 +1,77 @@
#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";
}
void ClassicFragementShader_Phong::begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
{
m_pipe = pipe;
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
material->getParam("u_materialColor", m_materialColor);
material->getParam("u_specularShininess", m_specularShininess);
material->getParam("u_specularStrength", m_specularStrength);
if (davinci::fVector3* p_ambient_light_color = (davinci::fVector3*)(uniformBuffer.getAttribute("ambient_light:color")))
{
m_ambientLightColor = *p_ambient_light_color;
}
if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:position")))
{
m_light0Position = *p_light0_position;
}
if (davinci::fVector3* p_light0_color = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:color")))
{
m_light0Color = *p_light0_color;
}
if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(uniformBuffer.getAttribute("camera:eye_pos")))
{
m_cameraPosition = *p_camera_position;
}
}
void ClassicFragementShader_Phong::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
{
const VertexDesc::Element* v_worldpos_Element = veretexDesc.getElement(davinci::VET_POSITION_WORLD);
fVector3 v_worldpos = v_worldpos_Element ? *((const fVector3*)(pixelData + (v_worldpos_Element->offset / sizeof(float32_t)))) : fVector3::ZERO;
const VertexDesc::Element* v_normal_Element = veretexDesc.getElement(davinci::VET_NORMAL);
fVector3 v_normal = v_normal_Element ? *((const fVector3*)(pixelData + (v_normal_Element->offset / sizeof(float32_t)))) : fVector3::UNIT_Z;
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,27 @@
#pragma once
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class ClassicFragementShader_Phong : public davinci::PixelShaderInterface
{
public:
virtual const char* getName(void) const;
virtual void begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
virtual void end();
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::ZERO;
davinci::fVector3 m_light0Color = davinci::fVector3::WHITE;
davinci::fVector3 m_cameraPosition = davinci::fVector3::ZERO;
};
@@ -1,29 +0,0 @@
#include "fs_debug_base_color.h"
#include "device/dvc_device.h"
#include "device/dvc_uniform_buffer.h"
#include "asset/dvc_texture.h"
#include "shader/dvc_shader_functions.h"
#include "pipe/dvc_render_pipe.h"
const char* DebugFragementShader_BaseColor::getName(void) const
{
return "debug:base_color";
}
void DebugFragementShader_BaseColor::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
{
m_pipe = pipe;
m_material = material;
}
void DebugFragementShader_BaseColor::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
{
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv");
const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t)));
davinci::fVector4 u_baseColor = davinci::shader::texture2D(*m_pipe, m_material->getBaseColorTexture(), (*v_uv), davinci::fVector4::WHITE);
color = davinci::fVector4(u_baseColor.xyz(), 1);
}
@@ -10,7 +10,7 @@ const char* DebugFragementShader_GeometryNormal::getName(void) const
return "debug:geometry_normal";
}
void DebugFragementShader_GeometryNormal::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
void DebugFragementShader_GeometryNormal::begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
{
m_pipe = pipe;
m_material = material;
@@ -26,5 +26,10 @@ void DebugFragementShader_GeometryNormal::process(const davinci::VertexDesc& ver
davinci::fVector3 normalColor = *v_normal;
normalColor.normalise();
normalColor = normalColor * 0.5f + 0.5f;
color = davinci::fVector4(normalColor, 1);
}
void DebugFragementShader_GeometryNormal::end()
{
}
@@ -8,11 +8,13 @@ class DebugFragementShader_GeometryNormal : public davinci::PixelShaderInterface
public:
virtual const char* getName(void) const;
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
virtual void end();
private:
const davinci::RenderPipe* m_pipe = nullptr;
davinci::MaterialConstPtr m_material;
@@ -1,29 +0,0 @@
#include "fs_debug_normal_texture.h"
#include "device/dvc_device.h"
#include "device/dvc_uniform_buffer.h"
#include "asset/dvc_texture.h"
#include "shader/dvc_shader_functions.h"
#include "pipe/dvc_render_pipe.h"
const char* DebugFragementShader_NormalTexture::getName(void) const
{
return "debug:normal_texture";
}
void DebugFragementShader_NormalTexture::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
{
m_pipe = pipe;
m_material = material;
}
void DebugFragementShader_NormalTexture::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
{
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv");
const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t)));
davinci::fVector4 u_baseColor = davinci::shader::texture2D(*m_pipe, m_material->getNormalTexture(), (*v_uv), davinci::fVector4::WHITE);
color = davinci::fVector4(u_baseColor.xyz(), 1);
}
@@ -0,0 +1,37 @@
#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"
const char* DebugFragementShader_ShowTexture::getName(void) const
{
return "debug:show_texture";
}
void DebugFragementShader_ShowTexture::begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
{
m_pipe = pipe;
m_material = material;
material->getParam("u_textureInfo", m_textureInfo);
}
void DebugFragementShader_ShowTexture::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
{
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv");
const davinci::fVector2* v_uv = (const davinci::fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t)));
davinci::fVector4 textureColor = davinci::shader::texture2D(*m_pipe,
m_textureInfo, (*v_uv), davinci::fVector4::WHITE);
color = textureColor;
}
void DebugFragementShader_ShowTexture::end()
{
m_material.reset();
m_textureInfo.texture.reset();
}
@@ -2,18 +2,23 @@
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
#include "asset/dvc_material.h"
class DebugFragementShader_NormalTexture : public davinci::PixelShaderInterface
class DebugFragementShader_ShowTexture : public davinci::PixelShaderInterface
{
public:
virtual const char* getName(void) const;
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
virtual void end();
private:
const davinci::RenderPipe* m_pipe = nullptr;
davinci::MaterialConstPtr m_material;
davinci::TextureInfo m_textureInfo;
};
@@ -1,16 +1,21 @@
#include "fs_debug_solid_color.h"
#include "asset/dvc_material.h"
const char* DebugFragementShader_SolidColor::getName(void) const
{
return "debug:solid_color";
}
void DebugFragementShader_SolidColor::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
void DebugFragementShader_SolidColor::begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
{
material->getParam("u_solidColor", m_solidColor);
}
void DebugFragementShader_SolidColor::process( const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
{
color = davinci::fVector4::RED;
color = m_solidColor;
}
void DebugFragementShader_SolidColor::end()
{
}
@@ -8,8 +8,13 @@ class DebugFragementShader_SolidColor : public davinci::PixelShaderInterface
public:
virtual const char* getName(void) const;
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
virtual void end();
private:
davinci::fVector4 m_solidColor = davinci::fVector4::WHITE;
};
@@ -10,10 +10,12 @@ const char* DebugFragementShader_TextureCoordinates::getName(void) const
return "debug:texture_coordinates";
}
void DebugFragementShader_TextureCoordinates::prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
void DebugFragementShader_TextureCoordinates::begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
{
m_pipe = pipe;
m_material = material;
material->getParam("u_texCoord", m_texCoord);
}
void DebugFragementShader_TextureCoordinates::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
@@ -29,3 +31,7 @@ void DebugFragementShader_TextureCoordinates::process(const davinci::VertexDesc&
color = davinci::fVector4(uv_x, uv_y, 0, 1);
}
void DebugFragementShader_TextureCoordinates::end()
{
}
@@ -8,12 +8,16 @@ class DebugFragementShader_TextureCoordinates : public davinci::PixelShaderInter
public:
virtual const char* getName(void) const;
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
virtual void end();
private:
const davinci::RenderPipe* m_pipe = nullptr;
davinci::MaterialConstPtr m_material;
int32_t m_texCoord=0;
};
+25
View File
@@ -0,0 +1,25 @@
#include "fs_pbr_gltf.h"
#include "pipe/dvc_render_pipe.h"
using namespace davinci;
const char* PbrFragementShader_GLTF::getName(void) const
{
return "pbr:gltf";
}
void PbrFragementShader_GLTF::begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material)
{
m_pipe = pipe;
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
}
void PbrFragementShader_GLTF::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
{
color = fVector4::BLUE;
}
void PbrFragementShader_GLTF::end()
{
}
@@ -3,17 +3,20 @@
#include "dvc_config.h"
#include "shader/dvc_shader_interface.h"
class DebugFragementShader_BaseColor : public davinci::PixelShaderInterface
class PbrFragementShader_GLTF : public davinci::PixelShaderInterface
{
public:
virtual const char* getName(void) const;
virtual void prepare(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void begin(const davinci::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
virtual void end();
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.getElement(davinci::VET_POSITION_NDC)->offset, &vertexNDCPos, sizeof(davinci::fVector3));
}
//output world position
if (m_withWorldPos)
{
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(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.getElement(davinci::VET_NORMAL)->offset, &v_normal, sizeof(davinci::fVector3));
}
//output uv0
if (m_withTexCoord0)
{
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(davinci::VET_TEXCOORD_0)->offset, &a_uv, sizeof(float32_t) * 2);
}
}
void StandardVertexShader::end()
{
}
+34 -24
View File
@@ -4,38 +4,48 @@
#include "shader/dvc_shader_interface.h"
template<bool WithNormal, bool WithUV>
class StandardVertexShader : public davinci::VertexShaderInterface
{
public:
virtual const char* getName(void) const;
virtual const davinci::VertexDesc& getOutputVertexDesc() const;
virtual size_t getOutputVertexByteSize(void) const;
virtual void prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc);
virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos);
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;
using InputBufferMembers = decltype(std::tuple_cat(
std::tuple<davinci::DeviceBufferAccessorConstPtr>(),
std::conditional_t<WithNormal, std::tuple<davinci::DeviceBufferAccessorConstPtr>, std::tuple<>>{},
std::conditional_t<WithUV, std::tuple<davinci::DeviceBufferAccessorConstPtr>, std::tuple<>>{}
));
InputBufferMembers m_inputAccessors;
static constexpr int32_t iPOSITION() { return 0; };
static constexpr int32_t iNORMAL() { return 1; };
static constexpr int32_t iUV() { return WithNormal+1; };
//davinci::DeviceBufferAccessorConstPtr& getPositionAccessor();
//davinci::DeviceBufferAccessorConstPtr& getNormalAccessor();
//davinci::DeviceBufferAccessorConstPtr& getUVAccessor();
public:
StandardVertexShader();
StandardVertexShader(bool withWorldPos, bool withNormal, bool withTexCoord0);
virtual ~StandardVertexShader();
};
#include "vs_standard.inc"
-181
View File
@@ -1,181 +0,0 @@
#include "device/dvc_buffer_accessor.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector3.h"
#include "math/dvc_matrix4.h"
#include "pipe/dvc_render_pipe.h"
#include "device/dvc_uniform_buffer.h"
template<bool WithNormal, bool WithUV>
StandardVertexShader<WithNormal, WithUV>::StandardVertexShader()
{
m_outputVertexDesc.addElement("v_position", davinci::VET_POSITION, davinci::CT_Float32, davinci::DT_Vec3);
if constexpr(WithNormal)
m_outputVertexDesc.addElement("v_normal", davinci::VET_NORMAL, davinci::CT_Float32, davinci::DT_Vec3);
if constexpr(WithUV)
m_outputVertexDesc.addElement("v_uv", davinci::VET_TEXCOORD_0, davinci::CT_Float32, davinci::DT_Vec2);
}
template<bool WithNormal, bool WithUV>
StandardVertexShader<WithNormal, WithUV>::~StandardVertexShader()
{
}
template<std::size_t N>
struct CompileTimeString {
std::array<char, N> data{};
std::size_t size = 0;
constexpr void append(const char* str, std::size_t len) {
for (std::size_t i = 0; i < len; ++i) {
data[size++] = str[i];
}
}
constexpr void append(const char* str) {
while (*str) {
data[size++] = *str++;
}
}
constexpr const char* c_str() const { return data.data(); }
constexpr std::string_view view() const { return {data.data(), size}; }
};
template<bool WithNormal, bool WithUV>
constexpr auto buildNameString()
{
constexpr const char* prefix = "standard";
constexpr const char* normalString = WithNormal ? "+normal" : "-normal";
constexpr const char* uvString = WithUV ? "+uv" : "-uv";
constexpr std::size_t total_len = std::char_traits<char>::length(prefix)
+ std::char_traits<char>::length(normalString)
+ std::char_traits<char>::length(uvString);
CompileTimeString<total_len> result;
result.append(prefix);
result.append(normalString);
result.append(uvString);
result.append(""); // null terminator
return result;
}
template<bool WithNormal, bool WithUV>
const char* StandardVertexShader<WithNormal, WithUV>::getName(void) const
{
static constexpr auto strName = buildNameString<WithNormal, WithUV>();
const char* p = strName.c_str();
return p;
}
template<bool WithNormal, bool WithUV>
const davinci::VertexDesc& StandardVertexShader<WithNormal, WithUV>::getOutputVertexDesc() const
{
return m_outputVertexDesc;
}
template<bool WithNormal, bool WithUV>
size_t StandardVertexShader<WithNormal, WithUV>::getOutputVertexByteSize(void) const
{
return m_outputVertexDesc.vertexByteSize();
}
template<bool WithNormal, bool WithUV>
void StandardVertexShader<WithNormal, WithUV>::prepare(const davinci::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc)
{
m_pipe = pipe;
std::get<iPOSITION()>(m_inputAccessors) = setInputAttributeFunc(davinci::VET_POSITION);
if constexpr(WithNormal)
std::get<iNORMAL()>(m_inputAccessors) = setInputAttributeFunc(davinci::VET_NORMAL);
if constexpr(WithUV)
std::get<iUV()>(m_inputAccessors) = setInputAttributeFunc(davinci::VET_TEXCOORD_0);
}
/*
template<bool WithNormal, bool WithUV>
davinci::DeviceBufferAccessorConstPtr& StandardVertexShader<WithNormal, WithUV>::getPositionAccessor()
{
return std::get<0>(m_inputAccessors);
}
template<bool WithNormal, bool WithUV>
davinci::DeviceBufferAccessorConstPtr& StandardVertexShader<WithNormal, WithUV>::getNormalAccessor()
{
if constexpr(WithNormal)
return std::get<1>(m_inputAccessors);
else
return nullptr;
}
template<bool WithNormal, bool WithUV>
davinci::DeviceBufferAccessorConstPtr& StandardVertexShader<WithNormal, WithUV>::getUVAccessor()
{
if constexpr(WithUV)
return std::get<WithNormal+1>(m_inputAccessors);
else
return nullptr;
}
*/
template<bool WithNormal, bool WithUV>
void StandardVertexShader<WithNormal, WithUV>::process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos)
{
static const float DEFAULT_POSITION[] = {0.f, 0.f, 0.f};
static const float32_t DEFAULT_NORMAL[] = { 0.f, 0.f, 0.f };
static const float32_t DEFAULT_TEXCOORD0[] = { 0.f, 0.f};
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
davinci::DeviceBufferAccessorConstPtr inputPosition = std::get<0>(m_inputAccessors);
davinci::DeviceBufferAccessorConstPtr inputNormal = nullptr;
if constexpr(WithNormal)
inputNormal = std::get<iNORMAL()>(m_inputAccessors);
davinci::DeviceBufferAccessorConstPtr inputUV = nullptr;
if constexpr(WithUV)
inputUV = std::get<iUV()>(m_inputAccessors);
davinci::fVector3 a_position(inputPosition ? (const float32_t*)(inputPosition->getElement(index)) : DEFAULT_POSITION);
davinci::fVector3 a_normal(inputNormal ? (const float32_t*)(inputNormal->getElement(index)) : DEFAULT_NORMAL);
davinci::fVector2 a_uv(inputUV ? (const float32_t*)(inputUV->getElement(index)) : DEFAULT_TEXCOORD0);
davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY;
if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform")))
{
mat_world = *p_mat_world;
}
davinci::fMatrix4 mat_camera_view_proj = davinci::fMatrix4::IDENTITY;
if (davinci::fMatrix4* p_mat_camera_view_proj = (davinci::fMatrix4*)(uniformBuffer.getAttribute("camera:view_proj_mat")))
{
mat_camera_view_proj = *p_mat_camera_view_proj;
}
vertexWorldPos = mat_world * a_position;
davinci::fVector3 v_position = mat_camera_view_proj * vertexWorldPos;
int32_t outputOffset=0;
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &v_position, sizeof(davinci::fVector3));
if constexpr(WithNormal)
{
davinci::fMatrix4 matNormal = davinci::fMatrix4::IDENTITY;
if (davinci::fMatrix4* p_mat_normal = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform_inverse_trans")))
{
matNormal = *p_mat_normal;
}
davinci::fVector3 v_normal = (matNormal * davinci::fVector4(a_normal, 0)).xyz().normalise();
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &v_normal, sizeof(davinci::fVector3));
}
if constexpr(WithUV)
{
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(outputOffset++)->offset, &a_uv, sizeof(float32_t) * 2);
}
}
+26 -15
View File
@@ -138,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")
{
@@ -296,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)
@@ -558,6 +558,15 @@ bool _loadMesh(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Sce
}
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;
@@ -665,41 +674,43 @@ bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci:
tinygltf::Material& gltfMaterial = gltfModel.materials[i];
//create new material
davinci::MaterialPtr materialPtr = create_standard_material(&device, "debug:geometry_normal");
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;
materialPtr->setBaseColorTexture(
davinci::TextureInfo baseColorTextureInfo = davinci::TextureInfo{
_getTexturePtr(gltfModel, device, gltfBaseColorTextureInfo.index),
gltfBaseColorTextureInfo.texCoord,
_getTextureTransform(gltfBaseColorTextureInfo.extensions),
_getVector4FromDoubleArray(gltfPBRMetllicRoughness.baseColorFactor)
);
_getTextureTransform(gltfBaseColorTextureInfo.extensions)
};
materialPtr->setParam("u_baseColorTextureInfo", baseColorTextureInfo);
//matallic & roughness texture
const tinygltf::TextureInfo& gltfMetalicRoughnessTextureInfo = gltfPBRMetllicRoughness.metallicRoughnessTexture;
materialPtr->setMetallicRoughnessTexture(
davinci::TextureInfo metalicRoughnessTextureInfo = davinci::TextureInfo{
_getTexturePtr(gltfModel, device, gltfMetalicRoughnessTextureInfo.index),
gltfMetalicRoughnessTextureInfo.texCoord,
_getTextureTransform(gltfMetalicRoughnessTextureInfo.extensions),
gltfPBRMetllicRoughness.metallicFactor,
gltfPBRMetllicRoughness.roughnessFactor
);
};
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;
materialPtr->setNormalTexture(
davinci::TextureInfo normalTextureInfo = davinci::TextureInfo{
_getTexturePtr(gltfModel, device, gltfNormalTextureInfo.index),
gltfNormalTextureInfo.texCoord,
_getTextureTransform(gltfNormalTextureInfo.extensions),
gltfMaterial.normalTexture.scale
);
};
materialPtr->setParam("u_normalTextureInfo", normalTextureInfo);
materialPtr->setParam("u_normalTextureScale", static_cast<float32_t>(gltfMaterial.normalTexture.scale));
//load shader
if (!(materialPtr->loadShader()))
if (!(materialPtr->setupMaterial()))
{
return false;
}
@@ -4,34 +4,168 @@
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::GREEN);
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, "debug:base_color")==0)
if(strcmp(name, "pbr:gltf")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:base_color");
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();
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:normal_texture");
davinci::MaterialPtr materialPtr = device->newMaterialAsset<NormalTextureMaterial>(name);
return materialPtr;
}
else if (strcmp(name, "debug:texture_coordinates")==0)
else if (strcmp(name, "debug:texture_coordinates_0")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard-normal+uv");
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:texture_coordinates");
davinci::MaterialPtr materialPtr = device->newMaterialAsset<TextureCoordinates0Material>(name);
return materialPtr;
}
else if (strcmp(name, "debug:geometry_normal")==0)
{
davinci::MaterialPtr materialPtr = device->newMaterialAsset();
materialPtr->setVertexShader("dv_standard_shaders.dll", "standard+normal-uv");
materialPtr->setFragementShader("dv_standard_shaders.dll", "debug:geometry_normal");
davinci::MaterialPtr materialPtr = device->newMaterialAsset<GeometryNormalMaterial>(name);
return materialPtr;
}
return nullptr;
-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>)
-39
View File
@@ -1,39 +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, "test") == 0)
{
static TestVertexShader testVertexShader;
return &testVertexShader;
}
return nullptr;
}
virtual davinci::PixelShaderInterface* getPixelShader(const char* szName)
{
static TestPixelShader testPixelShader;
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
-108
View File
@@ -1,108 +0,0 @@
#include "test_pixel_shader.h"
#include "device/dvc_device.h"
#include "device/dvc_uniform_buffer.h"
#include "asset/dvc_texture.h"
#include "shader/dvc_shader_functions.h"
#include "pipe/dvc_render_pipe.h"
using namespace davinci;
const char* TestPixelShader::getName(void) const
{
return "test";
}
void TestPixelShader::prepare(const RenderPipe* pipe, davinci::MaterialConstPtr material)
{
m_pipe = pipe;
}
void TestPixelShader::process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color)
{
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
const VertexDesc::Element* v_normal_Element = veretexDesc.getElement("v_normal");
const fVector3* v_normal = (const fVector3*)(pixelData + (v_normal_Element->offset / sizeof(float32_t)));
const VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv");
const fVector2* v_uv = (const fVector2*)(pixelData + (v_uv_Element->offset / sizeof(float32_t)));
const VertexDesc::Element* v_toCamera_Element = veretexDesc.getElement("v_toCamera");
const fVector3* v_toCamera = (const fVector3*)(pixelData + (v_toCamera_Element->offset / sizeof(float32_t)));
const VertexDesc::Element* v_toLight_Element = veretexDesc.getElement("v_toLight");
const fVector3* v_toLight = (const fVector3*)(pixelData + (v_toLight_Element->offset / sizeof(float32_t)));
const VertexDesc::Element* v_tangent_Element = veretexDesc.getElement("v_tangent");
const fVector3* v_tangent = (const fVector3*)(pixelData + (v_tangent_Element->offset / sizeof(float32_t)));
const VertexDesc::Element* v_binormal_Element = veretexDesc.getElement("v_binormal");
const fVector3* v_binormal = (const fVector3*)(pixelData + (v_binormal_Element->offset / sizeof(float32_t)));
const VertexDesc::Element* v_wposition_Element = veretexDesc.getElement("v_wposition");
const fVector3* v_wposition = (const fVector3*)(pixelData + (v_wposition_Element->offset / sizeof(float32_t)));
fMatrix3 tbnMatrix = fMatrix3(
v_tangent->x, v_binormal->x, v_normal->x,
v_tangent->y, v_binormal->y, v_normal->y,
v_tangent->z, v_binormal->z, v_normal->z);
davinci::fVector3 light0_position = davinci::fVector3::ZERO;
if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(uniformBuffer.getAttribute("light0:position")))
{
light0_position = *p_light0_position;
}
//davinci::fVector3 camera_position = davinci::fVector3::ZERO;
//if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(uniformBuffer.getAttribute("camera:eye_pos")))
//{
// camera_position = *p_camera_position;
//}
fVector3 lp = light0_position - *v_wposition;
fVector3 vLight = lp.normalise() * tbnMatrix;
fVector3 u_ambientColor = fVector3(0.2, 0.2, 0.2);
fVector3 u_matColor = fVector3(1.0, 1.0, 1.0);
fVector3 u_lightColor = fVector3(1.0, 1.0, 1.0);
fVector4 u_textureColor;// = shader::texture2D(*m_pipe, "material:pbr:base_color_texture", *v_uv, fVector4::WHITE);
const fVector4 defaultNormal = fVector4(0.5f, 0.5f, 1.0f, 1.0f);
fVector4 u_normalColor;// = shader::texture2D(*m_pipe, "material:pbr:normal_texture", *v_uv, defaultNormal);
//----------------
fVector3 vN = u_normalColor.xyz() * 2.0f - fVector3::ONE;
vN.normalise();
fVector3 vL =vLight;
vL.normalise();
fVector3 vV = *v_toCamera;
vV.normalise();
float ndotl = vL.dotProduct(vN);
fVector3 vR = 2 * ndotl * vN - vL;
float rdotv = vR.dotProduct(vV);
fVector3 colAmbient = u_ambientColor * u_matColor;
fVector3 colDiff = shader::max(0.0f, ndotl) * u_matColor* u_lightColor;
const float _m = 1.0f;
float spec = shader::step(0.0f, ndotl) * shader::pow(shader::max(0.0f, rdotv), 32.f);
fVector3 colSpec = spec * u_matColor* u_lightColor;
fVector3 final = fVector3::BLACK;
//final += colAmbient;
final += colDiff;
//final += colSpec;
//final *= u_textureColor.xyz();
//final = u_normalColor.xyz();
//color = fVector4::RED;
color = fVector4(final.saturate(), 1.0f);// fVector4::RED;
}
-18
View File
@@ -1,18 +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::RenderPipe* pipe, davinci::MaterialConstPtr material);
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
davinci::fVector4& color);
private:
const davinci::RenderPipe* m_pipe = nullptr;
};
-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));
}
-195
View File
@@ -1,195 +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 "pipe/dvc_render_pipe.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_tangent", davinci::VET_TANGENT, davinci::CT_Float32, davinci::DT_Vec3);
m_outputVertexDesc.addElement("v_binormal", davinci::VET_BINORMAL, davinci::CT_Float32, davinci::DT_Vec3);
m_outputVertexDesc.addElement("v_wposition", davinci::VET_UNKNOWN, davinci::CT_Float32, davinci::DT_Vec3);
}
TestVertexShader::~TestVertexShader()
{
}
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::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc)
{
m_pipe = pipe;
//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_TANGENT = setInputAttributeFunc(davinci::VET_TANGENT);
}
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_TANGENT[] = { 1.f, 0.f, 0.f, 1.f };
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
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::fVector4 a_tangent(m_TANGENT ? (const float32_t*)(m_TANGENT->getElement(index)) : DEFAULT_TANGENT);
davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY;
if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(uniformBuffer.getAttribute("self:world_transform")))
{
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 camera_position = davinci::fVector3::ZERO;
if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(uniformBuffer.getAttribute("camera:eye_pos")))
{
camera_position = *p_camera_position;
}
davinci::fVector3 light0_position = davinci::fVector3::ZERO;
if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(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*)(uniformBuffer.getAttribute("self:world_transform_inverse_trans")))
{
matNormal = *p_mat_normal;
}
davinci::fVector3 v_normal = (matNormal * davinci::fVector4(a_normal, 0)).xyz().normalise();
davinci::fVector3 v_tangent = (matNormal * davinci::fVector4(a_tangent.xyz(), 0)).xyz().normalise();
davinci::fVector3 v_binormal = v_normal.crossProduct(v_tangent).normalise();// *a_tangent.w;
davinci::fMatrix3 tbnMatrix = davinci::fMatrix3(
v_tangent.x, v_binormal.x, v_normal.x,
v_tangent.y, v_binormal.y, v_normal.y,
v_tangent.z, v_binormal.z, v_normal.z);
davinci::fVector3 v_to_camera = (camera_position - temp1).normalise() * tbnMatrix;
davinci::fVector3 v_to_light = (light0_position - temp1).normalise() * tbnMatrix;
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(0)->offset, &v_position, sizeof(davinci::fVector3));
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(1)->offset, &v_normal, sizeof(davinci::fVector3));
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(2)->offset, &a_uv, sizeof(float32_t) * 2);
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(3)->offset, &v_to_camera, sizeof(davinci::fVector3));
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(4)->offset, &v_to_light, sizeof(davinci::fVector3));
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(5)->offset, &v_tangent, sizeof(davinci::fVector3));
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(6)->offset, &v_binormal, sizeof(davinci::fVector3));
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(7)->offset, &temp1, sizeof(davinci::fVector3));
vertexWorldPos = mat_world * a_position;
}
StandardVertexShader::StandardVertexShader()
{
//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::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc)
{
m_pipe = pipe;
//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};
const davinci::UniformBuffer& uniformBuffer = m_pipe->getUniformBuffer();
davinci::fVector3 a_position(m_POSITION ? (const float32_t*)(m_POSITION->getElement(index)) : DEFAULT_POSITION);
davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY;
if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(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 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;
}
-53
View File
@@ -1,53 +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::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc);
virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos);
private:
const davinci::RenderPipe* m_pipe = nullptr;
davinci::DeviceBufferAccessorConstPtr m_POSITION;
davinci::DeviceBufferAccessorConstPtr m_NORMAL;
davinci::DeviceBufferAccessorConstPtr m_TEXCOORD_0;
davinci::DeviceBufferAccessorConstPtr m_TANGENT;
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::RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc);
virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos);
private:
const davinci::RenderPipe* m_pipe = 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);
}