setup planning (wip)
This commit is contained in:
parent
119f5f069c
commit
25899ff448
@ -1,35 +1,51 @@
|
||||
import dlib;
|
||||
import vulkan;
|
||||
|
||||
const u32 IMG_MAX = 100;
|
||||
const u32 BUF_MAX = 25;
|
||||
const u32 UNI_MAX = 50;
|
||||
const u32 IMG_MAX = 100;
|
||||
const u32 BUF_MAX = 25;
|
||||
const u32 UNI_MAX = 50;
|
||||
const u32 DESC_SET_MAX = 4;
|
||||
|
||||
ImageView[256] TEXTURES;
|
||||
Buffer[256] MATERIALS;
|
||||
Buffer[256] MODEL_STATES;
|
||||
|
||||
DescIndices[DESC_SET_MAX] DESC_INDICES];
|
||||
|
||||
struct GameState
|
||||
{
|
||||
RenderState rds;
|
||||
u64 frame;
|
||||
u64 frame_idx;
|
||||
}
|
||||
|
||||
struct RenderState
|
||||
{
|
||||
Renderer rd;
|
||||
Arena[2] frame_arenas;
|
||||
Arena perm_arena;
|
||||
Renderer rd;
|
||||
Arena[2] frame_arenas;
|
||||
Arena perm_arena;
|
||||
|
||||
PushConst pc;
|
||||
ShaderGlobals globals;
|
||||
PushConst pc;
|
||||
ShaderGlobals globals;
|
||||
|
||||
Pipeline[PID.Max] pipelines;
|
||||
DescSetLayout desc_layout_globals;
|
||||
DescSetLayout desc_layout_resources;
|
||||
DescSet[2] desc_set_globals;
|
||||
DescSet[2] desc_set_resources;
|
||||
PipelineLayout pipeline_layout_pbr;
|
||||
Pipeline[PID.Max] pipelines;
|
||||
DescSetLayout desc_layout_globals;
|
||||
DescSetLayout desc_layout_resources;
|
||||
DescSet[2] desc_set_globals;
|
||||
PipelineLayout pipeline_layout_pbr;
|
||||
|
||||
ImageView placeholder_tex;
|
||||
Buffer globals_buffer;
|
||||
ImageView[] textures;
|
||||
Buffer[] materials;
|
||||
Buffer[] model_states;
|
||||
u64 itex, imat, istate;
|
||||
|
||||
ImageView placeholder_tex;
|
||||
Buffer globals_buffer;
|
||||
}
|
||||
|
||||
struct DescIndices
|
||||
{
|
||||
u32 tex;
|
||||
u32 mat;
|
||||
u32 state;
|
||||
}
|
||||
|
||||
struct ShaderGlobals
|
||||
@ -41,15 +57,39 @@ struct ShaderGlobals
|
||||
f32 alpha = 0.0;
|
||||
}
|
||||
|
||||
struct MeshPart
|
||||
{
|
||||
u32 mat;
|
||||
u32 offset;
|
||||
u32 length;
|
||||
PushConst pc;
|
||||
|
||||
alias pc this;
|
||||
}
|
||||
|
||||
struct ModelData
|
||||
{
|
||||
MeshPart[] parts;
|
||||
ModelState state;
|
||||
Vertex[] v;
|
||||
u32[] idx;
|
||||
TextureData[] tex;
|
||||
}
|
||||
|
||||
struct TextureData
|
||||
{
|
||||
u8[] name;
|
||||
u8[] data;
|
||||
u32 width;
|
||||
u32 height;
|
||||
u32 ch;
|
||||
}
|
||||
|
||||
struct Model
|
||||
{
|
||||
Buffer v_buf;
|
||||
Buffer i_buf;
|
||||
Buffer s_buf;
|
||||
ModelState state;
|
||||
ModelRenderInfo info;
|
||||
Vertex[] v;
|
||||
u32[] idx;
|
||||
Buffer v_buf;
|
||||
Buffer i_buf;
|
||||
MeshPart[] parts;
|
||||
}
|
||||
|
||||
struct ModelRenderInfo
|
||||
@ -65,6 +105,15 @@ struct ModelState
|
||||
Mat4 matrix;
|
||||
}
|
||||
|
||||
struct Material
|
||||
{
|
||||
Vec4 ambient;
|
||||
Vec4 diffuse;
|
||||
Vec4 specular;
|
||||
float shininess;
|
||||
float alpha;
|
||||
}
|
||||
|
||||
enum PBRMod : u32
|
||||
{
|
||||
AlbedoValue = 0x0001,
|
||||
@ -101,7 +150,12 @@ enum PipelineID : u32
|
||||
|
||||
alias PID = PipelineID;
|
||||
|
||||
const PID[] PBR_PIPELINES = [PID.PBRVVVV, PID.PBRTVVV, PID.PBRVTVV, PID.PBRVVTV, PID.PBRVVVT, PID.PBRTTVV, PID.PBRTVTV, PID.PBRTVVT, PID.PBRVTTV, PID.PBRVTVT, PID.PBRVVTT, PID.PBRTTTV, PID.PBRTTVT, PID.PBRTVTT, PID.PBRVTTT, PID.PBRTTTT];
|
||||
const PID[] PBR_PIPELINES = [
|
||||
PID.PBRVVVV, PID.PBRTVVV, PID.PBRVTVV, PID.PBRVVTV,
|
||||
PID.PBRVVVT, PID.PBRTTVV, PID.PBRTVTV, PID.PBRTVVT,
|
||||
PID.PBRVTTV, PID.PBRVTVT, PID.PBRVVTT, PID.PBRTTTV,
|
||||
PID.PBRTTVT, PID.PBRTVTT, PID.PBRVTTT, PID.PBRTTTT,
|
||||
];
|
||||
|
||||
struct PushConst
|
||||
{
|
||||
@ -137,7 +191,7 @@ struct Vertex
|
||||
Vec2 uv;
|
||||
}
|
||||
|
||||
Model g_box;
|
||||
ModelData g_box;
|
||||
|
||||
GameState
|
||||
InitGame(PlatformWindow* window)
|
||||
@ -148,34 +202,6 @@ InitGame(PlatformWindow* window)
|
||||
return g;
|
||||
}
|
||||
|
||||
void
|
||||
RunCycle(GameState* g)
|
||||
{
|
||||
g.frame_idx = g.frame % 2;
|
||||
|
||||
Reset(&g.rds.frame_arenas[g.frame_idx]);
|
||||
|
||||
Renderer* rd = &g.rds.rd;
|
||||
|
||||
BeginFrame(rd);
|
||||
|
||||
BeginRendering(rd);
|
||||
|
||||
PushConstants(rd, g.rds.pipelines[g_box.info.pid], &g_box.info.pc);
|
||||
|
||||
Bind(rd, g.rds.pipelines[g_box.info.pid], [g.rds.desc_set_globals[g.frame_idx], g.rds.desc_set_resources[g.frame_idx]]);
|
||||
|
||||
BindBuffers(rd, &g_box.i_buf, &g_box.v_buf);
|
||||
|
||||
DrawIndexed(rd, cast(u32)g_box.idx.length, 1, 0);
|
||||
|
||||
FinishRendering(rd);
|
||||
|
||||
SubmitAndPresent(rd);
|
||||
|
||||
g.frame += 1;
|
||||
}
|
||||
|
||||
void
|
||||
Init(RenderState* rds, PlatformWindow* window)
|
||||
{
|
||||
@ -187,6 +213,12 @@ Init(RenderState* rds, PlatformWindow* window)
|
||||
};
|
||||
}
|
||||
|
||||
const u64 resource_max = 256;
|
||||
|
||||
rds.textures = TEXTURES;
|
||||
rds.materials = MATERIALS;
|
||||
rds.model_states = MODEL_STATES;
|
||||
|
||||
DescLayoutBinding[3] global_bindings = [
|
||||
{ binding: 0, descriptorType: DT.Uniform, descriptorCount: 1, stageFlags: SS.All },
|
||||
{ binding: 1, descriptorType: DT.StorageTexelBuf, descriptorCount: 1, stageFlags: SS.All },
|
||||
@ -194,9 +226,12 @@ Init(RenderState* rds, PlatformWindow* window)
|
||||
];
|
||||
|
||||
DescLayoutBinding[3] resource_bindings = [
|
||||
{ binding: 0, descriptorType: DT.Image, descriptorCount: IMG_MAX, stageFlags: SS.All },
|
||||
{ binding: 1, descriptorType: DT.Uniform, descriptorCount: BUF_MAX, stageFlags: SS.All },
|
||||
{ binding: 2, descriptorType: DT.Uniform, descriptorCount: UNI_MAX, stageFlags: SS.All },
|
||||
{ binding: 0, descriptorType: DT.Image, descriptorCount: 1, stageFlags: SS.All },
|
||||
{ binding: 1, descriptorType: DT.Image, descriptorCount: 1, stageFlags: SS.All },
|
||||
{ binding: 2, descriptorType: DT.Image, descriptorCount: 1, stageFlags: SS.All },
|
||||
{ binding: 3, descriptorType: DT.Image, descriptorCount: 1, stageFlags: SS.All },
|
||||
{ binding: 4, descriptorType: DT.Uniform, descriptorCount: 1, stageFlags: SS.All },
|
||||
{ binding: 5, descriptorType: DT.Uniform, descriptorCount: 1, stageFlags: SS.All },
|
||||
];
|
||||
|
||||
Attribute[5] attributes = [
|
||||
@ -222,7 +257,6 @@ Init(RenderState* rds, PlatformWindow* window)
|
||||
foreach(i; 0 .. 2)
|
||||
{
|
||||
rds.desc_set_globals[i] = AllocDescSet(&rds.rd, rds.desc_layout_globals);
|
||||
rds.desc_set_resources[i] = AllocDescSet(&rds.rd, rds.desc_layout_resources);
|
||||
}
|
||||
|
||||
u32[4] spec_data;
|
||||
@ -280,27 +314,8 @@ Init(RenderState* rds, PlatformWindow* window)
|
||||
CreateImageView(&rds.rd, &rds.placeholder_tex, 32, 32, 4, placeholder_tex);
|
||||
|
||||
CreateBuffer(&rds.rd, &rds.globals_buffer, BT.Uniform, ShaderGlobals.sizeof, false);
|
||||
bool transfer = Transfer(&rds.rd, &rds.globals_buffer, &rds.globals);
|
||||
|
||||
assert(transfer);
|
||||
|
||||
g_box = MakeBox(rds, 6.0, 6.0, Vec4(0.3, 0.4, 0.8, 1.0));
|
||||
|
||||
CreateBuffer(&rds.rd, &g_box.v_buf, BT.Vertex, g_box.v.length * Vertex.sizeof, false);
|
||||
CreateBuffer(&rds.rd, &g_box.i_buf, BT.Index, g_box.idx.length * u32.sizeof, false);
|
||||
CreateBuffer(&rds.rd, &g_box.s_buf, BT.Uniform, ModelState.sizeof, false);
|
||||
|
||||
transfer = Transfer(&rds.rd, &g_box.v_buf, g_box.v);
|
||||
transfer &= Transfer(&rds.rd, &g_box.i_buf, g_box.idx);
|
||||
transfer &= Transfer(&rds.rd, &g_box.s_buf, &g_box.state);
|
||||
|
||||
assert(transfer);
|
||||
|
||||
Write(&rds.rd, rds.desc_set_globals[0], &rds.globals_buffer, 0, DT.Uniform);
|
||||
Write(&rds.rd, rds.desc_set_globals[1], &rds.globals_buffer, 0, DT.Uniform);
|
||||
|
||||
Write(&rds.rd, rds.desc_set_resources[0], &g_box.s_buf, 2, 0, DT.Uniform);
|
||||
Write(&rds.rd, rds.desc_set_resources[1], &g_box.s_buf, 2, 0, DT.Uniform);
|
||||
g_box = MakeBox
|
||||
}
|
||||
|
||||
PipelineID
|
||||
@ -366,26 +381,24 @@ GetPBRMod(bool albedo = false, bool ambient = false, bool specular = false, bool
|
||||
}
|
||||
}
|
||||
|
||||
Model
|
||||
ModelData
|
||||
MakeBox(RenderState* rds, f32 width, f32 height, Vec4 col)
|
||||
{
|
||||
Model box = {
|
||||
v: Alloc!(Vertex)(&rds.perm_arena, 8),
|
||||
idx: Alloc!(u32)(&rds.perm_arena, 36),
|
||||
info: {
|
||||
pid: PID.PBRVVVV,
|
||||
},
|
||||
ModelData box = {
|
||||
v: Alloc!(Vertex)(&rds.frame_arenas[0], 8),
|
||||
idx: Alloc!(u32)(&rds.frame_arenas[0], 36),
|
||||
parts: Alloc!(MeshPart)(&rds.frame_arenas[0], 1),
|
||||
state: {
|
||||
matrix: Mat4Identity(),
|
||||
},
|
||||
};
|
||||
|
||||
const Vec3[8] positions = [
|
||||
static const Vec3[8] positions = [
|
||||
Vec3(-1.0, 0.0, -1.0), Vec3(+1.0, 0.0, -1.0), Vec3(+1.0, 1.0, -1.0), Vec3(-1.0, 1.0, -1.0),
|
||||
Vec3(-1.0, 0.0, +1.0), Vec3(+1.0, 0.0, +1.0), Vec3(+1.0, 1.0, +1.0), Vec3(-1.0, 1.0, +1.0),
|
||||
];
|
||||
|
||||
const u32[36] indices = [
|
||||
static const u32[36] indices = [
|
||||
0, 1, 3, 3, 1, 2,
|
||||
1, 5, 2, 2, 5, 6,
|
||||
5, 4, 6, 6, 4, 7,
|
||||
@ -403,10 +416,55 @@ MakeBox(RenderState* rds, f32 width, f32 height, Vec4 col)
|
||||
}
|
||||
|
||||
box.idx[] = indices[];
|
||||
|
||||
box.parts[0].mat = 0;
|
||||
box.parts[0].offset = 0;
|
||||
box.parts[0].length = indices.length;
|
||||
|
||||
return box;
|
||||
}
|
||||
|
||||
Model
|
||||
Upload(RenderState* rds, ModelData* data)
|
||||
{
|
||||
Model model;
|
||||
u32[] tex_idx = Alloc!(&rds.frame_arenas[0], data.text.length);
|
||||
u32[] mat_idx = Alloc!(&rds.frame_arenas[0], data.materials.length);
|
||||
u32[] state_idx = Alloc!(&rds.frame_arenas[0], data.model_states.length);
|
||||
|
||||
bool result = true;
|
||||
|
||||
result &= Transfer(&rds.rd, &model.v_buf, data.v);
|
||||
result &= Transfer(&rds.rd, &model.i_buf, data.idx);
|
||||
|
||||
assert(result);
|
||||
|
||||
for(u64 i = 0; i < data.tex.length; i += 1)
|
||||
{
|
||||
TextureData* tex = &data.tex[i];
|
||||
ImageView* view = &rds.textures[rds.itex++];
|
||||
CreateImageView(&rds.rd, view, tex.width, tex.height, tex.ch, tex.data);
|
||||
}
|
||||
|
||||
for(u64 i = 0; i < data.materials.length; i += 1)
|
||||
{
|
||||
Buffer* buf = &rds.materials[rds.imat++];
|
||||
CreateBuffer(&rds.rd, buf, BT.Uniform, Material.sizeof);
|
||||
result = Transfer(&rds.rd, buf, )
|
||||
}
|
||||
|
||||
for(u64 i = 0; i < data.model_states.length; i += 1)
|
||||
{
|
||||
Buffer* buf = &rds.model_states[rds.istate++];
|
||||
CreateBuffer(&rds)
|
||||
}
|
||||
|
||||
model.parts = data.parts;
|
||||
}
|
||||
|
||||
DescIndices*
|
||||
GetDescIndices()
|
||||
|
||||
unittest
|
||||
{
|
||||
with(PBRMod) with(PID)
|
||||
|
||||
@ -8,6 +8,7 @@ import core.stdc.string : memcpy;
|
||||
void main(string[] argv)
|
||||
{
|
||||
PlatformWindow window = CreateWindow("Video Game", 1920, 1080);
|
||||
StartPlatformThread(&window);
|
||||
|
||||
GameState g = InitGame(&window);
|
||||
|
||||
|
||||
@ -16,8 +16,7 @@ layout (constant_id = 3) const bool ALPHA_TEX = false;
|
||||
// SET 0
|
||||
|
||||
layout (rgba16f, set = 0, binding = 0) uniform image2D DrawImage;
|
||||
|
||||
layout (set = 0, binding = 1) uniform sampler SamplerNearest;
|
||||
layout ( set = 0, binding = 1) uniform sampler SamplerNearest;
|
||||
|
||||
// SET 1
|
||||
|
||||
@ -29,26 +28,25 @@ layout (set = 1, binding = 0) uniform Globals {
|
||||
vec3 light_direction;
|
||||
vec2 res;
|
||||
} G;
|
||||
|
||||
layout (set = 1, binding = 1, rg32ui) uniform coherent uimageBuffer ImageABuffer;
|
||||
|
||||
layout (set = 1, binding = 2, r32ui) uniform coherent uimage2D ImageAux;
|
||||
|
||||
// SET 2
|
||||
|
||||
layout (set = 2, binding = 0) uniform texture2D Textures[IMG_MAX];
|
||||
|
||||
layout (set = 2, binding = 1) uniform Material {
|
||||
layout (set = 2, binding = 0) uniform texture2D AlbedoTex;
|
||||
layout (set = 2, binding = 1) uniform texture2D AmbientTex;
|
||||
layout (set = 2, binding = 2) uniform texture2D SpecularTex;
|
||||
layout (set = 2, binding = 3) uniform texture2D AlphaTex;
|
||||
layout (set = 2, binding = 4) uniform MaterialData {
|
||||
vec4 ambient;
|
||||
vec4 diffuse;
|
||||
vec4 specular;
|
||||
float shininess;
|
||||
float alpha;
|
||||
} Materials[BUF_MAX];
|
||||
|
||||
layout (set = 2, binding = 2) uniform ModelState {
|
||||
} Material;
|
||||
layout (set = 2, binding = 5) uniform ModelState {
|
||||
mat4 model_matrix;
|
||||
} State[UNI_MAX];
|
||||
} State;
|
||||
|
||||
// PUSH CONSTANTS
|
||||
|
||||
@ -63,13 +61,9 @@ layout (push_constant) uniform PushConstants {
|
||||
|
||||
// ALIAS MACROS
|
||||
|
||||
#define ModelMatrix State[PC.s0].model_matrix
|
||||
#define AlbedoTex Textures[PC.t0]
|
||||
#define AmbientTex Textures[PC.t1]
|
||||
#define SpecularTex Textures[PC.t2]
|
||||
#define AlphaTex Textures[PC.t3]
|
||||
#define ModelAmbient Materials[PC.m0].ambient
|
||||
#define ModelDiffuse Materials[PC.m0].diffuse
|
||||
#define ModelSpecular Materials[PC.m0].specular
|
||||
#define ModelShininess Materials[PC.m0].shininess
|
||||
#define ModelAlpha Materials[PC.m0].alpha
|
||||
#define ModelMatrix State.model_matrix
|
||||
#define ModelAmbient Material.ambient
|
||||
#define ModelDiffuse Material.diffuse
|
||||
#define ModelSpecular Material.specular
|
||||
#define ModelShininess Material.shininess
|
||||
#define ModelAlpha Material.alpha
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user