2019-08-01 15:37:44 +08:00
|
|
|
|
// 3.HQFilterTexture
|
|
|
|
|
// 测试高质量纹理过滤函数
|
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKTexture.h>
|
|
|
|
|
#include<hgl/graph/VKSampler.h>
|
2019-08-01 15:37:44 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
constexpr uint32_t SCREEN_SIZE=512;
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=4;
|
|
|
|
|
|
|
|
|
|
constexpr float vertex_data[VERTEX_COUNT][2]=
|
|
|
|
|
{
|
2020-09-21 17:19:47 +08:00
|
|
|
|
{0, 0},
|
|
|
|
|
{1, 0},
|
|
|
|
|
{0, 1},
|
|
|
|
|
{1, 1}
|
2019-08-01 15:37:44 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr float tex_coord_data[VERTEX_COUNT][2]=
|
|
|
|
|
{
|
|
|
|
|
{0,0},
|
|
|
|
|
{1,0},
|
|
|
|
|
{0,1},
|
|
|
|
|
{1,1}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t INDEX_COUNT=6;
|
|
|
|
|
|
|
|
|
|
constexpr uint16 index_data[INDEX_COUNT]=
|
|
|
|
|
{
|
|
|
|
|
0,1,3,
|
|
|
|
|
0,3,2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
PipelineData * pipeline_data =nullptr;
|
|
|
|
|
Renderable * render_obj =nullptr;
|
|
|
|
|
Sampler * sampler_linear =nullptr;
|
|
|
|
|
Sampler * sampler_nearest =nullptr;
|
2020-09-21 17:19:47 +08:00
|
|
|
|
|
|
|
|
|
struct MP
|
2019-08-01 15:37:44 +08:00
|
|
|
|
{
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Material * material =nullptr;
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
2020-09-21 17:19:47 +08:00
|
|
|
|
}mp_normal,mp_hq;
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
struct MIR
|
|
|
|
|
{
|
2021-06-22 21:33:47 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2020-10-21 12:47:06 +08:00
|
|
|
|
RenderableInstance *renderable_instance =nullptr;
|
2020-09-21 17:19:47 +08:00
|
|
|
|
}mir_nearest,mir_linear,mir_nearest_hq,mir_linear_hq;
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Texture2D * texture =nullptr;
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
VAB * vertex_buffer =nullptr;
|
|
|
|
|
VAB * tex_coord_buffer =nullptr;
|
|
|
|
|
IndexBuffer * index_buffer =nullptr;
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
2021-06-22 21:33:47 +08:00
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList render_list;
|
|
|
|
|
|
2019-08-01 15:37:44 +08:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(index_buffer);
|
|
|
|
|
SAFE_CLEAR(tex_coord_buffer);
|
|
|
|
|
SAFE_CLEAR(vertex_buffer);
|
|
|
|
|
SAFE_CLEAR(sampler_nearest);
|
|
|
|
|
SAFE_CLEAR(sampler_linear);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
2021-06-24 19:25:43 +08:00
|
|
|
|
vertex_buffer =device->CreateVAB(VF_VEC2,VERTEX_COUNT,vertex_data);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
if(!vertex_buffer)return(false);
|
|
|
|
|
|
2021-06-24 19:25:43 +08:00
|
|
|
|
tex_coord_buffer=device->CreateVAB(VF_VEC2,VERTEX_COUNT,tex_coord_data);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
if(!tex_coord_buffer)return(false);
|
|
|
|
|
|
|
|
|
|
index_buffer =device->CreateIBO16(INDEX_COUNT,index_data);
|
|
|
|
|
if(!index_buffer)return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 19:05:25 +08:00
|
|
|
|
bool InitRenderObject()
|
|
|
|
|
{
|
|
|
|
|
render_obj=db->CreateRenderable(VERTEX_COUNT);
|
|
|
|
|
if(!render_obj)return(false);
|
|
|
|
|
|
|
|
|
|
render_obj->Set(VAN::Position,vertex_buffer);
|
|
|
|
|
render_obj->Set(VAN::TexCoord,tex_coord_buffer);
|
|
|
|
|
render_obj->Set(index_buffer);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
Sampler *InitSampler(VkFilter filter)
|
2019-08-01 15:37:44 +08:00
|
|
|
|
{
|
2020-10-21 12:47:06 +08:00
|
|
|
|
SamplerCreateInfo sampler_create_info;
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
|
|
|
|
sampler_create_info.magFilter = filter;
|
|
|
|
|
sampler_create_info.minFilter = filter;
|
|
|
|
|
sampler_create_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
|
|
|
sampler_create_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
|
|
|
sampler_create_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
|
|
|
sampler_create_info.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
|
|
|
sampler_create_info.mipLodBias = 0.0f;
|
|
|
|
|
sampler_create_info.anisotropyEnable = false;
|
|
|
|
|
sampler_create_info.maxAnisotropy = 0;
|
|
|
|
|
sampler_create_info.compareEnable = false;
|
|
|
|
|
sampler_create_info.compareOp = VK_COMPARE_OP_NEVER;
|
|
|
|
|
sampler_create_info.minLod = 0.0f;
|
|
|
|
|
sampler_create_info.maxLod = 1.0f;
|
|
|
|
|
sampler_create_info.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
|
|
|
|
|
sampler_create_info.unnormalizedCoordinates = false;
|
|
|
|
|
|
|
|
|
|
return device->CreateSampler(&sampler_create_info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitTexture()
|
|
|
|
|
{
|
2020-10-25 21:29:18 +08:00
|
|
|
|
texture=db->LoadTexture2D(OS_TEXT("res/image/heightmap.Tex2D"));
|
2019-08-01 15:37:44 +08:00
|
|
|
|
return texture;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
bool InitMaterial(MP *mp,const OSString &mtl_name)
|
2019-08-01 15:37:44 +08:00
|
|
|
|
{
|
2020-09-21 17:19:47 +08:00
|
|
|
|
mp->material=db->CreateMaterial(mtl_name);
|
|
|
|
|
if(!mp->material)return(false);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
mp->pipeline=CreatePipeline(mp->material,pipeline_data);
|
|
|
|
|
if(!mp->pipeline)return(false);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2020-10-21 12:47:06 +08:00
|
|
|
|
pipeline_data=GetPipelineData(InlinePipeline::Solid2D);
|
2020-09-21 17:19:47 +08:00
|
|
|
|
if(!pipeline_data)return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial(&mp_normal,OS_TEXT("res/material/Texture2DPC")))return(false);
|
|
|
|
|
if(!InitMaterial(&mp_hq, OS_TEXT("res/material/Texture2DHQPC")))return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:47:06 +08:00
|
|
|
|
bool InitMIR(struct MIR *mir,Sampler *sampler,MP *mp)
|
2020-09-21 17:19:47 +08:00
|
|
|
|
{
|
|
|
|
|
mir->material_instance=db->CreateMaterialInstance(mp->material);
|
|
|
|
|
if(!mir->material_instance)return(false);
|
2021-06-22 21:33:47 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
MaterialParameters *mp_texture=mir->material_instance->GetMP(DescriptorSetType::Value);
|
|
|
|
|
|
|
|
|
|
if(!mp_texture)
|
|
|
|
|
return(false);
|
2020-09-21 17:19:47 +08:00
|
|
|
|
|
2021-06-22 21:33:47 +08:00
|
|
|
|
if(!mp_texture->BindSampler("tex",texture,sampler))return(false);
|
|
|
|
|
|
|
|
|
|
mp_texture->Update();
|
|
|
|
|
}
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
2020-09-21 19:05:25 +08:00
|
|
|
|
mir->renderable_instance=db->CreateRenderableInstance(render_obj,mir->material_instance,mp->pipeline);
|
|
|
|
|
|
|
|
|
|
if(!mir->renderable_instance)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
return(true);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
bool Add(struct MIR *mir,const Matrix4f &offset)
|
2019-08-01 15:37:44 +08:00
|
|
|
|
{
|
2021-06-22 21:33:47 +08:00
|
|
|
|
render_root.CreateSubNode(offset,mir->renderable_instance);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitScene()
|
|
|
|
|
{
|
2020-09-21 17:19:47 +08:00
|
|
|
|
Add(&mir_nearest, translate(-1,-1,0));
|
|
|
|
|
Add(&mir_linear, translate( 0,-1,0));
|
|
|
|
|
Add(&mir_nearest_hq,translate(-1, 0,0));
|
|
|
|
|
Add(&mir_linear_hq, translate( 0, 0,0));
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
|
|
|
|
render_root.RefreshMatrix();
|
2021-06-22 21:33:47 +08:00
|
|
|
|
render_list.Expend(&render_root);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
BuildCommandBuffer(&render_list);
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2020-06-11 17:00:28 +08:00
|
|
|
|
|
2019-08-01 15:37:44 +08:00
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
2020-09-21 17:19:47 +08:00
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_SIZE,SCREEN_SIZE))
|
2019-08-01 15:37:44 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
2020-09-21 19:05:25 +08:00
|
|
|
|
|
|
|
|
|
if(!InitRenderObject())
|
|
|
|
|
return(false);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
|
|
|
|
if(!InitTexture())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2020-09-21 17:19:47 +08:00
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
sampler_nearest=InitSampler(VK_FILTER_NEAREST);
|
|
|
|
|
sampler_linear=InitSampler(VK_FILTER_LINEAR);
|
|
|
|
|
|
|
|
|
|
if(!InitMIR(&mir_nearest, sampler_nearest ,&mp_normal ))return(false);
|
|
|
|
|
if(!InitMIR(&mir_linear, sampler_linear ,&mp_normal ))return(false);
|
|
|
|
|
if(!InitMIR(&mir_nearest_hq, sampler_nearest ,&mp_hq ))return(false);
|
|
|
|
|
if(!InitMIR(&mir_linear_hq, sampler_linear ,&mp_hq ))return(false);
|
2019-08-01 15:37:44 +08:00
|
|
|
|
|
|
|
|
|
if(!InitScene())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h) override
|
|
|
|
|
{
|
|
|
|
|
BuildCommandBuffer(&render_list);
|
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
#ifdef _DEBUG
|
2020-10-21 12:47:06 +08:00
|
|
|
|
if(!CheckStrideBytesByFormat())
|
2019-08-01 15:37:44 +08:00
|
|
|
|
return 0xff;
|
|
|
|
|
#endif//
|
|
|
|
|
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|