增加FragCoord属性测试

This commit is contained in:
hyzboy 2020-01-20 15:02:40 +08:00
parent d532e0d04c
commit e2811f2b2b
12 changed files with 282 additions and 38 deletions

View File

@ -10,25 +10,26 @@
endmacro() endmacro()
CreateProject(00.triangle first_triangle.cpp) CreateProject(00.triangle first_triangle.cpp)
CreateProject(01.indices_rect indices_rect.cpp) CreateProject(01.FragCoord FragCoordTest.cpp)
CreateProject(02.TextureFormat TextureFormat.cpp) CreateProject(02.indices_rect indices_rect.cpp)
CreateProject(03.texture_rect texture_rect.cpp) CreateProject(03.TextureFormat TextureFormat.cpp)
CreateProject(04.HQFilterTexture HQFilterTexture.cpp) CreateProject(04.texture_rect texture_rect.cpp)
CreateProject(05.Geometry2D Geometry2D.cpp) CreateProject(05.HQFilterTexture HQFilterTexture.cpp)
CreateProject(06.Geometry3D Geometry3D.cpp) CreateProject(06.Geometry2D Geometry2D.cpp)
CreateProject(07.SceneTree SceneTree.cpp) CreateProject(07.Geometry3D Geometry3D.cpp)
CreateProject(08.SceneTree SceneTree.cpp)
CreateProject(08.LoadModel LoadModel.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp ViewModelFramework.h) CreateProject(09.LoadModel LoadModel.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp ViewModelFramework.h)
target_link_libraries(08.LoadModel assimp) target_link_libraries(09.LoadModel assimp)
CreateProject(09.InlineGeometryScene InlineGeometryScene.cpp) CreateProject(10.InlineGeometryScene InlineGeometryScene.cpp)
CreateProject(10.Atomsphere Atomsphere.cpp) CreateProject(11.Atomsphere Atomsphere.cpp)
CreateProject(11.PBRBasic PBRBasic.cpp) CreateProject(12.PBRBasic PBRBasic.cpp)
#CreateProject(12.Deferred Deferred.cpp) #CreateProject(12.Deferred Deferred.cpp)
CreateProject(12.DeferredModel DeferredModel.cpp) CreateProject(13.DeferredModel DeferredModel.cpp)
CreateProject(13.AutoMaterial auto_material.cpp) CreateProject(14.AutoMaterial auto_material.cpp)

View File

@ -0,0 +1,175 @@
// 该范例主要用于测试gl_FragCoord值
#include"VulkanAppFramework.h"
#include<hgl/math/Math.h>
#include<hgl/filesystem/FileSystem.h>
using namespace hgl;
using namespace hgl::graph;
bool SaveToFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
bool LoadFromFile(const OSString &filename,VK_NAMESPACE::PipelineCreater *pc);
constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128;
constexpr uint32_t VERTEX_COUNT=4;
constexpr float vertex_data[VERTEX_COUNT][2]=
{
{0,0},
{SCREEN_WIDTH,0},
{0,SCREEN_HEIGHT},
{SCREEN_WIDTH,SCREEN_HEIGHT}
};
class TestApp:public VulkanApplicationFramework
{
private:
Camera cam;
vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Renderable * render_obj =nullptr;
vulkan::Buffer * ubo_mvp =nullptr;
vulkan::Buffer * ubo_mvp_fs =nullptr;
vulkan::Pipeline * pipeline =nullptr;
vulkan::VertexBuffer * vertex_buffer =nullptr;
public:
~TestApp()
{
SAFE_CLEAR(vertex_buffer);
SAFE_CLEAR(pipeline);
SAFE_CLEAR(ubo_mvp_fs);
SAFE_CLEAR(ubo_mvp);
SAFE_CLEAR(render_obj);
SAFE_CLEAR(descriptor_sets);
SAFE_CLEAR(material);
}
private:
bool InitMaterial()
{
material=shader_manage->CreateMaterial(OS_TEXT("res/shader/OnlyPosition.vert.spv"),
OS_TEXT("res/shader/FragCoord.frag.spv"));
if(!material)
return(false);
render_obj=material->CreateRenderable(VERTEX_COUNT);
descriptor_sets=material->CreateDescriptorSets();
return(true);
}
vulkan::Buffer *CreateUBO(const UTF8String &name,const VkDeviceSize size,void *data)
{
vulkan::Buffer *ubo=device->CreateUBO(size,data);
if(!ubo)
return(nullptr);
const int index=material->GetUBO(name);
if(index<0)
{
SAFE_CLEAR(ubo);
return(nullptr);
}
if(!descriptor_sets->BindUBO(index,ubo))
{
SAFE_CLEAR(ubo);
return(nullptr);
}
return ubo;
}
bool InitUBO()
{
const VkExtent2D extent=sc_render_target->GetExtent();
cam.width=extent.width;
cam.height=extent.height;
cam.Refresh();
ubo_mvp =CreateUBO("world", sizeof(WorldMatrix),&cam.matrix);
ubo_mvp_fs =CreateUBO("fragment_world",sizeof(WorldMatrix),&cam.matrix);
descriptor_sets->Update();
return(true);
}
void InitVBO()
{
vertex_buffer =device->CreateVBO(FMT_RG32F, VERTEX_COUNT,vertex_data);
render_obj->Set("Vertex", vertex_buffer);
}
bool InitPipeline()
{
AutoDelete<vulkan::PipelineCreater>
pipeline_creater=new vulkan::PipelineCreater(device,material,sc_render_target);
pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLE_STRIP);
pipeline=pipeline_creater->Create();
return pipeline;
}
public:
bool Init()
{
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
if(!InitMaterial())
return(false);
if(!InitUBO())
return(false);
InitVBO();
if(!InitPipeline())
return(false);
BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
return(true);
}
void Resize(int w,int h)override
{
cam.width=w;
cam.height=h;
cam.Refresh();
ubo_mvp->Write(&cam.matrix);
ubo_mvp_fs->Write(&cam.matrix);
BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
}
};//class TestApp:public VulkanApplicationFramework
int main(int,char **)
{
TestApp app;
if(!app.Init())
return(-1);
while(app.Run());
return 0;
}

View File

@ -1,5 +1,5 @@
// 1.indices_rect // indices_rect
// 该示例是0.triangle的进化演示使用索引数据画一个矩形 // 该示例演示使用索引数据画一个矩形,并使用了颜色材质
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"
#include<hgl/math/Math.h> #include<hgl/math/Math.h>
@ -12,6 +12,8 @@ constexpr uint32_t SCREEN_HEIGHT=128;
constexpr uint32_t VERTEX_COUNT=4; constexpr uint32_t VERTEX_COUNT=4;
static Vector4f color(1,1,0,1);
constexpr float SSP=0.25; constexpr float SSP=0.25;
constexpr float SSN=1-SSP; constexpr float SSN=1-SSP;
@ -41,6 +43,7 @@ private:
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Renderable * render_obj =nullptr; vulkan::Renderable * render_obj =nullptr;
vulkan::Buffer * ubo_mvp =nullptr; vulkan::Buffer * ubo_mvp =nullptr;
vulkan::Buffer * ubo_color_material =nullptr;
vulkan::Pipeline * pipeline =nullptr; vulkan::Pipeline * pipeline =nullptr;
@ -54,6 +57,7 @@ public:
SAFE_CLEAR(index_buffer); SAFE_CLEAR(index_buffer);
SAFE_CLEAR(vertex_buffer); SAFE_CLEAR(vertex_buffer);
SAFE_CLEAR(pipeline); SAFE_CLEAR(pipeline);
SAFE_CLEAR(ubo_color_material);
SAFE_CLEAR(ubo_mvp); SAFE_CLEAR(ubo_mvp);
SAFE_CLEAR(render_obj); SAFE_CLEAR(render_obj);
SAFE_CLEAR(descriptor_sets); SAFE_CLEAR(descriptor_sets);
@ -74,19 +78,38 @@ private:
return(true); return(true);
} }
vulkan::Buffer *CreateUBO(const UTF8String &name,const VkDeviceSize size,void *data)
{
vulkan::Buffer *ubo=device->CreateUBO(size,data);
if(!ubo)
return(nullptr);
const int index=material->GetUBO(name);
if(index<0)
{
SAFE_CLEAR(ubo);
return(nullptr);
}
if(!descriptor_sets->BindUBO(index,ubo))
{
SAFE_CLEAR(ubo);
return(nullptr);
}
return ubo;
}
bool InitUBO() bool InitUBO()
{ {
const VkExtent2D extent=sc_render_target->GetExtent(); const VkExtent2D extent=sc_render_target->GetExtent();
wm.ortho=ortho(extent.width,extent.height); wm.ortho=ortho(extent.width,extent.height);
ubo_mvp=device->CreateUBO(sizeof(WorldMatrix),&wm); ubo_mvp =CreateUBO("world", sizeof(WorldMatrix),&wm);
ubo_color_material =CreateUBO("color_material",sizeof(Vector4f),&color);
if(!ubo_mvp)
return(false);
if(!descriptor_sets->BindUBO(material->GetUBO("world"),ubo_mvp))
return(false);
descriptor_sets->Update(); descriptor_sets->Update();
return(true); return(true);

View File

@ -3,7 +3,12 @@
layout(location = 0) in vec4 FragmentColor; layout(location = 0) in vec4 FragmentColor;
layout(location = 0) out vec4 FragColor; layout(location = 0) out vec4 FragColor;
layout(binding=2) uniform ColorMaterial
{
vec4 color;
} color_material;
void main() void main()
{ {
FragColor=FragmentColor; FragColor=color_material.color;
} }

View File

@ -3,13 +3,21 @@
layout(location = 0) in vec2 Vertex; layout(location = 0) in vec2 Vertex;
layout(location = 1) in vec3 Color; layout(location = 1) in vec3 Color;
layout(binding = 0) uniform WorldMatrix layout(binding=0) uniform WorldMatrix // hgl/math/Math.h
{ {
mat4 ortho; mat4 ortho;
mat4 projection; mat4 projection;
mat4 inverse_projection;
mat4 modelview; mat4 modelview;
mat4 inverse_modelview;
mat4 mvp; mat4 mvp;
mat4 inverse_mvp;
vec4 view_pos; vec4 view_pos;
vec2 resolution;
} world; } world;
layout(location = 0) out vec4 FragmentColor; layout(location = 0) out vec4 FragmentColor;

25
res/shader/FragCoord.frag Normal file
View File

@ -0,0 +1,25 @@
#version 450 core
layout(binding=1) uniform WorldMatrix // hgl/math/Math.h
{
mat4 ortho;
mat4 projection;
mat4 inverse_projection;
mat4 modelview;
mat4 inverse_modelview;
mat4 mvp;
mat4 inverse_mvp;
vec4 view_pos;
vec2 resolution;
}fragment_world;
layout(location = 0) out vec4 FragColor;
void main()
{
FragColor=vec4(gl_FragCoord.xy/fragment_world.resolution,0,1);
}

View File

@ -2,20 +2,24 @@
layout(location = 0) in vec2 Vertex; layout(location = 0) in vec2 Vertex;
layout(binding = 0) uniform WorldMatrix layout(binding=0) uniform WorldMatrix // hgl/math/Math.h
{ {
mat4 ortho; mat4 ortho;
mat4 projection;
mat4 modelview;
mat4 mvp;
vec4 view_pos;
} world;
layout(location = 0) out vec4 FragmentColor; mat4 projection;
mat4 inverse_projection;
mat4 modelview;
mat4 inverse_modelview;
mat4 mvp;
mat4 inverse_mvp;
vec4 view_pos;
vec2 resolution;
} world;
void main() void main()
{ {
FragmentColor=vec4(1.0);
gl_Position=vec4(Vertex,0.0,1.0)*world.ortho; gl_Position=vec4(Vertex,0.0,1.0)*world.ortho;
} }

View File

@ -2,7 +2,7 @@
vec4 BaseColor; vec4 BaseColor;
[output] [gbuffer]
vec4 gb_color; vec4 gb_color;

1
res/shader/sc.bat Normal file
View File

@ -0,0 +1 @@
glslangValidator -V -o %1.spv %1

View File

@ -27,4 +27,6 @@ glslangValidator -V -o gbuffer_debug.frag.spv gbuffer_debug.frag
glslangValidator -V -o drand48.frag.spv drand48.frag glslangValidator -V -o drand48.frag.spv drand48.frag
glslangValidator -V -o Texture2D.vert.spv Texture2D.vert glslangValidator -V -o Texture2D.vert.spv Texture2D.vert
glslangValidator -V -o hqfilter.frag.spv hqfilter.frag glslangValidator -V -o hqfilter.frag.spv hqfilter.frag
glslangValidator -V -o FragCoord.frag.spv FragCoord.frag

View File

@ -46,7 +46,7 @@ namespace hgl
//注意: C++中要 projection * model_view * local_to_world * position //注意: C++中要 projection * model_view * local_to_world * position
//而GLSL中要 position * local_to_world * model_view * projection //而GLSL中要 position * local_to_world * model_view * projection
matrix.ortho=ortho(width,height,znear,zfar); matrix.ortho=ortho(width,height);
matrix.view_pos=eye; matrix.view_pos=eye;
matrix.resolution.x=width; matrix.resolution.x=width;

View File

@ -9,7 +9,7 @@ MATERIAL_NAMESPACE_BEGIN
MCC_DEFINE(ShadingModel, Uint, 1, false ), MCC_DEFINE(ShadingModel, Uint, 1, false ),
MCC_DEFINE(Color, Float, 3, true ), MCC_DEFINE(BaseColor, Float, 3, true ),
MCC_DEFINE(Opacity, Float, 1, false ), MCC_DEFINE(Opacity, Float, 1, false ),
MCC_DEFINE(Normal, Float, 3, false ), MCC_DEFINE(Normal, Float, 3, false ),