shader
This commit is contained in:
parent
e08d5f104a
commit
9f62233a07
20 changed files with 298 additions and 322 deletions
|
|
@ -1,23 +0,0 @@
|
|||
# HelloWindow Example
|
||||
|
||||
## Description
|
||||
|
||||
This example showcases how to use the camera to get a rotating 3D cube.
|
||||
|
||||
## Expected Result
|
||||
|
||||
A blue tinted vulkan window with a white rotating 3D cube.
|
||||
|
||||
## Highlighted Code Snippet
|
||||
|
||||
```cpp
|
||||
Camera camera(ToRadian(90), 1280.0f / 720.0f, 0.01, 512);
|
||||
camera.view = MatrixRowMajor<float, 4, 4, 1>::Translation(0, 0, -5);
|
||||
camera.Update();
|
||||
```
|
||||
|
||||
## How to Run
|
||||
|
||||
```bash
|
||||
crafter-build build executable -r
|
||||
```
|
||||
|
|
@ -1,86 +0,0 @@
|
|||
#include <vulkan/vulkan.h>
|
||||
|
||||
import Crafter.Math;
|
||||
import Crafter.Event;
|
||||
import Crafter.Graphics;
|
||||
using namespace Crafter;
|
||||
|
||||
typedef VulkanShader<"MeshShaderXYZ.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 3, {{{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}}}> MeshShaderSpirv;
|
||||
typedef VulkanShader<"FragmentShaderSolidWhite.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 0, {}> FragmentShader;
|
||||
typedef VulkanPipeline<MeshShaderSpirv, FragmentShader> Pipeline;
|
||||
|
||||
int main() {
|
||||
VulkanDevice::CreateDevice();
|
||||
MeshShaderSpirv::CreateShader();
|
||||
FragmentShader::CreateShader();
|
||||
Pipeline::CreatePipeline();
|
||||
|
||||
WindowWaylandVulkan window("HelloWindow", 1280, 720);
|
||||
|
||||
/*
|
||||
Load the verticies and indicies for our cube.
|
||||
*/
|
||||
Mesh<Vertex> cube(8, 12);
|
||||
Vertex verticies[] {
|
||||
{-0.5f, -0.5f, -0.5f, 1.0f},
|
||||
{-0.5f, -0.5f, 0.5f, 1.0f},
|
||||
{-0.5f, 0.5f, -0.5f, 1.0f},
|
||||
{-0.5f, 0.5f, 0.5f, 1.0f},
|
||||
{0.5f, -0.5f, -0.5f, 1.0f},
|
||||
{0.5f, -0.5f, 0.5f, 1.0f},
|
||||
{0.5f, 0.5f, -0.5f, 1.0f},
|
||||
{0.5f, 0.5f, 0.5f, 1.0f}
|
||||
};
|
||||
std::memcpy(cube.verticies.value, &verticies, sizeof(verticies));
|
||||
uint32_t indicies[]
|
||||
{
|
||||
0, 2, 1,
|
||||
1, 2, 3,
|
||||
4, 5, 6,
|
||||
5, 7, 6,
|
||||
0, 1, 5,
|
||||
0, 5, 4,
|
||||
2, 6, 7,
|
||||
2, 7, 3,
|
||||
0, 4, 6,
|
||||
0, 6, 2,
|
||||
1, 3, 7,
|
||||
1, 7, 5
|
||||
};
|
||||
std::memcpy(cube.indicies.value, &indicies, sizeof(indicies));
|
||||
|
||||
/*
|
||||
Defines a perspective camera, with an FOV of 90, an aspect ratio of 16/9 a near clip of 0.01 and a far clip of 512.
|
||||
*/
|
||||
Camera camera(ToRadian(90), 1280.0f / 720.0f, 0.01, 512);
|
||||
camera.view = MatrixRowMajor<float, 4, 4, 1>::Translation(0, 0, -5);
|
||||
/*
|
||||
Calculates the viewprojection, must be called each time after updating the camera.
|
||||
*/
|
||||
camera.Update();
|
||||
|
||||
MeshShader<Vertex> meshShader(&cube, &camera);
|
||||
DescriptorSet<MeshShaderSpirv, FragmentShader> descriptors;
|
||||
meshShader.WriteDescriptors(descriptors.set[0]);
|
||||
|
||||
/*
|
||||
Calculates the modelviewprojection matrix.
|
||||
*/
|
||||
meshShader.Update();
|
||||
|
||||
float counter = 0;
|
||||
EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors, &meshShader, &counter](VkCommandBuffer cmd){
|
||||
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors.set[0], 0, NULL);
|
||||
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);
|
||||
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
|
||||
|
||||
meshShader.transform = MatrixRowMajor<float, 4, 4, 1>::Rotation(counter, counter, 0);
|
||||
/*
|
||||
We edited the transform so must recalculate the model view projection matrix.
|
||||
*/
|
||||
meshShader.Update();
|
||||
counter+=0.01;
|
||||
});
|
||||
|
||||
window.StartSync();
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
{
|
||||
"name": "crafter-graphics",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "example",
|
||||
"standard": "c++26",
|
||||
"source_files": ["main"],
|
||||
"module_files": [],
|
||||
"build_dir": "build",
|
||||
"output_dir": "bin",
|
||||
"type":"executable",
|
||||
"libs": [],
|
||||
"flags": ["-Wno-uninitialized"],
|
||||
"dependencies": [
|
||||
{
|
||||
"path":"../../project.json",
|
||||
"configuration":"lib-debug"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
Crafter®.Graphics
|
||||
Copyright (C) 2025 Catcrafts®
|
||||
Catcrafts.net
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3.0 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#version 450
|
||||
#extension GL_EXT_mesh_shader : require
|
||||
|
||||
layout (binding = 0) buffer COLORS
|
||||
{
|
||||
vec4 colors[];
|
||||
} colors;
|
||||
|
||||
layout (location = 0) out PerVertexData
|
||||
{
|
||||
vec4 color;
|
||||
} outVert[];
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
layout(triangles, max_vertices = 3, max_primitives = 1) out;
|
||||
void main()
|
||||
{
|
||||
SetMeshOutputsEXT(3, 1);
|
||||
|
||||
gl_MeshVerticesEXT[0].gl_Position = vec4(0, -0.3, 0, 1);
|
||||
gl_MeshVerticesEXT[1].gl_Position = vec4(0.3, 0.3, 0, 1);
|
||||
gl_MeshVerticesEXT[2].gl_Position = vec4(-0.3, 0.3, 0, 1);
|
||||
|
||||
outVert[0].color = colors.colors[0];
|
||||
outVert[1].color = colors.colors[1];
|
||||
outVert[2].color = colors.colors[2];
|
||||
|
||||
gl_PrimitiveTriangleIndicesEXT[0] = uvec3(0, 1, 2);
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
# HelloWindow Example
|
||||
|
||||
## Description
|
||||
|
||||
This example showcases how to define a custom shader.
|
||||
## Expected Result
|
||||
|
||||
A RGB triangle.
|
||||
|
||||
## Highlighted Code Snippet
|
||||
|
||||
```cpp
|
||||
class CustomShader {
|
||||
public:
|
||||
Buffer<Vector<float, 4>> colors;
|
||||
CustomShader(const std::vector<Vector<float, 4>>& colors) : colors(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, colors.size()) {
|
||||
memcpy(this->colors.value, colors.data(), colors.size()*sizeof(Vector<float, 4>));
|
||||
}
|
||||
void WriteDescriptors(VkDescriptorSet set) {
|
||||
VkWriteDescriptorSet write[1] = {
|
||||
vks::initializers::writeDescriptorSet(set, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, &colors.descriptor),
|
||||
};
|
||||
vkUpdateDescriptorSets(VulkanDevice::device, 1, &write[0], 0, nullptr);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## How to Run
|
||||
|
||||
```bash
|
||||
crafter-build build executable -r
|
||||
```
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
|
||||
#include "../../lib/VulkanInitializers.hpp"
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
//required for the camera matrix.
|
||||
import Crafter.Math;
|
||||
import Crafter.Event;
|
||||
import std;
|
||||
import Crafter.Graphics;
|
||||
using namespace Crafter;
|
||||
|
||||
typedef VulkanShader<"CustomShader.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 1, {{{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0}}}> CustomShaderSpirv;
|
||||
typedef VulkanShader<"FragmentShaderVertexColor.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 0, {}> FragmentShader;
|
||||
typedef VulkanPipeline<CustomShaderSpirv, FragmentShader> Pipeline;
|
||||
|
||||
|
||||
class CustomShader {
|
||||
public:
|
||||
/*
|
||||
Define a buffer holding our color vectors.
|
||||
*/
|
||||
Buffer<Vector<float, 4>> colors;
|
||||
/*
|
||||
Initialze our buffer as shader visible storage buffer with the size of the colors vector.
|
||||
*/
|
||||
CustomShader(const std::vector<Vector<float, 4>>& colors) : colors(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, colors.size()) {
|
||||
std::memcpy(this->colors.value, colors.data(), colors.size()*sizeof(Vector<float, 4>));
|
||||
}
|
||||
void WriteDescriptors(VkDescriptorSet set) {
|
||||
/*
|
||||
Write the color buffer descriptor to the set.
|
||||
*/
|
||||
VkWriteDescriptorSet write[1] = {
|
||||
vks::initializers::writeDescriptorSet(set, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 0, &colors.descriptor),
|
||||
};
|
||||
vkUpdateDescriptorSets(VulkanDevice::device, 1, &write[0], 0, nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
VulkanDevice::CreateDevice();
|
||||
CustomShaderSpirv::CreateShader();
|
||||
FragmentShader::CreateShader();
|
||||
Pipeline::CreatePipeline();
|
||||
|
||||
WindowWaylandVulkan window("HelloWindow", 1280, 720);
|
||||
|
||||
DescriptorSet<CustomShaderSpirv, FragmentShader> descriptors;
|
||||
CustomShader customShader({{1,0,0,1}, {0,1,0,1}, {0,0,1,1}});
|
||||
customShader.WriteDescriptors(descriptors.set[0]);
|
||||
|
||||
EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors](VkCommandBuffer cmd){
|
||||
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors.set[0], 0, NULL);
|
||||
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);
|
||||
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, 1, 1, 1);
|
||||
});
|
||||
|
||||
window.StartSync();
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
{
|
||||
"name": "crafter-graphics",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "example",
|
||||
"standard": "c++26",
|
||||
"source_files": ["main"],
|
||||
"module_files": [],
|
||||
"build_dir": "build",
|
||||
"output_dir": "bin",
|
||||
"type":"executable",
|
||||
"libs": [],
|
||||
"flags": ["-Wno-uninitialized"],
|
||||
"dependencies": [
|
||||
{
|
||||
"path":"../../project.json",
|
||||
"configuration":"lib-debug"
|
||||
}
|
||||
],
|
||||
"shaders": [
|
||||
{
|
||||
"path":"CustomShader.glsl",
|
||||
"type":13,
|
||||
"entrypoint":"main"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
0
examples/VulkanTriangle/closesthit.glsl
Normal file
0
examples/VulkanTriangle/closesthit.glsl
Normal file
|
|
@ -4,13 +4,18 @@ import Crafter.Graphics;
|
|||
using namespace Crafter;
|
||||
import std;
|
||||
|
||||
typedef VulkanShader<"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR, 2, {{{VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1}}}> Raygenspv;
|
||||
|
||||
int main() {
|
||||
|
||||
/*
|
||||
This sets up all necessary things and creates the vulkan device.
|
||||
This must be called before any vulkan related things.
|
||||
Things like VkDevice are static members of the VulkanDevice class.
|
||||
*/
|
||||
VulkanDevice::CreateDevice();
|
||||
Raygenspv::CreateShader();
|
||||
|
||||
WindowVulkan window(1280, 720, "HelloVulkan");
|
||||
|
||||
/*
|
||||
|
|
|
|||
0
examples/VulkanTriangle/miss.glsl
Normal file
0
examples/VulkanTriangle/miss.glsl
Normal file
|
|
@ -9,6 +9,13 @@
|
|||
"path":"../../project.json",
|
||||
"configuration":"lib-vulkan"
|
||||
}
|
||||
],
|
||||
"shaders": [
|
||||
{
|
||||
"path":"raygen.glsl",
|
||||
"type":6,
|
||||
"entrypoint":"main"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
|
|||
12
examples/VulkanTriangle/raygen.glsl
Normal file
12
examples/VulkanTriangle/raygen.glsl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
#extension GL_EXT_shader_image_load_formatted : enable
|
||||
|
||||
layout(binding = 0, set = 0) uniform accelerationStructureEXT topLevelAS;
|
||||
layout(binding = 1, set = 0) uniform image2D image;
|
||||
|
||||
layout(location = 0) rayPayloadEXT vec3 hitValue;
|
||||
|
||||
void main() {
|
||||
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(1,1,1, 0.0));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue