working heightmap shader?
This commit is contained in:
parent
43e1fda736
commit
fe15d3e8ca
15 changed files with 331 additions and 76 deletions
|
|
@ -29,7 +29,7 @@ import Crafter.Math;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
|
|
||||||
namespace Crafter {
|
namespace Crafter {
|
||||||
export class Camera : public Component {
|
export class Camera {
|
||||||
public:
|
public:
|
||||||
MatrixRowMajor<float, 4, 4, 1> projection;
|
MatrixRowMajor<float, 4, 4, 1> projection;
|
||||||
MatrixRowMajor<float, 4, 4, 1> view;
|
MatrixRowMajor<float, 4, 4, 1> view;
|
||||||
|
|
|
||||||
75
Crafter.Graphics-HeightmapShader.cppm
Normal file
75
Crafter.Graphics-HeightmapShader.cppm
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
/*
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vulkan/vulkan.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cmath>
|
||||||
|
#include "VulkanInitializers.hpp"
|
||||||
|
|
||||||
|
export module Crafter.Graphics:HeightmapShader;
|
||||||
|
import :Camera;
|
||||||
|
import :VulkanPipeline;
|
||||||
|
import :DescriptorSet;
|
||||||
|
import Crafter.Math;
|
||||||
|
|
||||||
|
namespace Crafter {
|
||||||
|
struct HeightMapData {
|
||||||
|
MatrixRowMajor<float, 4, 4, 1> mvp;
|
||||||
|
uint32_t sizeX;
|
||||||
|
uint32_t sizeZ;
|
||||||
|
float spacing;
|
||||||
|
uint32_t padding[13];
|
||||||
|
};
|
||||||
|
|
||||||
|
export template <typename VertexType>
|
||||||
|
class HeightmapShader {
|
||||||
|
public:
|
||||||
|
MatrixRowMajor<float, 4, 4, 1> transform;
|
||||||
|
Camera* camera;
|
||||||
|
Buffer<HeightMapData> data;
|
||||||
|
Buffer<VertexType> heights;
|
||||||
|
std::uint32_t threadCount;
|
||||||
|
EventListener<void> cameraUpdate;
|
||||||
|
HeightmapShader(uint32_t sizeX, uint32_t sizeZ, float spacing, Camera* camera) : threadCount(sizeX * sizeZ), heights(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, sizeX * sizeZ * 4 * 64), data(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), camera(camera), cameraUpdate(
|
||||||
|
&camera->onUpdate, [this](){
|
||||||
|
Update();
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
data.value->sizeX = sizeX*2;
|
||||||
|
data.value->sizeZ = sizeZ*2;
|
||||||
|
data.value->spacing = spacing;
|
||||||
|
transform = MatrixRowMajor<float, 4, 4, 1>::Identity();
|
||||||
|
}
|
||||||
|
void WriteDescriptors(VkDescriptorSet set) {
|
||||||
|
VkWriteDescriptorSet write[2] = {
|
||||||
|
vks::initializers::writeDescriptorSet(set, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &data.descriptor),
|
||||||
|
vks::initializers::writeDescriptorSet(set, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, &heights.descriptor)
|
||||||
|
};
|
||||||
|
vkUpdateDescriptorSets(VulkanDevice::device, 2, &write[0], 0, nullptr);
|
||||||
|
}
|
||||||
|
void Update() {
|
||||||
|
data.value->mvp = camera->projectionView*transform;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -26,7 +26,6 @@ module;
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
export module Crafter.Graphics:Mesh;
|
export module Crafter.Graphics:Mesh;
|
||||||
import Crafter.Component;
|
|
||||||
import Crafter.Math;
|
import Crafter.Math;
|
||||||
import :VulkanBuffer;
|
import :VulkanBuffer;
|
||||||
import :Types;
|
import :Types;
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ module;
|
||||||
#include "VulkanInitializers.hpp"
|
#include "VulkanInitializers.hpp"
|
||||||
|
|
||||||
export module Crafter.Graphics:MeshShader;
|
export module Crafter.Graphics:MeshShader;
|
||||||
import Crafter.Component;
|
|
||||||
import :Mesh;
|
import :Mesh;
|
||||||
import :Camera;
|
import :Camera;
|
||||||
import :VulkanPipeline;
|
import :VulkanPipeline;
|
||||||
|
|
|
||||||
|
|
@ -60,4 +60,27 @@ namespace Crafter {
|
||||||
|
|
||||||
float pad[2];
|
float pad[2];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export struct __attribute__((packed)) VertexRGBA {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
float w;
|
||||||
|
|
||||||
|
float r;
|
||||||
|
float g;
|
||||||
|
float b;
|
||||||
|
float a;
|
||||||
|
};
|
||||||
|
|
||||||
|
export struct __attribute__((packed)) HeightRGBA {
|
||||||
|
float height;
|
||||||
|
|
||||||
|
float pad[3];
|
||||||
|
|
||||||
|
float r;
|
||||||
|
float g;
|
||||||
|
float b;
|
||||||
|
float a;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,14 +29,6 @@ UiElement::UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, st
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UiElement::UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t absoluteWidth, std::uint32_t absoluteHeight, UiElement* parent, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), bufferWidth(bufferWidth), bufferHeight(bufferHeight), absoluteWidth(absoluteWidth), absoluteHeight(absoluteHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), buffer(bufferWidth*bufferHeight), parent(parent), useRelativeSize(false), ignoreScaling(ignoreScaling) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
UiElement::UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), bufferWidth(bufferWidth), bufferHeight(bufferHeight), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), buffer(bufferWidth*bufferHeight), useRelativeSize(true), ignoreScaling(ignoreScaling) {
|
UiElement::UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), bufferWidth(bufferWidth), bufferHeight(bufferHeight), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), buffer(bufferWidth*bufferHeight), useRelativeSize(true), ignoreScaling(ignoreScaling) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UiElement::UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, UiElement* parent, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), bufferWidth(bufferWidth), bufferHeight(bufferHeight), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), buffer(bufferWidth*bufferHeight), parent(parent), useRelativeSize(true), ignoreScaling(ignoreScaling) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -26,10 +26,9 @@ module;
|
||||||
export module Crafter.Graphics:UiElement;
|
export module Crafter.Graphics:UiElement;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
import :Types;
|
import :Types;
|
||||||
import Crafter.Component;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
class UiElement : public Component {
|
class UiElement {
|
||||||
public:
|
public:
|
||||||
Event<MouseMoveEvent> onMouseMove;
|
Event<MouseMoveEvent> onMouseMove;
|
||||||
Event<MouseMoveEvent> onMouseEnter;
|
Event<MouseMoveEvent> onMouseEnter;
|
||||||
|
|
@ -54,12 +53,8 @@ export namespace Crafter {
|
||||||
float anchorOffsetX;
|
float anchorOffsetX;
|
||||||
float anchorOffsetY;
|
float anchorOffsetY;
|
||||||
std::vector<Pixel_RU8_GU8_BU8_AU8> buffer;
|
std::vector<Pixel_RU8_GU8_BU8_AU8> buffer;
|
||||||
ComponentRef<UiElement> parent;
|
std::vector<UiElement> children;
|
||||||
ComponentRefVectorOwning<UiElement> children;
|
|
||||||
UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t absoluteWidth, std::uint32_t absoluteHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
|
UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t absoluteWidth, std::uint32_t absoluteHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
|
||||||
UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t absoluteWidth, std::uint32_t absoluteHeight, UiElement* parent, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
|
|
||||||
UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
|
UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
|
||||||
UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, UiElement* parent, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false);
|
|
||||||
virtual ~UiElement() {};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -22,12 +22,12 @@ module;
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
export module Crafter.Graphics:Window;
|
export module Crafter.Graphics:Window;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
import :UiElement;
|
import :UiElement;
|
||||||
import :Types;
|
import :Types;
|
||||||
import Crafter.Component;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
class Window {
|
class Window {
|
||||||
|
|
@ -52,7 +52,7 @@ export namespace Crafter {
|
||||||
Event<void> onKeyDown[255];
|
Event<void> onKeyDown[255];
|
||||||
Event<void> onKeyHold[255];
|
Event<void> onKeyHold[255];
|
||||||
Event<void> onKeyUp[255];
|
Event<void> onKeyUp[255];
|
||||||
ComponentRefVectorOwning<UiElement> elements;
|
std::vector<UiElement> elements;
|
||||||
std::string name;
|
std::string name;
|
||||||
std::uint32_t width;
|
std::uint32_t width;
|
||||||
std::uint32_t height;
|
std::uint32_t height;
|
||||||
|
|
|
||||||
|
|
@ -80,32 +80,32 @@ void WindowWaylandWayland::Start() {
|
||||||
thread = std::thread([this](){
|
thread = std::thread([this](){
|
||||||
while (open && wl_display_dispatch(display) != -1) {
|
while (open && wl_display_dispatch(display) != -1) {
|
||||||
wl_surface_attach(surface, buffer, 0, 0);
|
wl_surface_attach(surface, buffer, 0, 0);
|
||||||
for(UiElement* element : elements.components) {
|
for(const UiElement& element : elements) {
|
||||||
std::int32_t realX;
|
std::int32_t realX;
|
||||||
std::int32_t realY;
|
std::int32_t realY;
|
||||||
std::int32_t elementWidth;
|
std::int32_t elementWidth;
|
||||||
std::int32_t elementHeight;
|
std::int32_t elementHeight;
|
||||||
if(element->ignoreScaling) {
|
if(element.ignoreScaling) {
|
||||||
if(element->useRelativeSize) {
|
if(element.useRelativeSize) {
|
||||||
elementWidth = element->relativeWidth*width;
|
elementWidth = element.relativeWidth*width;
|
||||||
elementHeight = element->relativeHeight*height;
|
elementHeight = element.relativeHeight*height;
|
||||||
} else {
|
} else {
|
||||||
elementWidth = element->absoluteWidth;
|
elementWidth = element.absoluteWidth;
|
||||||
elementHeight = element->absoluteHeight;
|
elementHeight = element.absoluteHeight;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if(element->useRelativeSize) {
|
if(element.useRelativeSize) {
|
||||||
elementWidth = element->relativeWidth*width*scale;
|
elementWidth = element.relativeWidth*width*scale;
|
||||||
elementHeight = element->relativeHeight*height*scale;
|
elementHeight = element.relativeHeight*height*scale;
|
||||||
} else {
|
} else {
|
||||||
elementWidth = element->absoluteWidth*scale;
|
elementWidth = element.absoluteWidth*scale;
|
||||||
elementHeight = element->absoluteHeight*scale;
|
elementHeight = element.absoluteHeight*scale;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
realX = (element->anchorX*width)-(element->anchorOffsetX*elementWidth);
|
realX = (element.anchorX*width)-(element.anchorOffsetX*elementWidth);
|
||||||
realY = (element->anchorY*height)-(element->anchorOffsetY*elementHeight);
|
realY = (element.anchorY*height)-(element.anchorOffsetY*elementHeight);
|
||||||
std::vector<Pixel_RU8_GU8_BU8_AU8> scaled(elementWidth*elementHeight);
|
std::vector<Pixel_RU8_GU8_BU8_AU8> scaled(elementWidth*elementHeight);
|
||||||
ScaleBitmapR8G8B8(scaled.data(), element->buffer.data(), element->bufferWidth, element->bufferHeight, elementWidth, elementHeight);
|
ScaleBitmapR8G8B8(scaled.data(), element.buffer.data(), element.bufferWidth, element.bufferHeight, elementWidth, elementHeight);
|
||||||
for(std::int32_t x = realX; x-realX < elementWidth; x++) {
|
for(std::int32_t x = realX; x-realX < elementWidth; x++) {
|
||||||
for(std::int32_t y = realY; y-realY < elementHeight; y++) {
|
for(std::int32_t y = realY; y-realY < elementHeight; y++) {
|
||||||
if(x > 0 && x < width && y > 0 && y < height) {
|
if(x > 0 && x < width && y > 0 && y < height) {
|
||||||
|
|
|
||||||
|
|
@ -36,3 +36,4 @@ export import :MeshShader;
|
||||||
export import :VulkanTexture;
|
export import :VulkanTexture;
|
||||||
export import :TextureShader;
|
export import :TextureShader;
|
||||||
export import :DescriptorSet;
|
export import :DescriptorSet;
|
||||||
|
export import :HeightmapShader;
|
||||||
29
FragmentShaderVertexColor.glsl
Normal file
29
FragmentShaderVertexColor.glsl
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
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
|
||||||
|
|
||||||
|
layout(location = 0) in vec4 inColor;
|
||||||
|
layout(location = 0) out vec4 outColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
outColor = inColor;
|
||||||
|
}
|
||||||
83
MeshShaderHeightmapRGBA.glsl
Normal file
83
MeshShaderHeightmapRGBA.glsl
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
/*
|
||||||
|
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) uniform UBO
|
||||||
|
{
|
||||||
|
mat4 modelProjectionView;
|
||||||
|
uint sizeX;
|
||||||
|
uint sizeZ;
|
||||||
|
float spacing;
|
||||||
|
uint padding[13];
|
||||||
|
} ubo;
|
||||||
|
|
||||||
|
struct VertexType
|
||||||
|
{
|
||||||
|
float height;
|
||||||
|
float padding[3];
|
||||||
|
vec4 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
layout (binding = 1) buffer VERTEX
|
||||||
|
{
|
||||||
|
VertexType pos[];
|
||||||
|
} vertex;
|
||||||
|
|
||||||
|
|
||||||
|
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
||||||
|
layout(triangles, max_vertices = 256, max_primitives = 128) out;
|
||||||
|
|
||||||
|
layout (location = 0) out PerVertexData
|
||||||
|
{
|
||||||
|
vec4 color;
|
||||||
|
} outVert[];
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
SetMeshOutputsEXT(256, 128);
|
||||||
|
|
||||||
|
uint linearID = gl_LocalInvocationID.x;
|
||||||
|
uint quadX = linearID % ubo.sizeX;
|
||||||
|
uint quadZ = linearID / ubo.sizeX;
|
||||||
|
|
||||||
|
VertexType vertex1 = vertex.pos[quadZ * ubo.sizeX + quadX];
|
||||||
|
VertexType vertex2 = vertex.pos[quadZ * ubo.sizeX + quadX + 1];
|
||||||
|
VertexType vertex3 = vertex.pos[(quadZ+1) * ubo.sizeX + quadX];
|
||||||
|
VertexType vertex4 = vertex.pos[(quadZ+1) * ubo.sizeX + quadX + 1];
|
||||||
|
|
||||||
|
uint vertexID = gl_LocalInvocationID.x*4;
|
||||||
|
|
||||||
|
gl_MeshVerticesEXT[vertexID].gl_Position = ubo.modelProjectionView * vec4(ubo.spacing*quadX, 0, ubo.spacing*quadZ, 1); // Top-left
|
||||||
|
gl_MeshVerticesEXT[vertexID + 1].gl_Position = ubo.modelProjectionView * vec4(ubo.spacing*quadX+ubo.spacing, 0, ubo.spacing*quadZ, 1); // Top-right
|
||||||
|
gl_MeshVerticesEXT[vertexID + 2].gl_Position = ubo.modelProjectionView * vec4(ubo.spacing*quadX, 0, ubo.spacing*quadZ+ubo.spacing, 1); // Bottom-left
|
||||||
|
gl_MeshVerticesEXT[vertexID + 3].gl_Position = ubo.modelProjectionView * vec4(ubo.spacing*quadX+ubo.spacing, 0, ubo.spacing*quadZ+ubo.spacing, 1); // Bottom-right
|
||||||
|
|
||||||
|
outVert[vertexID].color = vertex1.color;
|
||||||
|
outVert[vertexID + 1].color = vertex2.color;
|
||||||
|
outVert[vertexID+ 2].color = vertex3.color;
|
||||||
|
outVert[vertexID + 3].color = vertex4.color;
|
||||||
|
|
||||||
|
uint triangleID = gl_LocalInvocationID.x*2;
|
||||||
|
|
||||||
|
gl_PrimitiveTriangleIndicesEXT[triangleID] = uvec3(vertexID, vertexID+1, vertexID+2);
|
||||||
|
gl_PrimitiveTriangleIndicesEXT[triangleID + 1] = uvec3(vertexID+2, vertexID+1, vertexID+3);
|
||||||
|
}
|
||||||
65
MeshShaderXYZRGBA.glsl
Normal file
65
MeshShaderXYZRGBA.glsl
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
/*
|
||||||
|
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) uniform UBO
|
||||||
|
{
|
||||||
|
mat4 modelProjectionView;
|
||||||
|
} ubo;
|
||||||
|
|
||||||
|
struct VertexType
|
||||||
|
{
|
||||||
|
vec4 position;
|
||||||
|
vec4 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
layout (std140, binding = 1) buffer VERTEX
|
||||||
|
{
|
||||||
|
VertexType pos[];
|
||||||
|
} vertex;
|
||||||
|
|
||||||
|
layout (binding = 2) buffer INDEX {
|
||||||
|
uint index[];
|
||||||
|
} index;
|
||||||
|
|
||||||
|
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
||||||
|
layout(triangles, max_vertices = 192, max_primitives = 64) out;
|
||||||
|
|
||||||
|
layout (location = 0) out PerVertexData
|
||||||
|
{
|
||||||
|
vec4 color;
|
||||||
|
} outVert[];
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
SetMeshOutputsEXT(192, 64);
|
||||||
|
uint triangleID = ((gl_WorkGroupID.x * gl_WorkGroupSize.x) + gl_LocalInvocationIndex.x)*3;
|
||||||
|
uint localID = gl_LocalInvocationIndex.x*3;
|
||||||
|
gl_MeshVerticesEXT[localID].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID]].position;
|
||||||
|
gl_MeshVerticesEXT[localID+1].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID+1]].position;
|
||||||
|
gl_MeshVerticesEXT[localID+2].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID+2]].position;
|
||||||
|
|
||||||
|
outVert[localID].color = vertex.pos[index.index[triangleID]].color;
|
||||||
|
outVert[localID + 1].color = vertex.pos[index.index[triangleID+1]].color;
|
||||||
|
outVert[localID + 2].color = vertex.pos[index.index[triangleID+2]].color;
|
||||||
|
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex.x] = uvec3(localID, localID+1, localID+2);
|
||||||
|
}
|
||||||
47
main.cpp
47
main.cpp
|
|
@ -31,8 +31,8 @@ import Crafter.Event;
|
||||||
import Crafter.Math;
|
import Crafter.Math;
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
typedef VulkanShader<"MeshShaderXYZUV.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}}}> MeshVulkanShader;
|
typedef VulkanShader<"MeshShaderHeightmapRGBA.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 2, {{{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}}}> MeshVulkanShader;
|
||||||
typedef VulkanShader<"FragmentShaderTexture.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 1, {{{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0}}}> FragmentShader;
|
typedef VulkanShader<"FragmentShaderVertexColor.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 0, {}> FragmentShader;
|
||||||
typedef VulkanPipeline<MeshVulkanShader, FragmentShader> Pipeline;
|
typedef VulkanPipeline<MeshVulkanShader, FragmentShader> Pipeline;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
@ -43,44 +43,31 @@ int main() {
|
||||||
|
|
||||||
WindowWaylandVulkan window("Crafter.Graphics", 1280, 720);
|
WindowWaylandVulkan window("Crafter.Graphics", 1280, 720);
|
||||||
|
|
||||||
AssetLoad asset("cannon.cras");
|
Camera camera(1.57079633, 1280.0f / 720.0f, 0.01, 512);
|
||||||
|
camera.view = MatrixRowMajor<float, 4, 4, 1>::Translation(0, 2, -10);
|
||||||
Camera camera(1.57079633, 16 / 9, 0.01, 512);
|
|
||||||
camera.Update();
|
camera.Update();
|
||||||
VkCommandBuffer cmd = window.StartInit();
|
VkCommandBuffer cmd = window.StartInit();
|
||||||
std::vector<char> meshVector = asset.Load(0);
|
|
||||||
Mesh<VertexUV> mesh(meshVector.data());
|
|
||||||
MeshShader<VertexUV> meshShader(&mesh, &camera);
|
|
||||||
AssetLoad asset2("texture.cras");
|
|
||||||
std::vector<char> texvector = asset2.Load(0);
|
|
||||||
VulkanTexture<Pixel_RU8_GU8_BU8_AU8> txt = VulkanTexture<Pixel_RU8_GU8_BU8_AU8>(texvector.data(), cmd);
|
|
||||||
TextureShader texShader(&txt);
|
|
||||||
DescriptorSet<MeshVulkanShader, FragmentShader> descriptors;
|
|
||||||
EventListener<void> bruhlistener(&descriptors.onDescriptorRefresh, [&meshShader, &texShader, &camera, &descriptors](){
|
|
||||||
meshShader.WriteDescriptors(descriptors.set[0]);
|
|
||||||
texShader.WriteDescriptors(descriptors.set[1]);
|
|
||||||
});
|
|
||||||
meshShader.WriteDescriptors(descriptors.set[0]);
|
|
||||||
texShader.WriteDescriptors(descriptors.set[1]);
|
|
||||||
MeshShader<VertexUV> meshShader2(&mesh, &camera);
|
|
||||||
TextureShader texShader2(&txt);
|
|
||||||
DescriptorSet<MeshVulkanShader, FragmentShader> descriptors2;
|
|
||||||
meshShader2.WriteDescriptors(descriptors2.set[0]);
|
|
||||||
texShader2.WriteDescriptors(descriptors2.set[1]);
|
|
||||||
|
|
||||||
|
|
||||||
|
DescriptorSet<MeshVulkanShader, FragmentShader> descriptors;
|
||||||
|
HeightmapShader<HeightRGBA> meshShader(4, 4, 1, &camera);
|
||||||
|
for(uint32_t i = 0; i < 1*1*4*64; i++) {
|
||||||
|
meshShader.heights.value[i].height = -0.5;
|
||||||
|
meshShader.heights.value[i].r = 255;
|
||||||
|
meshShader.heights.value[i].g = 255;
|
||||||
|
meshShader.heights.value[i].b = 255;
|
||||||
|
meshShader.heights.value[i].a = 255;
|
||||||
|
}
|
||||||
|
|
||||||
|
meshShader.WriteDescriptors(descriptors.set[0]);
|
||||||
meshShader.Update();
|
meshShader.Update();
|
||||||
meshShader2.Update();
|
|
||||||
|
|
||||||
window.FinishInit();
|
window.FinishInit();
|
||||||
|
|
||||||
EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors, &meshShader, &descriptors2, &meshShader2](VkCommandBuffer cmd){
|
EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors, &meshShader](VkCommandBuffer cmd){
|
||||||
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors.set[0], 0, NULL);
|
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);
|
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);
|
||||||
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
|
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
|
||||||
|
|
||||||
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipelineLayout, 0, 2, &descriptors2.set[0], 0, NULL);
|
|
||||||
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, Pipeline::pipeline);
|
|
||||||
VulkanDevice::vkCmdDrawMeshTasksEXTProc(cmd, meshShader2.threadCount, 1, 1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
window.Start();
|
window.Start();
|
||||||
|
|
|
||||||
25
project.json
25
project.json
|
|
@ -6,7 +6,7 @@
|
||||||
"standard": "c++26",
|
"standard": "c++26",
|
||||||
"source_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics-UiElement", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-WindowWaylandVulkan", "VulkanBuffer", "VulkanTools", "Crafter.Graphics-Camera"],
|
"source_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics-UiElement", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-WindowWaylandVulkan", "VulkanBuffer", "VulkanTools", "Crafter.Graphics-Camera"],
|
||||||
"c_files": ["wayland-xdg-decoration-unstable-v1-client-protocol", "xdg-shell-protocol", "shm"],
|
"c_files": ["wayland-xdg-decoration-unstable-v1-client-protocol", "xdg-shell-protocol", "shm"],
|
||||||
"module_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics", "Crafter.Graphics-UiElement", "Crafter.Graphics-Types", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-VulkanPipeline", "Crafter.Graphics-VulkanShader", "Crafter.Graphics-WindowWaylandVulkan", "Crafter.Graphics-Camera", "Crafter.Graphics-VulkanBuffer", "Crafter.Graphics-Mesh", "Crafter.Graphics-MeshShader", "Crafter.Graphics-VulkanTexture", "Crafter.Graphics-TextureShader", "Crafter.Graphics-DescriptorSet"],
|
"module_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics", "Crafter.Graphics-UiElement", "Crafter.Graphics-Types", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-VulkanPipeline", "Crafter.Graphics-VulkanShader", "Crafter.Graphics-WindowWaylandVulkan", "Crafter.Graphics-Camera", "Crafter.Graphics-VulkanBuffer", "Crafter.Graphics-Mesh", "Crafter.Graphics-MeshShader", "Crafter.Graphics-HeightmapShader", "Crafter.Graphics-VulkanTexture", "Crafter.Graphics-TextureShader", "Crafter.Graphics-DescriptorSet"],
|
||||||
"build_dir": "build",
|
"build_dir": "build",
|
||||||
"output_dir": "bin",
|
"output_dir": "bin",
|
||||||
"type":"library",
|
"type":"library",
|
||||||
|
|
@ -23,6 +23,16 @@
|
||||||
"type":13,
|
"type":13,
|
||||||
"entrypoint":"main"
|
"entrypoint":"main"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"path":"MeshShaderXYZRGBA.glsl",
|
||||||
|
"type":13,
|
||||||
|
"entrypoint":"main"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path":"MeshShaderHeightmapRGBA.glsl",
|
||||||
|
"type":13,
|
||||||
|
"entrypoint":"main"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"path":"FragmentShaderSolidWhite.glsl",
|
"path":"FragmentShaderSolidWhite.glsl",
|
||||||
"type":4,
|
"type":4,
|
||||||
|
|
@ -32,6 +42,11 @@
|
||||||
"path":"FragmentShaderTexture.glsl",
|
"path":"FragmentShaderTexture.glsl",
|
||||||
"type":4,
|
"type":4,
|
||||||
"entrypoint":"main"
|
"entrypoint":"main"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path":"FragmentShaderVertexColor.glsl",
|
||||||
|
"type":4,
|
||||||
|
"entrypoint":"main"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -49,10 +64,6 @@
|
||||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Asset.git",
|
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Asset.git",
|
||||||
"configuration":"lib-debug"
|
"configuration":"lib-debug"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Component.git",
|
|
||||||
"configuration":"lib-debug"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Math.git",
|
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Math.git",
|
||||||
"configuration":"lib-debug"
|
"configuration":"lib-debug"
|
||||||
|
|
@ -72,10 +83,6 @@
|
||||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Asset.git",
|
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Asset.git",
|
||||||
"configuration":"lib-release"
|
"configuration":"lib-release"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Component.git",
|
|
||||||
"configuration":"lib-release"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Math.git",
|
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Math.git",
|
||||||
"configuration":"lib-release"
|
"configuration":"lib-release"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue