start work on renderer + pipeline creation

This commit is contained in:
matthew 2025-07-10 07:20:26 +10:00
parent 3c5c759601
commit f4bbfcdf6c
14 changed files with 169 additions and 13 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,7 @@
import aliases;
import includes;
import std.stdio;
import core.memory;
version(linux)
{
@ -30,7 +31,7 @@ struct Function
void CheckErr(Window *window, xcb_void_cookie_t *cookie, xcb_generic_error_t *err, string msg)
{
assert(err == null, msg);
free(err);
pureFree(err);
};
Window CreateWindow(string name, u16 width, u16 height)
@ -132,9 +133,9 @@ Window CreateWindow(string name, u16 width, u16 height)
window.close_event = r_close.atom;
window.minimize_event = r_minimize.atom;
free(r_proto);
free(r_close);
free(r_minimize);
pureFree(r_proto);
pureFree(r_close);
pureFree(r_minimize);
xcb_map_window(window.conn, window.window);

View File

@ -2,3 +2,29 @@ import aliases;
import includes;
alias Shader = VkShaderModule;
alias Pipeline = VkPipeline;
alias Attribute = VkVertexInputAttributeDescription;
enum InputRate : int
{
Vertex = VK_VERTEX_INPUT_RATE_VERTEX,
Instance = VK_VERTEX_INPUT_RATE_INSTANCE,
}
enum Format: int
{
UINT = VK_FORMAT_R32_UINT,
R_F32 = VK_FORMAT_R32_SFLOAT,
RG_F32 = VK_FORMAT_R32G32_SFLOAT,
RGB_F32 = VK_FORMAT_R32G32B32_SFLOAT,
RGBA_F32 = VK_FORMAT_R32G32B32A32_SFLOAT,
}
struct GfxPipelineInfo
{
Shader vertex_shader;
Shader frag_shader;
InputRate input_rate;
u32 input_rate_stride;
Attribute[] vertex_attributes;
}

View File

@ -228,10 +228,109 @@ BuildShader(Vulkan* vk, u8[] bytes)
return shader;
}
Result!(VkPipeline)
VkPipeline
CreateGraphicsPipeline(Vulkan* vk, GfxPipelineInfo* build_info)
{
VkDynamicState[] dyn_state = [ VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR ];
VkPipelineDynamicStateCreateInfo dyn_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
pDynamicStates: dyn_state.ptr,
dynamicStateCount: cast(u32)dyn_state.length,
};
VkPipelineInputAssemblyStateCreateInfo assembly_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
primitiveRestartEnable: VK_FALSE,
};
VkPipelineRasterizationStateCreateInfo rasterization_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
polygonMode: VK_POLYGON_MODE_FILL,
lineWidth: 1.0,
frontFace: VK_FRONT_FACE_COUNTER_CLOCKWISE,
};
VkPipelineMultisampleStateCreateInfo multisample_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
minSampleShading: 1.0,
alphaToCoverageEnable: VK_FALSE,
alphaToOneEnable: VK_FALSE,
};
VkPipelineDepthStencilStateCreateInfo depth_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
depthTestEnable: VK_TRUE,
depthWriteEnable: VK_TRUE,
depthCompareOp: VK_COMPARE_OP_GREATER_OR_EQUAL,
depthBoundsTestEnable: VK_FALSE,
stencilTestEnable: VK_FALSE,
minDepthBounds: 0.0,
maxDepthBounds: 1.0,
};
VkPipelineRenderingCreateInfo rendering_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
colorAttachmentCount: 1,
};
VkPipelineColorBlendAttachmentState blend_state = {
colorWriteMask: VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
blendEnable: VK_FALSE,
};
VkPipelineColorBlendStateCreateInfo blend_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
logicOpEnable: VK_FALSE,
logicOp: VK_LOGIC_OP_COPY,
attachmentCount: 1,
pAttachments: &blend_state,
};
VkVertexInputBindingDescription vertex_input_desc = {
binding: 0,
inputRate: cast(VkVertexInputRate)build_info.input_rate,
stride: build_info.input_rate_stride,
};
VkPipelineVertexInputStateCreateInfo input_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
vertexBindingDescriptionCount: 1,
pVertexBindingDescriptions: &vertex_input_desc,
vertexAttributeDescriptionCount: cast(u32)build_info.vertex_attributes.length,
pVertexAttributeDescriptions: build_info.vertex_attributes.ptr,
};
VkPipelineViewportStateCreateInfo viewport_info = {
sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
viewportCount: 1,
scissorCount: 1,
};
VkGraphicsPipelineCreateInfo create_info = {
sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
pNext: &rendering_info,
pVertexInputState: &input_info,
pInputAssemblyState: &assembly_info,
pViewportState: &viewport_info,
pRasterizationState: &rasterization_info,
pMultisampleState: &multisample_info,
pColorBlendState: &blend_info,
pDepthStencilState: &depth_info,
pDynamicState: &dyn_info,
};
VkPipeline pipeline;
return pipeline;
}
VkPipeline
CreateComputePipeline(Vulkan* vk, string shader)
{
Result!(VkPipeline) pipeline;
VkPipeline pipeline;

View File

@ -0,0 +1,10 @@
#version 460
layout (location = 0) in vec3 fragColor;
layout (location = 0) out vec4 outColor;
void main()
{
outColor = vec4(fragColor, 1.0);
}

View File

@ -0,0 +1,22 @@
#version 460
layout (location = 0) out vec3 fragColor;
vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
void main()
{
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
fragColor = colors[gl_VertexIndex];
}

View File

@ -28,6 +28,3 @@ alias b32 = uint;
alias intptr = intptr_t;
alias uintptr = uintptr_t;
alias free = pureFree;
alias malloc = pureMalloc;
alias realloc = pureRealloc;

View File

@ -2,6 +2,7 @@ import aliases;
import m = math;
import std.stdio;
import core.stdc.string : memset;
import core.memory;
const DEFAULT_ALIGNMENT = (void *).sizeof * 2;
@ -15,7 +16,7 @@ struct Arena
T*
Alloc(T)()
{
void* mem = malloc(T.sizeof);
void* mem = pureMalloc(T.sizeof);
memset(mem, 0, T.sizeof);
return (cast(T*)mem);
}
@ -23,7 +24,7 @@ Alloc(T)()
T[]
AllocArray(T)(u64 count)
{
void* mem = malloc(T.sizeof * count);
void* mem = pureMalloc(T.sizeof * count);
memset(mem, 0, T.sizeof * count);
return (cast(T*)mem)[0 .. count];
}
@ -32,7 +33,7 @@ Arena
CreateArena(u64 size)
{
Arena arena = {
mem: cast(u8 *)malloc(size),
mem: cast(u8 *)pureMalloc(size),
length: size,
pos: 0,
};
@ -90,5 +91,5 @@ Reset(Arena* arena)
void
Free(Arena* arena)
{
free(arena.mem);
pureFree(arena.mem);
}