slang shader!
This commit is contained in:
1 parent
544e6da087
commit
35562dd3fb
8 files changed
+167
-146
No files matched your search
+9
-2
@@ -236,7 +236,7 @@ void createDevice() {
|
||||
|
||||
VkPhysicalDeviceVulkan13Features enabledVk13Features{
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES,
|
||||
.pNext = nullptr,
|
||||
.pNext = &enabledVk14Features,
|
||||
.robustImageAccess = false,
|
||||
.inlineUniformBlock = false,
|
||||
.descriptorBindingInlineUniformBlockUpdateAfterBind = false,
|
||||
@@ -264,13 +264,20 @@ void createDevice() {
|
||||
.runtimeDescriptorArray = true,
|
||||
.bufferDeviceAddress = true,
|
||||
};
|
||||
|
||||
VkPhysicalDeviceVulkan11Features enabledVk11Features{
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES,
|
||||
.pNext = &enabledVk12Features,
|
||||
.shaderDrawParameters = true,
|
||||
};
|
||||
|
||||
const VkPhysicalDeviceFeatures enabledVk10Features{
|
||||
.samplerAnisotropy = true,
|
||||
};
|
||||
|
||||
const VkDeviceCreateInfo dci{
|
||||
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||
.pNext = &enabledVk12Features,
|
||||
.pNext = &enabledVk11Features,
|
||||
.queueCreateInfoCount = 1,
|
||||
.pQueueCreateInfos = &qci,
|
||||
.enabledExtensionCount = (uint32_t)devExts.size(),
|
||||
|
||||
+20
-21
@@ -6,6 +6,7 @@
|
||||
#include "init.h"
|
||||
#include "sprite.h"
|
||||
#include <vma/vk_mem_alloc.h>
|
||||
#include <slang/slang.h>
|
||||
|
||||
bool SortKey::operator<(const SortKey& b) const {
|
||||
if (depth != b.depth) return depth < b.depth;
|
||||
@@ -16,7 +17,7 @@ bool SortKey::operator<(const SortKey& b) const {
|
||||
Renderer::Renderer(GLFWwindow *window) {
|
||||
|
||||
create_pipeline_layout();
|
||||
colored_quad_pipeline = create_graphics_pipeline<vertex_p2_st2_col4_a1_u32>(
|
||||
colored_quad_pipeline = create_graphics_pipeline<vertex_p2_s2_st2_col4_a1_u32>(
|
||||
device,
|
||||
pipelineLayout,
|
||||
swapchain_format.format,
|
||||
@@ -36,8 +37,7 @@ void Renderer::flush() {
|
||||
|
||||
}
|
||||
|
||||
void Renderer::submit_quad() {
|
||||
glm::vec2 pos = {0, 0};
|
||||
void Renderer::submit_quad(glm::vec2 pos) {
|
||||
RenderCommand cmd {};
|
||||
cmd.pipeline = PipelineType::ColoredQuad;
|
||||
cmd.key = {
|
||||
@@ -47,8 +47,8 @@ void Renderer::submit_quad() {
|
||||
};
|
||||
|
||||
cmd.colored_quad = {
|
||||
.position = pos,
|
||||
.size = {0.25, 0.25},
|
||||
.pos = pos,
|
||||
.scale = {0.25, 0.25},
|
||||
.color = {0, 1, 1, 1},
|
||||
};
|
||||
|
||||
@@ -552,7 +552,7 @@ void Renderer::end_frame() {
|
||||
return cmd.key.depth;
|
||||
});
|
||||
|
||||
std::vector<vertex_p2_st2_col4_a1_u32> vertices;
|
||||
std::vector<vertex_p2_s2_st2_col4_a1_u32> vertices;
|
||||
|
||||
for (auto& cmd : commands) {
|
||||
|
||||
@@ -562,10 +562,10 @@ void Renderer::end_frame() {
|
||||
const auto &q = cmd.colored_quad;
|
||||
|
||||
// Calculate spatial corners
|
||||
float x0 = q.position.x;
|
||||
float y0 = q.position.y;
|
||||
float x1 = q.position.x + q.size.x;
|
||||
float y1 = q.position.y + q.size.y;
|
||||
//float x0 = q.position.x;
|
||||
//float y0 = q.position.y;
|
||||
//float x1 = q.position.x + q.size.x;
|
||||
//float y1 = q.position.y + q.size.y;
|
||||
|
||||
// Calculate UV corners
|
||||
// float u0 = q.uvMin.x;
|
||||
@@ -574,25 +574,23 @@ void Renderer::end_frame() {
|
||||
// float v1 = q.uvMax.y;
|
||||
|
||||
// Define the 4 corners of the quad
|
||||
vertex_p2_st2_col4_a1_u32 vTL = { {x0, y0}, {0, 0}, q.color, 1, 0 };
|
||||
vertex_p2_st2_col4_a1_u32 vTR = { {x1, y0}, {0, 0}, q.color, 1, 0 };
|
||||
vertex_p2_st2_col4_a1_u32 vBL = { {x0, y1}, {0, 0}, q.color, 1, 0 };
|
||||
vertex_p2_st2_col4_a1_u32 vBR = { {x1, y1}, {0, 0}, q.color, 1, 0 };
|
||||
vertex_p2_s2_st2_col4_a1_u32 vTL = { q.pos, q.scale, {0, 0}, q.color, 1, 0 };
|
||||
vertex_p2_s2_st2_col4_a1_u32 vTR = { q.pos, q.scale, {0, 0}, q.color, 1, 0 };
|
||||
vertex_p2_s2_st2_col4_a1_u32 vBL = { q.pos, q.scale, {0, 0}, q.color, 1, 0 };
|
||||
vertex_p2_s2_st2_col4_a1_u32 vBR = { q.pos, q.scale, {0, 0}, q.color, 1, 0 };
|
||||
|
||||
// vertex_p2_st2_col4_a1_u32 vTL = { {x0, y0}, {u0, v0}, q.color, 1, q.textureID };
|
||||
// vertex_p2_st2_col4_a1_u32 vTR = { {x1, y0}, {u1, v0}, q.color, 1, q.textureID };
|
||||
// vertex_p2_st2_col4_a1_u32 vBL = { {x0, y1}, {u0, v1}, q.color, 1, q.textureID };
|
||||
// vertex_p2_st2_col4_a1_u32 vBR = { {x1, y1}, {u1, v1}, q.color, 1, q.textureID };
|
||||
|
||||
// --- Triangle 1 (TL, TR, BL) ---
|
||||
vertices.push_back(vTL);
|
||||
vertices.push_back(vTR);
|
||||
vertices.push_back(vBL);
|
||||
vertices.push_back(vTR);
|
||||
|
||||
// --- Triangle 2 (TR, BR, BL) ---
|
||||
vertices.push_back(vTR);
|
||||
vertices.push_back(vBR);
|
||||
vertices.push_back(vBL);
|
||||
vertices.push_back(vBR);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -670,14 +668,15 @@ void Renderer::end_frame() {
|
||||
void Renderer::upload_vertex_buffer(
|
||||
VkCommandBuffer cmd,
|
||||
const Frame &frame,
|
||||
std::span<const vertex_p2_st2_col4_a1_u32> vertices) const {
|
||||
std::span<const vertex_p2_s2_st2_col4_a1_u32> vertices) const
|
||||
{
|
||||
VkMemoryPropertyFlags memPropFlags;
|
||||
vmaGetAllocationMemoryProperties(allocator, frame.vertexBuffer.allocation, &memPropFlags);
|
||||
|
||||
if(memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
|
||||
// The Allocation ended up in a mappable memory.
|
||||
// Calling vmaCopyMemoryToAllocation() does vmaMapMemory(), memcpy(), vmaUnmapMemory(), and vmaFlushAllocation().
|
||||
VkResult result = vmaCopyMemoryToAllocation(allocator, vertices.data(), frame.vertexBuffer.allocation, 0, vertices.size() * sizeof(vertex_p2_st2_col4_a1_u32));
|
||||
VkResult result = vmaCopyMemoryToAllocation(allocator, vertices.data(), frame.vertexBuffer.allocation, 0, vertices.size() * sizeof(vertex_p2_s2_st2_col4_a1_u32));
|
||||
// Check result...
|
||||
|
||||
VkBufferMemoryBarrier bufMemBarrier = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER };
|
||||
@@ -702,7 +701,7 @@ void Renderer::recordCommandBuffer(
|
||||
VkExtent2D extent,
|
||||
VkImageLayout oldLayout,
|
||||
const Frame &frame,
|
||||
const std::vector<vertex_p2_st2_col4_a1_u32> &vertices) const
|
||||
const std::vector<vertex_p2_s2_st2_col4_a1_u32> &vertices) const
|
||||
{
|
||||
|
||||
{
|
||||
|
||||
+67
-39
@@ -15,6 +15,10 @@
|
||||
#include <misc.h>
|
||||
#include <array>
|
||||
#include <span>
|
||||
#include <slang/slang.h>
|
||||
#include <slang/slang-com-ptr.h>
|
||||
|
||||
inline Slang::ComPtr<slang::IGlobalSession> slangGlobalSession;
|
||||
|
||||
enum class PROJECTION_TYPE : uint8_t {
|
||||
NONE,
|
||||
@@ -25,25 +29,27 @@ enum class PROJECTION_TYPE : uint8_t {
|
||||
COUNT,
|
||||
};
|
||||
|
||||
struct vertex_p2_st2_col4_a1_u32 {
|
||||
struct vertex_p2_s2_st2_col4_a1_u32 {
|
||||
glm::vec2 pos;
|
||||
glm::vec2 st;
|
||||
glm::vec4 col;
|
||||
glm::vec2 scale;
|
||||
glm::vec2 uv;
|
||||
glm::vec4 color;
|
||||
float alpha;
|
||||
uint32_t textureID;
|
||||
|
||||
static VkVertexInputBindingDescription getBindingDescription() {
|
||||
return {0, sizeof(vertex_p2_st2_col4_a1_u32), VK_VERTEX_INPUT_RATE_VERTEX};
|
||||
return {0, sizeof(vertex_p2_s2_st2_col4_a1_u32), VK_VERTEX_INPUT_RATE_VERTEX};
|
||||
}
|
||||
|
||||
static std::array<VkVertexInputAttributeDescription, 5> getAttributeDescriptions() {
|
||||
static std::array<VkVertexInputAttributeDescription, 6> getAttributeDescriptions() {
|
||||
return {
|
||||
{
|
||||
{0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(vertex_p2_st2_col4_a1_u32, pos)},
|
||||
{1, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(vertex_p2_st2_col4_a1_u32, st)},
|
||||
{2, 0, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(vertex_p2_st2_col4_a1_u32, col)},
|
||||
{3, 0, VK_FORMAT_R32_SFLOAT, offsetof(vertex_p2_st2_col4_a1_u32, alpha)},
|
||||
{4, 0, VK_FORMAT_R32_UINT, offsetof(vertex_p2_st2_col4_a1_u32, textureID)},
|
||||
{0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(vertex_p2_s2_st2_col4_a1_u32, pos)},
|
||||
{1, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(vertex_p2_s2_st2_col4_a1_u32, scale)},
|
||||
{2, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(vertex_p2_s2_st2_col4_a1_u32, uv)},
|
||||
{3, 0, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(vertex_p2_s2_st2_col4_a1_u32, color)},
|
||||
{4, 0, VK_FORMAT_R32_SFLOAT, offsetof(vertex_p2_s2_st2_col4_a1_u32, alpha)},
|
||||
{5, 0, VK_FORMAT_R32_UINT, offsetof(vertex_p2_s2_st2_col4_a1_u32, textureID)},
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -70,8 +76,8 @@ struct TexturedQuadCmd {
|
||||
};
|
||||
|
||||
struct ColoredQuadCmd {
|
||||
glm::vec2 position;
|
||||
glm::vec2 size;
|
||||
glm::vec2 pos;
|
||||
glm::vec2 scale;
|
||||
glm::vec4 color;
|
||||
};
|
||||
|
||||
@@ -167,7 +173,7 @@ struct Renderer {
|
||||
void flush();
|
||||
|
||||
void submit_sprite(glm::vec2 pos, const sprite_t &sprite);
|
||||
void submit_quad();
|
||||
void submit_quad(glm::vec2 pos);
|
||||
|
||||
explicit Renderer(GLFWwindow *window);
|
||||
void create_pipeline_layout();
|
||||
@@ -180,7 +186,7 @@ struct Renderer {
|
||||
VkExtent2D extent,
|
||||
VkImageLayout oldLayout,
|
||||
const Frame &frame,
|
||||
const std::vector<vertex_p2_st2_col4_a1_u32> &vertices) const;
|
||||
const std::vector<vertex_p2_s2_st2_col4_a1_u32> &vertices) const;
|
||||
void immediate_submit(std::function<void(VkCommandBuffer)>&& func) const;
|
||||
void transition_image_layout(VkCommandBuffer cmd, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout) const;
|
||||
VkImageView create_image_view(VkImage image, VkFormat format) const;
|
||||
@@ -190,7 +196,7 @@ struct Renderer {
|
||||
void upload_vertex_buffer(
|
||||
VkCommandBuffer cmd,
|
||||
const Frame &frame,
|
||||
std::span<const vertex_p2_st2_col4_a1_u32> vertices) const;
|
||||
std::span<const vertex_p2_s2_st2_col4_a1_u32> vertices) const;
|
||||
|
||||
VkPipeline get_pipeline(PipelineType type) const;
|
||||
// void bind_material(VkCommandBuffer cmd, uint16_t materialID);
|
||||
@@ -211,6 +217,38 @@ struct Renderer {
|
||||
bool enableBlending)
|
||||
{
|
||||
|
||||
auto slangTargets{ std::to_array<slang::TargetDesc>({ {
|
||||
.format = SLANG_SPIRV,
|
||||
.profile = slangGlobalSession->findProfile("spirv_1_4")
|
||||
} })};
|
||||
auto slangOptions{ std::to_array<slang::CompilerOptionEntry>({ {
|
||||
slang::CompilerOptionName::EmitSpirvDirectly,
|
||||
{slang::CompilerOptionValueKind::Int, 1}
|
||||
} })};
|
||||
slang::SessionDesc slangSessionDesc{
|
||||
.targets = slangTargets.data(),
|
||||
.targetCount = SlangInt(slangTargets.size()),
|
||||
.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR,
|
||||
.compilerOptionEntries = slangOptions.data(),
|
||||
.compilerOptionEntryCount = uint32_t(slangOptions.size())
|
||||
};
|
||||
Slang::ComPtr<slang::ISession> slangSession;
|
||||
slangGlobalSession->createSession(slangSessionDesc, slangSession.writeRef());
|
||||
|
||||
Slang::ComPtr<slang::IModule> slangModule{
|
||||
slangSession->loadModuleFromSource("triangle", "shaders/shader.slang", nullptr, nullptr)
|
||||
};
|
||||
Slang::ComPtr<ISlangBlob> spirv;
|
||||
slangModule->getTargetCode(0, spirv.writeRef());
|
||||
|
||||
VkShaderModuleCreateInfo shaderModuleCI{
|
||||
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
|
||||
.codeSize = spirv->getBufferSize(),
|
||||
.pCode = (uint32_t*)spirv->getBufferPointer()
|
||||
};
|
||||
VkShaderModule shaderModule{};
|
||||
vkCreateShaderModule(device, &shaderModuleCI, nullptr, &shaderModule);
|
||||
|
||||
auto vsCode = loadFile("shaders/triangle.vert.spv");
|
||||
auto fsCode = loadFile("shaders/triangle.frag.spv");
|
||||
|
||||
@@ -220,32 +258,22 @@ struct Renderer {
|
||||
|
||||
smci.codeSize = vsCode.size();
|
||||
smci.pCode = reinterpret_cast<uint32_t*>(vsCode.data());
|
||||
VkShaderModule vs;
|
||||
vkCreateShaderModule(device, &smci, nullptr, &vs);
|
||||
// VkShaderModule vs;
|
||||
// vkCreateShaderModule(device, &smci, nullptr, &vs);
|
||||
|
||||
smci.codeSize = fsCode.size();
|
||||
smci.pCode = reinterpret_cast<uint32_t*>(fsCode.data());
|
||||
VkShaderModule fs;
|
||||
vkCreateShaderModule(device, &smci, nullptr, &fs);
|
||||
// VkShaderModule fs;
|
||||
// vkCreateShaderModule(device, &smci, nullptr, &fs);
|
||||
|
||||
// --- Shaders ---
|
||||
VkPipelineShaderStageCreateInfo stages[2] = {
|
||||
{
|
||||
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||
nullptr,
|
||||
0,
|
||||
VK_SHADER_STAGE_VERTEX_BIT,
|
||||
vs,
|
||||
"main"
|
||||
},
|
||||
{
|
||||
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||
nullptr,
|
||||
0,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
fs,
|
||||
"main"
|
||||
}
|
||||
std::vector<VkPipelineShaderStageCreateInfo> shaderStages{
|
||||
{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||
.stage = VK_SHADER_STAGE_VERTEX_BIT,
|
||||
.module = shaderModule, .pName = "main"},
|
||||
{ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
|
||||
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
.module = shaderModule, .pName = "main" }
|
||||
};
|
||||
|
||||
// --- Vertex Input (Generic) ---
|
||||
@@ -296,7 +324,7 @@ struct Renderer {
|
||||
0,
|
||||
VK_POLYGON_MODE_FILL,
|
||||
VK_CULL_MODE_NONE,
|
||||
VK_FRONT_FACE_CLOCKWISE,
|
||||
VK_FRONT_FACE_COUNTER_CLOCKWISE,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
@@ -341,8 +369,8 @@ struct Renderer {
|
||||
VkGraphicsPipelineCreateInfo gpci{
|
||||
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
|
||||
.pNext = &rci,
|
||||
.stageCount = 2,
|
||||
.pStages = stages,
|
||||
.stageCount = (uint32_t) shaderStages.size(),
|
||||
.pStages = shaderStages.data(),
|
||||
.pVertexInputState = &vi,
|
||||
.pInputAssemblyState = &ia,
|
||||
.pViewportState = &vp,
|
||||
|
||||
Reference in new issue
Block a user