重构材质系统
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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>)
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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();
|
||||
};
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user