init commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
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>)
|
||||
@@ -0,0 +1,31 @@
|
||||
#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)
|
||||
{
|
||||
static TestVertexShader testVertexShader;
|
||||
return &testVertexShader;
|
||||
}
|
||||
virtual davinci::PixelShaderInterface* getPixelShader(const char* szName)
|
||||
{
|
||||
static TestPixelShader testPixelShader;
|
||||
return &testPixelShader;
|
||||
}
|
||||
};
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
__declspec(dllexport) davinci::ShaderFileInterface* ShaderFileInterface()
|
||||
{
|
||||
static TestShaderInterface theInterface;
|
||||
return &theInterface;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
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
|
||||
@@ -0,0 +1,64 @@
|
||||
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
|
||||
@@ -0,0 +1,76 @@
|
||||
#include "test_pixel_shader.h"
|
||||
|
||||
const char* TestPixelShader::getName(void) const
|
||||
{
|
||||
return "test_pixel_shader";
|
||||
}
|
||||
|
||||
void TestPixelShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TestPixelShader::process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData, davinci::fVector4& color)
|
||||
{
|
||||
const davinci::VertexDesc::Element* v_position_Element = veretexDesc.getElement("v_position");
|
||||
const davinci::fVector3* v_position = (const davinci::fVector3*)(pixelData + (v_position_Element->offset / sizeof(float32_t)));
|
||||
|
||||
const davinci::VertexDesc::Element* v_normal_Element = veretexDesc.getElement("v_normal");
|
||||
const davinci::fVector3* v_normal = (const davinci::fVector3*)(pixelData + (v_normal_Element->offset / sizeof(float32_t)));
|
||||
|
||||
const davinci::VertexDesc::Element* v_uv_Element = veretexDesc.getElement("v_uv");
|
||||
const float32_t* v_uv = (const float32_t*)(pixelData + (v_uv_Element->offset / sizeof(float32_t)));
|
||||
|
||||
const davinci::VertexDesc::Element* v_toCamera_Element = veretexDesc.getElement("v_toCamera");
|
||||
const davinci::fVector3* v_toCamera = (const davinci::fVector3*)(pixelData + (v_toCamera_Element->offset / sizeof(float32_t)));
|
||||
|
||||
const davinci::VertexDesc::Element* v_toLight_Element = veretexDesc.getElement("v_toLight");
|
||||
const davinci::fVector3* v_toLight = (const davinci::fVector3*)(pixelData + (v_toLight_Element->offset / sizeof(float32_t)));
|
||||
|
||||
const davinci::VertexDesc::Element* v_color0_Element = veretexDesc.getElement("v_color0");
|
||||
const davinci::fVector3* v_color0 = (const davinci::fVector3*)(pixelData + (v_color0_Element->offset / sizeof(float32_t)));
|
||||
|
||||
//vector vL = normalize(v_toLight);
|
||||
davinci::fVector3 vL = *v_toLight;
|
||||
vL.normalise();
|
||||
|
||||
//vector vV = normalize(v_toCamera);
|
||||
davinci::fVector3 vV = *v_toCamera;
|
||||
vV.normalise();
|
||||
|
||||
//vector vN = normalize(v_normal);
|
||||
davinci::fVector3 vN = *v_normal;
|
||||
vN.normalise();
|
||||
|
||||
//vector vR = 2 * dot(vL, vN)*vN - vL;
|
||||
davinci::fVector3 vR = 2 * vL.dotProduct(vN) * vN - vL;
|
||||
|
||||
//vector vH = normalize(vL + vV);
|
||||
davinci::fVector3 vH = vL + vV;
|
||||
vH.normalise();
|
||||
|
||||
davinci::fVector3 u_ambientColor = davinci::fVector3(0.1, 0.1, 0.1);
|
||||
|
||||
davinci::fVector3 u_matColor = davinci::fVector3(1.0, 0.5, 0.7);
|
||||
|
||||
davinci::fVector3 u_lightColor = davinci::fVector3(1.0, 1.0, 1.0);
|
||||
|
||||
//color colAmbient = u_ambientColor*u_matColor;
|
||||
davinci::fVector3 colAmbient = u_ambientColor * u_matColor;
|
||||
|
||||
//float ndotl = max(dot(vN, vL), 0);
|
||||
float32_t ndotl = std::max(vN.dotProduct(vL), 0.0f);
|
||||
//color colDiff = ndotl * u_matColor * u_lightColor;
|
||||
davinci::fVector3 colDiff = ndotl * u_matColor * u_lightColor;
|
||||
|
||||
//float spec = max(dot(vN, vH), 0);
|
||||
float32_t spec = std::max(vN.dotProduct(vH), 0.0f);
|
||||
//color colSpec = step(0, ndotl) * pow(spec, 64) * u_matColor * u_lightColor;
|
||||
davinci::fVector3 colSpec = (ndotl < 0.f ? 0.0 : 1.0) * std::powf(spec, 64.f) * u_matColor * u_lightColor;
|
||||
|
||||
//color final = colAmbient + colDiff + colSpec;
|
||||
davinci::fVector3 final = colAmbient + colDiff + colSpec;
|
||||
//davinci::fVector3 final = *v_color0;
|
||||
|
||||
color = davinci::fVector4(final.saturate(), 1.0f);// davinci::fVector4::RED;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvc_config.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
|
||||
class TestPixelShader : public davinci::PixelShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual void prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc);
|
||||
|
||||
virtual void process(const davinci::VertexDesc& veretexDesc, const float32_t* pixelData,
|
||||
davinci::fVector4& color);
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
|
||||
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));
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
#include "test_vertex_shader.h"
|
||||
#include "device/dvc_buffer_accessor.h"
|
||||
#include "math/dvc_vector2.h"
|
||||
#include "math/dvc_vector3.h"
|
||||
#include "math/dvc_matrix4.h"
|
||||
#include "device/dvc_uniform_buffer.h"
|
||||
|
||||
TestVertexShader::TestVertexShader()
|
||||
{
|
||||
//output point v_position = point(0, 0, 0) [[ string attribute="POSITION"]],
|
||||
m_outputVertexDesc.addElement("v_position", davinci::VET_POSITION, davinci::CT_Float32, davinci::DT_Vec3);
|
||||
//output vector v_normal = vector(0,1,0) [[ string attribute="NORMAL"]],
|
||||
m_outputVertexDesc.addElement("v_normal", davinci::VET_NORMAL, davinci::CT_Float32, davinci::DT_Vec3);
|
||||
//output float v_uv[2] = {0, 0} [[ string attribute="TEXCOORD_0"]],
|
||||
m_outputVertexDesc.addElement("v_uv", davinci::VET_TEXCOORD_0, davinci::CT_Float32, davinci::DT_Vec2);
|
||||
//output vector v_toCamera = vector(0, 0, 0),
|
||||
m_outputVertexDesc.addElement("v_toCamera", davinci::VET_UNKNOWN, davinci::CT_Float32, davinci::DT_Vec3);
|
||||
//output vector v_toLight = vector(0,0,0)
|
||||
m_outputVertexDesc.addElement("v_toLight", davinci::VET_UNKNOWN, davinci::CT_Float32, davinci::DT_Vec3);
|
||||
|
||||
m_outputVertexDesc.addElement("v_color0", davinci::VET_UNKNOWN, davinci::CT_Float32, davinci::DT_Vec3);
|
||||
}
|
||||
|
||||
TestVertexShader::~TestVertexShader()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
const char* TestVertexShader::getName(void) const
|
||||
{
|
||||
return "test_vertex_shader";
|
||||
}
|
||||
|
||||
size_t TestVertexShader::getOutputVertexByteSize(void) const
|
||||
{
|
||||
return m_outputVertexDesc.vertexByteSize();
|
||||
}
|
||||
|
||||
const davinci::VertexDesc& TestVertexShader::getOutputVertexDesc() const
|
||||
{
|
||||
return m_outputVertexDesc;
|
||||
}
|
||||
|
||||
void TestVertexShader::prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc)
|
||||
{
|
||||
m_uniformBuffer = uniformBuffer;
|
||||
|
||||
//point a_position = vector(0, 0, 0) [[ string attribute="POSITION"]],
|
||||
m_POSITION = setInputAttributeFunc(davinci::VET_POSITION);
|
||||
//vector a_normal = vector(0, 1, 0) [[ string attribute="NORMAL"]],
|
||||
m_NORMAL = setInputAttributeFunc(davinci::VET_NORMAL);
|
||||
//float a_uv[2] = {0,0} [[ string attribute="TEXCOORD_0"]],
|
||||
m_TEXCOORD_0 = setInputAttributeFunc(davinci::VET_TEXCOORD_0);
|
||||
//
|
||||
m_COLOR_0 = setInputAttributeFunc(davinci::VET_COLOR_0);
|
||||
}
|
||||
|
||||
void TestVertexShader::process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos)
|
||||
{
|
||||
static const float DEFAULT_POSITION[] = {0.f, 0.f, 0.f};
|
||||
static const float DEFAULT_NORMAL[] = { 0.f, 0.f, 0.f };
|
||||
static const float DEFAULT_TEXCOORD0[] = { 0.f, 0.f};
|
||||
static const uint16_t DEFAULT_COLOR0[] = { 0, 0, 0 };
|
||||
|
||||
davinci::fVector3 a_position(m_POSITION ? (const float*)(m_POSITION->getElement(index)) : DEFAULT_POSITION);
|
||||
davinci::fVector3 a_normal(m_NORMAL ? (const float*)(m_NORMAL->getElement(index)) : DEFAULT_NORMAL);
|
||||
const float32_t* a_texcoord_0(m_TEXCOORD_0 ? (const float*)(m_TEXCOORD_0->getElement(index)) : DEFAULT_TEXCOORD0);
|
||||
const uint16_t* color0 = m_COLOR_0 ? (const uint16_t*)(m_COLOR_0->getElement(index)) : DEFAULT_COLOR0;
|
||||
|
||||
davinci::fVector3 a_color_0(
|
||||
color0[0] / float(std::numeric_limits<uint16_t>::max()),
|
||||
color0[1] / float(std::numeric_limits<uint16_t>::max()),
|
||||
color0[2] / float(std::numeric_limits<uint16_t>::max())
|
||||
);
|
||||
|
||||
davinci::fMatrix4 mat_world = davinci::fMatrix4::IDENTITY;
|
||||
if (davinci::fMatrix4* p_mat_world = (davinci::fMatrix4*)(m_uniformBuffer->getAttribute("self:world_transform")))
|
||||
{
|
||||
mat_world = *p_mat_world;
|
||||
}
|
||||
|
||||
davinci::fMatrix4 mat_camera_view_proj = davinci::fMatrix4::IDENTITY;
|
||||
if (davinci::fMatrix4* p_mat_camera_view_proj = (davinci::fMatrix4*)(m_uniformBuffer->getAttribute("camera:view_proj_mat")))
|
||||
{
|
||||
mat_camera_view_proj = *p_mat_camera_view_proj;
|
||||
}
|
||||
|
||||
davinci::fVector3 camera_position = davinci::fVector3::ZERO;
|
||||
if (davinci::fVector3* p_camera_position = (davinci::fVector3*)(m_uniformBuffer->getAttribute("camera:eye_pos")))
|
||||
{
|
||||
camera_position = *p_camera_position;
|
||||
}
|
||||
|
||||
davinci::fVector3 light0_position = davinci::fVector3::ZERO;
|
||||
if (davinci::fVector3* p_light0_position = (davinci::fVector3*)(m_uniformBuffer->getAttribute("light0:position")))
|
||||
{
|
||||
light0_position = *p_light0_position;
|
||||
}
|
||||
|
||||
davinci::fVector3 temp1 = mat_world * a_position;
|
||||
davinci::fVector3 v_position = mat_camera_view_proj * temp1;
|
||||
|
||||
davinci::fVector3 v_normal = a_normal;
|
||||
|
||||
davinci::fVector3 v_color_0 = a_color_0;
|
||||
|
||||
float32_t v_uv[2];
|
||||
memcpy(v_uv, a_texcoord_0, sizeof(float32_t) * 2);
|
||||
|
||||
davinci::fVector3 v_to_camera = (camera_position - v_position).normalise();
|
||||
davinci::fVector3 v_to_light = (light0_position - v_position).normalise();
|
||||
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(0)->offset, &v_position, sizeof(davinci::fVector3));
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(1)->offset, &v_normal, sizeof(davinci::fVector3));
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(2)->offset, v_uv, sizeof(float32_t) * 2);
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(3)->offset, &v_to_camera, sizeof(davinci::fVector3));
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(4)->offset, &v_to_light, sizeof(davinci::fVector3));
|
||||
memcpy((char*)outputVertex + m_outputVertexDesc.getElement(5)->offset, &v_color_0, sizeof(davinci::fVector3));
|
||||
|
||||
vertexWorldPos = mat_world * a_position;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvc_config.h"
|
||||
#include "dvc_pre_declare.h"
|
||||
#include "shader/dvc_shader_interface.h"
|
||||
#include "device/dvc_vertex_desc.h"
|
||||
|
||||
class TestVertexShader : public davinci::VertexShaderInterface
|
||||
{
|
||||
public:
|
||||
virtual const char* getName(void) const;
|
||||
|
||||
virtual const davinci::VertexDesc& getOutputVertexDesc() const;
|
||||
virtual size_t getOutputVertexByteSize(void) const;
|
||||
|
||||
virtual void prepare(const davinci::UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc);
|
||||
|
||||
virtual void process(size_t index, void* outputVertex, davinci::fVector3& vertexWorldPos);
|
||||
|
||||
private:
|
||||
const davinci::UniformBuffer* m_uniformBuffer;
|
||||
davinci::DeviceBufferAccessorConstPtr m_POSITION;
|
||||
davinci::DeviceBufferAccessorConstPtr m_NORMAL;
|
||||
davinci::DeviceBufferAccessorConstPtr m_TEXCOORD_0;
|
||||
davinci::DeviceBufferAccessorConstPtr m_COLOR_0;
|
||||
|
||||
davinci::VertexDesc m_outputVertexDesc;
|
||||
|
||||
public:
|
||||
TestVertexShader();
|
||||
virtual ~TestVertexShader();
|
||||
};
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
//
|
||||
|
||||
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