cleaned up renderer
This commit is contained in:
parent
afe23851f0
commit
d661c88ee2
58 changed files with 3030 additions and 4722 deletions
|
|
@ -2,18 +2,19 @@ import Crafter.Graphics;
|
||||||
import std;
|
import std;
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
|
constexpr std::uint32_t width = 1280;
|
||||||
|
constexpr std::uint32_t height = 720;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
constexpr std::uint_fast32_t width = 1280;
|
Device::Initialize();
|
||||||
constexpr std::uint_fast32_t height = 720;
|
Window window(width, height, "Hello Drawing!");
|
||||||
|
|
||||||
WindowWayland window(width, height, "Hello Drawing!");
|
for(std::uint32_t x = 0; x < width; x++) {
|
||||||
|
for(std::uint32_t y = 0; y < height; y++) {
|
||||||
for(std::uint_fast32_t x = 0; x < width; x++) {
|
window.renderer.buffer[x*height+y].r = 255;
|
||||||
for(std::uint_fast32_t y = 0; y < height; y++) {
|
window.renderer.buffer[x*height+y].g = 0;
|
||||||
window.framebuffer[x*height+y].r = 255;
|
window.renderer.buffer[x*height+y].b = 0;
|
||||||
window.framebuffer[x*height+y].g = 0;
|
window.renderer.buffer[x*height+y].a = 255;
|
||||||
window.framebuffer[x*height+y].b = 0;
|
|
||||||
window.framebuffer[x*height+y].a = 255;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,10 @@
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
{
|
{
|
||||||
"path":"../../project.json",
|
"path":"../../project.json",
|
||||||
"configuration":"lib-wayland"
|
"configuration":"lib-wayland-debug"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"debug": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,16 +4,17 @@ import std;
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
WindowWayland window(1280, 720, "Hello Input!");
|
Device::Initialize();
|
||||||
|
Window window(1280, 720, "Hello Drawing!");
|
||||||
|
|
||||||
RenderingElement<true, true, false> element(
|
RenderingElement2D<true, true, false> element(
|
||||||
{
|
{
|
||||||
FractionalToMapped<std::int32_t>(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor
|
0.5, //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor
|
||||||
FractionalToMapped<std::int32_t>(0.5), //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor
|
0.5, //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor
|
||||||
FractionalToMapped<std::int32_t>(0.5), //relativeSizeX: the relative x size this element should be scaled to compared to its parent
|
0.5, //relativeSizeX: the relative x size this element should be scaled to compared to its parent
|
||||||
FractionalToMapped<std::int32_t>(0.5), //relativeSizeY: the relative y size this element should be scaled to compared to its parent
|
0.5, //relativeSizeY: the relative y size this element should be scaled to compared to its parent
|
||||||
FractionalToMapped<std::int32_t>(0.5), //anchorOffsetX: the amount this element's anchor should be offset from the top left corner (0.5 to in the middle)
|
0.5, //anchorOffsetX: the amount this element's anchor should be offset from the top left corner (0.5 to in the middle)
|
||||||
FractionalToMapped<std::int32_t>(0.5), //anchorOffsetY: the amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle)
|
0.5, //anchorOffsetY: the amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle)
|
||||||
0 //z: this elements Z position
|
0 //z: this elements Z position
|
||||||
},
|
},
|
||||||
OpaqueType::FullyOpaque,
|
OpaqueType::FullyOpaque,
|
||||||
|
|
@ -23,20 +24,14 @@ int main() {
|
||||||
|
|
||||||
MouseElement mouse(window);
|
MouseElement mouse(window);
|
||||||
element.children.push_back(&mouse);
|
element.children.push_back(&mouse);
|
||||||
window.elements.push_back(&element);
|
window.renderer.elements.push_back(&element);
|
||||||
|
|
||||||
element.scalingBuffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}};
|
element.scalingBuffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}};
|
||||||
element.UpdatePosition(window);
|
element.UpdatePosition(window.renderer);
|
||||||
|
|
||||||
EventListener<MousePoint> clickListener(&mouse.onMouseLeftClick, [&mouse, &window](MousePoint point) {
|
EventListener<void> clickListener(&mouse.onMouseLeftClick, [&window]() {
|
||||||
// Print the coordinates where the user clicked relative to the element's top left corner.
|
std::println("Clicked on X:{} Y:{}!",
|
||||||
std::println("Clicked on Mapped X:{} Y:{}!\nClicked on Fraction X:{} Y:{}!\nClicked on Screen X:{} Y:{}!\n",
|
window.currentMousePos.x, window.currentMousePos.y
|
||||||
//Mapped space
|
|
||||||
point.x, point.y,
|
|
||||||
// Fraction space
|
|
||||||
MappedToFractionalBoundless(point.x), MappedToFractionalBoundless(point.y),
|
|
||||||
// Screen space
|
|
||||||
MappedToAbsoluteBoundless(point.x, MappedToAbsoluteBoundless(mouse.mouseScaled.width, window.width)), MappedToAbsoluteBoundless(point.y, MappedToAbsoluteBoundless(mouse.mouseScaled.width, window.height))
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import std;
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
WindowWayland window(1280, 720, "Hello Window!");
|
Device::Initialize();
|
||||||
|
Window window(1280, 720, "Hello Window!");
|
||||||
window.StartSync();
|
window.StartSync();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,10 @@
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
{
|
{
|
||||||
"path":"../../project.json",
|
"path":"../../project.json",
|
||||||
"configuration":"lib-wayland"
|
"configuration":"lib-wayland-debug"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"debug": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ typedef DescriptorSetLayoutVulkanConst<1, {{
|
||||||
}}> descriptorSetLayoutImage;
|
}}> descriptorSetLayoutImage;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
VulkanDevice::CreateDevice();
|
Device::CreateDevice();
|
||||||
WindowVulkan window(1280, 720, "HelloVulkan");
|
WindowVulkan window(1280, 720, "HelloVulkan");
|
||||||
VkCommandBuffer cmd = window.StartInit();
|
VkCommandBuffer cmd = window.StartInit();
|
||||||
|
|
||||||
|
|
@ -60,7 +60,7 @@ int main() {
|
||||||
std::array<std::uint32_t, 3> index {{2,1,0}};
|
std::array<std::uint32_t, 3> index {{2,1,0}};
|
||||||
triangleMesh.Build(verts, index, cmd);
|
triangleMesh.Build(verts, index, cmd);
|
||||||
|
|
||||||
RenderingElement3DVulkan renderer = {
|
RenderingElement3D renderer = {
|
||||||
.instance = {
|
.instance = {
|
||||||
.instanceCustomIndex = 0,
|
.instanceCustomIndex = 0,
|
||||||
.mask = 0xFF,
|
.mask = 0xFF,
|
||||||
|
|
@ -70,16 +70,16 @@ int main() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
RenderingElement3DVulkan::elements.emplace_back(&renderer);
|
RenderingElement3D::elements.emplace_back(&renderer);
|
||||||
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
|
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
|
||||||
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
|
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
|
||||||
|
|
||||||
RenderingElement3DVulkan::tlases.resize(WindowVulkan::numFrames);
|
RenderingElement3D::tlases.resize(Window::numFrames);
|
||||||
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
|
RenderingElement3D::BuildTLAS(cmd, 0);
|
||||||
RenderingElement3DVulkan::BuildTLAS(cmd, 1);
|
RenderingElement3D::BuildTLAS(cmd, 1);
|
||||||
RenderingElement3DVulkan::BuildTLAS(cmd, 2);
|
RenderingElement3D::BuildTLAS(cmd, 2);
|
||||||
|
|
||||||
VkDescriptorImageInfo imageInfo[WindowVulkan::numFrames] = {
|
VkDescriptorImageInfo imageInfo[Window::numFrames] = {
|
||||||
{
|
{
|
||||||
.imageView = window.imageViews[0],
|
.imageView = window.imageViews[0],
|
||||||
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
|
.imageLayout = VK_IMAGE_LAYOUT_GENERAL
|
||||||
|
|
@ -97,7 +97,7 @@ int main() {
|
||||||
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
|
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
|
||||||
.accelerationStructureCount = 1,
|
.accelerationStructureCount = 1,
|
||||||
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[0].accelerationStructure
|
.pAccelerationStructures = &RenderingElement3D::tlases[0].accelerationStructure
|
||||||
};
|
};
|
||||||
|
|
||||||
VkWriteDescriptorSet write[4];
|
VkWriteDescriptorSet write[4];
|
||||||
|
|
@ -112,7 +112,7 @@ int main() {
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
|
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
|
||||||
};
|
};
|
||||||
|
|
||||||
for(std::uint32_t i = 0; i < WindowVulkan::numFrames; i++) {
|
for(std::uint32_t i = 0; i < Window::numFrames; i++) {
|
||||||
write[i+1] = {
|
write[i+1] = {
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
|
||||||
.dstSet = pool.sets[i+1],
|
.dstSet = pool.sets[i+1],
|
||||||
|
|
@ -123,7 +123,7 @@ int main() {
|
||||||
.pImageInfo = &imageInfo[i]
|
.pImageInfo = &imageInfo[i]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
vkUpdateDescriptorSets(VulkanDevice::device, 4, write, 0, nullptr);
|
vkUpdateDescriptorSets(Device::device, 4, write, 0, nullptr);
|
||||||
window.descriptorsRt = {pool.sets[0], pool.sets[1]};
|
window.descriptorsRt = {pool.sets[0], pool.sets[1]};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -147,12 +147,12 @@ int main() {
|
||||||
|
|
||||||
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Translation(value, 0, 0);
|
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Translation(value, 0, 0);
|
||||||
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
|
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
|
||||||
RenderingElement3DVulkan::BuildTLAS(window.drawCmdBuffers[window.currentBuffer], window.currentBuffer);
|
RenderingElement3D::BuildTLAS(window.drawCmdBuffers[window.currentBuffer], window.currentBuffer);
|
||||||
|
|
||||||
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
|
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
|
||||||
.accelerationStructureCount = 1,
|
.accelerationStructureCount = 1,
|
||||||
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[window.currentBuffer].accelerationStructure
|
.pAccelerationStructures = &RenderingElement3D::tlases[window.currentBuffer].accelerationStructure
|
||||||
};
|
};
|
||||||
|
|
||||||
VkWriteDescriptorSet write = {
|
VkWriteDescriptorSet write = {
|
||||||
|
|
@ -164,7 +164,7 @@ int main() {
|
||||||
.descriptorCount = 1,
|
.descriptorCount = 1,
|
||||||
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
|
.descriptorType = VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR,
|
||||||
};
|
};
|
||||||
vkUpdateDescriptorSets(VulkanDevice::device, 1, &write, 0, nullptr);
|
vkUpdateDescriptorSets(Device::device, 1, &write, 0, nullptr);
|
||||||
|
|
||||||
window.descriptorsRt[1] = pool.sets[window.currentBuffer+1];
|
window.descriptorsRt[1] = pool.sets[window.currentBuffer+1];
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ A blue tinted vulkan window with a white triangle in the center.
|
||||||
EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors, &meshShader](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);
|
Device::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,8 +38,8 @@ typedef DescriptorSetLayoutVulkanConst<3, {{
|
||||||
}}> descriptorSetLayout;
|
}}> descriptorSetLayout;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
VulkanDevice::CreateDevice();
|
Device::Initialize();
|
||||||
WindowVulkan window(1280, 720, "HelloVulkan");
|
Window window(1280, 720, "HelloVulkan");
|
||||||
VkCommandBuffer cmd = window.StartInit();
|
VkCommandBuffer cmd = window.StartInit();
|
||||||
|
|
||||||
Raygenspv::CreateShader();
|
Raygenspv::CreateShader();
|
||||||
|
|
@ -61,7 +61,7 @@ int main() {
|
||||||
std::array<std::uint32_t, 3> index {{2,1,0}};
|
std::array<std::uint32_t, 3> index {{2,1,0}};
|
||||||
triangleMesh.Build(verts, index, cmd);
|
triangleMesh.Build(verts, index, cmd);
|
||||||
|
|
||||||
RenderingElement3DVulkan renderer = {
|
RenderingElement3D renderer = {
|
||||||
.instance = {
|
.instance = {
|
||||||
.instanceCustomIndex = 0,
|
.instanceCustomIndex = 0,
|
||||||
.mask = 0xFF,
|
.mask = 0xFF,
|
||||||
|
|
@ -71,12 +71,12 @@ int main() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
RenderingElement3DVulkan::elements.emplace_back(&renderer);
|
RenderingElement3D::elements.emplace_back(&renderer);
|
||||||
|
|
||||||
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
|
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
|
||||||
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
|
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
|
||||||
RenderingElement3DVulkan::tlases.resize(1);
|
RenderingElement3D::tlases.resize(1);
|
||||||
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
|
RenderingElement3D::BuildTLAS(cmd, 0);
|
||||||
|
|
||||||
VkDescriptorImageInfo imageInfo = {
|
VkDescriptorImageInfo imageInfo = {
|
||||||
.imageView = window.imageViews[0],
|
.imageView = window.imageViews[0],
|
||||||
|
|
@ -86,7 +86,7 @@ int main() {
|
||||||
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
|
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
|
||||||
.accelerationStructureCount = 1,
|
.accelerationStructureCount = 1,
|
||||||
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[0].accelerationStructure
|
.pAccelerationStructures = &RenderingElement3D::tlases[0].accelerationStructure
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanBuffer<std::uint32_t, true, false, false> lightBuffer;
|
VulkanBuffer<std::uint32_t, true, false, false> lightBuffer;
|
||||||
|
|
@ -121,7 +121,7 @@ int main() {
|
||||||
.pBufferInfo = &lightBuffer.descriptor
|
.pBufferInfo = &lightBuffer.descriptor
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
vkUpdateDescriptorSets(VulkanDevice::device, 3, write, 0, nullptr);
|
vkUpdateDescriptorSets(Device::device, 3, write, 0, nullptr);
|
||||||
|
|
||||||
|
|
||||||
window.SetPipelineRT<Pipeline>();
|
window.SetPipelineRT<Pipeline>();
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,11 @@
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
{
|
{
|
||||||
"path":"../../project.json",
|
"path":"../../project.json",
|
||||||
"configuration":"lib-vulkan"
|
"configuration":"lib-win32-vulkan-debug"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"target": "x86_64-w64-mingw32",
|
||||||
|
"debug": true,
|
||||||
"shaders": [
|
"shaders": [
|
||||||
{
|
{
|
||||||
"path":"raygen.glsl",
|
"path":"raygen.glsl",
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ A blue tinted vulkan window with a white triangle in the center.
|
||||||
EventListener<VkCommandBuffer> listener(&window.onDraw, [&descriptors, &meshShader](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);
|
Device::vkCmdDrawMeshTasksEXTProc(cmd, meshShader.threadCount, 1, 1);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import Crafter.Math;
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
VulkanDevice::CreateDevice();
|
Device::CreateDevice();
|
||||||
WindowVulkan window(1280, 720, "HelloVulkan");
|
WindowVulkan window(1280, 720, "HelloVulkan");
|
||||||
VkCommandBuffer cmd = window.StartInit();
|
VkCommandBuffer cmd = window.StartInit();
|
||||||
|
|
||||||
|
|
@ -81,7 +81,7 @@ int main() {
|
||||||
std::array<Vector<float, 3, 3>, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
|
std::array<Vector<float, 3, 3>, 3> verts {{{-150, -150, 100}, {0, 150, 100}, {150, -150, 100}}};
|
||||||
std::array<std::uint32_t, 3> index {{2,1,0}};
|
std::array<std::uint32_t, 3> index {{2,1,0}};
|
||||||
triangleMesh.Build(verts, index, cmd);
|
triangleMesh.Build(verts, index, cmd);
|
||||||
RenderingElement3DVulkan renderer = {
|
RenderingElement3D renderer = {
|
||||||
.instance = {
|
.instance = {
|
||||||
.instanceCustomIndex = 0,
|
.instanceCustomIndex = 0,
|
||||||
.mask = 0xFF,
|
.mask = 0xFF,
|
||||||
|
|
@ -92,9 +92,9 @@ int main() {
|
||||||
};
|
};
|
||||||
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
|
MatrixRowMajor<float, 4, 3, 1> transform = MatrixRowMajor<float, 4, 3, 1>::Identity();
|
||||||
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
|
std::memcpy(renderer.instance.transform.matrix, transform.m, sizeof(transform.m));
|
||||||
RenderingElement3DVulkan::tlases.resize(1);
|
RenderingElement3D::tlases.resize(1);
|
||||||
RenderingElement3DVulkan::elements.push_back(&renderer);
|
RenderingElement3D::elements.push_back(&renderer);
|
||||||
RenderingElement3DVulkan::BuildTLAS(cmd, 0);
|
RenderingElement3D::BuildTLAS(cmd, 0);
|
||||||
|
|
||||||
VkDescriptorImageInfo imageInfo = {
|
VkDescriptorImageInfo imageInfo = {
|
||||||
.imageView = window.imageViews[0],
|
.imageView = window.imageViews[0],
|
||||||
|
|
@ -104,7 +104,7 @@ int main() {
|
||||||
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
|
VkWriteDescriptorSetAccelerationStructureKHR writeDescriptorSetAccelerationStructure {
|
||||||
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
|
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
|
||||||
.accelerationStructureCount = 1,
|
.accelerationStructureCount = 1,
|
||||||
.pAccelerationStructures = &RenderingElement3DVulkan::tlases[0].accelerationStructure
|
.pAccelerationStructures = &RenderingElement3D::tlases[0].accelerationStructure
|
||||||
};
|
};
|
||||||
|
|
||||||
VkWriteDescriptorSet write[2] = {
|
VkWriteDescriptorSet write[2] = {
|
||||||
|
|
@ -127,7 +127,7 @@ int main() {
|
||||||
.pImageInfo = &imageInfo
|
.pImageInfo = &imageInfo
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
vkUpdateDescriptorSets(VulkanDevice::device, 2, write, 0, nullptr);
|
vkUpdateDescriptorSets(Device::device, 2, write, 0, nullptr);
|
||||||
|
|
||||||
window.SetPipelineRT(pipeline);
|
window.SetPipelineRT(pipeline);
|
||||||
window.descriptorsRt = pool.sets;
|
window.descriptorsRt = pool.sets;
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
# HelloWindow Example
|
|
||||||
|
|
||||||
## Description
|
|
||||||
|
|
||||||
This example demonstrates the minimal code needed to create a vulkan window and show it on the screen.
|
|
||||||
|
|
||||||
## Expected Result
|
|
||||||
|
|
||||||
A blue tinted vulkan window with the title "HelloWindow" shows onscreen, with debug info in the console.
|
|
||||||
|
|
||||||
## Highlighted Code Snippet
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
VulkanDevice::CreateDevice();
|
|
||||||
WindowWaylandVulkan window("HelloWindow", 1280, 720);
|
|
||||||
VkCommandBuffer cmd = window.StartInit();
|
|
||||||
window.FinishInit();
|
|
||||||
window.StartSync();
|
|
||||||
```
|
|
||||||
|
|
||||||
## How to Run
|
|
||||||
|
|
||||||
```bash
|
|
||||||
crafter-build build executable -r
|
|
||||||
```
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
#include "vulkan/vulkan.h"
|
|
||||||
|
|
||||||
import Crafter.Graphics;
|
|
||||||
using namespace Crafter;
|
|
||||||
|
|
||||||
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();
|
|
||||||
WindowVulkan window(1280, 720, "HelloVulkan");
|
|
||||||
|
|
||||||
// /*
|
|
||||||
// StartInit gives you a VkCommandBuffer to use before the event loop starts
|
|
||||||
// Use this for inititializing things like textures.
|
|
||||||
// */
|
|
||||||
// VkCommandBuffer cmd = window.StartInit();
|
|
||||||
|
|
||||||
// /*
|
|
||||||
// FinishInit executes all commands recorded to StartInit.
|
|
||||||
// This must be called before the the event loops starts if you called StartInit before.
|
|
||||||
// */
|
|
||||||
// window.FinishInit();
|
|
||||||
|
|
||||||
window.Render();
|
|
||||||
window.StartSync();
|
|
||||||
}
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
{
|
|
||||||
"name": "crafter-graphics",
|
|
||||||
"configurations": [
|
|
||||||
{
|
|
||||||
"name": "executable",
|
|
||||||
"implementations": ["main"],
|
|
||||||
"dependencies": [
|
|
||||||
{
|
|
||||||
"path":"../../project.json",
|
|
||||||
"configuration":"lib-vulkan"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
700
implementations/Crafter.Graphics-Device.cpp
Normal file
700
implementations/Crafter.Graphics-Device.cpp
Normal file
|
|
@ -0,0 +1,700 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2026 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 version 3.0 as published by the Free Software Foundation;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
|
#include "vulkan/vulkan.h"
|
||||||
|
#include "vulkan/vk_enum_string_helper.h"
|
||||||
|
#define GET_EXTENSION_FUNCTION(_id) ((PFN_##_id)(vkGetInstanceProcAddr(instance, #_id)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||||
|
#include <linux/input-event-codes.h>
|
||||||
|
#include <xkbcommon/xkbcommon.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "../lib/xdg-shell-client-protocol.h"
|
||||||
|
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
|
||||||
|
#include "../lib/fractional-scale-v1.h"
|
||||||
|
#include "../lib/viewporter.h"
|
||||||
|
#include <wayland-client.h>
|
||||||
|
#include <wayland-client-protocol.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
module Crafter.Graphics:Device_impl;
|
||||||
|
import :Device;
|
||||||
|
import :Window;
|
||||||
|
import :MouseElement;
|
||||||
|
import :Types;
|
||||||
|
import std;
|
||||||
|
using namespace Crafter;
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
|
const char* const instanceExtensionNames[] = {
|
||||||
|
"VK_EXT_debug_utils",
|
||||||
|
"VK_KHR_surface",
|
||||||
|
#ifdef CRAFTER_BUILD_CONFIGURATION_TARGET_x86_64_w64_mingw32
|
||||||
|
"VK_KHR_win32_surface"
|
||||||
|
#else
|
||||||
|
"VK_KHR_wayland_surface"
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
const char* const deviceExtensionNames[] = {
|
||||||
|
"VK_KHR_swapchain",
|
||||||
|
"VK_KHR_spirv_1_4",
|
||||||
|
"VK_EXT_mesh_shader",
|
||||||
|
"VK_KHR_shader_float_controls",
|
||||||
|
"VK_KHR_dynamic_rendering",
|
||||||
|
"VK_KHR_acceleration_structure",
|
||||||
|
"VK_KHR_deferred_host_operations",
|
||||||
|
"VK_KHR_ray_tracing_pipeline",
|
||||||
|
"VK_KHR_ray_tracing_position_fetch"
|
||||||
|
};
|
||||||
|
const char* const layerNames[] = {
|
||||||
|
"VK_LAYER_KHRONOS_validation"
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void Device::CheckVkResult(VkResult result) {
|
||||||
|
if (result != VK_SUCCESS)
|
||||||
|
{
|
||||||
|
throw std::runtime_error(string_VkResult(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VkBool32 onError(VkDebugUtilsMessageSeverityFlagBitsEXT severity, VkDebugUtilsMessageTypeFlagsEXT type, const VkDebugUtilsMessengerCallbackDataEXT* callbackData, void* userData)
|
||||||
|
{
|
||||||
|
printf("Vulkan ");
|
||||||
|
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT :
|
||||||
|
printf("general ");
|
||||||
|
break;
|
||||||
|
case VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT :
|
||||||
|
printf("validation ");
|
||||||
|
break;
|
||||||
|
case VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT :
|
||||||
|
printf("performance ");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (severity)
|
||||||
|
{
|
||||||
|
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT :
|
||||||
|
printf("(verbose): ");
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT :
|
||||||
|
printf("(info): ");
|
||||||
|
break;
|
||||||
|
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT :
|
||||||
|
printf("(warning): ");
|
||||||
|
break;
|
||||||
|
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT :
|
||||||
|
printf("(error): ");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s\n", callbackData->pMessage);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||||
|
constexpr CrafterKeys keysym_to_crafter_key(xkb_keysym_t sym)
|
||||||
|
{
|
||||||
|
switch (sym)
|
||||||
|
{
|
||||||
|
// Alphabet
|
||||||
|
case XKB_KEY_a: return CrafterKeys::A;
|
||||||
|
case XKB_KEY_b: return CrafterKeys::B;
|
||||||
|
case XKB_KEY_c: return CrafterKeys::C;
|
||||||
|
case XKB_KEY_d: return CrafterKeys::D;
|
||||||
|
case XKB_KEY_e: return CrafterKeys::E;
|
||||||
|
case XKB_KEY_f: return CrafterKeys::F;
|
||||||
|
case XKB_KEY_g: return CrafterKeys::G;
|
||||||
|
case XKB_KEY_h: return CrafterKeys::H;
|
||||||
|
case XKB_KEY_i: return CrafterKeys::I;
|
||||||
|
case XKB_KEY_j: return CrafterKeys::J;
|
||||||
|
case XKB_KEY_k: return CrafterKeys::K;
|
||||||
|
case XKB_KEY_l: return CrafterKeys::L;
|
||||||
|
case XKB_KEY_m: return CrafterKeys::M;
|
||||||
|
case XKB_KEY_n: return CrafterKeys::N;
|
||||||
|
case XKB_KEY_o: return CrafterKeys::O;
|
||||||
|
case XKB_KEY_p: return CrafterKeys::P;
|
||||||
|
case XKB_KEY_q: return CrafterKeys::Q;
|
||||||
|
case XKB_KEY_r: return CrafterKeys::R;
|
||||||
|
case XKB_KEY_s: return CrafterKeys::S;
|
||||||
|
case XKB_KEY_t: return CrafterKeys::T;
|
||||||
|
case XKB_KEY_u: return CrafterKeys::U;
|
||||||
|
case XKB_KEY_v: return CrafterKeys::V;
|
||||||
|
case XKB_KEY_w: return CrafterKeys::W;
|
||||||
|
case XKB_KEY_x: return CrafterKeys::X;
|
||||||
|
case XKB_KEY_y: return CrafterKeys::Y;
|
||||||
|
case XKB_KEY_z: return CrafterKeys::Z;
|
||||||
|
|
||||||
|
// Numbers
|
||||||
|
case XKB_KEY_0: return CrafterKeys::_0;
|
||||||
|
case XKB_KEY_1: return CrafterKeys::_1;
|
||||||
|
case XKB_KEY_2: return CrafterKeys::_2;
|
||||||
|
case XKB_KEY_3: return CrafterKeys::_3;
|
||||||
|
case XKB_KEY_4: return CrafterKeys::_4;
|
||||||
|
case XKB_KEY_5: return CrafterKeys::_5;
|
||||||
|
case XKB_KEY_6: return CrafterKeys::_6;
|
||||||
|
case XKB_KEY_7: return CrafterKeys::_7;
|
||||||
|
case XKB_KEY_8: return CrafterKeys::_8;
|
||||||
|
case XKB_KEY_9: return CrafterKeys::_9;
|
||||||
|
|
||||||
|
// Function keys
|
||||||
|
case XKB_KEY_F1: return CrafterKeys::F1;
|
||||||
|
case XKB_KEY_F2: return CrafterKeys::F2;
|
||||||
|
case XKB_KEY_F3: return CrafterKeys::F3;
|
||||||
|
case XKB_KEY_F4: return CrafterKeys::F4;
|
||||||
|
case XKB_KEY_F5: return CrafterKeys::F5;
|
||||||
|
case XKB_KEY_F6: return CrafterKeys::F6;
|
||||||
|
case XKB_KEY_F7: return CrafterKeys::F7;
|
||||||
|
case XKB_KEY_F8: return CrafterKeys::F8;
|
||||||
|
case XKB_KEY_F9: return CrafterKeys::F9;
|
||||||
|
case XKB_KEY_F10: return CrafterKeys::F10;
|
||||||
|
case XKB_KEY_F11: return CrafterKeys::F11;
|
||||||
|
case XKB_KEY_F12: return CrafterKeys::F12;
|
||||||
|
|
||||||
|
// Control keys
|
||||||
|
case XKB_KEY_Escape: return CrafterKeys::Escape;
|
||||||
|
case XKB_KEY_Tab: return CrafterKeys::Tab;
|
||||||
|
case XKB_KEY_Return: return CrafterKeys::Enter;
|
||||||
|
case XKB_KEY_space: return CrafterKeys::Space;
|
||||||
|
case XKB_KEY_BackSpace: return CrafterKeys::Backspace;
|
||||||
|
case XKB_KEY_Delete: return CrafterKeys::Delete;
|
||||||
|
case XKB_KEY_Insert: return CrafterKeys::Insert;
|
||||||
|
case XKB_KEY_Home: return CrafterKeys::Home;
|
||||||
|
case XKB_KEY_End: return CrafterKeys::End;
|
||||||
|
case XKB_KEY_Page_Up: return CrafterKeys::PageUp;
|
||||||
|
case XKB_KEY_Page_Down: return CrafterKeys::PageDown;
|
||||||
|
case XKB_KEY_Caps_Lock: return CrafterKeys::CapsLock;
|
||||||
|
case XKB_KEY_Num_Lock: return CrafterKeys::NumLock;
|
||||||
|
case XKB_KEY_Scroll_Lock:return CrafterKeys::ScrollLock;
|
||||||
|
|
||||||
|
// Modifiers
|
||||||
|
case XKB_KEY_Shift_L: return CrafterKeys::LeftShift;
|
||||||
|
case XKB_KEY_Shift_R: return CrafterKeys::RightShift;
|
||||||
|
case XKB_KEY_Control_L: return CrafterKeys::LeftCtrl;
|
||||||
|
case XKB_KEY_Control_R: return CrafterKeys::RightCtrl;
|
||||||
|
case XKB_KEY_Alt_L: return CrafterKeys::LeftAlt;
|
||||||
|
case XKB_KEY_Alt_R: return CrafterKeys::RightAlt;
|
||||||
|
case XKB_KEY_Super_L: return CrafterKeys::LeftSuper;
|
||||||
|
case XKB_KEY_Super_R: return CrafterKeys::RightSuper;
|
||||||
|
|
||||||
|
// Arrows
|
||||||
|
case XKB_KEY_Up: return CrafterKeys::Up;
|
||||||
|
case XKB_KEY_Down: return CrafterKeys::Down;
|
||||||
|
case XKB_KEY_Left: return CrafterKeys::Left;
|
||||||
|
case XKB_KEY_Right: return CrafterKeys::Right;
|
||||||
|
|
||||||
|
// Keypad
|
||||||
|
case XKB_KEY_KP_0: return CrafterKeys::keypad_0;
|
||||||
|
case XKB_KEY_KP_1: return CrafterKeys::keypad_1;
|
||||||
|
case XKB_KEY_KP_2: return CrafterKeys::keypad_2;
|
||||||
|
case XKB_KEY_KP_3: return CrafterKeys::keypad_3;
|
||||||
|
case XKB_KEY_KP_4: return CrafterKeys::keypad_4;
|
||||||
|
case XKB_KEY_KP_5: return CrafterKeys::keypad_5;
|
||||||
|
case XKB_KEY_KP_6: return CrafterKeys::keypad_6;
|
||||||
|
case XKB_KEY_KP_7: return CrafterKeys::keypad_7;
|
||||||
|
case XKB_KEY_KP_8: return CrafterKeys::keypad_8;
|
||||||
|
case XKB_KEY_KP_9: return CrafterKeys::keypad_9;
|
||||||
|
case XKB_KEY_KP_Enter: return CrafterKeys::keypad_enter;
|
||||||
|
case XKB_KEY_KP_Add: return CrafterKeys::keypad_plus;
|
||||||
|
case XKB_KEY_KP_Subtract: return CrafterKeys::keypad_minus;
|
||||||
|
case XKB_KEY_KP_Multiply: return CrafterKeys::keypad_multiply;
|
||||||
|
case XKB_KEY_KP_Divide: return CrafterKeys::keypad_divide;
|
||||||
|
case XKB_KEY_KP_Decimal: return CrafterKeys::keypad_decimal;
|
||||||
|
|
||||||
|
// Punctuation
|
||||||
|
case XKB_KEY_grave: return CrafterKeys::grave;
|
||||||
|
case XKB_KEY_minus: return CrafterKeys::minus;
|
||||||
|
case XKB_KEY_equal: return CrafterKeys::equal;
|
||||||
|
case XKB_KEY_bracketleft: return CrafterKeys::bracket_left;
|
||||||
|
case XKB_KEY_bracketright:return CrafterKeys::bracket_right;
|
||||||
|
case XKB_KEY_backslash: return CrafterKeys::backslash;
|
||||||
|
case XKB_KEY_semicolon: return CrafterKeys::semicolon;
|
||||||
|
case XKB_KEY_apostrophe: return CrafterKeys::quote;
|
||||||
|
case XKB_KEY_comma: return CrafterKeys::comma;
|
||||||
|
case XKB_KEY_period: return CrafterKeys::period;
|
||||||
|
case XKB_KEY_slash: return CrafterKeys::slash;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw std::runtime_error(std::format("Unkown XKB_KEY: {}", sym));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial) {
|
||||||
|
xdg_wm_base_pong(xdg_wm_base, serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) {
|
||||||
|
if (strcmp(interface, wl_shm_interface.name) == 0) {
|
||||||
|
shm = reinterpret_cast<wl_shm*>(wl_registry_bind(registry, name, &wl_shm_interface, 1));
|
||||||
|
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
||||||
|
wl_seat* seat = reinterpret_cast<wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface, 1));
|
||||||
|
wl_seat_add_listener(seat, &seat_listener, nullptr);
|
||||||
|
} else if (compositor == nullptr && strcmp(interface, wl_compositor_interface.name) == 0) {
|
||||||
|
compositor = reinterpret_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 3));
|
||||||
|
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
||||||
|
xdgWmBase = reinterpret_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1));
|
||||||
|
xdg_wm_base_add_listener(xdgWmBase, &xdgWmBaseListener, nullptr);
|
||||||
|
} else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
|
||||||
|
manager = reinterpret_cast<zxdg_decoration_manager_v1*>(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1));
|
||||||
|
} else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
|
||||||
|
wpViewporter = reinterpret_cast<wp_viewporter*>(wl_registry_bind(registry, name, &wp_viewporter_interface, 1));
|
||||||
|
} else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
|
||||||
|
fractionalScaleManager = reinterpret_cast<wp_fractional_scale_manager_v1*>(wl_registry_bind(registry, name, &wp_fractional_scale_manager_v1_interface, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::handle_global_remove(void* data, wl_registry* registry, uint32_t name) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) {
|
||||||
|
if (button == BTN_LEFT) {
|
||||||
|
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
||||||
|
Device::focusedWindow->mouseLeftHeld = true;
|
||||||
|
Device::focusedWindow->onMouseLeftClick.Invoke();
|
||||||
|
for(MouseElement* element : Device::focusedWindow->mouseElements) {
|
||||||
|
if(element) {
|
||||||
|
if(Device::focusedWindow->currentMousePos.x >= element->scaled.position.x && Device::focusedWindow->currentMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->currentMousePos.y > element->scaled.position.y && Device::focusedWindow->currentMousePos.y < element->scaled.position.y+element->scaled.size.y) {
|
||||||
|
element->onMouseLeftClick.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Device::focusedWindow->mouseLeftHeld = false;
|
||||||
|
Device::focusedWindow->onMouseLeftRelease.Invoke();
|
||||||
|
for(MouseElement* element : Device::focusedWindow->mouseElements) {
|
||||||
|
if(element) {
|
||||||
|
if(Device::focusedWindow->currentMousePos.x >= element->scaled.position.x && Device::focusedWindow->currentMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->currentMousePos.y > element->scaled.position.y && Device::focusedWindow->currentMousePos.y < element->scaled.position.y+element->scaled.size.y) {
|
||||||
|
element->onMouseLeftRelease.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if(button == BTN_RIGHT){
|
||||||
|
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
||||||
|
Device::focusedWindow->mouseRightHeld = true;
|
||||||
|
Device::focusedWindow->onMouseRightClick.Invoke();
|
||||||
|
for(MouseElement* element : Device::focusedWindow->mouseElements) {
|
||||||
|
if(element) {
|
||||||
|
if(Device::focusedWindow->currentMousePos.x >= element->scaled.position.x && Device::focusedWindow->currentMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->currentMousePos.y > element->scaled.position.y && Device::focusedWindow->currentMousePos.y < element->scaled.position.y+element->scaled.size.y) {
|
||||||
|
element->onMouseRightClick.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Device::focusedWindow->mouseRightHeld = true;
|
||||||
|
Device::focusedWindow->onMouseRightRelease.Invoke();
|
||||||
|
for(MouseElement* element : Device::focusedWindow->mouseElements) {
|
||||||
|
if(element) {
|
||||||
|
if(Device::focusedWindow->currentMousePos.x >= element->scaled.position.x && Device::focusedWindow->currentMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->currentMousePos.y > element->scaled.position.y && Device::focusedWindow->currentMousePos.y < element->scaled.position.y+element->scaled.size.y) {
|
||||||
|
element->onMouseRightRelease.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Device::focusedWindow->mouseElements.erase(std::remove(Device::focusedWindow->mouseElements.begin(), Device::focusedWindow->mouseElements.end(), static_cast<MouseElement*>(nullptr)), Device::focusedWindow->mouseElements.end());
|
||||||
|
Device::focusedWindow->mouseElements.insert(Device::focusedWindow->mouseElements.end(), Device::focusedWindow->pendingMouseElements.begin(), Device::focusedWindow->pendingMouseElements.end());
|
||||||
|
Device::focusedWindow->pendingMouseElements.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, std::uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
||||||
|
Vector<float, 2> pos(wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y));
|
||||||
|
Device::focusedWindow->lastMousePos = Device::focusedWindow->currentMousePos;
|
||||||
|
Device::focusedWindow->currentMousePos = pos * Device::focusedWindow->scale;
|
||||||
|
Device::focusedWindow->mouseDelta = {Device::focusedWindow->currentMousePos.x-Device::focusedWindow->lastMousePos.x, Device::focusedWindow->currentMousePos.y-Device::focusedWindow->lastMousePos.y};
|
||||||
|
Device::focusedWindow->onMouseMove.Invoke();
|
||||||
|
for(MouseElement* element : Device::focusedWindow->mouseElements) {
|
||||||
|
if(element) {
|
||||||
|
if(Device::focusedWindow->currentMousePos.x >= element->scaled.position.x && Device::focusedWindow->currentMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->currentMousePos.y > element->scaled.position.y && Device::focusedWindow->currentMousePos.y < element->scaled.position.y+element->scaled.size.y) {
|
||||||
|
element->onMouseMove.Invoke();
|
||||||
|
if(!(Device::focusedWindow->lastMousePos.x >= element->scaled.position.x && Device::focusedWindow->lastMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->lastMousePos.y > element->scaled.position.y && Device::focusedWindow->lastMousePos.y < element->scaled.position.y+element->scaled.size.y)) {
|
||||||
|
element->onMouseEnter.Invoke();
|
||||||
|
}
|
||||||
|
} else if(Device::focusedWindow->lastMousePos.x >= element->scaled.position.x && Device::focusedWindow->lastMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->lastMousePos.y > element->scaled.position.y && Device::focusedWindow->lastMousePos.y < element->scaled.position.y+element->scaled.size.y) {
|
||||||
|
element->onMouseLeave.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Device::focusedWindow->mouseElements.erase(std::remove(Device::focusedWindow->mouseElements.begin(), Device::focusedWindow->mouseElements.end(), static_cast<MouseElement*>(nullptr)), Device::focusedWindow->mouseElements.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, std::uint32_t serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
||||||
|
for(Window* window : windows) {
|
||||||
|
if(window->surface == surface) {
|
||||||
|
focusedWindow = window;
|
||||||
|
window->onMouseEnter.Invoke();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) {
|
||||||
|
Device::focusedWindow->onMouseLeave.Invoke();
|
||||||
|
focusedWindow = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Device::keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {
|
||||||
|
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
|
||||||
|
close(fd);
|
||||||
|
fprintf(stderr, "Unsupported keymap format\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
if (map == MAP_FAILED) {
|
||||||
|
close(fd);
|
||||||
|
perror("mmap");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||||||
|
xkb_keymap = xkb_keymap_new_from_string(xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS);
|
||||||
|
munmap(map, size);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
xkb_state = xkb_state_new(xkb_keymap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Device::keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
|
||||||
|
xkb_keycode_t keycode = key + 8;
|
||||||
|
xkb_keysym_t keysym = xkb_state_key_get_one_sym(xkb_state, keycode);
|
||||||
|
CrafterKeys crafterKey = keysym_to_crafter_key(keysym);
|
||||||
|
|
||||||
|
if(state == WL_KEYBOARD_KEY_STATE_PRESSED) {
|
||||||
|
if(Device::focusedWindow->heldkeys[static_cast<std::uint8_t>(crafterKey)]) {
|
||||||
|
Device::focusedWindow->onKeyHold[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
||||||
|
Device::focusedWindow->onAnyKeyHold.Invoke(crafterKey);
|
||||||
|
} else{
|
||||||
|
Device::focusedWindow->heldkeys[static_cast<std::uint8_t>(crafterKey)] = true;
|
||||||
|
Device::focusedWindow->onKeyDown[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
||||||
|
Device::focusedWindow->onAnyKeyDown.Invoke(crafterKey);
|
||||||
|
}
|
||||||
|
} else{
|
||||||
|
Device::focusedWindow->heldkeys[static_cast<std::uint8_t>(crafterKey)] = false;
|
||||||
|
Device::focusedWindow->onKeyUp[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
||||||
|
Device::focusedWindow->onAnyKeyUp.Invoke(crafterKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Device::seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) {
|
||||||
|
seat = seat;
|
||||||
|
if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
|
||||||
|
wl_pointer* pointer = wl_seat_get_pointer(seat);
|
||||||
|
wl_pointer_add_listener(pointer, &pointer_listener, nullptr);
|
||||||
|
}
|
||||||
|
if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
|
||||||
|
wl_keyboard* keyboard = wl_seat_get_keyboard(seat);
|
||||||
|
wl_keyboard_add_listener(keyboard, &keyboard_listener, nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void Device::Initialize() {
|
||||||
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||||
|
display = wl_display_connect(NULL);
|
||||||
|
if (display == nullptr) {
|
||||||
|
throw std::runtime_error("Could not connect to wayland display");
|
||||||
|
}
|
||||||
|
|
||||||
|
wl_registry* registry = wl_display_get_registry(display);
|
||||||
|
wl_registry_add_listener(registry, ®istry_listener, nullptr);
|
||||||
|
|
||||||
|
if (wl_display_roundtrip(display) == -1) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shm == NULL || compositor == NULL || xdgWmBase == NULL) {
|
||||||
|
throw std::runtime_error("No wl_shm, wl_compositor or xdg_wm_base support");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
|
VkApplicationInfo app{VK_STRUCTURE_TYPE_APPLICATION_INFO};
|
||||||
|
app.pApplicationName = "";
|
||||||
|
app.pEngineName = "Crafter.Graphics";
|
||||||
|
app.apiVersion = VK_MAKE_VERSION(1, 4, 0);
|
||||||
|
|
||||||
|
VkInstanceCreateInfo instanceCreateInfo = {};
|
||||||
|
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||||
|
instanceCreateInfo.pApplicationInfo = &app;
|
||||||
|
instanceCreateInfo.enabledExtensionCount = sizeof(instanceExtensionNames) / sizeof(const char*);
|
||||||
|
instanceCreateInfo.ppEnabledExtensionNames = instanceExtensionNames;
|
||||||
|
|
||||||
|
size_t foundInstanceLayers = 0;
|
||||||
|
|
||||||
|
std::uint32_t instanceLayerCount;
|
||||||
|
CheckVkResult(vkEnumerateInstanceLayerProperties(&instanceLayerCount, NULL));
|
||||||
|
|
||||||
|
std::vector<VkLayerProperties> instanceLayerProperties(instanceLayerCount);
|
||||||
|
CheckVkResult(vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProperties.data()));
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < instanceLayerCount; i++)
|
||||||
|
{
|
||||||
|
for (size_t j = 0; j < sizeof(layerNames) / sizeof(const char*); j++)
|
||||||
|
{
|
||||||
|
if (std::strcmp(instanceLayerProperties[i].layerName, layerNames[j]) == 0)
|
||||||
|
{
|
||||||
|
foundInstanceLayers++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (foundInstanceLayers >= sizeof(layerNames) / sizeof(const char*))
|
||||||
|
{
|
||||||
|
instanceCreateInfo.enabledLayerCount = sizeof(layerNames) / sizeof(const char*);
|
||||||
|
instanceCreateInfo.ppEnabledLayerNames = layerNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckVkResult(vkCreateInstance(&instanceCreateInfo, NULL, &instance));
|
||||||
|
|
||||||
|
VkDebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfo = {};
|
||||||
|
debugUtilsMessengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
||||||
|
debugUtilsMessengerCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
||||||
|
debugUtilsMessengerCreateInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
||||||
|
debugUtilsMessengerCreateInfo.pfnUserCallback = onError;
|
||||||
|
CheckVkResult(GET_EXTENSION_FUNCTION(vkCreateDebugUtilsMessengerEXT)(instance, &debugUtilsMessengerCreateInfo, NULL, &debugMessenger));
|
||||||
|
|
||||||
|
uint32_t physDeviceCount;
|
||||||
|
vkEnumeratePhysicalDevices(instance, &physDeviceCount, NULL);
|
||||||
|
|
||||||
|
std::vector<VkPhysicalDevice> physDevices(physDeviceCount);
|
||||||
|
vkEnumeratePhysicalDevices(instance, &physDeviceCount, physDevices.data());
|
||||||
|
|
||||||
|
uint32_t bestScore = 0;
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < physDeviceCount; i++)
|
||||||
|
{
|
||||||
|
VkPhysicalDevice device = physDevices[i];
|
||||||
|
|
||||||
|
VkPhysicalDeviceProperties2 properties2{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
|
||||||
|
.pNext = &rayTracingProperties
|
||||||
|
};
|
||||||
|
vkGetPhysicalDeviceProperties2(device, &properties2);
|
||||||
|
|
||||||
|
VkPhysicalDeviceProperties properties;
|
||||||
|
vkGetPhysicalDeviceProperties(device, &properties);
|
||||||
|
|
||||||
|
uint32_t score;
|
||||||
|
|
||||||
|
switch (properties.deviceType)
|
||||||
|
{
|
||||||
|
default :
|
||||||
|
continue;
|
||||||
|
case VK_PHYSICAL_DEVICE_TYPE_OTHER :
|
||||||
|
score = 1;
|
||||||
|
break;
|
||||||
|
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU :
|
||||||
|
score = 4;
|
||||||
|
break;
|
||||||
|
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU :
|
||||||
|
score = 5;
|
||||||
|
break;
|
||||||
|
case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU :
|
||||||
|
score = 3;
|
||||||
|
break;
|
||||||
|
case VK_PHYSICAL_DEVICE_TYPE_CPU :
|
||||||
|
score = 2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (score > bestScore)
|
||||||
|
{
|
||||||
|
physDevice = device;
|
||||||
|
bestScore = score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t queueFamilyCount;
|
||||||
|
vkGetPhysicalDeviceQueueFamilyProperties(physDevice, &queueFamilyCount, NULL);
|
||||||
|
|
||||||
|
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
||||||
|
vkGetPhysicalDeviceQueueFamilyProperties(physDevice, &queueFamilyCount, queueFamilies.data());
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < queueFamilyCount; i++)
|
||||||
|
{
|
||||||
|
if (queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
||||||
|
{
|
||||||
|
queueFamilyIndex = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float priority = 1;
|
||||||
|
|
||||||
|
VkDeviceQueueCreateInfo queueCreateInfo = {};
|
||||||
|
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
||||||
|
queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
|
||||||
|
queueCreateInfo.queueCount = 1;
|
||||||
|
queueCreateInfo.pQueuePriorities = &priority;
|
||||||
|
|
||||||
|
VkPhysicalDeviceVulkan12Features features12 {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
|
||||||
|
.runtimeDescriptorArray = VK_TRUE,
|
||||||
|
.bufferDeviceAddress = VK_TRUE
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPhysicalDeviceRayTracingPositionFetchFeaturesKHR vkPhysicalDeviceRayTracingPositionFetchFeatures {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_POSITION_FETCH_FEATURES_KHR,
|
||||||
|
.pNext = &features12,
|
||||||
|
.rayTracingPositionFetch = VK_TRUE
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPhysicalDeviceRayTracingPipelineFeaturesKHR physicalDeviceRayTracingPipelineFeatures{
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR,
|
||||||
|
.pNext = &vkPhysicalDeviceRayTracingPositionFetchFeatures,
|
||||||
|
.rayTracingPipeline = VK_TRUE
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPhysicalDeviceAccelerationStructureFeaturesKHR deviceAccelerationStructureFeature = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR,
|
||||||
|
.pNext = &physicalDeviceRayTracingPipelineFeatures,
|
||||||
|
.accelerationStructure = VK_TRUE
|
||||||
|
};
|
||||||
|
|
||||||
|
VkPhysicalDeviceDynamicRenderingFeaturesKHR dynamicRenderingFeature = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES_KHR};
|
||||||
|
dynamicRenderingFeature.dynamicRendering = VK_FALSE;
|
||||||
|
dynamicRenderingFeature.pNext = &deviceAccelerationStructureFeature;
|
||||||
|
|
||||||
|
VkPhysicalDeviceMeshShaderFeaturesEXT ext_feature = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT};
|
||||||
|
ext_feature.meshShader = VK_FALSE;
|
||||||
|
ext_feature.pNext = &dynamicRenderingFeature;
|
||||||
|
|
||||||
|
VkPhysicalDeviceFeatures2 physical_features2 = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
|
||||||
|
.features = {
|
||||||
|
.samplerAnisotropy = VK_TRUE
|
||||||
|
}
|
||||||
|
};
|
||||||
|
physical_features2.pNext = &ext_feature;
|
||||||
|
|
||||||
|
VkDeviceCreateInfo deviceCreateInfo = {};
|
||||||
|
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
||||||
|
deviceCreateInfo.queueCreateInfoCount = 1;
|
||||||
|
deviceCreateInfo.pQueueCreateInfos = &queueCreateInfo;
|
||||||
|
deviceCreateInfo.enabledExtensionCount = sizeof(deviceExtensionNames) / sizeof(const char*);
|
||||||
|
deviceCreateInfo.ppEnabledExtensionNames = deviceExtensionNames;
|
||||||
|
deviceCreateInfo.pNext = &physical_features2;
|
||||||
|
|
||||||
|
uint32_t deviceLayerCount;
|
||||||
|
CheckVkResult(vkEnumerateDeviceLayerProperties(physDevice, &deviceLayerCount, NULL));
|
||||||
|
|
||||||
|
std::vector<VkLayerProperties> deviceLayerProperties(deviceLayerCount);
|
||||||
|
CheckVkResult(vkEnumerateDeviceLayerProperties(physDevice, &deviceLayerCount, deviceLayerProperties.data()));
|
||||||
|
|
||||||
|
size_t foundDeviceLayers = 0;
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < deviceLayerCount; i++)
|
||||||
|
{
|
||||||
|
for (size_t j = 0; j < sizeof(layerNames) / sizeof(const char*); j++)
|
||||||
|
{
|
||||||
|
if (std::strcmp(deviceLayerProperties[i].layerName, layerNames[j]) == 0)
|
||||||
|
{
|
||||||
|
foundDeviceLayers++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (foundDeviceLayers >= sizeof(layerNames) / sizeof(const char*))
|
||||||
|
{
|
||||||
|
deviceCreateInfo.enabledLayerCount = sizeof(layerNames) / sizeof(const char*);
|
||||||
|
deviceCreateInfo.ppEnabledLayerNames = layerNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckVkResult(vkCreateDevice(physDevice, &deviceCreateInfo, NULL, &device));
|
||||||
|
vkGetDeviceQueue(device, queueFamilyIndex, 0, &queue);
|
||||||
|
|
||||||
|
VkCommandPoolCreateInfo commandPoolcreateInfo = {};
|
||||||
|
commandPoolcreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
||||||
|
commandPoolcreateInfo.queueFamilyIndex = queueFamilyIndex;
|
||||||
|
commandPoolcreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
||||||
|
CheckVkResult(vkCreateCommandPool(device, &commandPoolcreateInfo, NULL, &commandPool));
|
||||||
|
|
||||||
|
vkGetPhysicalDeviceMemoryProperties(physDevice, &memoryProperties);
|
||||||
|
|
||||||
|
vkGetAccelerationStructureBuildSizesKHR = reinterpret_cast<PFN_vkGetAccelerationStructureBuildSizesKHR>(vkGetInstanceProcAddr(instance, "vkGetAccelerationStructureBuildSizesKHR"));
|
||||||
|
vkCreateAccelerationStructureKHR = reinterpret_cast<PFN_vkCreateAccelerationStructureKHR>(vkGetInstanceProcAddr(instance, "vkCreateAccelerationStructureKHR"));
|
||||||
|
vkCmdBuildAccelerationStructuresKHR = reinterpret_cast<PFN_vkCmdBuildAccelerationStructuresKHR>(vkGetInstanceProcAddr(instance, "vkCmdBuildAccelerationStructuresKHR"));
|
||||||
|
vkGetAccelerationStructureDeviceAddressKHR = reinterpret_cast<PFN_vkGetAccelerationStructureDeviceAddressKHR>(vkGetInstanceProcAddr(instance, "vkGetAccelerationStructureDeviceAddressKHR"));
|
||||||
|
vkCreateRayTracingPipelinesKHR = reinterpret_cast<PFN_vkCreateRayTracingPipelinesKHR>(vkGetInstanceProcAddr(instance, "vkCreateRayTracingPipelinesKHR"));
|
||||||
|
vkGetRayTracingShaderGroupHandlesKHR = reinterpret_cast<PFN_vkGetRayTracingShaderGroupHandlesKHR>(vkGetInstanceProcAddr(instance, "vkGetRayTracingShaderGroupHandlesKHR"));
|
||||||
|
vkCmdTraceRaysKHR = reinterpret_cast<PFN_vkCmdTraceRaysKHR>(vkGetInstanceProcAddr(instance, "vkCmdTraceRaysKHR"));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
|
std::uint32_t Device::GetMemoryType(uint32_t typeBits, VkMemoryPropertyFlags properties) {
|
||||||
|
for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++)
|
||||||
|
{
|
||||||
|
if ((typeBits & 1) == 1)
|
||||||
|
{
|
||||||
|
if ((memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
typeBits >>= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw std::runtime_error("Could not find a matching memory type");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -20,24 +20,24 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
module Crafter.Graphics:GridElement_impl;
|
module Crafter.Graphics:GridElement_impl;
|
||||||
import :GridElement;
|
import :GridElement;
|
||||||
import :Window;
|
import :Rendertarget;
|
||||||
import :Types;
|
import :Types;
|
||||||
import std;
|
import std;
|
||||||
|
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
GridElement::GridElement(std::uint32_t columns, std::uint32_t rows, std::int32_t spacingX, std::int32_t spacingY, std::int32_t paddingX, std::int32_t paddingY, Anchor anchor) : Transform(anchor), columns(columns), rows(rows), spacingX(spacingX), spacingY(spacingY), paddingX(paddingX), paddingY(paddingY) {
|
GridElement::GridElement(std::uint32_t columns, std::uint32_t rows, std::int32_t spacingX, std::int32_t spacingY, std::int32_t paddingX, std::int32_t paddingY, Anchor2D anchor) : Transform2D(anchor), columns(columns), rows(rows), spacingX(spacingX), spacingY(spacingY), paddingX(paddingX), paddingY(paddingY) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GridElement::UpdatePositionScaled(Window& window) {
|
void GridElement::UpdatePositionScaled(Rendertarget& window) {
|
||||||
std::int32_t cellWidth = (SCALE32 - (paddingX * 2) - (spacingX * (columns - 1))) / columns;
|
std::int32_t cellWidth = (paddingX * 2) - (spacingX * (columns - 1)) / columns;
|
||||||
std::int32_t cellHeight = (SCALE32 - (paddingY * 2) - (spacingY * (rows - 1))) / rows;
|
std::int32_t cellHeight = (paddingY * 2) - (spacingY * (rows - 1)) / rows;
|
||||||
|
|
||||||
std::size_t childIndex = 0;
|
std::size_t childIndex = 0;
|
||||||
for (std::uint32_t row = 0; row < rows && childIndex < children.size(); ++row) {
|
for (std::uint32_t row = 0; row < rows && childIndex < children.size(); ++row) {
|
||||||
for (std::uint32_t col = 0; col < columns && childIndex < children.size(); ++col) {
|
for (std::uint32_t col = 0; col < columns && childIndex < children.size(); ++col) {
|
||||||
Transform* child = children[childIndex];
|
Transform2D* child = children[childIndex];
|
||||||
|
|
||||||
// Calculate position for this child
|
// Calculate position for this child
|
||||||
std::int32_t childX = (cellWidth * col) + (spacingX * col) + paddingX;
|
std::int32_t childX = (cellWidth * col) + (spacingX * col) + paddingX;
|
||||||
|
|
@ -57,12 +57,12 @@ void GridElement::UpdatePositionScaled(Window& window) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GridElement::UpdatePosition(Window& window) {
|
void GridElement::UpdatePosition(Rendertarget& window) {
|
||||||
window.ScaleElement(*this);
|
ScaleElement(window.transform);
|
||||||
UpdatePositionScaled(window);
|
UpdatePositionScaled(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GridElement::UpdatePosition(Window& window, Transform& parent) {
|
void GridElement::UpdatePosition(Rendertarget& window, Transform2D& parent) {
|
||||||
window.ScaleElement(*this, parent);
|
ScaleElement(parent);
|
||||||
UpdatePositionScaled(window);
|
UpdatePositionScaled(window);
|
||||||
}
|
}
|
||||||
|
|
@ -1,63 +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
|
|
||||||
*/
|
|
||||||
|
|
||||||
module;
|
|
||||||
#include "../lib/stb_image.h"
|
|
||||||
module Crafter.Graphics:Image_impl;
|
|
||||||
import :Image;
|
|
||||||
import std;
|
|
||||||
|
|
||||||
using namespace Crafter;
|
|
||||||
|
|
||||||
Image::Image(const std::string_view path) {
|
|
||||||
std::filesystem::path abs = std::filesystem::absolute(path);
|
|
||||||
int xSize;
|
|
||||||
int ySize;
|
|
||||||
unsigned char* bgData = stbi_load(abs.string().c_str(), &xSize, &ySize, nullptr, 4);
|
|
||||||
|
|
||||||
buffer.resize(xSize*ySize);
|
|
||||||
width = xSize;
|
|
||||||
height = ySize;
|
|
||||||
|
|
||||||
opaque = OpaqueType::FullyOpaque;
|
|
||||||
|
|
||||||
for(std::uint_fast32_t x = 0; x < xSize; x++) {
|
|
||||||
for(std::uint_fast32_t y = 0; y < ySize; y++) {
|
|
||||||
std::uint_fast32_t idx = (x*ySize+y)*4;
|
|
||||||
buffer[x*ySize+y].r = bgData[idx];
|
|
||||||
buffer[x*ySize+y].g = bgData[idx+1];
|
|
||||||
buffer[x*ySize+y].b = bgData[idx+2];
|
|
||||||
buffer[x*ySize+y].a = bgData[idx+3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(std::uint_fast32_t i = 0; i < xSize*ySize; i++) {
|
|
||||||
if(buffer[i].a != 255) {
|
|
||||||
opaque = OpaqueType::SemiOpaque;
|
|
||||||
for(std::uint_fast32_t i2 = 0; i2 < xSize*ySize; i2++) {
|
|
||||||
if(buffer[i2].a != 0 && buffer[i2].a != 255) {
|
|
||||||
opaque = OpaqueType::Transparent;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -24,7 +24,7 @@ module;
|
||||||
module Crafter.Graphics:Mesh_impl;
|
module Crafter.Graphics:Mesh_impl;
|
||||||
import Crafter.Math;
|
import Crafter.Math;
|
||||||
import :Mesh;
|
import :Mesh;
|
||||||
import :VulkanDevice;
|
import :Device;
|
||||||
import :Types;
|
import :Types;
|
||||||
import std;
|
import std;
|
||||||
|
|
||||||
|
|
@ -79,8 +79,8 @@ void Mesh::Build(std::span<Vector<float, 3, 3>> verticies, std::span<std::uint32
|
||||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::vkGetAccelerationStructureBuildSizesKHR(
|
Device::vkGetAccelerationStructureBuildSizesKHR(
|
||||||
VulkanDevice::device,
|
Device::device,
|
||||||
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
||||||
&blasBuildGeometryInfo,
|
&blasBuildGeometryInfo,
|
||||||
&primitiveCount,
|
&primitiveCount,
|
||||||
|
|
@ -108,7 +108,7 @@ void Mesh::Build(std::span<Vector<float, 3, 3>> verticies, std::span<std::uint32
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateAccelerationStructureKHR(VulkanDevice::device, &blasCreateInfo, nullptr, &accelerationStructure));
|
Device::CheckVkResult(Device::vkCreateAccelerationStructureKHR(Device::device, &blasCreateInfo, nullptr, &accelerationStructure));
|
||||||
blasBuildGeometryInfo.dstAccelerationStructure = accelerationStructure;
|
blasBuildGeometryInfo.dstAccelerationStructure = accelerationStructure;
|
||||||
|
|
||||||
// Prepare the build range for the BLAS
|
// Prepare the build range for the BLAS
|
||||||
|
|
@ -120,11 +120,11 @@ void Mesh::Build(std::span<Vector<float, 3, 3>> verticies, std::span<std::uint32
|
||||||
};
|
};
|
||||||
|
|
||||||
VkAccelerationStructureBuildRangeInfoKHR* blasRangeInfoPP = &blasRangeInfo;
|
VkAccelerationStructureBuildRangeInfoKHR* blasRangeInfoPP = &blasRangeInfo;
|
||||||
VulkanDevice::vkCmdBuildAccelerationStructuresKHR(cmd, 1, &blasBuildGeometryInfo, &blasRangeInfoPP);
|
Device::vkCmdBuildAccelerationStructuresKHR(cmd, 1, &blasBuildGeometryInfo, &blasRangeInfoPP);
|
||||||
|
|
||||||
VkAccelerationStructureDeviceAddressInfoKHR addrInfo {
|
VkAccelerationStructureDeviceAddressInfoKHR addrInfo {
|
||||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR,
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR,
|
||||||
.accelerationStructure = accelerationStructure
|
.accelerationStructure = accelerationStructure
|
||||||
};
|
};
|
||||||
blasAddr = VulkanDevice::vkGetAccelerationStructureDeviceAddressKHR(VulkanDevice::device, &addrInfo);
|
blasAddr = Device::vkGetAccelerationStructureDeviceAddressKHR(Device::device, &addrInfo);
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -27,35 +27,18 @@ import std;
|
||||||
|
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
MouseElement::MouseElement(Anchor anchor, WindowMouse& window) : Transform(anchor) {
|
MouseElement::MouseElement(Anchor2D anchor, Window& window) : Transform2D(anchor) {
|
||||||
window.mouseElements.push_back(this);
|
window.mouseElements.push_back(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseElement::MouseElement(Anchor anchor) : Transform(anchor) {
|
MouseElement::MouseElement(Anchor2D anchor) : Transform2D(anchor) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseElement::MouseElement(WindowMouse& window) : Transform({FractionalToMapped<std::uint32_t>(0), FractionalToMapped<std::uint32_t>(0), FractionalToMapped<std::uint32_t>(1), FractionalToMapped<std::uint32_t>(1), FractionalToMapped<std::uint32_t>(0), FractionalToMapped<std::uint32_t>(0), 0}) {
|
MouseElement::MouseElement(Window& window) : Transform2D({0, 0, 1, 1, 0, 0, 0}) {
|
||||||
window.mouseElements.push_back(this);
|
window.mouseElements.push_back(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseElement::MouseElement() : Transform({FractionalToMapped<std::uint32_t>(0), FractionalToMapped<std::uint32_t>(0), FractionalToMapped<std::uint32_t>(1), FractionalToMapped<std::uint32_t>(1), FractionalToMapped<std::uint32_t>(0), FractionalToMapped<std::uint32_t>(0), 0}) {
|
MouseElement::MouseElement() : Transform2D({0, 0, 1, 1, 0, 0, 0}) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MouseElement::UpdatePosition(Window& window) {
|
|
||||||
window.ScaleMouse(*this);
|
|
||||||
window.ScaleElement(*this);
|
|
||||||
for(Transform* child : children) {
|
|
||||||
child->UpdatePosition(window, *this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MouseElement::UpdatePosition(Window& window, Transform& parent) {
|
|
||||||
window.ScaleMouse(*this, parent);
|
|
||||||
window.ScaleElement(*this, parent);
|
|
||||||
for(Transform* child : children) {
|
|
||||||
child->UpdatePosition(window, *this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -19,15 +19,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
module;
|
module;
|
||||||
#include <vulkan/vulkan_core.h>
|
#include <vulkan/vulkan_core.h>
|
||||||
module Crafter.Graphics:RenderingElement3DVulkan_impl;
|
module Crafter.Graphics:RenderingElement3D_impl;
|
||||||
import :RenderingElement3DVulkan;
|
import :RenderingElement3D;
|
||||||
import std;
|
import std;
|
||||||
|
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
std::vector<RenderingElement3DVulkan*> RenderingElement3DVulkan::elements;
|
std::vector<RenderingElement3D*> RenderingElement3D::elements;
|
||||||
|
|
||||||
void RenderingElement3DVulkan::BuildTLAS(VkCommandBuffer cmd, std::uint32_t index) {
|
void RenderingElement3D::BuildTLAS(VkCommandBuffer cmd, std::uint32_t index) {
|
||||||
tlases[index].instanceBuffer.Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, elements.size());
|
tlases[index].instanceBuffer.Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, elements.size());
|
||||||
|
|
||||||
for(std::uint32_t i = 0; i < elements.size(); i++) {
|
for(std::uint32_t i = 0; i < elements.size(); i++) {
|
||||||
|
|
@ -65,8 +65,8 @@ void RenderingElement3DVulkan::BuildTLAS(VkCommandBuffer cmd, std::uint32_t inde
|
||||||
VkAccelerationStructureBuildSizesInfoKHR tlasBuildSizes {
|
VkAccelerationStructureBuildSizesInfoKHR tlasBuildSizes {
|
||||||
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR
|
.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR
|
||||||
};
|
};
|
||||||
VulkanDevice::vkGetAccelerationStructureBuildSizesKHR(
|
Device::vkGetAccelerationStructureBuildSizesKHR(
|
||||||
VulkanDevice::device,
|
Device::device,
|
||||||
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
|
||||||
&tlasBuildGeometryInfo,
|
&tlasBuildGeometryInfo,
|
||||||
&primitiveCount,
|
&primitiveCount,
|
||||||
|
|
@ -87,7 +87,7 @@ void RenderingElement3DVulkan::BuildTLAS(VkCommandBuffer cmd, std::uint32_t inde
|
||||||
.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR,
|
.type = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR,
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateAccelerationStructureKHR(VulkanDevice::device, &tlasCreateInfo, nullptr, &tlases[index].accelerationStructure));
|
Device::CheckVkResult(Device::vkCreateAccelerationStructureKHR(Device::device, &tlasCreateInfo, nullptr, &tlases[index].accelerationStructure));
|
||||||
tlasBuildGeometryInfo.dstAccelerationStructure = tlases[index].accelerationStructure;
|
tlasBuildGeometryInfo.dstAccelerationStructure = tlases[index].accelerationStructure;
|
||||||
|
|
||||||
// Prepare the build range for the TLAS
|
// Prepare the build range for the TLAS
|
||||||
|
|
@ -99,7 +99,7 @@ void RenderingElement3DVulkan::BuildTLAS(VkCommandBuffer cmd, std::uint32_t inde
|
||||||
};
|
};
|
||||||
|
|
||||||
VkAccelerationStructureBuildRangeInfoKHR* tlasRangeInfoPP = &tlasRangeInfo;
|
VkAccelerationStructureBuildRangeInfoKHR* tlasRangeInfoPP = &tlasRangeInfo;
|
||||||
VulkanDevice::vkCmdBuildAccelerationStructuresKHR(cmd, 1, &tlasBuildGeometryInfo, &tlasRangeInfoPP);
|
Device::vkCmdBuildAccelerationStructuresKHR(cmd, 1, &tlasBuildGeometryInfo, &tlasRangeInfoPP);
|
||||||
|
|
||||||
vkCmdPipelineBarrier(
|
vkCmdPipelineBarrier(
|
||||||
cmd,
|
cmd,
|
||||||
249
implementations/Crafter.Graphics-Rendertarget.cpp
Normal file
249
implementations/Crafter.Graphics-Rendertarget.cpp
Normal file
|
|
@ -0,0 +1,249 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2026 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 Crafter.Graphics:Rendertarget_impl;
|
||||||
|
import :Window;
|
||||||
|
import :RenderingElement2D;
|
||||||
|
import std;
|
||||||
|
|
||||||
|
using namespace Crafter;
|
||||||
|
|
||||||
|
Rendertarget::Rendertarget(std::int32_t sizeX, std::int32_t sizeY) : transform({0, 0, static_cast<float>(sizeX), static_cast<float>(sizeY), 0, 0, 0}) {
|
||||||
|
transform.scaled.size.x = sizeX;
|
||||||
|
transform.scaled.size.y = sizeY;
|
||||||
|
transform.scaled.position.x = 0;
|
||||||
|
transform.scaled.position.y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Rendertarget::AddDirtyRect(ScaleData2D scale) {
|
||||||
|
ClipRect rect {
|
||||||
|
.left = std::max(scale.position.x, std::int32_t(0)),
|
||||||
|
.right = std::min(scale.position.x + scale.size.x, sizeX),
|
||||||
|
.top = std::max(scale.position.y, std::int32_t(0)),
|
||||||
|
.bottom = std::min(scale.position.y + scale.size.y, sizeY),
|
||||||
|
};
|
||||||
|
dirtyRects.push_back(rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void blend_pixel_optimized(Vector<std::uint8_t, 4>& dst, const Vector<std::uint8_t, 4>& src) {
|
||||||
|
if(src.a == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float srcA = src.a / 255.0f;
|
||||||
|
float dstA = dst.a / 255.0f;
|
||||||
|
|
||||||
|
float outA = srcA + dstA * (1.0f - srcA);
|
||||||
|
if (outA > 0.0f) {
|
||||||
|
dst = Vector<std::uint8_t, 4>(
|
||||||
|
static_cast<std::uint8_t>((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA),
|
||||||
|
static_cast<std::uint8_t>((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA),
|
||||||
|
static_cast<std::uint8_t>((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA),
|
||||||
|
static_cast<std::uint8_t>(outA * 255)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Rendertarget::RenderElement(RenderingElement2DBase* element) {
|
||||||
|
#ifdef CRAFTER_TIMING
|
||||||
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(element->scaled.size.x < 1 || element->scaled.size.y < 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(ClipRect dirty : dirtyRects) {
|
||||||
|
dirty.left = std::max(element->scaled.position.x, dirty.left);
|
||||||
|
dirty.top = std::max(element->scaled.position.y, dirty.top);
|
||||||
|
dirty.right = std::min(element->scaled.position.x+element->scaled.size.x, dirty.right);
|
||||||
|
dirty.bottom = std::min(element->scaled.position.y+element->scaled.size.y, dirty.bottom);
|
||||||
|
|
||||||
|
const Vector<std::uint8_t, 4>* src_buffer = element->buffer.data();
|
||||||
|
std::int32_t src_width = element->scaled.size.x;
|
||||||
|
std::int32_t src_height = element->scaled.size.y;
|
||||||
|
|
||||||
|
switch (element->opaque) {
|
||||||
|
case OpaqueType::FullyOpaque:
|
||||||
|
// For fully opaque, just copy pixels directly
|
||||||
|
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
|
||||||
|
std::int32_t src_y = y - element->scaled.position.y;
|
||||||
|
|
||||||
|
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
|
||||||
|
std::int32_t src_x = x - element->scaled.position.x;
|
||||||
|
|
||||||
|
buffer[y * sizeX + x] = src_buffer[src_y * src_width + src_x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OpaqueType::SemiOpaque:
|
||||||
|
// For semi-opaque, we can avoid blending when alpha is 0 or 255
|
||||||
|
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
|
||||||
|
std::int32_t src_y = y - element->scaled.position.y;
|
||||||
|
|
||||||
|
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
|
||||||
|
std::int32_t src_x = x - element->scaled.position.x;
|
||||||
|
Vector<std::uint8_t, 4> src_pixel = src_buffer[src_y * src_width + src_x];
|
||||||
|
|
||||||
|
if (src_pixel.a == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buffer[y * sizeX + x] = src_pixel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OpaqueType::Transparent:
|
||||||
|
// For transparent, always perform blending
|
||||||
|
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
|
||||||
|
std::int32_t src_y = y - element->scaled.position.y;
|
||||||
|
|
||||||
|
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
|
||||||
|
std::int32_t src_x = x - element->scaled.position.x;
|
||||||
|
blend_pixel_optimized(buffer[y * sizeX + x], src_buffer[src_y * src_width + src_x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CRAFTER_TIMING
|
||||||
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
renderTimings.push_back({element, element->scaled.size.x, element->scaled.size.y, end-start});
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void Rendertarget::Render() {
|
||||||
|
//std::vector<ClipRect> newClip;
|
||||||
|
|
||||||
|
// for (std::uint32_t i = 0; i < dirtyRects.size(); i++) {
|
||||||
|
// ClipRect rect = dirtyRects[i];
|
||||||
|
// for (std::uint32_t i2 = i + 1; i2 < dirtyRects.size(); i2++) {
|
||||||
|
// ClipRect existing = dirtyRects[i2];
|
||||||
|
// if(rect.bottom >= existing.top && rect.top <= existing.top) {
|
||||||
|
// newClip.push_back({
|
||||||
|
// .left = rect.left,
|
||||||
|
// .right = rect.right,
|
||||||
|
// .top = rect.top,
|
||||||
|
// .bottom = existing.top,
|
||||||
|
// });
|
||||||
|
// //-| shape
|
||||||
|
// if(rect.right > existing.right) {
|
||||||
|
// newClip.push_back({
|
||||||
|
// .left = existing.right,
|
||||||
|
// .right = rect.right,
|
||||||
|
// .top = existing.top,
|
||||||
|
// .bottom = existing.bottom,
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// //|- shape
|
||||||
|
// if(rect.left < existing.left) {
|
||||||
|
// newClip.push_back({
|
||||||
|
// .left = rect.left,
|
||||||
|
// .right = existing.left,
|
||||||
|
// .top = existing.top,
|
||||||
|
// .bottom = existing.bottom,
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// //-| or |- shape where rect extends further down
|
||||||
|
// if(rect.bottom > existing.bottom) {
|
||||||
|
// newClip.push_back({
|
||||||
|
// .left = rect.left,
|
||||||
|
// .right = rect.right,
|
||||||
|
// .top = existing.bottom,
|
||||||
|
// .bottom = rect.bottom,
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// goto inner;
|
||||||
|
// }
|
||||||
|
// if (rect.left <= existing.right && rect.right >= existing.left) {
|
||||||
|
// newClip.push_back({
|
||||||
|
// .left = rect.left,
|
||||||
|
// .right = existing.left,
|
||||||
|
// .top = rect.top,
|
||||||
|
// .bottom = rect.bottom,
|
||||||
|
// });
|
||||||
|
// if (rect.right > existing.right) {
|
||||||
|
// newClip.push_back({
|
||||||
|
// .left = existing.right,
|
||||||
|
// .right = rect.right,
|
||||||
|
// .top = rect.top,
|
||||||
|
// .bottom = rect.bottom,
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// goto inner;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// newClip.push_back(rect);
|
||||||
|
// inner:;
|
||||||
|
// }
|
||||||
|
|
||||||
|
//dirtyRects = std::move(newClip);
|
||||||
|
|
||||||
|
// std::memset(buffer, 0, width*height*4);
|
||||||
|
|
||||||
|
// std::cout << dirtyRects.size() << std::endl;
|
||||||
|
// // Color palette
|
||||||
|
// static const std::vector<Vector<std::uint8_t, 4>> colors = {
|
||||||
|
// {255, 0, 0, 255}, // red
|
||||||
|
// { 0, 255, 0, 255}, // green
|
||||||
|
// { 0, 0, 255, 255}, // blue
|
||||||
|
// {255, 255, 0, 255}, // yellow
|
||||||
|
// {255, 0, 255, 255}, // magenta
|
||||||
|
// { 0, 255, 255, 255}, // cyan
|
||||||
|
// };
|
||||||
|
|
||||||
|
// std::size_t rectIndex = 0;
|
||||||
|
|
||||||
|
// for (const ClipRect& rect : dirtyRects) {
|
||||||
|
// const Vector<std::uint8_t, 4>& color = colors[rectIndex % colors.size()];
|
||||||
|
|
||||||
|
// std::cout << std::format(
|
||||||
|
// "ClipRect {}: [{}, {}, {}, {}] Color = RGBA({}, {}, {}, {})",
|
||||||
|
// rectIndex,
|
||||||
|
// rect.left, rect.top, rect.right, rect.bottom,
|
||||||
|
// color.r, color.g, color.b, color.a
|
||||||
|
// ) << std::endl;
|
||||||
|
|
||||||
|
// for (std::int32_t y = rect.top; y < rect.bottom; ++y) {
|
||||||
|
// for (std::int32_t x = rect.left; x < rect.right; ++x) {
|
||||||
|
// buffer[y * width + x] = color;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ++rectIndex;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (!dirtyRects.empty()) {
|
||||||
|
for (ClipRect rect : dirtyRects) {
|
||||||
|
for (std::int32_t y = rect.top; y < rect.bottom; y++) {
|
||||||
|
for (std::int32_t x = rect.left; x < rect.right; x++) {
|
||||||
|
buffer[y * sizeX + x] = {0, 0, 0, 0};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(RenderingElement2DBase* child : elements) {
|
||||||
|
RenderElement(child);
|
||||||
|
}
|
||||||
|
dirtyRects.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
catcrafts.net
|
catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
|
||||||
|
|
@ -1,51 +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
|
|
||||||
*/
|
|
||||||
|
|
||||||
module Crafter.Graphics:Transform_impl;
|
|
||||||
import :Transform;
|
|
||||||
import :Window;
|
|
||||||
import :Types;
|
|
||||||
import :Font;
|
|
||||||
import std;
|
|
||||||
|
|
||||||
using namespace Crafter;
|
|
||||||
|
|
||||||
|
|
||||||
Anchor::Anchor(std::int32_t x, std::int32_t y, std::uint32_t width, std::uint32_t height, std::int32_t offsetX, std::int32_t offsetY, std::int32_t z, bool maintainAspectRatio): x(x), y(y), width(width), height(height), offsetX(offsetX), offsetY(offsetY), z(z), maintainAspectRatio(maintainAspectRatio) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Transform::Transform(Anchor anchor) : anchor(anchor) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void Transform::UpdatePosition(Window& window) {
|
|
||||||
window.ScaleElement(*this);
|
|
||||||
for(Transform* child : children) {
|
|
||||||
child->UpdatePosition(window, *this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Transform::UpdatePosition(Window& window, Transform& parent) {
|
|
||||||
window.ScaleElement(*this, parent);
|
|
||||||
for(Transform* child : children) {
|
|
||||||
child->UpdatePosition(window, *this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
70
implementations/Crafter.Graphics-Transform2D.cpp
Normal file
70
implementations/Crafter.Graphics-Transform2D.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2026 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 Crafter.Graphics:Transform2D_impl;
|
||||||
|
import :Transform2D;
|
||||||
|
import :Rendertarget;
|
||||||
|
import :Types;
|
||||||
|
import :Font;
|
||||||
|
import std;
|
||||||
|
|
||||||
|
using namespace Crafter;
|
||||||
|
|
||||||
|
|
||||||
|
Anchor2D::Anchor2D(float x, float y, float width, float height, float offsetX, float offsetY, std::int32_t z, bool maintainAspectRatio): x(x), y(y), width(width), height(height), offsetX(offsetX), offsetY(offsetY), z(z), maintainAspectRatio(maintainAspectRatio) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Transform2D::Transform2D(Anchor2D anchor) : anchor(anchor) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Transform2D::UpdatePosition(Rendertarget& window) {
|
||||||
|
ScaleElement(window.transform);
|
||||||
|
for(Transform2D* child : children) {
|
||||||
|
child->UpdatePosition(window, *this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Transform2D::UpdatePosition(Rendertarget& window, Transform2D& parent) {
|
||||||
|
ScaleElement(parent);
|
||||||
|
for(Transform2D* child : children) {
|
||||||
|
child->UpdatePosition(window, *this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Transform2D::ScaleElement(Transform2D& parent) {
|
||||||
|
if(anchor.maintainAspectRatio) {
|
||||||
|
if(parent.scaled.size.x > parent.scaled.size.y) {
|
||||||
|
scaled.size.x = anchor.width * parent.scaled.size.y;
|
||||||
|
scaled.size.y = anchor.height * parent.scaled.size.y;
|
||||||
|
} else {
|
||||||
|
scaled.size.x = anchor.width * parent.scaled.size.x;
|
||||||
|
scaled.size.y = anchor.height * parent.scaled.size.x;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
scaled.size.x = anchor.width * parent.scaled.size.x;
|
||||||
|
scaled.size.y = anchor.height * parent.scaled.size.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
scaled.position.x = parent.scaled.position.x + (anchor.x * parent.scaled.size.x - anchor.offsetX * scaled.size.x);
|
||||||
|
scaled.position.y = parent.scaled.position.y + (anchor.y * parent.scaled.size.y - anchor.offsetY * scaled.size.y);
|
||||||
|
}
|
||||||
|
|
@ -1,337 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2026 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 version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
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 "vulkan/vulkan.h"
|
|
||||||
#include "vulkan/vk_enum_string_helper.h"
|
|
||||||
|
|
||||||
#define GET_EXTENSION_FUNCTION(_id) ((PFN_##_id)(vkGetInstanceProcAddr(instance, #_id)))
|
|
||||||
|
|
||||||
module Crafter.Graphics:VulkanDevice_impl;
|
|
||||||
import :VulkanDevice;
|
|
||||||
import std;
|
|
||||||
|
|
||||||
|
|
||||||
using namespace Crafter;
|
|
||||||
const char* const instanceExtensionNames[] = {
|
|
||||||
"VK_EXT_debug_utils",
|
|
||||||
"VK_KHR_surface",
|
|
||||||
#ifdef CRAFTER_BUILD_CONFIGURATION_TARGET_x86_64_w64_mingw32
|
|
||||||
"VK_KHR_win32_surface"
|
|
||||||
#else
|
|
||||||
"VK_KHR_wayland_surface"
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
const char* const deviceExtensionNames[] = {
|
|
||||||
"VK_KHR_swapchain",
|
|
||||||
"VK_KHR_spirv_1_4",
|
|
||||||
"VK_EXT_mesh_shader",
|
|
||||||
"VK_KHR_shader_float_controls",
|
|
||||||
"VK_KHR_dynamic_rendering",
|
|
||||||
"VK_KHR_acceleration_structure",
|
|
||||||
"VK_KHR_deferred_host_operations",
|
|
||||||
"VK_KHR_ray_tracing_pipeline",
|
|
||||||
"VK_KHR_ray_tracing_position_fetch"
|
|
||||||
};
|
|
||||||
const char* const layerNames[] = {
|
|
||||||
"VK_LAYER_KHRONOS_validation"
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
void VulkanDevice::CheckVkResult(VkResult result) {
|
|
||||||
if (result != VK_SUCCESS)
|
|
||||||
{
|
|
||||||
throw std::runtime_error(string_VkResult(result));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VkBool32 onError(VkDebugUtilsMessageSeverityFlagBitsEXT severity, VkDebugUtilsMessageTypeFlagsEXT type, const VkDebugUtilsMessengerCallbackDataEXT* callbackData, void* userData)
|
|
||||||
{
|
|
||||||
printf("Vulkan ");
|
|
||||||
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT :
|
|
||||||
printf("general ");
|
|
||||||
break;
|
|
||||||
case VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT :
|
|
||||||
printf("validation ");
|
|
||||||
break;
|
|
||||||
case VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT :
|
|
||||||
printf("performance ");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (severity)
|
|
||||||
{
|
|
||||||
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT :
|
|
||||||
printf("(verbose): ");
|
|
||||||
break;
|
|
||||||
default :
|
|
||||||
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT :
|
|
||||||
printf("(info): ");
|
|
||||||
break;
|
|
||||||
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT :
|
|
||||||
printf("(warning): ");
|
|
||||||
break;
|
|
||||||
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT :
|
|
||||||
printf("(error): ");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%s\n", callbackData->pMessage);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void VulkanDevice::CreateDevice() {
|
|
||||||
VkApplicationInfo app{VK_STRUCTURE_TYPE_APPLICATION_INFO};
|
|
||||||
app.pApplicationName = "";
|
|
||||||
app.pEngineName = "Crafter.Graphics";
|
|
||||||
app.apiVersion = VK_MAKE_VERSION(1, 4, 0);
|
|
||||||
|
|
||||||
VkInstanceCreateInfo instanceCreateInfo = {};
|
|
||||||
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
||||||
instanceCreateInfo.pApplicationInfo = &app;
|
|
||||||
instanceCreateInfo.enabledExtensionCount = sizeof(instanceExtensionNames) / sizeof(const char*);
|
|
||||||
instanceCreateInfo.ppEnabledExtensionNames = instanceExtensionNames;
|
|
||||||
|
|
||||||
size_t foundInstanceLayers = 0;
|
|
||||||
|
|
||||||
std::uint32_t instanceLayerCount;
|
|
||||||
CheckVkResult(vkEnumerateInstanceLayerProperties(&instanceLayerCount, NULL));
|
|
||||||
|
|
||||||
std::vector<VkLayerProperties> instanceLayerProperties(instanceLayerCount);
|
|
||||||
CheckVkResult(vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProperties.data()));
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < instanceLayerCount; i++)
|
|
||||||
{
|
|
||||||
for (size_t j = 0; j < sizeof(layerNames) / sizeof(const char*); j++)
|
|
||||||
{
|
|
||||||
if (std::strcmp(instanceLayerProperties[i].layerName, layerNames[j]) == 0)
|
|
||||||
{
|
|
||||||
foundInstanceLayers++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (foundInstanceLayers >= sizeof(layerNames) / sizeof(const char*))
|
|
||||||
{
|
|
||||||
instanceCreateInfo.enabledLayerCount = sizeof(layerNames) / sizeof(const char*);
|
|
||||||
instanceCreateInfo.ppEnabledLayerNames = layerNames;
|
|
||||||
}
|
|
||||||
|
|
||||||
CheckVkResult(vkCreateInstance(&instanceCreateInfo, NULL, &instance));
|
|
||||||
|
|
||||||
VkDebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfo = {};
|
|
||||||
debugUtilsMessengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
|
||||||
debugUtilsMessengerCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
|
||||||
debugUtilsMessengerCreateInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
|
||||||
debugUtilsMessengerCreateInfo.pfnUserCallback = onError;
|
|
||||||
CheckVkResult(GET_EXTENSION_FUNCTION(vkCreateDebugUtilsMessengerEXT)(instance, &debugUtilsMessengerCreateInfo, NULL, &debugMessenger));
|
|
||||||
|
|
||||||
uint32_t physDeviceCount;
|
|
||||||
vkEnumeratePhysicalDevices(instance, &physDeviceCount, NULL);
|
|
||||||
|
|
||||||
std::vector<VkPhysicalDevice> physDevices(physDeviceCount);
|
|
||||||
vkEnumeratePhysicalDevices(instance, &physDeviceCount, physDevices.data());
|
|
||||||
|
|
||||||
uint32_t bestScore = 0;
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < physDeviceCount; i++)
|
|
||||||
{
|
|
||||||
VkPhysicalDevice device = physDevices[i];
|
|
||||||
|
|
||||||
VkPhysicalDeviceProperties2 properties2{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
|
|
||||||
.pNext = &rayTracingProperties
|
|
||||||
};
|
|
||||||
vkGetPhysicalDeviceProperties2(device, &properties2);
|
|
||||||
|
|
||||||
VkPhysicalDeviceProperties properties;
|
|
||||||
vkGetPhysicalDeviceProperties(device, &properties);
|
|
||||||
|
|
||||||
uint32_t score;
|
|
||||||
|
|
||||||
switch (properties.deviceType)
|
|
||||||
{
|
|
||||||
default :
|
|
||||||
continue;
|
|
||||||
case VK_PHYSICAL_DEVICE_TYPE_OTHER :
|
|
||||||
score = 1;
|
|
||||||
break;
|
|
||||||
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU :
|
|
||||||
score = 4;
|
|
||||||
break;
|
|
||||||
case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU :
|
|
||||||
score = 5;
|
|
||||||
break;
|
|
||||||
case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU :
|
|
||||||
score = 3;
|
|
||||||
break;
|
|
||||||
case VK_PHYSICAL_DEVICE_TYPE_CPU :
|
|
||||||
score = 2;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (score > bestScore)
|
|
||||||
{
|
|
||||||
physDevice = device;
|
|
||||||
bestScore = score;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
uint32_t queueFamilyCount;
|
|
||||||
vkGetPhysicalDeviceQueueFamilyProperties(physDevice, &queueFamilyCount, NULL);
|
|
||||||
|
|
||||||
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
|
||||||
vkGetPhysicalDeviceQueueFamilyProperties(physDevice, &queueFamilyCount, queueFamilies.data());
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < queueFamilyCount; i++)
|
|
||||||
{
|
|
||||||
if (queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
|
||||||
{
|
|
||||||
queueFamilyIndex = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
float priority = 1;
|
|
||||||
|
|
||||||
VkDeviceQueueCreateInfo queueCreateInfo = {};
|
|
||||||
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|
||||||
queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
|
|
||||||
queueCreateInfo.queueCount = 1;
|
|
||||||
queueCreateInfo.pQueuePriorities = &priority;
|
|
||||||
|
|
||||||
VkPhysicalDeviceVulkan12Features features12 {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
|
|
||||||
.runtimeDescriptorArray = VK_TRUE,
|
|
||||||
.bufferDeviceAddress = VK_TRUE
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPhysicalDeviceRayTracingPositionFetchFeaturesKHR vkPhysicalDeviceRayTracingPositionFetchFeatures {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_POSITION_FETCH_FEATURES_KHR,
|
|
||||||
.pNext = &features12,
|
|
||||||
.rayTracingPositionFetch = VK_TRUE
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPhysicalDeviceRayTracingPipelineFeaturesKHR physicalDeviceRayTracingPipelineFeatures{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR,
|
|
||||||
.pNext = &vkPhysicalDeviceRayTracingPositionFetchFeatures,
|
|
||||||
.rayTracingPipeline = VK_TRUE
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPhysicalDeviceAccelerationStructureFeaturesKHR deviceAccelerationStructureFeature = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR,
|
|
||||||
.pNext = &physicalDeviceRayTracingPipelineFeatures,
|
|
||||||
.accelerationStructure = VK_TRUE
|
|
||||||
};
|
|
||||||
|
|
||||||
VkPhysicalDeviceDynamicRenderingFeaturesKHR dynamicRenderingFeature = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES_KHR};
|
|
||||||
dynamicRenderingFeature.dynamicRendering = VK_FALSE;
|
|
||||||
dynamicRenderingFeature.pNext = &deviceAccelerationStructureFeature;
|
|
||||||
|
|
||||||
VkPhysicalDeviceMeshShaderFeaturesEXT ext_feature = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT};
|
|
||||||
ext_feature.meshShader = VK_FALSE;
|
|
||||||
ext_feature.pNext = &dynamicRenderingFeature;
|
|
||||||
|
|
||||||
VkPhysicalDeviceFeatures2 physical_features2 = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
|
|
||||||
.features = {
|
|
||||||
.samplerAnisotropy = VK_TRUE
|
|
||||||
}
|
|
||||||
};
|
|
||||||
physical_features2.pNext = &ext_feature;
|
|
||||||
|
|
||||||
VkDeviceCreateInfo deviceCreateInfo = {};
|
|
||||||
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
|
||||||
deviceCreateInfo.queueCreateInfoCount = 1;
|
|
||||||
deviceCreateInfo.pQueueCreateInfos = &queueCreateInfo;
|
|
||||||
deviceCreateInfo.enabledExtensionCount = sizeof(deviceExtensionNames) / sizeof(const char*);
|
|
||||||
deviceCreateInfo.ppEnabledExtensionNames = deviceExtensionNames;
|
|
||||||
deviceCreateInfo.pNext = &physical_features2;
|
|
||||||
|
|
||||||
uint32_t deviceLayerCount;
|
|
||||||
CheckVkResult(vkEnumerateDeviceLayerProperties(physDevice, &deviceLayerCount, NULL));
|
|
||||||
|
|
||||||
std::vector<VkLayerProperties> deviceLayerProperties(deviceLayerCount);
|
|
||||||
CheckVkResult(vkEnumerateDeviceLayerProperties(physDevice, &deviceLayerCount, deviceLayerProperties.data()));
|
|
||||||
|
|
||||||
size_t foundDeviceLayers = 0;
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < deviceLayerCount; i++)
|
|
||||||
{
|
|
||||||
for (size_t j = 0; j < sizeof(layerNames) / sizeof(const char*); j++)
|
|
||||||
{
|
|
||||||
if (std::strcmp(deviceLayerProperties[i].layerName, layerNames[j]) == 0)
|
|
||||||
{
|
|
||||||
foundDeviceLayers++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (foundDeviceLayers >= sizeof(layerNames) / sizeof(const char*))
|
|
||||||
{
|
|
||||||
deviceCreateInfo.enabledLayerCount = sizeof(layerNames) / sizeof(const char*);
|
|
||||||
deviceCreateInfo.ppEnabledLayerNames = layerNames;
|
|
||||||
}
|
|
||||||
|
|
||||||
CheckVkResult(vkCreateDevice(physDevice, &deviceCreateInfo, NULL, &device));
|
|
||||||
vkGetDeviceQueue(device, queueFamilyIndex, 0, &queue);
|
|
||||||
|
|
||||||
VkCommandPoolCreateInfo commandPoolcreateInfo = {};
|
|
||||||
commandPoolcreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
|
||||||
commandPoolcreateInfo.queueFamilyIndex = queueFamilyIndex;
|
|
||||||
commandPoolcreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
|
||||||
CheckVkResult(vkCreateCommandPool(device, &commandPoolcreateInfo, NULL, &commandPool));
|
|
||||||
|
|
||||||
vkGetPhysicalDeviceMemoryProperties(physDevice, &memoryProperties);
|
|
||||||
|
|
||||||
// vkCmdDrawMeshTasksEXTProc = reinterpret_cast<PFN_vkCmdDrawMeshTasksEXT>(vkGetDeviceProcAddr(device, "vkCmdDrawMeshTasksEXT"));
|
|
||||||
// vkCmdBeginRenderingKHRProc = reinterpret_cast<PFN_vkCmdBeginRenderingKHR>(vkGetInstanceProcAddr(instance, "vkCmdBeginRenderingKHR"));
|
|
||||||
// vkCmdEndRenderingKHRProc = reinterpret_cast<PFN_vkCmdEndRenderingKHR>(vkGetInstanceProcAddr(instance, "vkCmdEndRenderingKHR"));
|
|
||||||
vkGetAccelerationStructureBuildSizesKHR = reinterpret_cast<PFN_vkGetAccelerationStructureBuildSizesKHR>(vkGetInstanceProcAddr(instance, "vkGetAccelerationStructureBuildSizesKHR"));
|
|
||||||
vkCreateAccelerationStructureKHR = reinterpret_cast<PFN_vkCreateAccelerationStructureKHR>(vkGetInstanceProcAddr(instance, "vkCreateAccelerationStructureKHR"));
|
|
||||||
vkCmdBuildAccelerationStructuresKHR = reinterpret_cast<PFN_vkCmdBuildAccelerationStructuresKHR>(vkGetInstanceProcAddr(instance, "vkCmdBuildAccelerationStructuresKHR"));
|
|
||||||
vkGetAccelerationStructureDeviceAddressKHR = reinterpret_cast<PFN_vkGetAccelerationStructureDeviceAddressKHR>(vkGetInstanceProcAddr(instance, "vkGetAccelerationStructureDeviceAddressKHR"));
|
|
||||||
vkCreateRayTracingPipelinesKHR = reinterpret_cast<PFN_vkCreateRayTracingPipelinesKHR>(vkGetInstanceProcAddr(instance, "vkCreateRayTracingPipelinesKHR"));
|
|
||||||
vkGetRayTracingShaderGroupHandlesKHR = reinterpret_cast<PFN_vkGetRayTracingShaderGroupHandlesKHR>(vkGetInstanceProcAddr(instance, "vkGetRayTracingShaderGroupHandlesKHR"));
|
|
||||||
vkCmdTraceRaysKHR = reinterpret_cast<PFN_vkCmdTraceRaysKHR>(vkGetInstanceProcAddr(instance, "vkCmdTraceRaysKHR"));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::uint32_t VulkanDevice::GetMemoryType(uint32_t typeBits, VkMemoryPropertyFlags properties) {
|
|
||||||
for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++)
|
|
||||||
{
|
|
||||||
if ((typeBits & 1) == 1)
|
|
||||||
{
|
|
||||||
if ((memoryProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
|
||||||
{
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
typeBits >>= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw std::runtime_error("Could not find a matching memory type");
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,860 +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 version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
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 <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <linux/input-event-codes.h>
|
|
||||||
#include "../lib/xdg-shell-client-protocol.h"
|
|
||||||
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
|
|
||||||
#include "../lib/fractional-scale-v1.h"
|
|
||||||
#include "../lib/viewporter.h"
|
|
||||||
#include <string.h>
|
|
||||||
#include <linux/input.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <wayland-cursor.h>
|
|
||||||
#include <xkbcommon/xkbcommon.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <print>
|
|
||||||
#include <wayland-client.h>
|
|
||||||
#include <wayland-client-protocol.h>
|
|
||||||
#include "vulkan/vulkan.h"
|
|
||||||
#include "vulkan/vulkan_wayland.h"
|
|
||||||
|
|
||||||
module Crafter.Graphics:Window_vulkan_impl;
|
|
||||||
import :Window;
|
|
||||||
import :MouseElement;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import :Shm;
|
|
||||||
import :VulkanDevice;
|
|
||||||
import :VulkanTransition;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
using namespace Crafter;
|
|
||||||
|
|
||||||
|
|
||||||
void WindowVulkan::CreateSwapchain()
|
|
||||||
{
|
|
||||||
// Store the current swap chain handle so we can use it later on to ease up recreation
|
|
||||||
VkSwapchainKHR oldSwapchain = swapChain;
|
|
||||||
|
|
||||||
// Get physical device surface properties and formats
|
|
||||||
VkSurfaceCapabilitiesKHR surfCaps;
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VulkanDevice::physDevice, vulkanSurface, &surfCaps));
|
|
||||||
|
|
||||||
VkExtent2D swapchainExtent = {};
|
|
||||||
// If width (and height) equals the special value 0xFFFFFFFF, the size of the surface will be set by the swapchain
|
|
||||||
if (surfCaps.currentExtent.width == (uint32_t)-1)
|
|
||||||
{
|
|
||||||
// If the surface size is undefined, the size is set to the size of the images requested
|
|
||||||
swapchainExtent.width = width;
|
|
||||||
swapchainExtent.height = height;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// If the surface size is defined, the swap chain size must match
|
|
||||||
swapchainExtent = surfCaps.currentExtent;
|
|
||||||
width = surfCaps.currentExtent.width;
|
|
||||||
height = surfCaps.currentExtent.height;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Select a present mode for the swapchain
|
|
||||||
uint32_t presentModeCount;
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfacePresentModesKHR(VulkanDevice::physDevice, vulkanSurface, &presentModeCount, NULL));
|
|
||||||
assert(presentModeCount > 0);
|
|
||||||
|
|
||||||
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfacePresentModesKHR(VulkanDevice::physDevice, vulkanSurface, &presentModeCount, presentModes.data()));
|
|
||||||
|
|
||||||
// The VK_PRESENT_MODE_FIFO_KHR mode must always be present as per spec
|
|
||||||
// This mode waits for the vertical blank ("v-sync")
|
|
||||||
VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
|
|
||||||
|
|
||||||
// Find the transformation of the surface
|
|
||||||
VkSurfaceTransformFlagsKHR preTransform;
|
|
||||||
if (surfCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
|
|
||||||
{
|
|
||||||
// We prefer a non-rotated transform
|
|
||||||
preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
preTransform = surfCaps.currentTransform;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find a supported composite alpha format (not all devices support alpha opaque)
|
|
||||||
VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
||||||
// Simply select the first composite alpha format available
|
|
||||||
std::vector<VkCompositeAlphaFlagBitsKHR> compositeAlphaFlags = {
|
|
||||||
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
|
||||||
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
|
|
||||||
VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
|
|
||||||
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
|
|
||||||
};
|
|
||||||
for (auto& compositeAlphaFlag : compositeAlphaFlags) {
|
|
||||||
if (surfCaps.supportedCompositeAlpha & compositeAlphaFlag) {
|
|
||||||
compositeAlpha = compositeAlphaFlag;
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
VkSwapchainCreateInfoKHR swapchainCI = {};
|
|
||||||
swapchainCI.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
|
||||||
swapchainCI.surface = vulkanSurface;
|
|
||||||
swapchainCI.minImageCount = numFrames;
|
|
||||||
swapchainCI.imageFormat = colorFormat;
|
|
||||||
swapchainCI.imageColorSpace = colorSpace;
|
|
||||||
swapchainCI.imageExtent = { swapchainExtent.width, swapchainExtent.height };
|
|
||||||
swapchainCI.imageUsage = VK_IMAGE_USAGE_STORAGE_BIT;
|
|
||||||
swapchainCI.preTransform = (VkSurfaceTransformFlagBitsKHR)preTransform;
|
|
||||||
swapchainCI.imageArrayLayers = 1;
|
|
||||||
swapchainCI.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
||||||
swapchainCI.queueFamilyIndexCount = 0;
|
|
||||||
swapchainCI.presentMode = swapchainPresentMode;
|
|
||||||
// Setting oldSwapChain to the saved handle of the previous swapchain aids in resource reuse and makes sure that we can still present already acquired images
|
|
||||||
swapchainCI.oldSwapchain = oldSwapchain;
|
|
||||||
// Setting clipped to VK_TRUE allows the implementation to discard rendering outside of the surface area
|
|
||||||
swapchainCI.clipped = VK_TRUE;
|
|
||||||
swapchainCI.compositeAlpha = compositeAlpha;
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateSwapchainKHR(VulkanDevice::device, &swapchainCI, nullptr, &swapChain));
|
|
||||||
|
|
||||||
// If an existing swap chain is re-created, destroy the old swap chain and the ressources owned by the application (image views, images are owned by the swap chain)
|
|
||||||
if (oldSwapchain != VK_NULL_HANDLE) {
|
|
||||||
for (auto i = 0; i < numFrames; i++) {
|
|
||||||
vkDestroyImageView(VulkanDevice::device, imageViews[i], nullptr);
|
|
||||||
}
|
|
||||||
vkDestroySwapchainKHR(VulkanDevice::device, oldSwapchain, nullptr);
|
|
||||||
}
|
|
||||||
uint32_t imageCount{ 0 };
|
|
||||||
VulkanDevice::CheckVkResult(vkGetSwapchainImagesKHR(VulkanDevice::device, swapChain, &imageCount, nullptr));
|
|
||||||
|
|
||||||
// Get the swap chain images
|
|
||||||
VulkanDevice::CheckVkResult(vkGetSwapchainImagesKHR(VulkanDevice::device, swapChain, &imageCount, images));
|
|
||||||
|
|
||||||
for (auto i = 0; i < numFrames; i++)
|
|
||||||
{
|
|
||||||
VkImageViewCreateInfo colorAttachmentView = {};
|
|
||||||
colorAttachmentView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
||||||
colorAttachmentView.pNext = NULL;
|
|
||||||
colorAttachmentView.format = colorFormat;
|
|
||||||
colorAttachmentView.components = {
|
|
||||||
VK_COMPONENT_SWIZZLE_R,
|
|
||||||
VK_COMPONENT_SWIZZLE_G,
|
|
||||||
VK_COMPONENT_SWIZZLE_B,
|
|
||||||
VK_COMPONENT_SWIZZLE_A
|
|
||||||
};
|
|
||||||
colorAttachmentView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
colorAttachmentView.subresourceRange.baseMipLevel = 0;
|
|
||||||
colorAttachmentView.subresourceRange.levelCount = 1;
|
|
||||||
colorAttachmentView.subresourceRange.baseArrayLayer = 0;
|
|
||||||
colorAttachmentView.subresourceRange.layerCount = 1;
|
|
||||||
colorAttachmentView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
||||||
colorAttachmentView.flags = 0;
|
|
||||||
colorAttachmentView.image = images[i];
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateImageView(VulkanDevice::device, &colorAttachmentView, nullptr, &imageViews[i]));
|
|
||||||
|
|
||||||
VkImageSubresourceRange range{};
|
|
||||||
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
range.baseMipLevel = 0;
|
|
||||||
range.levelCount = VK_REMAINING_MIP_LEVELS;
|
|
||||||
range.baseArrayLayer = 0;
|
|
||||||
range.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowVulkan::WindowVulkan(std::uint32_t width, std::uint32_t height) : Window(width, height) {
|
|
||||||
|
|
||||||
display = wl_display_connect(NULL);
|
|
||||||
if (display == NULL) {
|
|
||||||
std::cerr << "failed to create display" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
wl_registry* registry = wl_display_get_registry(display);
|
|
||||||
wl_registry_add_listener(registry, ®istry_listener, this);
|
|
||||||
if (wl_display_roundtrip(display) == -1) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (shm == NULL || compositor == NULL || xdgWmBase == NULL) {
|
|
||||||
std::cerr << "no wl_shm, wl_compositor or xdg_wm_base support" << std::endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
surface = wl_compositor_create_surface(compositor);
|
|
||||||
xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface);
|
|
||||||
xdgToplevel = xdg_surface_get_toplevel(xdgSurface);
|
|
||||||
|
|
||||||
xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this);
|
|
||||||
xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this);
|
|
||||||
wl_surface_commit(surface);
|
|
||||||
|
|
||||||
wp_scale = wp_fractional_scale_manager_v1_get_fractional_scale(fractionalScaleManager, surface);
|
|
||||||
wp_fractional_scale_v1_add_listener(wp_scale, &wp_fractional_scale_v1_listener, this);
|
|
||||||
|
|
||||||
while (wl_display_dispatch(display) != -1 && !configured) {}
|
|
||||||
|
|
||||||
wl_surface_commit(surface);
|
|
||||||
|
|
||||||
zxdg_toplevel_decoration_v1* decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, xdgToplevel);
|
|
||||||
zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
|
|
||||||
|
|
||||||
wpViewport = wp_viewporter_get_viewport(wpViewporter, surface);
|
|
||||||
wp_viewport_set_destination(wpViewport, std::ceil(width/scale), std::ceil(height/scale));
|
|
||||||
|
|
||||||
wl_surface_commit(surface);
|
|
||||||
|
|
||||||
VkWaylandSurfaceCreateInfoKHR createInfo = {};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
|
|
||||||
createInfo.display = display;
|
|
||||||
createInfo.surface = surface;
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateWaylandSurfaceKHR(VulkanDevice::instance, &createInfo, NULL, &vulkanSurface));
|
|
||||||
|
|
||||||
// Get list of supported surface formats
|
|
||||||
std::uint32_t formatCount;
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(VulkanDevice::physDevice, vulkanSurface, &formatCount, NULL));
|
|
||||||
assert(formatCount > 0);
|
|
||||||
|
|
||||||
std::vector<VkSurfaceFormatKHR> surfaceFormats(formatCount);
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(VulkanDevice::physDevice, vulkanSurface, &formatCount, surfaceFormats.data()));
|
|
||||||
|
|
||||||
// We want to get a format that best suits our needs, so we try to get one from a set of preferred formats
|
|
||||||
// Initialize the format to the first one returned by the implementation in case we can't find one of the preffered formats
|
|
||||||
VkSurfaceFormatKHR selectedFormat = surfaceFormats[0];
|
|
||||||
std::vector<VkFormat> preferredImageFormats = {
|
|
||||||
VK_FORMAT_R8G8B8A8_UNORM,
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto& availableFormat : surfaceFormats) {
|
|
||||||
if (std::find(preferredImageFormats.begin(), preferredImageFormats.end(), availableFormat.format) != preferredImageFormats.end()) {
|
|
||||||
selectedFormat = availableFormat;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
colorFormat = selectedFormat.format;
|
|
||||||
colorSpace = selectedFormat.colorSpace;
|
|
||||||
|
|
||||||
CreateSwapchain();
|
|
||||||
|
|
||||||
VkCommandBufferAllocateInfo cmdBufAllocateInfo {};
|
|
||||||
cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
||||||
cmdBufAllocateInfo.commandPool = VulkanDevice::commandPool;
|
|
||||||
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
||||||
cmdBufAllocateInfo.commandBufferCount = numFrames;
|
|
||||||
VulkanDevice::CheckVkResult(vkAllocateCommandBuffers(VulkanDevice::device, &cmdBufAllocateInfo, drawCmdBuffers));
|
|
||||||
|
|
||||||
VkSemaphoreCreateInfo semaphoreCreateInfo {};
|
|
||||||
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateSemaphore(VulkanDevice::device, &semaphoreCreateInfo, nullptr, &semaphores.presentComplete));
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateSemaphore(VulkanDevice::device, &semaphoreCreateInfo, nullptr, &semaphores.renderComplete));
|
|
||||||
|
|
||||||
// Set up submit info structure
|
|
||||||
// Semaphores will stay the same during application lifetime
|
|
||||||
// Command buffer submission info is set by each example
|
|
||||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
||||||
submitInfo.pWaitDstStageMask = &submitPipelineStages;
|
|
||||||
submitInfo.waitSemaphoreCount = 1;
|
|
||||||
submitInfo.pWaitSemaphores = &semaphores.presentComplete;
|
|
||||||
submitInfo.signalSemaphoreCount = 1;
|
|
||||||
submitInfo.pSignalSemaphores = &semaphores.renderComplete;
|
|
||||||
submitInfo.pNext = VK_NULL_HANDLE;
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowVulkan::WindowVulkan(std::uint32_t width, std::uint32_t height, const std::string_view title) : WindowVulkan(width, height) {
|
|
||||||
xdg_toplevel_set_title(xdgToplevel, title.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowVulkan::~WindowVulkan() {
|
|
||||||
xdg_toplevel_destroy(xdgToplevel);
|
|
||||||
xdg_surface_destroy(xdgSurface);
|
|
||||||
wl_surface_destroy(surface);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::StartSync() {
|
|
||||||
while (open && wl_display_dispatch(display) != -1) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::Render() {
|
|
||||||
// Acquire the next image from the swap chain
|
|
||||||
VulkanDevice::CheckVkResult(vkAcquireNextImageKHR(VulkanDevice::device, swapChain, UINT64_MAX, semaphores.presentComplete, (VkFence)nullptr, ¤tBuffer));
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
||||||
|
|
||||||
VkCommandBufferBeginInfo cmdBufInfo {};
|
|
||||||
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkBeginCommandBuffer(drawCmdBuffers[currentBuffer], &cmdBufInfo));
|
|
||||||
|
|
||||||
VkImageSubresourceRange range{};
|
|
||||||
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
range.baseMipLevel = 0;
|
|
||||||
range.levelCount = VK_REMAINING_MIP_LEVELS;
|
|
||||||
range.baseArrayLayer = 0;
|
|
||||||
range.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
|
||||||
|
|
||||||
VkImageMemoryBarrier image_memory_barrier {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
||||||
.srcAccessMask = 0,
|
|
||||||
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
|
|
||||||
.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
||||||
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
||||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.image = images[currentBuffer],
|
|
||||||
.subresourceRange = range
|
|
||||||
};
|
|
||||||
|
|
||||||
vkCmdPipelineBarrier(drawCmdBuffers[currentBuffer], VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier);
|
|
||||||
|
|
||||||
onRender.Invoke();
|
|
||||||
|
|
||||||
vkCmdBindPipeline(drawCmdBuffers[currentBuffer], VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, rtPipeline);
|
|
||||||
VkBindDescriptorSetsInfo bindDescriptorSetsInfo{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_BIND_DESCRIPTOR_SETS_INFO,
|
|
||||||
.stageFlags = VK_SHADER_STAGE_ALL,
|
|
||||||
.layout = rtPipelineLayout,
|
|
||||||
.firstSet = 0,
|
|
||||||
.descriptorSetCount = static_cast<std::uint32_t>(descriptorsRt.size()),
|
|
||||||
.pDescriptorSets = descriptorsRt.data()
|
|
||||||
};
|
|
||||||
|
|
||||||
vkCmdBindDescriptorSets2(drawCmdBuffers[currentBuffer], &bindDescriptorSetsInfo);
|
|
||||||
VulkanDevice::vkCmdTraceRaysKHR(drawCmdBuffers[currentBuffer], &raygenRegion, &missRegion, &hitRegion, &callableRegion, width, height, 1);
|
|
||||||
|
|
||||||
|
|
||||||
VkImageMemoryBarrier image_memory_barrier2 {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
||||||
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
|
|
||||||
.dstAccessMask = 0,
|
|
||||||
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
||||||
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
||||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.image = images[currentBuffer],
|
|
||||||
.subresourceRange = range
|
|
||||||
};
|
|
||||||
|
|
||||||
vkCmdPipelineBarrier(drawCmdBuffers[currentBuffer], VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier2);
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkEndCommandBuffer(drawCmdBuffers[currentBuffer]));
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkQueueSubmit(VulkanDevice::queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
||||||
VkPresentInfoKHR presentInfo = {};
|
|
||||||
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
|
||||||
presentInfo.pNext = NULL;
|
|
||||||
presentInfo.swapchainCount = 1;
|
|
||||||
presentInfo.pSwapchains = &swapChain;
|
|
||||||
presentInfo.pImageIndices = ¤tBuffer;
|
|
||||||
// Check if a wait semaphore has been specified to wait for before presenting the image
|
|
||||||
if (semaphores.renderComplete != VK_NULL_HANDLE)
|
|
||||||
{
|
|
||||||
presentInfo.pWaitSemaphores = &semaphores.renderComplete;
|
|
||||||
presentInfo.waitSemaphoreCount = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkResult result = vkQueuePresentKHR(VulkanDevice::queue, &presentInfo);
|
|
||||||
if(result == VK_SUBOPTIMAL_KHR) {
|
|
||||||
CreateSwapchain();
|
|
||||||
} else {
|
|
||||||
VulkanDevice::CheckVkResult(result);
|
|
||||||
}
|
|
||||||
VulkanDevice::CheckVkResult(vkQueueWaitIdle(VulkanDevice::queue));
|
|
||||||
}
|
|
||||||
|
|
||||||
VkCommandBuffer WindowVulkan::StartInit() {
|
|
||||||
VkCommandBufferBeginInfo cmdBufInfo {};
|
|
||||||
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
||||||
VulkanDevice::CheckVkResult(vkBeginCommandBuffer(drawCmdBuffers[currentBuffer], &cmdBufInfo));
|
|
||||||
|
|
||||||
VkImageSubresourceRange range{};
|
|
||||||
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
range.baseMipLevel = 0;
|
|
||||||
range.levelCount = VK_REMAINING_MIP_LEVELS;
|
|
||||||
range.baseArrayLayer = 0;
|
|
||||||
range.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
|
||||||
|
|
||||||
for(std::uint32_t i = 0; i < numFrames; i++) {
|
|
||||||
image_layout_transition(drawCmdBuffers[currentBuffer],
|
|
||||||
images[i],
|
|
||||||
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
||||||
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
||||||
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
||||||
range
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return drawCmdBuffers[currentBuffer];
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::FinishInit() {
|
|
||||||
VkSubmitInfo submitInfo{};
|
|
||||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
||||||
VulkanDevice::CheckVkResult(vkEndCommandBuffer(drawCmdBuffers[currentBuffer]));
|
|
||||||
VulkanDevice::CheckVkResult(vkQueueSubmit(VulkanDevice::queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
||||||
VulkanDevice::CheckVkResult(vkQueueWaitIdle(VulkanDevice::queue));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void WindowVulkan::QueueRender() {
|
|
||||||
if(cb == nullptr) {
|
|
||||||
cb = wl_surface_frame(surface);
|
|
||||||
wl_callback_add_listener(cb, &wl_callback_listener, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::StartUpdate() {
|
|
||||||
lastFrameBegin = std::chrono::high_resolution_clock::now();
|
|
||||||
cb = wl_surface_frame(surface);
|
|
||||||
wl_callback_add_listener(cb, &wl_callback_listener, this);
|
|
||||||
updating = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::StopUpdate() {
|
|
||||||
updating = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::SetTitle(const std::string_view title) {
|
|
||||||
xdg_toplevel_set_title(xdgToplevel, title.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::Resize(std::uint32_t width, std::uint32_t height) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial) {
|
|
||||||
xdg_wm_base_pong(xdg_wm_base, serial);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> framEnd;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void WindowVulkan::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time)
|
|
||||||
{
|
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
|
||||||
wl_callback_destroy(cb);
|
|
||||||
cb = nullptr;
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
window->vblank = duration_cast<std::chrono::milliseconds>(start - window->frameEnd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(window->updating) {
|
|
||||||
window->mouseDelta = {std::int64_t(window->currentMousePos.x)-window->lastMousePos.x, std::int64_t(window->currentMousePos.y)-window->lastMousePos.y};
|
|
||||||
cb = wl_surface_frame(window->surface);
|
|
||||||
wl_callback_add_listener(cb, &WindowVulkan::wl_callback_listener, window);
|
|
||||||
window->currentFrameTime = {start, start-window->lastFrameBegin};
|
|
||||||
window->onUpdate.Invoke({start, start-window->lastFrameBegin});
|
|
||||||
window->lastMousePos = window->currentMousePos;
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
window->totalUpdate = std::chrono::nanoseconds(0);
|
|
||||||
window->updateTimings.clear();
|
|
||||||
for (const std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>& entry : window->onUpdate.listenerTimes) {
|
|
||||||
window->updateTimings.push_back(entry);
|
|
||||||
window->totalUpdate += entry.second;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
auto renderStart = std::chrono::high_resolution_clock::now();
|
|
||||||
window->renderTimings.clear();
|
|
||||||
#endif
|
|
||||||
window->Render();
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
auto renderEnd = std::chrono::high_resolution_clock::now();
|
|
||||||
window->totalRender = renderEnd - renderStart;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
window->frameEnd = std::chrono::high_resolution_clock::now();
|
|
||||||
|
|
||||||
window->frameTimes.push_back(window->totalUpdate+window->totalRender);
|
|
||||||
|
|
||||||
// Keep only the last 100 frame times
|
|
||||||
if (window->frameTimes.size() > 100) {
|
|
||||||
window->frameTimes.erase(window->frameTimes.begin());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
window->lastFrameBegin = start;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
|
|
||||||
if (button == BTN_LEFT) {
|
|
||||||
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
|
||||||
window->mouseLeftHeld = true;
|
|
||||||
window->onMouseLeftClick.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeftClick.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
window->mouseLeftHeld = false;
|
|
||||||
window->onMouseLeftRelease.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeftRelease.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if(button == BTN_RIGHT){
|
|
||||||
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
|
||||||
window->mouseRightHeld = true;
|
|
||||||
window->onMouseRightClick.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseRightClick.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
window->mouseRightHeld = false;
|
|
||||||
window->onMouseRightRelease.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseRightRelease.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
window->mouseElements.erase(std::remove(window->mouseElements.begin(), window->mouseElements.end(), static_cast<MouseElement*>(nullptr)), window->mouseElements.end());
|
|
||||||
window->mouseElements.insert(window->mouseElements.end(), window->pendingMouseElements.begin(), window->pendingMouseElements.end());
|
|
||||||
window->pendingMouseElements.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
MousePoint pos = {FractionalToMappedBoundless<std::uint32_t>((wl_fixed_to_double(surface_x) * window->scale) / window->width), FractionalToMappedBoundless<std::uint32_t>((wl_fixed_to_double(surface_y) * window->scale) / window->height)};
|
|
||||||
window->currentMousePos = pos;
|
|
||||||
window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseMove.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
if(!(window->lastMousePos.x >= element->mouseScaled.x && window->lastMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->lastMousePos.y > element->mouseScaled.y && window->lastMousePos.y < element->mouseScaled.y+element->mouseScaled.height)) {
|
|
||||||
element->onMouseEnter.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
} else if(window->lastMousePos.x >= element->mouseScaled.x && window->lastMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->lastMousePos.y > element->mouseScaled.y && window->lastMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeave.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
window->mouseElements.erase(std::remove(window->mouseElements.begin(), window->mouseElements.end(), static_cast<MouseElement*>(nullptr)), window->mouseElements.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
window->onMouseLeave.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void WindowVulkan::keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
|
|
||||||
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
|
|
||||||
close(fd);
|
|
||||||
fprintf(stderr, "Unsupported keymap format\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
|
||||||
if (map == MAP_FAILED) {
|
|
||||||
close(fd);
|
|
||||||
perror("mmap");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
window->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
||||||
window->xkb_keymap = xkb_keymap_new_from_string(window->xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS);
|
|
||||||
munmap(map, size);
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
window->xkb_state = xkb_state_new(window->xkb_keymap);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
CrafterKeys keysym_to_crafter_key(xkb_keysym_t sym)
|
|
||||||
{
|
|
||||||
switch (sym)
|
|
||||||
{
|
|
||||||
// Alphabet
|
|
||||||
case XKB_KEY_a: return CrafterKeys::A;
|
|
||||||
case XKB_KEY_b: return CrafterKeys::B;
|
|
||||||
case XKB_KEY_c: return CrafterKeys::C;
|
|
||||||
case XKB_KEY_d: return CrafterKeys::D;
|
|
||||||
case XKB_KEY_e: return CrafterKeys::E;
|
|
||||||
case XKB_KEY_f: return CrafterKeys::F;
|
|
||||||
case XKB_KEY_g: return CrafterKeys::G;
|
|
||||||
case XKB_KEY_h: return CrafterKeys::H;
|
|
||||||
case XKB_KEY_i: return CrafterKeys::I;
|
|
||||||
case XKB_KEY_j: return CrafterKeys::J;
|
|
||||||
case XKB_KEY_k: return CrafterKeys::K;
|
|
||||||
case XKB_KEY_l: return CrafterKeys::L;
|
|
||||||
case XKB_KEY_m: return CrafterKeys::M;
|
|
||||||
case XKB_KEY_n: return CrafterKeys::N;
|
|
||||||
case XKB_KEY_o: return CrafterKeys::O;
|
|
||||||
case XKB_KEY_p: return CrafterKeys::P;
|
|
||||||
case XKB_KEY_q: return CrafterKeys::Q;
|
|
||||||
case XKB_KEY_r: return CrafterKeys::R;
|
|
||||||
case XKB_KEY_s: return CrafterKeys::S;
|
|
||||||
case XKB_KEY_t: return CrafterKeys::T;
|
|
||||||
case XKB_KEY_u: return CrafterKeys::U;
|
|
||||||
case XKB_KEY_v: return CrafterKeys::V;
|
|
||||||
case XKB_KEY_w: return CrafterKeys::W;
|
|
||||||
case XKB_KEY_x: return CrafterKeys::X;
|
|
||||||
case XKB_KEY_y: return CrafterKeys::Y;
|
|
||||||
case XKB_KEY_z: return CrafterKeys::Z;
|
|
||||||
|
|
||||||
// Numbers
|
|
||||||
case XKB_KEY_0: return CrafterKeys::_0;
|
|
||||||
case XKB_KEY_1: return CrafterKeys::_1;
|
|
||||||
case XKB_KEY_2: return CrafterKeys::_2;
|
|
||||||
case XKB_KEY_3: return CrafterKeys::_3;
|
|
||||||
case XKB_KEY_4: return CrafterKeys::_4;
|
|
||||||
case XKB_KEY_5: return CrafterKeys::_5;
|
|
||||||
case XKB_KEY_6: return CrafterKeys::_6;
|
|
||||||
case XKB_KEY_7: return CrafterKeys::_7;
|
|
||||||
case XKB_KEY_8: return CrafterKeys::_8;
|
|
||||||
case XKB_KEY_9: return CrafterKeys::_9;
|
|
||||||
|
|
||||||
// Function keys
|
|
||||||
case XKB_KEY_F1: return CrafterKeys::F1;
|
|
||||||
case XKB_KEY_F2: return CrafterKeys::F2;
|
|
||||||
case XKB_KEY_F3: return CrafterKeys::F3;
|
|
||||||
case XKB_KEY_F4: return CrafterKeys::F4;
|
|
||||||
case XKB_KEY_F5: return CrafterKeys::F5;
|
|
||||||
case XKB_KEY_F6: return CrafterKeys::F6;
|
|
||||||
case XKB_KEY_F7: return CrafterKeys::F7;
|
|
||||||
case XKB_KEY_F8: return CrafterKeys::F8;
|
|
||||||
case XKB_KEY_F9: return CrafterKeys::F9;
|
|
||||||
case XKB_KEY_F10: return CrafterKeys::F10;
|
|
||||||
case XKB_KEY_F11: return CrafterKeys::F11;
|
|
||||||
case XKB_KEY_F12: return CrafterKeys::F12;
|
|
||||||
|
|
||||||
// Control keys
|
|
||||||
case XKB_KEY_Escape: return CrafterKeys::Escape;
|
|
||||||
case XKB_KEY_Tab: return CrafterKeys::Tab;
|
|
||||||
case XKB_KEY_Return: return CrafterKeys::Enter;
|
|
||||||
case XKB_KEY_space: return CrafterKeys::Space;
|
|
||||||
case XKB_KEY_BackSpace: return CrafterKeys::Backspace;
|
|
||||||
case XKB_KEY_Delete: return CrafterKeys::Delete;
|
|
||||||
case XKB_KEY_Insert: return CrafterKeys::Insert;
|
|
||||||
case XKB_KEY_Home: return CrafterKeys::Home;
|
|
||||||
case XKB_KEY_End: return CrafterKeys::End;
|
|
||||||
case XKB_KEY_Page_Up: return CrafterKeys::PageUp;
|
|
||||||
case XKB_KEY_Page_Down: return CrafterKeys::PageDown;
|
|
||||||
case XKB_KEY_Caps_Lock: return CrafterKeys::CapsLock;
|
|
||||||
case XKB_KEY_Num_Lock: return CrafterKeys::NumLock;
|
|
||||||
case XKB_KEY_Scroll_Lock:return CrafterKeys::ScrollLock;
|
|
||||||
|
|
||||||
// Modifiers
|
|
||||||
case XKB_KEY_Shift_L: return CrafterKeys::LeftShift;
|
|
||||||
case XKB_KEY_Shift_R: return CrafterKeys::RightShift;
|
|
||||||
case XKB_KEY_Control_L: return CrafterKeys::LeftCtrl;
|
|
||||||
case XKB_KEY_Control_R: return CrafterKeys::RightCtrl;
|
|
||||||
case XKB_KEY_Alt_L: return CrafterKeys::LeftAlt;
|
|
||||||
case XKB_KEY_Alt_R: return CrafterKeys::RightAlt;
|
|
||||||
case XKB_KEY_Super_L: return CrafterKeys::LeftSuper;
|
|
||||||
case XKB_KEY_Super_R: return CrafterKeys::RightSuper;
|
|
||||||
|
|
||||||
// Arrows
|
|
||||||
case XKB_KEY_Up: return CrafterKeys::Up;
|
|
||||||
case XKB_KEY_Down: return CrafterKeys::Down;
|
|
||||||
case XKB_KEY_Left: return CrafterKeys::Left;
|
|
||||||
case XKB_KEY_Right: return CrafterKeys::Right;
|
|
||||||
|
|
||||||
// Keypad
|
|
||||||
case XKB_KEY_KP_0: return CrafterKeys::keypad_0;
|
|
||||||
case XKB_KEY_KP_1: return CrafterKeys::keypad_1;
|
|
||||||
case XKB_KEY_KP_2: return CrafterKeys::keypad_2;
|
|
||||||
case XKB_KEY_KP_3: return CrafterKeys::keypad_3;
|
|
||||||
case XKB_KEY_KP_4: return CrafterKeys::keypad_4;
|
|
||||||
case XKB_KEY_KP_5: return CrafterKeys::keypad_5;
|
|
||||||
case XKB_KEY_KP_6: return CrafterKeys::keypad_6;
|
|
||||||
case XKB_KEY_KP_7: return CrafterKeys::keypad_7;
|
|
||||||
case XKB_KEY_KP_8: return CrafterKeys::keypad_8;
|
|
||||||
case XKB_KEY_KP_9: return CrafterKeys::keypad_9;
|
|
||||||
case XKB_KEY_KP_Enter: return CrafterKeys::keypad_enter;
|
|
||||||
case XKB_KEY_KP_Add: return CrafterKeys::keypad_plus;
|
|
||||||
case XKB_KEY_KP_Subtract: return CrafterKeys::keypad_minus;
|
|
||||||
case XKB_KEY_KP_Multiply: return CrafterKeys::keypad_multiply;
|
|
||||||
case XKB_KEY_KP_Divide: return CrafterKeys::keypad_divide;
|
|
||||||
case XKB_KEY_KP_Decimal: return CrafterKeys::keypad_decimal;
|
|
||||||
|
|
||||||
// Punctuation
|
|
||||||
case XKB_KEY_grave: return CrafterKeys::grave;
|
|
||||||
case XKB_KEY_minus: return CrafterKeys::minus;
|
|
||||||
case XKB_KEY_equal: return CrafterKeys::equal;
|
|
||||||
case XKB_KEY_bracketleft: return CrafterKeys::bracket_left;
|
|
||||||
case XKB_KEY_bracketright:return CrafterKeys::bracket_right;
|
|
||||||
case XKB_KEY_backslash: return CrafterKeys::backslash;
|
|
||||||
case XKB_KEY_semicolon: return CrafterKeys::semicolon;
|
|
||||||
case XKB_KEY_apostrophe: return CrafterKeys::quote;
|
|
||||||
case XKB_KEY_comma: return CrafterKeys::comma;
|
|
||||||
case XKB_KEY_period: return CrafterKeys::period;
|
|
||||||
case XKB_KEY_slash: return CrafterKeys::slash;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return CrafterKeys::CrafterKeysMax;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
|
|
||||||
if (!window->xkb_state) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
xkb_keycode_t keycode = key + 8;
|
|
||||||
xkb_keysym_t keysym = xkb_state_key_get_one_sym(window->xkb_state, keycode);
|
|
||||||
CrafterKeys crafterKey = keysym_to_crafter_key(keysym);
|
|
||||||
|
|
||||||
if(state == WL_KEYBOARD_KEY_STATE_PRESSED) {
|
|
||||||
if(window->heldkeys[static_cast<std::uint8_t>(crafterKey)]) {
|
|
||||||
window->onKeyHold[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyHold.Invoke(crafterKey);
|
|
||||||
} else{
|
|
||||||
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = true;
|
|
||||||
window->onKeyDown[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyDown.Invoke(crafterKey);
|
|
||||||
}
|
|
||||||
} else{
|
|
||||||
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = false;
|
|
||||||
window->onKeyUp[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyUp.Invoke(crafterKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
window->seat = seat;
|
|
||||||
if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
|
|
||||||
wl_pointer* pointer = wl_seat_get_pointer(seat);
|
|
||||||
wl_pointer_add_listener(pointer, &pointer_listener, window);
|
|
||||||
}
|
|
||||||
if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
|
|
||||||
wl_keyboard* keyboard = wl_seat_get_keyboard(seat);
|
|
||||||
wl_keyboard_add_listener(keyboard, &keyboard_listener, window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
if (strcmp(interface, wl_shm_interface.name) == 0) {
|
|
||||||
window->shm = reinterpret_cast<wl_shm*>(wl_registry_bind(registry, name, &wl_shm_interface, 1));
|
|
||||||
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
|
||||||
wl_seat* seat = reinterpret_cast<wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface, 1));
|
|
||||||
wl_seat_add_listener(seat, &seat_listener, window);
|
|
||||||
} else if (compositor == NULL && strcmp(interface, wl_compositor_interface.name) == 0) {
|
|
||||||
compositor = reinterpret_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 3));
|
|
||||||
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
|
||||||
window->xdgWmBase = reinterpret_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1));
|
|
||||||
xdg_wm_base_add_listener(window->xdgWmBase, &xdgWmBaseListener, NULL);
|
|
||||||
} else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
|
|
||||||
window->manager = reinterpret_cast<zxdg_decoration_manager_v1*>(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1));
|
|
||||||
} else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
|
|
||||||
window->wpViewporter = reinterpret_cast<wp_viewporter*>(wl_registry_bind(registry, name, &wp_viewporter_interface, 1));
|
|
||||||
} else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
|
|
||||||
window->fractionalScaleManager = reinterpret_cast<wp_fractional_scale_manager_v1*>(wl_registry_bind(registry, name, &wp_fractional_scale_manager_v1_interface, 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::handle_global_remove(void* data, wl_registry* registry, uint32_t name) {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::xdg_toplevel_handle_close(void* data, xdg_toplevel*) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
window->onClose.Invoke();
|
|
||||||
window->open = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
// The compositor configures our surface, acknowledge the configure event
|
|
||||||
xdg_surface_ack_configure(xdg_surface, serial);
|
|
||||||
|
|
||||||
if (window->configured) {
|
|
||||||
// If this isn't the first configure event we've received, we already
|
|
||||||
// have a buffer attached, so no need to do anything. Commit the
|
|
||||||
// surface to apply the configure acknowledgement.
|
|
||||||
wl_surface_commit(window->surface);
|
|
||||||
}
|
|
||||||
|
|
||||||
window->configured = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale) {
|
|
||||||
WindowVulkan* window = reinterpret_cast<WindowVulkan*>(data);
|
|
||||||
|
|
||||||
window->scale = scale / 120.0f;
|
|
||||||
}
|
|
||||||
|
|
@ -1,682 +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 version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
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 <windows.h>
|
|
||||||
#include "vulkan/vulkan.h"
|
|
||||||
#include "vulkan/vulkan_win32.h"
|
|
||||||
#include <cassert>
|
|
||||||
module Crafter.Graphics:Window_wayland_impl;
|
|
||||||
import :Window;
|
|
||||||
import :RenderingElement;
|
|
||||||
import :MouseElement;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import :VulkanTransition;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
using namespace Crafter;
|
|
||||||
|
|
||||||
CrafterKeys vk_to_crafter_key(WPARAM vk)
|
|
||||||
{
|
|
||||||
switch (vk)
|
|
||||||
{
|
|
||||||
// Alphabet
|
|
||||||
case 'A': return CrafterKeys::A;
|
|
||||||
case 'B': return CrafterKeys::B;
|
|
||||||
case 'C': return CrafterKeys::C;
|
|
||||||
case 'D': return CrafterKeys::D;
|
|
||||||
case 'E': return CrafterKeys::E;
|
|
||||||
case 'F': return CrafterKeys::F;
|
|
||||||
case 'G': return CrafterKeys::G;
|
|
||||||
case 'H': return CrafterKeys::H;
|
|
||||||
case 'I': return CrafterKeys::I;
|
|
||||||
case 'J': return CrafterKeys::J;
|
|
||||||
case 'K': return CrafterKeys::K;
|
|
||||||
case 'L': return CrafterKeys::L;
|
|
||||||
case 'M': return CrafterKeys::M;
|
|
||||||
case 'N': return CrafterKeys::N;
|
|
||||||
case 'O': return CrafterKeys::O;
|
|
||||||
case 'P': return CrafterKeys::P;
|
|
||||||
case 'Q': return CrafterKeys::Q;
|
|
||||||
case 'R': return CrafterKeys::R;
|
|
||||||
case 'S': return CrafterKeys::S;
|
|
||||||
case 'T': return CrafterKeys::T;
|
|
||||||
case 'U': return CrafterKeys::U;
|
|
||||||
case 'V': return CrafterKeys::V;
|
|
||||||
case 'W': return CrafterKeys::W;
|
|
||||||
case 'X': return CrafterKeys::X;
|
|
||||||
case 'Y': return CrafterKeys::Y;
|
|
||||||
case 'Z': return CrafterKeys::Z;
|
|
||||||
|
|
||||||
// Numbers
|
|
||||||
case '0': return CrafterKeys::_0;
|
|
||||||
case '1': return CrafterKeys::_1;
|
|
||||||
case '2': return CrafterKeys::_2;
|
|
||||||
case '3': return CrafterKeys::_3;
|
|
||||||
case '4': return CrafterKeys::_4;
|
|
||||||
case '5': return CrafterKeys::_5;
|
|
||||||
case '6': return CrafterKeys::_6;
|
|
||||||
case '7': return CrafterKeys::_7;
|
|
||||||
case '8': return CrafterKeys::_8;
|
|
||||||
case '9': return CrafterKeys::_9;
|
|
||||||
|
|
||||||
// Function keys
|
|
||||||
case VK_F1: return CrafterKeys::F1;
|
|
||||||
case VK_F2: return CrafterKeys::F2;
|
|
||||||
case VK_F3: return CrafterKeys::F3;
|
|
||||||
case VK_F4: return CrafterKeys::F4;
|
|
||||||
case VK_F5: return CrafterKeys::F5;
|
|
||||||
case VK_F6: return CrafterKeys::F6;
|
|
||||||
case VK_F7: return CrafterKeys::F7;
|
|
||||||
case VK_F8: return CrafterKeys::F8;
|
|
||||||
case VK_F9: return CrafterKeys::F9;
|
|
||||||
case VK_F10: return CrafterKeys::F10;
|
|
||||||
case VK_F11: return CrafterKeys::F11;
|
|
||||||
case VK_F12: return CrafterKeys::F12;
|
|
||||||
|
|
||||||
// Control keys
|
|
||||||
case VK_ESCAPE: return CrafterKeys::Escape;
|
|
||||||
case VK_TAB: return CrafterKeys::Tab;
|
|
||||||
case VK_RETURN: return CrafterKeys::Enter;
|
|
||||||
case VK_SPACE: return CrafterKeys::Space;
|
|
||||||
case VK_BACK: return CrafterKeys::Backspace;
|
|
||||||
case VK_DELETE: return CrafterKeys::Delete;
|
|
||||||
case VK_INSERT: return CrafterKeys::Insert;
|
|
||||||
case VK_HOME: return CrafterKeys::Home;
|
|
||||||
case VK_END: return CrafterKeys::End;
|
|
||||||
case VK_PRIOR: return CrafterKeys::PageUp;
|
|
||||||
case VK_NEXT: return CrafterKeys::PageDown;
|
|
||||||
case VK_CAPITAL: return CrafterKeys::CapsLock;
|
|
||||||
case VK_NUMLOCK: return CrafterKeys::NumLock;
|
|
||||||
case VK_SCROLL: return CrafterKeys::ScrollLock;
|
|
||||||
|
|
||||||
// Modifiers
|
|
||||||
case VK_LSHIFT: return CrafterKeys::LeftShift;
|
|
||||||
case VK_RSHIFT: return CrafterKeys::RightShift;
|
|
||||||
case VK_LCONTROL: return CrafterKeys::LeftCtrl;
|
|
||||||
case VK_RCONTROL: return CrafterKeys::RightCtrl;
|
|
||||||
case VK_LMENU: return CrafterKeys::LeftAlt;
|
|
||||||
case VK_RMENU: return CrafterKeys::RightAlt;
|
|
||||||
case VK_LWIN: return CrafterKeys::LeftSuper;
|
|
||||||
case VK_RWIN: return CrafterKeys::RightSuper;
|
|
||||||
|
|
||||||
// Arrows
|
|
||||||
case VK_UP: return CrafterKeys::Up;
|
|
||||||
case VK_DOWN: return CrafterKeys::Down;
|
|
||||||
case VK_LEFT: return CrafterKeys::Left;
|
|
||||||
case VK_RIGHT: return CrafterKeys::Right;
|
|
||||||
|
|
||||||
// Keypad
|
|
||||||
case VK_NUMPAD0: return CrafterKeys::keypad_0;
|
|
||||||
case VK_NUMPAD1: return CrafterKeys::keypad_1;
|
|
||||||
case VK_NUMPAD2: return CrafterKeys::keypad_2;
|
|
||||||
case VK_NUMPAD3: return CrafterKeys::keypad_3;
|
|
||||||
case VK_NUMPAD4: return CrafterKeys::keypad_4;
|
|
||||||
case VK_NUMPAD5: return CrafterKeys::keypad_5;
|
|
||||||
case VK_NUMPAD6: return CrafterKeys::keypad_6;
|
|
||||||
case VK_NUMPAD7: return CrafterKeys::keypad_7;
|
|
||||||
case VK_NUMPAD8: return CrafterKeys::keypad_8;
|
|
||||||
case VK_NUMPAD9: return CrafterKeys::keypad_9;
|
|
||||||
case VK_SEPARATOR: return CrafterKeys::keypad_enter;
|
|
||||||
case VK_ADD: return CrafterKeys::keypad_plus;
|
|
||||||
case VK_SUBTRACT: return CrafterKeys::keypad_minus;
|
|
||||||
case VK_MULTIPLY: return CrafterKeys::keypad_multiply;
|
|
||||||
case VK_DIVIDE: return CrafterKeys::keypad_divide;
|
|
||||||
case VK_DECIMAL: return CrafterKeys::keypad_decimal;
|
|
||||||
|
|
||||||
// Punctuation
|
|
||||||
case VK_OEM_3: return CrafterKeys::grave; // `
|
|
||||||
case VK_OEM_MINUS: return CrafterKeys::minus; // -
|
|
||||||
case VK_OEM_PLUS: return CrafterKeys::equal; // =
|
|
||||||
case VK_OEM_4: return CrafterKeys::bracket_left; // [
|
|
||||||
case VK_OEM_6: return CrafterKeys::bracket_right; // ]
|
|
||||||
case VK_OEM_5: return CrafterKeys::backslash; // \
|
|
||||||
case VK_OEM_1: return CrafterKeys::semicolon; // ;
|
|
||||||
case VK_OEM_7: return CrafterKeys::quote; // '
|
|
||||||
case VK_OEM_COMMA:return CrafterKeys::comma; // ,
|
|
||||||
case VK_OEM_PERIOD:return CrafterKeys::period; // .
|
|
||||||
case VK_OEM_2: return CrafterKeys::slash; // /
|
|
||||||
|
|
||||||
default:
|
|
||||||
return CrafterKeys::CrafterKeysMax;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::CreateSwapchain()
|
|
||||||
{
|
|
||||||
// Store the current swap chain handle so we can use it later on to ease up recreation
|
|
||||||
VkSwapchainKHR oldSwapchain = swapChain;
|
|
||||||
|
|
||||||
// Get physical device surface properties and formats
|
|
||||||
VkSurfaceCapabilitiesKHR surfCaps;
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VulkanDevice::physDevice, vulkanSurface, &surfCaps));
|
|
||||||
|
|
||||||
VkExtent2D swapchainExtent = {};
|
|
||||||
// If width (and height) equals the special value 0xFFFFFFFF, the size of the surface will be set by the swapchain
|
|
||||||
if (surfCaps.currentExtent.width == (uint32_t)-1)
|
|
||||||
{
|
|
||||||
// If the surface size is undefined, the size is set to the size of the images requested
|
|
||||||
swapchainExtent.width = width;
|
|
||||||
swapchainExtent.height = height;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// If the surface size is defined, the swap chain size must match
|
|
||||||
swapchainExtent = surfCaps.currentExtent;
|
|
||||||
width = surfCaps.currentExtent.width;
|
|
||||||
height = surfCaps.currentExtent.height;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Select a present mode for the swapchain
|
|
||||||
uint32_t presentModeCount;
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfacePresentModesKHR(VulkanDevice::physDevice, vulkanSurface, &presentModeCount, NULL));
|
|
||||||
assert(presentModeCount > 0);
|
|
||||||
|
|
||||||
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfacePresentModesKHR(VulkanDevice::physDevice, vulkanSurface, &presentModeCount, presentModes.data()));
|
|
||||||
|
|
||||||
// The VK_PRESENT_MODE_FIFO_KHR mode must always be present as per spec
|
|
||||||
// This mode waits for the vertical blank ("v-sync")
|
|
||||||
VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
|
|
||||||
|
|
||||||
// Find the transformation of the surface
|
|
||||||
VkSurfaceTransformFlagsKHR preTransform;
|
|
||||||
if (surfCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
|
|
||||||
{
|
|
||||||
// We prefer a non-rotated transform
|
|
||||||
preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
preTransform = surfCaps.currentTransform;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find a supported composite alpha format (not all devices support alpha opaque)
|
|
||||||
VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
||||||
// Simply select the first composite alpha format available
|
|
||||||
std::vector<VkCompositeAlphaFlagBitsKHR> compositeAlphaFlags = {
|
|
||||||
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
|
||||||
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
|
|
||||||
VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
|
|
||||||
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
|
|
||||||
};
|
|
||||||
for (auto& compositeAlphaFlag : compositeAlphaFlags) {
|
|
||||||
if (surfCaps.supportedCompositeAlpha & compositeAlphaFlag) {
|
|
||||||
compositeAlpha = compositeAlphaFlag;
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
VkSwapchainCreateInfoKHR swapchainCI = {};
|
|
||||||
swapchainCI.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
|
||||||
swapchainCI.surface = vulkanSurface;
|
|
||||||
swapchainCI.minImageCount = numFrames;
|
|
||||||
swapchainCI.imageFormat = colorFormat;
|
|
||||||
swapchainCI.imageColorSpace = colorSpace;
|
|
||||||
swapchainCI.imageExtent = { swapchainExtent.width, swapchainExtent.height };
|
|
||||||
swapchainCI.imageUsage = VK_IMAGE_USAGE_STORAGE_BIT;
|
|
||||||
swapchainCI.preTransform = (VkSurfaceTransformFlagBitsKHR)preTransform;
|
|
||||||
swapchainCI.imageArrayLayers = 1;
|
|
||||||
swapchainCI.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
||||||
swapchainCI.queueFamilyIndexCount = 0;
|
|
||||||
swapchainCI.presentMode = swapchainPresentMode;
|
|
||||||
// Setting oldSwapChain to the saved handle of the previous swapchain aids in resource reuse and makes sure that we can still present already acquired images
|
|
||||||
swapchainCI.oldSwapchain = oldSwapchain;
|
|
||||||
// Setting clipped to VK_TRUE allows the implementation to discard rendering outside of the surface area
|
|
||||||
swapchainCI.clipped = VK_TRUE;
|
|
||||||
swapchainCI.compositeAlpha = compositeAlpha;
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateSwapchainKHR(VulkanDevice::device, &swapchainCI, nullptr, &swapChain));
|
|
||||||
|
|
||||||
// If an existing swap chain is re-created, destroy the old swap chain and the ressources owned by the application (image views, images are owned by the swap chain)
|
|
||||||
if (oldSwapchain != VK_NULL_HANDLE) {
|
|
||||||
for (auto i = 0; i < numFrames; i++) {
|
|
||||||
vkDestroyImageView(VulkanDevice::device, imageViews[i], nullptr);
|
|
||||||
}
|
|
||||||
vkDestroySwapchainKHR(VulkanDevice::device, oldSwapchain, nullptr);
|
|
||||||
}
|
|
||||||
uint32_t imageCount{ 0 };
|
|
||||||
VulkanDevice::CheckVkResult(vkGetSwapchainImagesKHR(VulkanDevice::device, swapChain, &imageCount, nullptr));
|
|
||||||
|
|
||||||
// Get the swap chain images
|
|
||||||
VulkanDevice::CheckVkResult(vkGetSwapchainImagesKHR(VulkanDevice::device, swapChain, &imageCount, images));
|
|
||||||
|
|
||||||
for (auto i = 0; i < numFrames; i++)
|
|
||||||
{
|
|
||||||
VkImageViewCreateInfo colorAttachmentView = {};
|
|
||||||
colorAttachmentView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
||||||
colorAttachmentView.pNext = NULL;
|
|
||||||
colorAttachmentView.format = colorFormat;
|
|
||||||
colorAttachmentView.components = {
|
|
||||||
VK_COMPONENT_SWIZZLE_R,
|
|
||||||
VK_COMPONENT_SWIZZLE_G,
|
|
||||||
VK_COMPONENT_SWIZZLE_B,
|
|
||||||
VK_COMPONENT_SWIZZLE_A
|
|
||||||
};
|
|
||||||
colorAttachmentView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
colorAttachmentView.subresourceRange.baseMipLevel = 0;
|
|
||||||
colorAttachmentView.subresourceRange.levelCount = 1;
|
|
||||||
colorAttachmentView.subresourceRange.baseArrayLayer = 0;
|
|
||||||
colorAttachmentView.subresourceRange.layerCount = 1;
|
|
||||||
colorAttachmentView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
||||||
colorAttachmentView.flags = 0;
|
|
||||||
colorAttachmentView.image = images[i];
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateImageView(VulkanDevice::device, &colorAttachmentView, nullptr, &imageViews[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define a window class name
|
|
||||||
const char g_szClassName[] = "myWindowClass";
|
|
||||||
|
|
||||||
// Window procedure function that processes messages
|
|
||||||
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
||||||
|
|
||||||
WindowVulkan* window = nullptr;
|
|
||||||
|
|
||||||
if (msg == WM_NCCREATE)
|
|
||||||
{
|
|
||||||
CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lParam);
|
|
||||||
window = static_cast<WindowVulkan*>(pCreate->lpCreateParams);
|
|
||||||
|
|
||||||
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(window));
|
|
||||||
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
window = reinterpret_cast<WindowVulkan*>(
|
|
||||||
GetWindowLongPtr(hwnd, GWLP_USERDATA)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (msg) {
|
|
||||||
case WM_DESTROY:{
|
|
||||||
PostQuitMessage(0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case WM_KEYDOWN:{
|
|
||||||
if ((lParam & (1 << 30)) == 0) { // only first press
|
|
||||||
CrafterKeys crafterKey = vk_to_crafter_key(wParam);
|
|
||||||
if(window->heldkeys[static_cast<std::uint8_t>(crafterKey)]) {
|
|
||||||
window->onKeyHold[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyHold.Invoke(crafterKey);
|
|
||||||
} else{
|
|
||||||
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = true;
|
|
||||||
window->onKeyDown[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyDown.Invoke(crafterKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case WM_KEYUP: {
|
|
||||||
CrafterKeys crafterKey = vk_to_crafter_key(wParam);
|
|
||||||
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = false;
|
|
||||||
window->onKeyUp[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyUp.Invoke(crafterKey);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case WM_MOUSEMOVE: {
|
|
||||||
int x = LOWORD(lParam);
|
|
||||||
int y = HIWORD(lParam);
|
|
||||||
|
|
||||||
MousePoint pos = {FractionalToMappedBoundless<std::uint32_t>(static_cast<float>(x) / window->width), FractionalToMappedBoundless<std::uint32_t>(static_cast<float>(y) / window->height)};
|
|
||||||
window->currentMousePos = pos;
|
|
||||||
window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseMove.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
if(!(window->lastMousePos.x >= element->mouseScaled.x && window->lastMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->lastMousePos.y > element->mouseScaled.y && window->lastMousePos.y < element->mouseScaled.y+element->mouseScaled.height)) {
|
|
||||||
element->onMouseEnter.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
} else if(window->lastMousePos.x >= element->mouseScaled.x && window->lastMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->lastMousePos.y > element->mouseScaled.y && window->lastMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeave.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
window->mouseElements.erase(std::remove(window->mouseElements.begin(), window->mouseElements.end(), static_cast<MouseElement*>(nullptr)), window->mouseElements.end());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case WM_LBUTTONDOWN: {
|
|
||||||
window->mouseLeftHeld = true;
|
|
||||||
window->onMouseLeftClick.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeftClick.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case WM_LBUTTONUP: {
|
|
||||||
window->mouseLeftHeld = false;
|
|
||||||
window->onMouseLeftRelease.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeftRelease.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case WM_RBUTTONDOWN: {
|
|
||||||
window->mouseRightHeld = true;
|
|
||||||
window->onMouseRightClick.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseRightClick.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case WM_RBUTTONUP: {
|
|
||||||
window->mouseRightHeld = false;
|
|
||||||
window->onMouseRightRelease.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseRightRelease.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
return DefWindowProc(hwnd, msg, wParam, lParam);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
WindowVulkan::WindowVulkan(std::uint32_t width, std::uint32_t height) : Window(width, height) {
|
|
||||||
|
|
||||||
}
|
|
||||||
WindowVulkan::WindowVulkan(std::uint32_t width, std::uint32_t height, std::string_view title) : Window(width, height) {
|
|
||||||
// Initialize the window class
|
|
||||||
WNDCLASS wc = {0};
|
|
||||||
wc.lpfnWndProc = WndProc; // Set window procedure
|
|
||||||
wc.hInstance = GetModuleHandle(NULL); // Get instance handle
|
|
||||||
wc.lpszClassName = g_szClassName;
|
|
||||||
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
||||||
|
|
||||||
if (!RegisterClass(&wc)) {
|
|
||||||
MessageBox(NULL, "Window Class Registration Failed!", "Error", MB_ICONERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
RECT rc = {0, 0, static_cast<LONG>(width), static_cast<LONG>(height)};
|
|
||||||
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
|
|
||||||
|
|
||||||
HWND hwnd = CreateWindowEx(
|
|
||||||
0,
|
|
||||||
g_szClassName,
|
|
||||||
title.data(),
|
|
||||||
WS_OVERLAPPEDWINDOW,
|
|
||||||
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
||||||
rc.right - rc.left,
|
|
||||||
rc.bottom - rc.top,
|
|
||||||
NULL, NULL, wc.hInstance, this
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
if (hwnd == NULL) {
|
|
||||||
MessageBox(NULL, "Window Creation Failed!", "Error", MB_ICONERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show the window
|
|
||||||
ShowWindow(hwnd, SW_SHOWNORMAL);
|
|
||||||
UpdateWindow(hwnd);
|
|
||||||
|
|
||||||
MSG msg;
|
|
||||||
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
||||||
TranslateMessage(&msg);
|
|
||||||
DispatchMessage(&msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
VkWin32SurfaceCreateInfoKHR createInfo = {};
|
|
||||||
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
|
|
||||||
createInfo.hinstance = wc.hInstance;
|
|
||||||
createInfo.hwnd = hwnd;
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateWin32SurfaceKHR(VulkanDevice::instance, &createInfo, NULL, &vulkanSurface));
|
|
||||||
|
|
||||||
// Get list of supported surface formats
|
|
||||||
std::uint32_t formatCount;
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(VulkanDevice::physDevice, vulkanSurface, &formatCount, NULL));
|
|
||||||
assert(formatCount > 0);
|
|
||||||
|
|
||||||
std::vector<VkSurfaceFormatKHR> surfaceFormats(formatCount);
|
|
||||||
VulkanDevice::CheckVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(VulkanDevice::physDevice, vulkanSurface, &formatCount, surfaceFormats.data()));
|
|
||||||
|
|
||||||
// We want to get a format that best suits our needs, so we try to get one from a set of preferred formats
|
|
||||||
// Initialize the format to the first one returned by the implementation in case we can't find one of the preffered formats
|
|
||||||
VkSurfaceFormatKHR selectedFormat = surfaceFormats[0];
|
|
||||||
std::vector<VkFormat> preferredImageFormats = {
|
|
||||||
VK_FORMAT_R8G8B8A8_UNORM,
|
|
||||||
VK_FORMAT_B8G8R8A8_UNORM
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto& availableFormat : surfaceFormats) {
|
|
||||||
if (std::find(preferredImageFormats.begin(), preferredImageFormats.end(), availableFormat.format) != preferredImageFormats.end()) {
|
|
||||||
selectedFormat = availableFormat;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
colorFormat = selectedFormat.format;
|
|
||||||
colorSpace = selectedFormat.colorSpace;
|
|
||||||
|
|
||||||
CreateSwapchain();
|
|
||||||
|
|
||||||
VkCommandBufferAllocateInfo cmdBufAllocateInfo {};
|
|
||||||
cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
||||||
cmdBufAllocateInfo.commandPool = VulkanDevice::commandPool;
|
|
||||||
cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
||||||
cmdBufAllocateInfo.commandBufferCount = numFrames;
|
|
||||||
VulkanDevice::CheckVkResult(vkAllocateCommandBuffers(VulkanDevice::device, &cmdBufAllocateInfo, drawCmdBuffers));
|
|
||||||
|
|
||||||
VkSemaphoreCreateInfo semaphoreCreateInfo {};
|
|
||||||
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateSemaphore(VulkanDevice::device, &semaphoreCreateInfo, nullptr, &semaphores.presentComplete));
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateSemaphore(VulkanDevice::device, &semaphoreCreateInfo, nullptr, &semaphores.renderComplete));
|
|
||||||
|
|
||||||
// Set up submit info structure
|
|
||||||
// Semaphores will stay the same during application lifetime
|
|
||||||
// Command buffer submission info is set by each example
|
|
||||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
||||||
submitInfo.pWaitDstStageMask = &submitPipelineStages;
|
|
||||||
submitInfo.waitSemaphoreCount = 1;
|
|
||||||
submitInfo.pWaitSemaphores = &semaphores.presentComplete;
|
|
||||||
submitInfo.signalSemaphoreCount = 1;
|
|
||||||
submitInfo.pSignalSemaphores = &semaphores.renderComplete;
|
|
||||||
submitInfo.pNext = VK_NULL_HANDLE;
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowVulkan::~WindowVulkan() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::StartSync() {
|
|
||||||
MSG msg;
|
|
||||||
while (GetMessage(&msg, NULL, 0, 0) > 0) {
|
|
||||||
TranslateMessage(&msg);
|
|
||||||
DispatchMessage(&msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::SetTitle(const std::string_view title) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> framEnd;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void WindowVulkan::StartUpdate() {
|
|
||||||
lastFrameBegin = std::chrono::high_resolution_clock::now();
|
|
||||||
updating = true;
|
|
||||||
while(updating) {
|
|
||||||
MSG msg;
|
|
||||||
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
||||||
TranslateMessage(&msg);
|
|
||||||
DispatchMessage(&msg);
|
|
||||||
}
|
|
||||||
mouseDelta = {std::int64_t(currentMousePos.x)-lastMousePos.x, std::int64_t(currentMousePos.y)-lastMousePos.y};
|
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
|
||||||
Render();
|
|
||||||
lastMousePos = currentMousePos;
|
|
||||||
currentFrameTime = {start, start-lastFrameBegin};
|
|
||||||
lastFrameBegin = start;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::StopUpdate() {
|
|
||||||
updating = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkCommandBuffer WindowVulkan::StartInit() {
|
|
||||||
VkCommandBufferBeginInfo cmdBufInfo {};
|
|
||||||
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
||||||
VulkanDevice::CheckVkResult(vkBeginCommandBuffer(drawCmdBuffers[currentBuffer], &cmdBufInfo));
|
|
||||||
|
|
||||||
VkImageSubresourceRange range{};
|
|
||||||
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
range.baseMipLevel = 0;
|
|
||||||
range.levelCount = VK_REMAINING_MIP_LEVELS;
|
|
||||||
range.baseArrayLayer = 0;
|
|
||||||
range.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
|
||||||
|
|
||||||
for(std::uint32_t i = 0; i < numFrames; i++) {
|
|
||||||
image_layout_transition(drawCmdBuffers[currentBuffer],
|
|
||||||
images[i],
|
|
||||||
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
||||||
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
||||||
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
||||||
range
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return drawCmdBuffers[currentBuffer];
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::FinishInit() {
|
|
||||||
VkSubmitInfo submitInfo{};
|
|
||||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
||||||
VulkanDevice::CheckVkResult(vkEndCommandBuffer(drawCmdBuffers[currentBuffer]));
|
|
||||||
VulkanDevice::CheckVkResult(vkQueueSubmit(VulkanDevice::queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
||||||
VulkanDevice::CheckVkResult(vkQueueWaitIdle(VulkanDevice::queue));
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowVulkan::Render() {
|
|
||||||
// Acquire the next image from the swap chain
|
|
||||||
VulkanDevice::CheckVkResult(vkAcquireNextImageKHR(VulkanDevice::device, swapChain, UINT64_MAX, semaphores.presentComplete, (VkFence)nullptr, ¤tBuffer));
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
||||||
|
|
||||||
VkCommandBufferBeginInfo cmdBufInfo {};
|
|
||||||
cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkBeginCommandBuffer(drawCmdBuffers[currentBuffer], &cmdBufInfo));
|
|
||||||
|
|
||||||
VkImageSubresourceRange range{};
|
|
||||||
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
range.baseMipLevel = 0;
|
|
||||||
range.levelCount = VK_REMAINING_MIP_LEVELS;
|
|
||||||
range.baseArrayLayer = 0;
|
|
||||||
range.layerCount = VK_REMAINING_ARRAY_LAYERS;
|
|
||||||
|
|
||||||
VkImageMemoryBarrier image_memory_barrier {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
||||||
.srcAccessMask = 0,
|
|
||||||
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
|
|
||||||
.oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
||||||
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
||||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.image = images[currentBuffer],
|
|
||||||
.subresourceRange = range
|
|
||||||
};
|
|
||||||
|
|
||||||
vkCmdPipelineBarrier(drawCmdBuffers[currentBuffer], VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier);
|
|
||||||
|
|
||||||
onRender.Invoke();
|
|
||||||
|
|
||||||
vkCmdBindPipeline(drawCmdBuffers[currentBuffer], VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, rtPipeline);
|
|
||||||
VkBindDescriptorSetsInfo bindDescriptorSetsInfo{
|
|
||||||
.sType = VK_STRUCTURE_TYPE_BIND_DESCRIPTOR_SETS_INFO,
|
|
||||||
.stageFlags = VK_SHADER_STAGE_ALL,
|
|
||||||
.layout = rtPipelineLayout,
|
|
||||||
.firstSet = 0,
|
|
||||||
.descriptorSetCount = static_cast<std::uint32_t>(descriptorsRt.size()),
|
|
||||||
.pDescriptorSets = descriptorsRt.data()
|
|
||||||
};
|
|
||||||
|
|
||||||
vkCmdBindDescriptorSets2(drawCmdBuffers[currentBuffer], &bindDescriptorSetsInfo);
|
|
||||||
VulkanDevice::vkCmdTraceRaysKHR(drawCmdBuffers[currentBuffer], &raygenRegion, &missRegion, &hitRegion, &callableRegion, width, height, 1);
|
|
||||||
|
|
||||||
|
|
||||||
VkImageMemoryBarrier image_memory_barrier2 {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
|
|
||||||
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
|
|
||||||
.dstAccessMask = 0,
|
|
||||||
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
||||||
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
||||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
|
||||||
.image = images[currentBuffer],
|
|
||||||
.subresourceRange = range
|
|
||||||
};
|
|
||||||
|
|
||||||
vkCmdPipelineBarrier(drawCmdBuffers[currentBuffer], VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier2);
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkEndCommandBuffer(drawCmdBuffers[currentBuffer]));
|
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkQueueSubmit(VulkanDevice::queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
||||||
VkPresentInfoKHR presentInfo = {};
|
|
||||||
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
|
||||||
presentInfo.pNext = NULL;
|
|
||||||
presentInfo.swapchainCount = 1;
|
|
||||||
presentInfo.pSwapchains = &swapChain;
|
|
||||||
presentInfo.pImageIndices = ¤tBuffer;
|
|
||||||
// Check if a wait semaphore has been specified to wait for before presenting the image
|
|
||||||
if (semaphores.renderComplete != VK_NULL_HANDLE)
|
|
||||||
{
|
|
||||||
presentInfo.pWaitSemaphores = &semaphores.renderComplete;
|
|
||||||
presentInfo.waitSemaphoreCount = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
VkResult result = vkQueuePresentKHR(VulkanDevice::queue, &presentInfo);
|
|
||||||
if(result == VK_SUBOPTIMAL_KHR) {
|
|
||||||
CreateSwapchain();
|
|
||||||
} else {
|
|
||||||
VulkanDevice::CheckVkResult(result);
|
|
||||||
}
|
|
||||||
VulkanDevice::CheckVkResult(vkQueueWaitIdle(VulkanDevice::queue));
|
|
||||||
}
|
|
||||||
|
|
@ -1,829 +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 version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
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 <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <linux/input-event-codes.h>
|
|
||||||
#include "../lib/xdg-shell-client-protocol.h"
|
|
||||||
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
|
|
||||||
#include "../lib/fractional-scale-v1.h"
|
|
||||||
#include "../lib/viewporter.h"
|
|
||||||
#include <string.h>
|
|
||||||
#include <linux/input.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <wayland-cursor.h>
|
|
||||||
#include <xkbcommon/xkbcommon.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <print>
|
|
||||||
#include <wayland-client.h>
|
|
||||||
#include <wayland-client-protocol.h>
|
|
||||||
|
|
||||||
module Crafter.Graphics:Window_wayland_impl;
|
|
||||||
import :Window;
|
|
||||||
import :RenderingElement;
|
|
||||||
import :MouseElement;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
import :Shm;
|
|
||||||
import Crafter.Event;
|
|
||||||
|
|
||||||
using namespace Crafter;
|
|
||||||
|
|
||||||
WindowFramebuffer::WindowFramebuffer(std::uint32_t width, std::uint32_t height) : Window(width, height) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowWayland::WindowWayland(std::uint32_t width, std::uint32_t height) : WindowFramebuffer(width, height) {
|
|
||||||
display = wl_display_connect(NULL);
|
|
||||||
if (display == NULL) {
|
|
||||||
std::cerr << "failed to create display" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
wl_registry* registry = wl_display_get_registry(display);
|
|
||||||
wl_registry_add_listener(registry, ®istry_listener, this);
|
|
||||||
if (wl_display_roundtrip(display) == -1) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (shm == NULL || compositor == NULL || xdgWmBase == NULL) {
|
|
||||||
std::cerr << "no wl_shm, wl_compositor or xdg_wm_base support" << std::endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
surface = wl_compositor_create_surface(compositor);
|
|
||||||
xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface);
|
|
||||||
xdgToplevel = xdg_surface_get_toplevel(xdgSurface);
|
|
||||||
|
|
||||||
xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this);
|
|
||||||
xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this);
|
|
||||||
wl_surface_commit(surface);
|
|
||||||
|
|
||||||
wp_scale = wp_fractional_scale_manager_v1_get_fractional_scale(fractionalScaleManager, surface);
|
|
||||||
wp_fractional_scale_v1_add_listener(wp_scale, &wp_fractional_scale_v1_listener, this);
|
|
||||||
|
|
||||||
while (wl_display_dispatch(display) != -1 && !configured) {}
|
|
||||||
|
|
||||||
wl_surface_commit(surface);
|
|
||||||
|
|
||||||
zxdg_toplevel_decoration_v1* decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, xdgToplevel);
|
|
||||||
zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
|
|
||||||
|
|
||||||
wpViewport = wp_viewporter_get_viewport(wpViewporter, surface);
|
|
||||||
wp_viewport_set_destination(wpViewport, std::ceil(width/scale), std::ceil(height/scale));
|
|
||||||
|
|
||||||
wl_surface_commit(surface);
|
|
||||||
|
|
||||||
|
|
||||||
// Create a wl_buffer, attach it to the surface and commit the surface
|
|
||||||
int stride = width * 4;
|
|
||||||
int size = stride * height;
|
|
||||||
|
|
||||||
// Allocate a shared memory file with the right size
|
|
||||||
int fd = create_shm_file(size);
|
|
||||||
if (fd < 0) {
|
|
||||||
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map the shared memory file
|
|
||||||
framebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
|
|
||||||
if (framebuffer == MAP_FAILED) {
|
|
||||||
fprintf(stderr, "mmap failed: %m\n");
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a wl_buffer from our shared memory file descriptor
|
|
||||||
wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
|
|
||||||
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
|
|
||||||
wl_shm_pool_destroy(pool);
|
|
||||||
|
|
||||||
// Now that we've mapped the file and created the wl_buffer, we no longer
|
|
||||||
// need to keep file descriptor opened
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
if (buffer == NULL) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
wl_surface_attach(surface, buffer, 0, 0);
|
|
||||||
wl_surface_commit(surface);
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowWayland::WindowWayland(std::uint32_t width, std::uint32_t height, const std::string_view title) : WindowWayland(width, height) {
|
|
||||||
xdg_toplevel_set_title(xdgToplevel, title.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowWayland::~WindowWayland() {
|
|
||||||
xdg_toplevel_destroy(xdgToplevel);
|
|
||||||
xdg_surface_destroy(xdgSurface);
|
|
||||||
wl_surface_destroy(surface);
|
|
||||||
wl_buffer_destroy(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::StartSync() {
|
|
||||||
while (open && wl_display_dispatch(display) != -1) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Optimized pixel blending function using SIMD-like operations
|
|
||||||
inline void blend_pixel_optimized(Pixel_BU8_GU8_RU8_AU8& dst, const Pixel_BU8_GU8_RU8_AU8& src) {
|
|
||||||
if(src.a == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float srcA = src.a / 255.0f;
|
|
||||||
float dstA = dst.a / 255.0f;
|
|
||||||
|
|
||||||
float outA = srcA + dstA * (1.0f - srcA);
|
|
||||||
if (outA > 0.0f) {
|
|
||||||
dst = {
|
|
||||||
static_cast<uint8_t>((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA),
|
|
||||||
static_cast<uint8_t>((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA),
|
|
||||||
static_cast<uint8_t>((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA),
|
|
||||||
static_cast<uint8_t>(outA * 255)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::RenderElement(Transform* transform) {
|
|
||||||
RenderingElementBase* element = dynamic_cast<RenderingElementBase*>(transform);
|
|
||||||
if(element) {
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(element->scaled.width < 1 || element->scaled.height < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(ClipRect dirty : dirtyRects) {
|
|
||||||
dirty.left = std::max(element->scaled.x, dirty.left);
|
|
||||||
dirty.top = std::max(element->scaled.y, dirty.top);
|
|
||||||
dirty.right = std::min(element->scaled.x+element->scaled.width, dirty.right);
|
|
||||||
dirty.bottom = std::min(element->scaled.y+element->scaled.height, dirty.bottom);
|
|
||||||
|
|
||||||
const Pixel_BU8_GU8_RU8_AU8* src_buffer = element->buffer.data();
|
|
||||||
std::int32_t src_width = element->scaled.width;
|
|
||||||
std::int32_t src_height = element->scaled.height;
|
|
||||||
|
|
||||||
switch (element->opaque) {
|
|
||||||
case OpaqueType::FullyOpaque:
|
|
||||||
// For fully opaque, just copy pixels directly
|
|
||||||
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
|
|
||||||
std::int32_t src_y = y - element->scaled.y;
|
|
||||||
|
|
||||||
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
|
|
||||||
std::int32_t src_x = x - element->scaled.x;
|
|
||||||
|
|
||||||
framebuffer[y * width + x] = src_buffer[src_y * src_width + src_x];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case OpaqueType::SemiOpaque:
|
|
||||||
// For semi-opaque, we can avoid blending when alpha is 0 or 255
|
|
||||||
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
|
|
||||||
std::int32_t src_y = y - element->scaled.y;
|
|
||||||
|
|
||||||
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
|
|
||||||
std::int32_t src_x = x - element->scaled.x;
|
|
||||||
Pixel_BU8_GU8_RU8_AU8 src_pixel = src_buffer[src_y * src_width + src_x];
|
|
||||||
|
|
||||||
if (src_pixel.a == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
framebuffer[y * width + x] = src_pixel;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case OpaqueType::Transparent:
|
|
||||||
// For transparent, always perform blending
|
|
||||||
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
|
|
||||||
std::int32_t src_y = y - element->scaled.y;
|
|
||||||
|
|
||||||
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
|
|
||||||
std::int32_t src_x = x - element->scaled.x;
|
|
||||||
blend_pixel_optimized(framebuffer[y * width + x], src_buffer[src_y * src_width + src_x]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
|
||||||
renderTimings.push_back({element, element->scaled.width, element->scaled.height, end-start});
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
std::sort(transform->children.begin(), transform->children.end(), [](Transform* a, Transform* b){ return a->anchor.z < b->anchor.z; });
|
|
||||||
for(Transform* child : transform->children) {
|
|
||||||
this->RenderElement(child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::Render() {
|
|
||||||
elements.erase(std::remove(elements.begin(), elements.end(), static_cast<Transform*>(nullptr)), elements.end());
|
|
||||||
std::sort(elements.begin(), elements.end(), [](Transform* a, Transform* b){ return a->anchor.z < b->anchor.z; });
|
|
||||||
|
|
||||||
//std::vector<ClipRect> newClip;
|
|
||||||
|
|
||||||
// for (std::uint32_t i = 0; i < dirtyRects.size(); i++) {
|
|
||||||
// ClipRect rect = dirtyRects[i];
|
|
||||||
// for (std::uint32_t i2 = i + 1; i2 < dirtyRects.size(); i2++) {
|
|
||||||
// ClipRect existing = dirtyRects[i2];
|
|
||||||
// if(rect.bottom >= existing.top && rect.top <= existing.top) {
|
|
||||||
// newClip.push_back({
|
|
||||||
// .left = rect.left,
|
|
||||||
// .right = rect.right,
|
|
||||||
// .top = rect.top,
|
|
||||||
// .bottom = existing.top,
|
|
||||||
// });
|
|
||||||
// //-| shape
|
|
||||||
// if(rect.right > existing.right) {
|
|
||||||
// newClip.push_back({
|
|
||||||
// .left = existing.right,
|
|
||||||
// .right = rect.right,
|
|
||||||
// .top = existing.top,
|
|
||||||
// .bottom = existing.bottom,
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// //|- shape
|
|
||||||
// if(rect.left < existing.left) {
|
|
||||||
// newClip.push_back({
|
|
||||||
// .left = rect.left,
|
|
||||||
// .right = existing.left,
|
|
||||||
// .top = existing.top,
|
|
||||||
// .bottom = existing.bottom,
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// //-| or |- shape where rect extends further down
|
|
||||||
// if(rect.bottom > existing.bottom) {
|
|
||||||
// newClip.push_back({
|
|
||||||
// .left = rect.left,
|
|
||||||
// .right = rect.right,
|
|
||||||
// .top = existing.bottom,
|
|
||||||
// .bottom = rect.bottom,
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// goto inner;
|
|
||||||
// }
|
|
||||||
// if (rect.left <= existing.right && rect.right >= existing.left) {
|
|
||||||
// newClip.push_back({
|
|
||||||
// .left = rect.left,
|
|
||||||
// .right = existing.left,
|
|
||||||
// .top = rect.top,
|
|
||||||
// .bottom = rect.bottom,
|
|
||||||
// });
|
|
||||||
// if (rect.right > existing.right) {
|
|
||||||
// newClip.push_back({
|
|
||||||
// .left = existing.right,
|
|
||||||
// .right = rect.right,
|
|
||||||
// .top = rect.top,
|
|
||||||
// .bottom = rect.bottom,
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// goto inner;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// newClip.push_back(rect);
|
|
||||||
// inner:;
|
|
||||||
// }
|
|
||||||
|
|
||||||
//dirtyRects = std::move(newClip);
|
|
||||||
|
|
||||||
// std::memset(framebuffer, 0, width*height*4);
|
|
||||||
|
|
||||||
// std::cout << dirtyRects.size() << std::endl;
|
|
||||||
// // Color palette
|
|
||||||
// static const std::vector<Pixel_BU8_GU8_RU8_AU8> colors = {
|
|
||||||
// {255, 0, 0, 255}, // red
|
|
||||||
// { 0, 255, 0, 255}, // green
|
|
||||||
// { 0, 0, 255, 255}, // blue
|
|
||||||
// {255, 255, 0, 255}, // yellow
|
|
||||||
// {255, 0, 255, 255}, // magenta
|
|
||||||
// { 0, 255, 255, 255}, // cyan
|
|
||||||
// };
|
|
||||||
|
|
||||||
// std::size_t rectIndex = 0;
|
|
||||||
|
|
||||||
// for (const ClipRect& rect : dirtyRects) {
|
|
||||||
// const Pixel_BU8_GU8_RU8_AU8& color = colors[rectIndex % colors.size()];
|
|
||||||
|
|
||||||
// std::cout << std::format(
|
|
||||||
// "ClipRect {}: [{}, {}, {}, {}] Color = RGBA({}, {}, {}, {})",
|
|
||||||
// rectIndex,
|
|
||||||
// rect.left, rect.top, rect.right, rect.bottom,
|
|
||||||
// color.r, color.g, color.b, color.a
|
|
||||||
// ) << std::endl;
|
|
||||||
|
|
||||||
// for (std::int32_t y = rect.top; y < rect.bottom; ++y) {
|
|
||||||
// for (std::int32_t x = rect.left; x < rect.right; ++x) {
|
|
||||||
// framebuffer[y * width + x] = color;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// ++rectIndex;
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (!dirtyRects.empty()) {
|
|
||||||
for (ClipRect rect : dirtyRects) {
|
|
||||||
for (std::int32_t y = rect.top; y < rect.bottom; y++) {
|
|
||||||
for (std::int32_t x = rect.left; x < rect.right; x++) {
|
|
||||||
framebuffer[y * width + x] = {0, 0, 0, 0};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(Transform* child : elements) {
|
|
||||||
RenderElement(child);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ClipRect rect : dirtyRects) {
|
|
||||||
wl_surface_damage(surface, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
|
|
||||||
}
|
|
||||||
dirtyRects.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
wl_surface_attach(surface, buffer, 0, 0);
|
|
||||||
wl_surface_commit(surface);
|
|
||||||
wl_surface_damage(surface, 0, 0, 10000, 100000);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::QueueRender() {
|
|
||||||
if(cb == nullptr) {
|
|
||||||
cb = wl_surface_frame(surface);
|
|
||||||
wl_callback_add_listener(cb, &wl_callback_listener, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::StartUpdate() {
|
|
||||||
lastFrameBegin = std::chrono::high_resolution_clock::now();
|
|
||||||
cb = wl_surface_frame(surface);
|
|
||||||
wl_callback_add_listener(cb, &wl_callback_listener, this);
|
|
||||||
updating = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::StopUpdate() {
|
|
||||||
updating = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::SetTitle(const std::string_view title) {
|
|
||||||
xdg_toplevel_set_title(xdgToplevel, title.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::Resize(std::uint32_t width, std::uint32_t height) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::Write(Pixel_BU8_GU8_RU8_AU8* pixels) {
|
|
||||||
std::memcpy(framebuffer, pixels, width*height*sizeof(Pixel_BU8_GU8_RU8_AU8));
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::Write(std::uint32_t x, std::uint32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) {
|
|
||||||
framebuffer[y * width + x] = pixel;
|
|
||||||
}
|
|
||||||
|
|
||||||
Pixel_BU8_GU8_RU8_AU8 WindowWayland::Read(std::uint32_t x, std::uint32_t y) const{
|
|
||||||
return framebuffer[y * width + x];
|
|
||||||
}
|
|
||||||
|
|
||||||
const Pixel_BU8_GU8_RU8_AU8* WindowWayland::Read() const{
|
|
||||||
return framebuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
Pixel_BU8_GU8_RU8_AU8* WindowWayland::Get() {
|
|
||||||
return framebuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::Store() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial) {
|
|
||||||
xdg_wm_base_pong(xdg_wm_base, serial);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> framEnd;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time)
|
|
||||||
{
|
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
|
||||||
wl_callback_destroy(cb);
|
|
||||||
cb = nullptr;
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
window->vblank = duration_cast<std::chrono::milliseconds>(start - window->frameEnd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(window->updating) {
|
|
||||||
cb = wl_surface_frame(window->surface);
|
|
||||||
wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window);
|
|
||||||
window->onUpdate.Invoke({start, start-window->lastFrameBegin});
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
window->totalUpdate = std::chrono::nanoseconds(0);
|
|
||||||
window->updateTimings.clear();
|
|
||||||
for (const std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>& entry : window->onUpdate.listenerTimes) {
|
|
||||||
window->updateTimings.push_back(entry);
|
|
||||||
window->totalUpdate += entry.second;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
auto renderStart = std::chrono::high_resolution_clock::now();
|
|
||||||
window->renderTimings.clear();
|
|
||||||
#endif
|
|
||||||
window->Render();
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
auto renderEnd = std::chrono::high_resolution_clock::now();
|
|
||||||
window->totalRender = renderEnd - renderStart;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
window->frameEnd = std::chrono::high_resolution_clock::now();
|
|
||||||
|
|
||||||
window->frameTimes.push_back(window->totalUpdate+window->totalRender);
|
|
||||||
|
|
||||||
// Keep only the last 100 frame times
|
|
||||||
if (window->frameTimes.size() > 100) {
|
|
||||||
window->frameTimes.erase(window->frameTimes.begin());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
window->lastFrameBegin = start;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
|
|
||||||
if (button == BTN_LEFT) {
|
|
||||||
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
|
||||||
window->mouseLeftHeld = true;
|
|
||||||
window->onMouseLeftClick.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeftClick.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
window->mouseLeftHeld = false;
|
|
||||||
window->onMouseLeftRelease.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeftRelease.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if(button == BTN_RIGHT){
|
|
||||||
if(state == WL_POINTER_BUTTON_STATE_PRESSED) {
|
|
||||||
window->mouseRightHeld = true;
|
|
||||||
window->onMouseRightClick.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseRightClick.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
window->mouseRightHeld = true;
|
|
||||||
window->onMouseRightRelease.Invoke(window->currentMousePos);
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseRightRelease.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
window->mouseElements.erase(std::remove(window->mouseElements.begin(), window->mouseElements.end(), static_cast<MouseElement*>(nullptr)), window->mouseElements.end());
|
|
||||||
window->mouseElements.insert(window->mouseElements.end(), window->pendingMouseElements.begin(), window->pendingMouseElements.end());
|
|
||||||
window->pendingMouseElements.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
MousePoint pos = {FractionalToMappedBoundless<std::uint32_t>((wl_fixed_to_double(surface_x) * window->scale) / window->width), FractionalToMappedBoundless<std::uint32_t>((wl_fixed_to_double(surface_y) * window->scale) / window->height)};
|
|
||||||
window->lastMousePos = window->currentMousePos;
|
|
||||||
window->currentMousePos = pos;
|
|
||||||
window->mouseDelta = {window->currentMousePos.x-window->lastMousePos.x, window->currentMousePos.y-window->lastMousePos.y};
|
|
||||||
window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
for(MouseElement* element : window->mouseElements) {
|
|
||||||
if(element) {
|
|
||||||
if(window->currentMousePos.x >= element->mouseScaled.x && window->currentMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->currentMousePos.y > element->mouseScaled.y && window->currentMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseMove.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
if(!(window->lastMousePos.x >= element->mouseScaled.x && window->lastMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->lastMousePos.y > element->mouseScaled.y && window->lastMousePos.y < element->mouseScaled.y+element->mouseScaled.height)) {
|
|
||||||
element->onMouseEnter.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
} else if(window->lastMousePos.x >= element->mouseScaled.x && window->lastMousePos.x <= element->mouseScaled.x+element->mouseScaled.width && window->lastMousePos.y > element->mouseScaled.y && window->lastMousePos.y < element->mouseScaled.y+element->mouseScaled.height) {
|
|
||||||
element->onMouseLeave.Invoke({AbsoluteToMappedBoundless(window->currentMousePos.x - element->mouseScaled.x, element->mouseScaled.width), AbsoluteToMappedBoundless(window->currentMousePos.y - element->mouseScaled.y, element->mouseScaled.height)});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
window->mouseElements.erase(std::remove(window->mouseElements.begin(), window->mouseElements.end(), static_cast<MouseElement*>(nullptr)), window->mouseElements.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
window->onMouseLeave.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta});
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void WindowWayland::keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
|
|
||||||
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
|
|
||||||
close(fd);
|
|
||||||
fprintf(stderr, "Unsupported keymap format\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
|
|
||||||
if (map == MAP_FAILED) {
|
|
||||||
close(fd);
|
|
||||||
perror("mmap");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
window->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
||||||
window->xkb_keymap = xkb_keymap_new_from_string(window->xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS);
|
|
||||||
munmap(map, size);
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
window->xkb_state = xkb_state_new(window->xkb_keymap);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
CrafterKeys keysym_to_crafter_key(xkb_keysym_t sym)
|
|
||||||
{
|
|
||||||
switch (sym)
|
|
||||||
{
|
|
||||||
// Alphabet
|
|
||||||
case XKB_KEY_a: return CrafterKeys::A;
|
|
||||||
case XKB_KEY_b: return CrafterKeys::B;
|
|
||||||
case XKB_KEY_c: return CrafterKeys::C;
|
|
||||||
case XKB_KEY_d: return CrafterKeys::D;
|
|
||||||
case XKB_KEY_e: return CrafterKeys::E;
|
|
||||||
case XKB_KEY_f: return CrafterKeys::F;
|
|
||||||
case XKB_KEY_g: return CrafterKeys::G;
|
|
||||||
case XKB_KEY_h: return CrafterKeys::H;
|
|
||||||
case XKB_KEY_i: return CrafterKeys::I;
|
|
||||||
case XKB_KEY_j: return CrafterKeys::J;
|
|
||||||
case XKB_KEY_k: return CrafterKeys::K;
|
|
||||||
case XKB_KEY_l: return CrafterKeys::L;
|
|
||||||
case XKB_KEY_m: return CrafterKeys::M;
|
|
||||||
case XKB_KEY_n: return CrafterKeys::N;
|
|
||||||
case XKB_KEY_o: return CrafterKeys::O;
|
|
||||||
case XKB_KEY_p: return CrafterKeys::P;
|
|
||||||
case XKB_KEY_q: return CrafterKeys::Q;
|
|
||||||
case XKB_KEY_r: return CrafterKeys::R;
|
|
||||||
case XKB_KEY_s: return CrafterKeys::S;
|
|
||||||
case XKB_KEY_t: return CrafterKeys::T;
|
|
||||||
case XKB_KEY_u: return CrafterKeys::U;
|
|
||||||
case XKB_KEY_v: return CrafterKeys::V;
|
|
||||||
case XKB_KEY_w: return CrafterKeys::W;
|
|
||||||
case XKB_KEY_x: return CrafterKeys::X;
|
|
||||||
case XKB_KEY_y: return CrafterKeys::Y;
|
|
||||||
case XKB_KEY_z: return CrafterKeys::Z;
|
|
||||||
|
|
||||||
// Numbers
|
|
||||||
case XKB_KEY_0: return CrafterKeys::_0;
|
|
||||||
case XKB_KEY_1: return CrafterKeys::_1;
|
|
||||||
case XKB_KEY_2: return CrafterKeys::_2;
|
|
||||||
case XKB_KEY_3: return CrafterKeys::_3;
|
|
||||||
case XKB_KEY_4: return CrafterKeys::_4;
|
|
||||||
case XKB_KEY_5: return CrafterKeys::_5;
|
|
||||||
case XKB_KEY_6: return CrafterKeys::_6;
|
|
||||||
case XKB_KEY_7: return CrafterKeys::_7;
|
|
||||||
case XKB_KEY_8: return CrafterKeys::_8;
|
|
||||||
case XKB_KEY_9: return CrafterKeys::_9;
|
|
||||||
|
|
||||||
// Function keys
|
|
||||||
case XKB_KEY_F1: return CrafterKeys::F1;
|
|
||||||
case XKB_KEY_F2: return CrafterKeys::F2;
|
|
||||||
case XKB_KEY_F3: return CrafterKeys::F3;
|
|
||||||
case XKB_KEY_F4: return CrafterKeys::F4;
|
|
||||||
case XKB_KEY_F5: return CrafterKeys::F5;
|
|
||||||
case XKB_KEY_F6: return CrafterKeys::F6;
|
|
||||||
case XKB_KEY_F7: return CrafterKeys::F7;
|
|
||||||
case XKB_KEY_F8: return CrafterKeys::F8;
|
|
||||||
case XKB_KEY_F9: return CrafterKeys::F9;
|
|
||||||
case XKB_KEY_F10: return CrafterKeys::F10;
|
|
||||||
case XKB_KEY_F11: return CrafterKeys::F11;
|
|
||||||
case XKB_KEY_F12: return CrafterKeys::F12;
|
|
||||||
|
|
||||||
// Control keys
|
|
||||||
case XKB_KEY_Escape: return CrafterKeys::Escape;
|
|
||||||
case XKB_KEY_Tab: return CrafterKeys::Tab;
|
|
||||||
case XKB_KEY_Return: return CrafterKeys::Enter;
|
|
||||||
case XKB_KEY_space: return CrafterKeys::Space;
|
|
||||||
case XKB_KEY_BackSpace: return CrafterKeys::Backspace;
|
|
||||||
case XKB_KEY_Delete: return CrafterKeys::Delete;
|
|
||||||
case XKB_KEY_Insert: return CrafterKeys::Insert;
|
|
||||||
case XKB_KEY_Home: return CrafterKeys::Home;
|
|
||||||
case XKB_KEY_End: return CrafterKeys::End;
|
|
||||||
case XKB_KEY_Page_Up: return CrafterKeys::PageUp;
|
|
||||||
case XKB_KEY_Page_Down: return CrafterKeys::PageDown;
|
|
||||||
case XKB_KEY_Caps_Lock: return CrafterKeys::CapsLock;
|
|
||||||
case XKB_KEY_Num_Lock: return CrafterKeys::NumLock;
|
|
||||||
case XKB_KEY_Scroll_Lock:return CrafterKeys::ScrollLock;
|
|
||||||
|
|
||||||
// Modifiers
|
|
||||||
case XKB_KEY_Shift_L: return CrafterKeys::LeftShift;
|
|
||||||
case XKB_KEY_Shift_R: return CrafterKeys::RightShift;
|
|
||||||
case XKB_KEY_Control_L: return CrafterKeys::LeftCtrl;
|
|
||||||
case XKB_KEY_Control_R: return CrafterKeys::RightCtrl;
|
|
||||||
case XKB_KEY_Alt_L: return CrafterKeys::LeftAlt;
|
|
||||||
case XKB_KEY_Alt_R: return CrafterKeys::RightAlt;
|
|
||||||
case XKB_KEY_Super_L: return CrafterKeys::LeftSuper;
|
|
||||||
case XKB_KEY_Super_R: return CrafterKeys::RightSuper;
|
|
||||||
|
|
||||||
// Arrows
|
|
||||||
case XKB_KEY_Up: return CrafterKeys::Up;
|
|
||||||
case XKB_KEY_Down: return CrafterKeys::Down;
|
|
||||||
case XKB_KEY_Left: return CrafterKeys::Left;
|
|
||||||
case XKB_KEY_Right: return CrafterKeys::Right;
|
|
||||||
|
|
||||||
// Keypad
|
|
||||||
case XKB_KEY_KP_0: return CrafterKeys::keypad_0;
|
|
||||||
case XKB_KEY_KP_1: return CrafterKeys::keypad_1;
|
|
||||||
case XKB_KEY_KP_2: return CrafterKeys::keypad_2;
|
|
||||||
case XKB_KEY_KP_3: return CrafterKeys::keypad_3;
|
|
||||||
case XKB_KEY_KP_4: return CrafterKeys::keypad_4;
|
|
||||||
case XKB_KEY_KP_5: return CrafterKeys::keypad_5;
|
|
||||||
case XKB_KEY_KP_6: return CrafterKeys::keypad_6;
|
|
||||||
case XKB_KEY_KP_7: return CrafterKeys::keypad_7;
|
|
||||||
case XKB_KEY_KP_8: return CrafterKeys::keypad_8;
|
|
||||||
case XKB_KEY_KP_9: return CrafterKeys::keypad_9;
|
|
||||||
case XKB_KEY_KP_Enter: return CrafterKeys::keypad_enter;
|
|
||||||
case XKB_KEY_KP_Add: return CrafterKeys::keypad_plus;
|
|
||||||
case XKB_KEY_KP_Subtract: return CrafterKeys::keypad_minus;
|
|
||||||
case XKB_KEY_KP_Multiply: return CrafterKeys::keypad_multiply;
|
|
||||||
case XKB_KEY_KP_Divide: return CrafterKeys::keypad_divide;
|
|
||||||
case XKB_KEY_KP_Decimal: return CrafterKeys::keypad_decimal;
|
|
||||||
|
|
||||||
// Punctuation
|
|
||||||
case XKB_KEY_grave: return CrafterKeys::grave;
|
|
||||||
case XKB_KEY_minus: return CrafterKeys::minus;
|
|
||||||
case XKB_KEY_equal: return CrafterKeys::equal;
|
|
||||||
case XKB_KEY_bracketleft: return CrafterKeys::bracket_left;
|
|
||||||
case XKB_KEY_bracketright:return CrafterKeys::bracket_right;
|
|
||||||
case XKB_KEY_backslash: return CrafterKeys::backslash;
|
|
||||||
case XKB_KEY_semicolon: return CrafterKeys::semicolon;
|
|
||||||
case XKB_KEY_apostrophe: return CrafterKeys::quote;
|
|
||||||
case XKB_KEY_comma: return CrafterKeys::comma;
|
|
||||||
case XKB_KEY_period: return CrafterKeys::period;
|
|
||||||
case XKB_KEY_slash: return CrafterKeys::slash;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return CrafterKeys::CrafterKeysMax;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
|
|
||||||
if (!window->xkb_state) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
xkb_keycode_t keycode = key + 8;
|
|
||||||
xkb_keysym_t keysym = xkb_state_key_get_one_sym(window->xkb_state, keycode);
|
|
||||||
CrafterKeys crafterKey = keysym_to_crafter_key(keysym);
|
|
||||||
|
|
||||||
if(state == WL_KEYBOARD_KEY_STATE_PRESSED) {
|
|
||||||
if(window->heldkeys[static_cast<std::uint8_t>(crafterKey)]) {
|
|
||||||
window->onKeyHold[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyHold.Invoke(crafterKey);
|
|
||||||
} else{
|
|
||||||
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = true;
|
|
||||||
window->onKeyDown[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyDown.Invoke(crafterKey);
|
|
||||||
}
|
|
||||||
} else{
|
|
||||||
window->heldkeys[static_cast<std::uint8_t>(crafterKey)] = false;
|
|
||||||
window->onKeyUp[static_cast<std::uint8_t>(crafterKey)].Invoke();
|
|
||||||
window->onAnyKeyUp.Invoke(crafterKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
window->seat = seat;
|
|
||||||
if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
|
|
||||||
wl_pointer* pointer = wl_seat_get_pointer(seat);
|
|
||||||
wl_pointer_add_listener(pointer, &pointer_listener, window);
|
|
||||||
}
|
|
||||||
if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) {
|
|
||||||
wl_keyboard* keyboard = wl_seat_get_keyboard(seat);
|
|
||||||
wl_keyboard_add_listener(keyboard, &keyboard_listener, window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
if (strcmp(interface, wl_shm_interface.name) == 0) {
|
|
||||||
window->shm = reinterpret_cast<wl_shm*>(wl_registry_bind(registry, name, &wl_shm_interface, 1));
|
|
||||||
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
|
||||||
wl_seat* seat = reinterpret_cast<wl_seat*>(wl_registry_bind(registry, name, &wl_seat_interface, 1));
|
|
||||||
wl_seat_add_listener(seat, &seat_listener, window);
|
|
||||||
} else if (compositor == NULL && strcmp(interface, wl_compositor_interface.name) == 0) {
|
|
||||||
compositor = reinterpret_cast<wl_compositor*>(wl_registry_bind(registry, name, &wl_compositor_interface, 3));
|
|
||||||
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
|
||||||
window->xdgWmBase = reinterpret_cast<xdg_wm_base*>(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1));
|
|
||||||
xdg_wm_base_add_listener(window->xdgWmBase, &xdgWmBaseListener, NULL);
|
|
||||||
} else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
|
|
||||||
window->manager = reinterpret_cast<zxdg_decoration_manager_v1*>(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1));
|
|
||||||
} else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
|
|
||||||
window->wpViewporter = reinterpret_cast<wp_viewporter*>(wl_registry_bind(registry, name, &wp_viewporter_interface, 1));
|
|
||||||
} else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
|
|
||||||
window->fractionalScaleManager = reinterpret_cast<wp_fractional_scale_manager_v1*>(wl_registry_bind(registry, name, &wp_fractional_scale_manager_v1_interface, 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::handle_global_remove(void* data, wl_registry* registry, uint32_t name) {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::xdg_toplevel_handle_close(void* data, xdg_toplevel*) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
window->onClose.Invoke();
|
|
||||||
window->open = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
// The compositor configures our surface, acknowledge the configure event
|
|
||||||
xdg_surface_ack_configure(xdg_surface, serial);
|
|
||||||
|
|
||||||
if (window->configured) {
|
|
||||||
// If this isn't the first configure event we've received, we already
|
|
||||||
// have a buffer attached, so no need to do anything. Commit the
|
|
||||||
// surface to apply the configure acknowledgement.
|
|
||||||
wl_surface_commit(window->surface);
|
|
||||||
}
|
|
||||||
|
|
||||||
window->configured = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowWayland::xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale) {
|
|
||||||
WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
|
|
||||||
|
|
||||||
window->scale = scale / 120.0f;
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module;
|
module;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
export module Crafter.Graphics:DescriptorPoolVulkan;
|
export module Crafter.Graphics:DescriptorPoolVulkan;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import std;
|
import std;
|
||||||
import :VulkanDevice;
|
import :Device;
|
||||||
import :Types;
|
import :Types;
|
||||||
import :DescriptorSetLayoutVulkan;
|
import :DescriptorSetLayoutVulkan;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
|
|
@ -155,7 +155,7 @@ export namespace Crafter {
|
||||||
public:
|
public:
|
||||||
void BuildPool(std::span<const VkDescriptorPoolSize> poolSizes, std::span<const VkDescriptorSetLayout> setLayouts) {
|
void BuildPool(std::span<const VkDescriptorPoolSize> poolSizes, std::span<const VkDescriptorSetLayout> setLayouts) {
|
||||||
if(descriptorPool != VK_NULL_HANDLE) {
|
if(descriptorPool != VK_NULL_HANDLE) {
|
||||||
vkDestroyDescriptorPool(VulkanDevice::device, descriptorPool, nullptr);
|
vkDestroyDescriptorPool(Device::device, descriptorPool, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -166,7 +166,7 @@ export namespace Crafter {
|
||||||
.pPoolSizes = poolSizes.data()
|
.pPoolSizes = poolSizes.data()
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateDescriptorPool(VulkanDevice::device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
Device::CheckVkResult(vkCreateDescriptorPool(Device::device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||||||
|
|
||||||
VkDescriptorSetAllocateInfo allocInfo {
|
VkDescriptorSetAllocateInfo allocInfo {
|
||||||
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
|
||||||
|
|
@ -175,7 +175,7 @@ export namespace Crafter {
|
||||||
.pSetLayouts = setLayouts.data(),
|
.pSetLayouts = setLayouts.data(),
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkAllocateDescriptorSets(VulkanDevice::device, &allocInfo, sets.data()));
|
Device::CheckVkResult(vkAllocateDescriptorSets(Device::device, &allocInfo, sets.data()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module;
|
module;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
export module Crafter.Graphics:DescriptorSetLayoutVulkan;
|
export module Crafter.Graphics:DescriptorSetLayoutVulkan;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import std;
|
import std;
|
||||||
import :VulkanDevice;
|
import :Device;
|
||||||
import :Types;
|
import :Types;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
|
|
||||||
|
|
@ -39,7 +39,7 @@ export namespace Crafter {
|
||||||
.bindingCount = static_cast<std::uint32_t>(descriptors.size()),
|
.bindingCount = static_cast<std::uint32_t>(descriptors.size()),
|
||||||
.pBindings = descriptors.data()
|
.pBindings = descriptors.data()
|
||||||
};
|
};
|
||||||
VulkanDevice::CheckVkResult(vkCreateDescriptorSetLayout(VulkanDevice::device, &descriptorLayoutInfoMesh, nullptr, &layout));
|
Device::CheckVkResult(vkCreateDescriptorSetLayout(Device::device, &descriptorLayoutInfoMesh, nullptr, &layout));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -54,7 +54,7 @@ export namespace Crafter {
|
||||||
.bindingCount = DescriptorCount,
|
.bindingCount = DescriptorCount,
|
||||||
.pBindings = Descriptors.data()
|
.pBindings = Descriptors.data()
|
||||||
};
|
};
|
||||||
VulkanDevice::CheckVkResult(vkCreateDescriptorSetLayout(VulkanDevice::device, &descriptorLayoutInfoMesh, nullptr, &layout));
|
Device::CheckVkResult(vkCreateDescriptorSetLayout(Device::device, &descriptorLayoutInfoMesh, nullptr, &layout));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
125
interfaces/Crafter.Graphics-Device.cppm
Normal file
125
interfaces/Crafter.Graphics-Device.cppm
Normal file
|
|
@ -0,0 +1,125 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2026 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 version 3.0 as published by the Free Software Foundation;
|
||||||
|
|
||||||
|
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;
|
||||||
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
|
#include "vulkan/vulkan.h"
|
||||||
|
#endif
|
||||||
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||||
|
#include <wayland-client.h>
|
||||||
|
#include <wayland-client-protocol.h>
|
||||||
|
#include "../lib/xdg-shell-client-protocol.h"
|
||||||
|
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
|
||||||
|
#include "../lib/fractional-scale-v1.h"
|
||||||
|
#include "../lib/viewporter.h"
|
||||||
|
#include <xkbcommon/xkbcommon.h>
|
||||||
|
#endif
|
||||||
|
export module Crafter.Graphics:Device;
|
||||||
|
import std;
|
||||||
|
|
||||||
|
export namespace Crafter {
|
||||||
|
struct Window;
|
||||||
|
struct Device {
|
||||||
|
static void Initialize();
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||||
|
inline static wl_display* display = nullptr;
|
||||||
|
inline static wl_seat* seat = nullptr;
|
||||||
|
inline static xdg_wm_base* xdgWmBase = nullptr;
|
||||||
|
inline static wp_fractional_scale_manager_v1* fractionalScaleManager = nullptr;
|
||||||
|
inline static zxdg_decoration_manager_v1* manager = nullptr;
|
||||||
|
inline static xkb_keymap* xkb_keymap;
|
||||||
|
inline static wl_compositor* compositor = nullptr;
|
||||||
|
inline static Window* focusedWindow = nullptr;
|
||||||
|
inline static wl_shm* shm = nullptr;
|
||||||
|
inline static wp_viewporter* wpViewporter = nullptr;
|
||||||
|
inline static xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
||||||
|
inline static xkb_state* xkb_state;
|
||||||
|
inline static std::vector<Window*> windows;
|
||||||
|
|
||||||
|
static void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities);
|
||||||
|
static void xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale);
|
||||||
|
static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial);
|
||||||
|
static void handle_global(void* data, wl_registry* registry, std::uint32_t name, const char* interface, std::uint32_t version);
|
||||||
|
static void handle_global_remove(void* data, wl_registry* registry, uint32_t name);
|
||||||
|
static void keyboard_keymap(void* data, wl_keyboard* keyboard, uint32_t format, int fd, uint32_t size);
|
||||||
|
static void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys);
|
||||||
|
static void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface);
|
||||||
|
static void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
|
||||||
|
static void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
|
||||||
|
static void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay);
|
||||||
|
static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state);
|
||||||
|
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, std::uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
||||||
|
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value);
|
||||||
|
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, std::uint32_t serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
||||||
|
static void PointerListenerHandleLeave(void*, wl_pointer*, std::uint32_t, wl_surface*);
|
||||||
|
|
||||||
|
constexpr static wl_pointer_listener pointer_listener = {
|
||||||
|
.enter = PointerListenerHandleEnter,
|
||||||
|
.leave = PointerListenerHandleLeave,
|
||||||
|
.motion = PointerListenerHandleMotion,
|
||||||
|
.button = pointer_handle_button,
|
||||||
|
.axis = PointerListenerHandleAxis,
|
||||||
|
};
|
||||||
|
constexpr static wl_keyboard_listener keyboard_listener = {
|
||||||
|
.keymap = keyboard_keymap,
|
||||||
|
.enter = keyboard_enter,
|
||||||
|
.leave = keyboard_leave,
|
||||||
|
.key = keyboard_key,
|
||||||
|
.modifiers = keyboard_modifiers,
|
||||||
|
.repeat_info = keyboard_repeat_info,
|
||||||
|
};
|
||||||
|
constexpr static wl_seat_listener seat_listener = {
|
||||||
|
.capabilities = seat_handle_capabilities,
|
||||||
|
};
|
||||||
|
constexpr static wl_registry_listener registry_listener = {
|
||||||
|
.global = handle_global,
|
||||||
|
.global_remove = handle_global_remove,
|
||||||
|
};
|
||||||
|
constexpr static xdg_wm_base_listener xdgWmBaseListener = {
|
||||||
|
.ping = xdg_wm_base_handle_ping,
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
|
inline static VkInstance instance = VK_NULL_HANDLE;
|
||||||
|
inline static VkDebugUtilsMessengerEXT debugMessenger = VK_NULL_HANDLE;
|
||||||
|
inline static VkPhysicalDevice physDevice = VK_NULL_HANDLE;
|
||||||
|
inline static VkDevice device = VK_NULL_HANDLE;
|
||||||
|
inline static std::uint32_t queueFamilyIndex = 0;
|
||||||
|
inline static VkQueue queue = VK_NULL_HANDLE;
|
||||||
|
inline static VkCommandPool commandPool = VK_NULL_HANDLE;
|
||||||
|
inline static VkSwapchainKHR swapchain = VK_NULL_HANDLE;
|
||||||
|
inline static PFN_vkGetAccelerationStructureBuildSizesKHR vkGetAccelerationStructureBuildSizesKHR;
|
||||||
|
inline static PFN_vkCreateAccelerationStructureKHR vkCreateAccelerationStructureKHR;
|
||||||
|
inline static PFN_vkCmdBuildAccelerationStructuresKHR vkCmdBuildAccelerationStructuresKHR;
|
||||||
|
inline static PFN_vkGetAccelerationStructureDeviceAddressKHR vkGetAccelerationStructureDeviceAddressKHR;
|
||||||
|
inline static PFN_vkCreateRayTracingPipelinesKHR vkCreateRayTracingPipelinesKHR;
|
||||||
|
inline static PFN_vkGetRayTracingShaderGroupHandlesKHR vkGetRayTracingShaderGroupHandlesKHR;
|
||||||
|
inline static PFN_vkCmdTraceRaysKHR vkCmdTraceRaysKHR;
|
||||||
|
inline static VkPhysicalDeviceMemoryProperties memoryProperties;
|
||||||
|
inline static VkPhysicalDeviceRayTracingPipelinePropertiesKHR rayTracingProperties = {
|
||||||
|
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR
|
||||||
|
};
|
||||||
|
|
||||||
|
static void CheckVkResult(VkResult result);
|
||||||
|
static std::uint32_t GetMemoryType(std::uint32_t typeBits, VkMemoryPropertyFlags properties);
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
catcrafts.net
|
catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -19,11 +19,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
export module Crafter.Graphics:GridElement;
|
export module Crafter.Graphics:GridElement;
|
||||||
import std;
|
import std;
|
||||||
import :Transform;
|
import :Transform2D;
|
||||||
import :Types;
|
import :Types;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
class GridElement : public Transform {
|
class GridElement : public Transform2D {
|
||||||
public:
|
public:
|
||||||
std::uint32_t columns;
|
std::uint32_t columns;
|
||||||
std::uint32_t rows;
|
std::uint32_t rows;
|
||||||
|
|
@ -31,9 +31,9 @@ export namespace Crafter {
|
||||||
std::int32_t spacingY;
|
std::int32_t spacingY;
|
||||||
std::int32_t paddingX;
|
std::int32_t paddingX;
|
||||||
std::int32_t paddingY;
|
std::int32_t paddingY;
|
||||||
GridElement(std::uint32_t columns, std::uint32_t rows, std::int32_t spacingX, std::int32_t spacingY, std::int32_t paddingX, std::int32_t paddingY, Anchor anchor);
|
GridElement(std::uint32_t columns, std::uint32_t rows, std::int32_t spacingX, std::int32_t spacingY, std::int32_t paddingX, std::int32_t paddingY, Anchor2D anchor);
|
||||||
void UpdatePositionScaled(Window& window);
|
void UpdatePositionScaled(Rendertarget& window);
|
||||||
void UpdatePosition(Window& window) override;
|
void UpdatePosition(Rendertarget& window) override;
|
||||||
void UpdatePosition(Window& window, Transform& parent) override;
|
void UpdatePosition(Rendertarget& window, Transform2D& parent) override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -1,34 +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
|
|
||||||
*/
|
|
||||||
|
|
||||||
export module Crafter.Graphics:Image;
|
|
||||||
import std;
|
|
||||||
|
|
||||||
import :Types;
|
|
||||||
|
|
||||||
namespace Crafter {
|
|
||||||
export struct Image {
|
|
||||||
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
|
|
||||||
std::uint_fast32_t width;
|
|
||||||
std::uint_fast32_t height;
|
|
||||||
OpaqueType opaque;
|
|
||||||
Image(const std::string_view path);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -19,7 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0215-1301 USA
|
||||||
|
|
||||||
module;
|
module;
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -28,7 +28,7 @@ import std;
|
||||||
import :VulkanBuffer;
|
import :VulkanBuffer;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
template <typename PixelType>
|
template <typename PixelType>
|
||||||
class ImageVulkan {
|
class ImageVulkan {
|
||||||
public:
|
public:
|
||||||
|
|
@ -61,18 +61,18 @@ export namespace Crafter {
|
||||||
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateImage(VulkanDevice::device, &imageInfo, nullptr, &image));
|
Device::CheckVkResult(vkCreateImage(Device::device, &imageInfo, nullptr, &image));
|
||||||
|
|
||||||
VkMemoryRequirements memRequirements;
|
VkMemoryRequirements memRequirements;
|
||||||
vkGetImageMemoryRequirements(VulkanDevice::device, image, &memRequirements);
|
vkGetImageMemoryRequirements(Device::device, image, &memRequirements);
|
||||||
|
|
||||||
VkMemoryAllocateInfo allocInfo = {};
|
VkMemoryAllocateInfo allocInfo = {};
|
||||||
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||||
allocInfo.allocationSize = memRequirements.size;
|
allocInfo.allocationSize = memRequirements.size;
|
||||||
allocInfo.memoryTypeIndex = VulkanDevice::GetMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
allocInfo.memoryTypeIndex = Device::GetMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkAllocateMemory(VulkanDevice::device, &allocInfo, nullptr, &imageMemory));
|
Device::CheckVkResult(vkAllocateMemory(Device::device, &allocInfo, nullptr, &imageMemory));
|
||||||
vkBindImageMemory(VulkanDevice::device, image, imageMemory, 0);
|
vkBindImageMemory(Device::device, image, imageMemory, 0);
|
||||||
|
|
||||||
VkImageViewCreateInfo viewInfo = {};
|
VkImageViewCreateInfo viewInfo = {};
|
||||||
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||||
|
|
@ -85,7 +85,7 @@ export namespace Crafter {
|
||||||
viewInfo.subresourceRange.baseArrayLayer = 0;
|
viewInfo.subresourceRange.baseArrayLayer = 0;
|
||||||
viewInfo.subresourceRange.layerCount = 1;
|
viewInfo.subresourceRange.layerCount = 1;
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateImageView(VulkanDevice::device, &viewInfo, nullptr, &imageView));
|
Device::CheckVkResult(vkCreateImageView(Device::device, &viewInfo, nullptr, &imageView));
|
||||||
|
|
||||||
// Final transition to shader read-only layout
|
// Final transition to shader read-only layout
|
||||||
TransitionImageLayout(cmd, image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0, 5);
|
TransitionImageLayout(cmd, image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 0, 5);
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
module;
|
module;
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -29,8 +29,7 @@ import Crafter.Math;
|
||||||
import :VulkanBuffer;
|
import :VulkanBuffer;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
|
||||||
class Mesh {
|
class Mesh {
|
||||||
public:
|
public:
|
||||||
VulkanBuffer<char, false, true, false> scratchBuffer;
|
VulkanBuffer<char, false, true, false> scratchBuffer;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
catcrafts.net
|
catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -21,29 +21,24 @@ export module Crafter.Graphics:MouseElement;
|
||||||
import std;
|
import std;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
import :Types;
|
import :Types;
|
||||||
import :Transform;
|
import :Transform2D;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
class Window;
|
class Window;
|
||||||
class WindowMouse;
|
struct MouseElement : Transform2D {
|
||||||
class MouseElement : public Transform {
|
Event<void> onMouseMove;
|
||||||
public:
|
Event<void> onMouseEnter;
|
||||||
Event<MouseMoveEvent> onMouseMove;
|
Event<void> onMouseLeave;
|
||||||
Event<MouseMoveEvent> onMouseEnter;
|
Event<void> onMouseRightClick;
|
||||||
Event<MouseMoveEvent> onMouseLeave;
|
Event<void> onMouseLeftClick;
|
||||||
Event<MousePoint> onMouseRightClick;
|
Event<void> onMouseRightHold;
|
||||||
Event<MousePoint> onMouseLeftClick;
|
Event<void> onMouseLeftHold;
|
||||||
Event<MousePoint> onMouseRightHold;
|
Event<void> onMouseRightRelease;
|
||||||
Event<MousePoint> onMouseLeftHold;
|
Event<void> onMouseLeftRelease;
|
||||||
Event<MousePoint> onMouseRightRelease;
|
|
||||||
Event<MousePoint> onMouseLeftRelease;
|
|
||||||
ScaleDataBoundless mouseScaled;
|
|
||||||
|
|
||||||
MouseElement();
|
MouseElement();
|
||||||
MouseElement(WindowMouse& window);
|
MouseElement(Window& window);
|
||||||
MouseElement(Anchor anchor);
|
MouseElement(Anchor2D anchor);
|
||||||
MouseElement(Anchor anchor, WindowMouse& window);
|
MouseElement(Anchor2D anchor, Window& window);
|
||||||
void UpdatePosition(Window& window) override;
|
|
||||||
void UpdatePosition(Window& window, Transform& parent) override;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -18,13 +18,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module;
|
module;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
export module Crafter.Graphics:PipelineRTVulkan;
|
export module Crafter.Graphics:PipelineRTVulkan;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import std;
|
import std;
|
||||||
import :VulkanDevice;
|
import :Device;
|
||||||
import :VulkanBuffer;
|
import :VulkanBuffer;
|
||||||
import :ShaderBindingTableVulkan;
|
import :ShaderBindingTableVulkan;
|
||||||
import :Types;
|
import :Types;
|
||||||
|
|
@ -48,7 +48,7 @@ export namespace Crafter {
|
||||||
.pSetLayouts = setLayouts.data()
|
.pSetLayouts = setLayouts.data()
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreatePipelineLayout(VulkanDevice::device, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
Device::CheckVkResult(vkCreatePipelineLayout(Device::device, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
||||||
|
|
||||||
std::vector<VkRayTracingShaderGroupCreateInfoKHR> groups;
|
std::vector<VkRayTracingShaderGroupCreateInfoKHR> groups;
|
||||||
groups.reserve(raygenGroups.size() + missGroups.size() + hitGroups.size());
|
groups.reserve(raygenGroups.size() + missGroups.size() + hitGroups.size());
|
||||||
|
|
@ -67,24 +67,24 @@ export namespace Crafter {
|
||||||
.layout = pipelineLayout
|
.layout = pipelineLayout
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateRayTracingPipelinesKHR(VulkanDevice::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline));
|
Device::CheckVkResult(Device::vkCreateRayTracingPipelinesKHR(Device::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline));
|
||||||
|
|
||||||
std::size_t dataSize = VulkanDevice::rayTracingProperties.shaderGroupHandleSize * rtPipelineInfo.groupCount;
|
std::size_t dataSize = Device::rayTracingProperties.shaderGroupHandleSize * rtPipelineInfo.groupCount;
|
||||||
shaderHandles.resize(dataSize);
|
shaderHandles.resize(dataSize);
|
||||||
VulkanDevice::CheckVkResult(VulkanDevice::vkGetRayTracingShaderGroupHandlesKHR(VulkanDevice::device, pipeline, 0, rtPipelineInfo.groupCount, dataSize, shaderHandles.data()));
|
Device::CheckVkResult(Device::vkGetRayTracingShaderGroupHandlesKHR(Device::device, pipeline, 0, rtPipelineInfo.groupCount, dataSize, shaderHandles.data()));
|
||||||
|
|
||||||
std::uint32_t sbtStride = AlignUp(VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleAlignment);
|
std::uint32_t sbtStride = AlignUp(Device::rayTracingProperties.shaderGroupHandleSize, Device::rayTracingProperties.shaderGroupHandleAlignment);
|
||||||
|
|
||||||
raygenRegion.stride = sbtStride;
|
raygenRegion.stride = sbtStride;
|
||||||
raygenRegion.deviceAddress = 0;
|
raygenRegion.deviceAddress = 0;
|
||||||
raygenRegion.size = raygenGroups.size() * sbtStride;
|
raygenRegion.size = raygenGroups.size() * sbtStride;
|
||||||
|
|
||||||
missRegion.stride = sbtStride;
|
missRegion.stride = sbtStride;
|
||||||
missRegion.deviceAddress = AlignUp(raygenRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
missRegion.deviceAddress = AlignUp(raygenRegion.size, Device::rayTracingProperties.shaderGroupBaseAlignment);
|
||||||
missRegion.size = missGroups.size() * sbtStride;
|
missRegion.size = missGroups.size() * sbtStride;
|
||||||
|
|
||||||
hitRegion.stride = sbtStride;
|
hitRegion.stride = sbtStride;
|
||||||
hitRegion.deviceAddress = AlignUp(missRegion.deviceAddress + missRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
hitRegion.deviceAddress = AlignUp(missRegion.deviceAddress + missRegion.size, Device::rayTracingProperties.shaderGroupBaseAlignment);
|
||||||
hitRegion.size = hitGroups.size() * sbtStride;
|
hitRegion.size = hitGroups.size() * sbtStride;
|
||||||
|
|
||||||
std::size_t bufferSize = hitRegion.deviceAddress + hitRegion.size;
|
std::size_t bufferSize = hitRegion.deviceAddress + hitRegion.size;
|
||||||
|
|
@ -93,15 +93,15 @@ export namespace Crafter {
|
||||||
std::uint8_t* offset = sbtBuffer.value;
|
std::uint8_t* offset = sbtBuffer.value;
|
||||||
std::uint8_t* handleOffset = shaderHandles.data();
|
std::uint8_t* handleOffset = shaderHandles.data();
|
||||||
|
|
||||||
std::memcpy(offset, handleOffset, raygenGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
std::memcpy(offset, handleOffset, raygenGroups.size() * Device::rayTracingProperties.shaderGroupHandleSize);
|
||||||
offset += AlignUp(raygenRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
offset += AlignUp(raygenRegion.size, Device::rayTracingProperties.shaderGroupBaseAlignment);
|
||||||
handleOffset += raygenGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize;
|
handleOffset += raygenGroups.size() * Device::rayTracingProperties.shaderGroupHandleSize;
|
||||||
|
|
||||||
std::memcpy(offset, handleOffset, missGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
std::memcpy(offset, handleOffset, missGroups.size() * Device::rayTracingProperties.shaderGroupHandleSize);
|
||||||
offset += AlignUp(missRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
offset += AlignUp(missRegion.size, Device::rayTracingProperties.shaderGroupBaseAlignment);
|
||||||
handleOffset += missGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize;
|
handleOffset += missGroups.size() * Device::rayTracingProperties.shaderGroupHandleSize;
|
||||||
|
|
||||||
std::memcpy(offset, handleOffset, hitGroups.size() * VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
std::memcpy(offset, handleOffset, hitGroups.size() * Device::rayTracingProperties.shaderGroupHandleSize);
|
||||||
|
|
||||||
sbtBuffer.FlushDevice(cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
sbtBuffer.FlushDevice(cmd, VK_ACCESS_MEMORY_READ_BIT, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
|
||||||
|
|
||||||
|
|
@ -142,7 +142,7 @@ export namespace Crafter {
|
||||||
.pSetLayouts = setLayouts.data()
|
.pSetLayouts = setLayouts.data()
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreatePipelineLayout(VulkanDevice::device, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
Device::CheckVkResult(vkCreatePipelineLayout(Device::device, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
||||||
|
|
||||||
constexpr auto groupIndexSeq = std::make_index_sequence<std::tuple_size_v<ShaderGroups>>{};
|
constexpr auto groupIndexSeq = std::make_index_sequence<std::tuple_size_v<ShaderGroups>>{};
|
||||||
|
|
||||||
|
|
@ -158,24 +158,24 @@ export namespace Crafter {
|
||||||
.layout = pipelineLayout
|
.layout = pipelineLayout
|
||||||
};
|
};
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(VulkanDevice::vkCreateRayTracingPipelinesKHR(VulkanDevice::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline));
|
Device::CheckVkResult(Device::vkCreateRayTracingPipelinesKHR(Device::device, {}, {}, 1, &rtPipelineInfo, nullptr, &pipeline));
|
||||||
|
|
||||||
std::size_t dataSize = VulkanDevice::rayTracingProperties.shaderGroupHandleSize * rtPipelineInfo.groupCount;
|
std::size_t dataSize = Device::rayTracingProperties.shaderGroupHandleSize * rtPipelineInfo.groupCount;
|
||||||
shaderHandles.resize(dataSize);
|
shaderHandles.resize(dataSize);
|
||||||
VulkanDevice::CheckVkResult(VulkanDevice::vkGetRayTracingShaderGroupHandlesKHR(VulkanDevice::device, pipeline, 0, rtPipelineInfo.groupCount, dataSize, shaderHandles.data()));
|
Device::CheckVkResult(Device::vkGetRayTracingShaderGroupHandlesKHR(Device::device, pipeline, 0, rtPipelineInfo.groupCount, dataSize, shaderHandles.data()));
|
||||||
|
|
||||||
std::uint32_t sbtStride = AlignUp(VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleAlignment);
|
std::uint32_t sbtStride = AlignUp(Device::rayTracingProperties.shaderGroupHandleSize, Device::rayTracingProperties.shaderGroupHandleAlignment);
|
||||||
|
|
||||||
raygenRegion.stride = sbtStride;
|
raygenRegion.stride = sbtStride;
|
||||||
raygenRegion.deviceAddress = 0;
|
raygenRegion.deviceAddress = 0;
|
||||||
raygenRegion.size = GetGroupCount<VK_SHADER_STAGE_RAYGEN_BIT_KHR>(groupIndexSeq) * sbtStride;
|
raygenRegion.size = GetGroupCount<VK_SHADER_STAGE_RAYGEN_BIT_KHR>(groupIndexSeq) * sbtStride;
|
||||||
|
|
||||||
missRegion.stride = sbtStride;
|
missRegion.stride = sbtStride;
|
||||||
missRegion.deviceAddress = AlignUp(raygenRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
missRegion.deviceAddress = AlignUp(raygenRegion.size, Device::rayTracingProperties.shaderGroupBaseAlignment);
|
||||||
missRegion.size = GetGroupCount<VK_SHADER_STAGE_MISS_BIT_KHR>(groupIndexSeq) * sbtStride;
|
missRegion.size = GetGroupCount<VK_SHADER_STAGE_MISS_BIT_KHR>(groupIndexSeq) * sbtStride;
|
||||||
|
|
||||||
hitRegion.stride = sbtStride;
|
hitRegion.stride = sbtStride;
|
||||||
hitRegion.deviceAddress = AlignUp(missRegion.deviceAddress + missRegion.size, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
hitRegion.deviceAddress = AlignUp(missRegion.deviceAddress + missRegion.size, Device::rayTracingProperties.shaderGroupBaseAlignment);
|
||||||
hitRegion.size = (GetGroupCount<VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR>(groupIndexSeq) * sbtStride) + (GetGroupCount<VK_SHADER_STAGE_ANY_HIT_BIT_KHR>(groupIndexSeq) * sbtStride);
|
hitRegion.size = (GetGroupCount<VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR>(groupIndexSeq) * sbtStride) + (GetGroupCount<VK_SHADER_STAGE_ANY_HIT_BIT_KHR>(groupIndexSeq) * sbtStride);
|
||||||
|
|
||||||
std::size_t bufferSize = hitRegion.deviceAddress + hitRegion.size;
|
std::size_t bufferSize = hitRegion.deviceAddress + hitRegion.size;
|
||||||
|
|
@ -263,19 +263,19 @@ export namespace Crafter {
|
||||||
if constexpr(groupTemplate::generalShader != VK_SHADER_UNUSED_KHR) {
|
if constexpr(groupTemplate::generalShader != VK_SHADER_UNUSED_KHR) {
|
||||||
using shaderTemplate = std::tuple_element_t<groupTemplate::generalShader, Shaders>;
|
using shaderTemplate = std::tuple_element_t<groupTemplate::generalShader, Shaders>;
|
||||||
if constexpr(shaderTemplate::_stage == stage) {
|
if constexpr(shaderTemplate::_stage == stage) {
|
||||||
std::memcpy(sbtBuffer.value + offset, shaderHandles.data() + index * VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
std::memcpy(sbtBuffer.value + offset, shaderHandles.data() + index * Device::rayTracingProperties.shaderGroupHandleSize, Device::rayTracingProperties.shaderGroupHandleSize);
|
||||||
offset += sbtStride;
|
offset += sbtStride;
|
||||||
}
|
}
|
||||||
} else if constexpr(groupTemplate::closestHitShader != VK_SHADER_UNUSED_KHR) {
|
} else if constexpr(groupTemplate::closestHitShader != VK_SHADER_UNUSED_KHR) {
|
||||||
using shaderTemplate = std::tuple_element_t<groupTemplate::closestHitShader, Shaders>;
|
using shaderTemplate = std::tuple_element_t<groupTemplate::closestHitShader, Shaders>;
|
||||||
if constexpr(shaderTemplate::_stage == stage) {
|
if constexpr(shaderTemplate::_stage == stage) {
|
||||||
std::memcpy(sbtBuffer.value + offset, shaderHandles.data() + index * VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
std::memcpy(sbtBuffer.value + offset, shaderHandles.data() + index * Device::rayTracingProperties.shaderGroupHandleSize, Device::rayTracingProperties.shaderGroupHandleSize);
|
||||||
offset += sbtStride;
|
offset += sbtStride;
|
||||||
}
|
}
|
||||||
} else if constexpr(groupTemplate::anyHitShader != VK_SHADER_UNUSED_KHR) {
|
} else if constexpr(groupTemplate::anyHitShader != VK_SHADER_UNUSED_KHR) {
|
||||||
using shaderTemplate = std::tuple_element_t<groupTemplate::anyHitShader, Shaders>;
|
using shaderTemplate = std::tuple_element_t<groupTemplate::anyHitShader, Shaders>;
|
||||||
if constexpr(shaderTemplate::_stage == stage) {
|
if constexpr(shaderTemplate::_stage == stage) {
|
||||||
std::memcpy(sbtBuffer.value + offset, shaderHandles.data() + index * VulkanDevice::rayTracingProperties.shaderGroupHandleSize, VulkanDevice::rayTracingProperties.shaderGroupHandleSize);
|
std::memcpy(sbtBuffer.value + offset, shaderHandles.data() + index * Device::rayTracingProperties.shaderGroupHandleSize, Device::rayTracingProperties.shaderGroupHandleSize);
|
||||||
offset += sbtStride;
|
offset += sbtStride;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -290,9 +290,9 @@ export namespace Crafter {
|
||||||
static void AddShaderGroupsToBuffer(std::uint32_t sbtStride, std::index_sequence<Is...>) {
|
static void AddShaderGroupsToBuffer(std::uint32_t sbtStride, std::index_sequence<Is...>) {
|
||||||
std::uint32_t offset = 0;
|
std::uint32_t offset = 0;
|
||||||
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_RAYGEN_BIT_KHR>(sbtStride, offset), ...);
|
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_RAYGEN_BIT_KHR>(sbtStride, offset), ...);
|
||||||
offset = AlignUp(offset, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
offset = AlignUp(offset, Device::rayTracingProperties.shaderGroupBaseAlignment);
|
||||||
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_MISS_BIT_KHR>(sbtStride, offset), ...);
|
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_MISS_BIT_KHR>(sbtStride, offset), ...);
|
||||||
offset = AlignUp(offset, VulkanDevice::rayTracingProperties.shaderGroupBaseAlignment);
|
offset = AlignUp(offset, Device::rayTracingProperties.shaderGroupBaseAlignment);
|
||||||
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR>(sbtStride, offset), ...);
|
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR>(sbtStride, offset), ...);
|
||||||
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_ANY_HIT_BIT_KHR>(sbtStride, offset), ...);
|
(AddShaderGroupToBuffer<Is, VK_SHADER_STAGE_ANY_HIT_BIT_KHR>(sbtStride, offset), ...);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,652 +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 version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
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 "../lib/stb_image.h"
|
|
||||||
#include "../lib/stb_truetype.h"
|
|
||||||
export module Crafter.Graphics:RenderingElement;
|
|
||||||
import std;
|
|
||||||
import :Transform;
|
|
||||||
import :Font;
|
|
||||||
import :Types;
|
|
||||||
import :Image;
|
|
||||||
import :Window;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
enum class TextAlignment {
|
|
||||||
Left,
|
|
||||||
Center,
|
|
||||||
Right
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class TextOverflowMode {
|
|
||||||
Clip,
|
|
||||||
Wrap
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class TextScaleMode {
|
|
||||||
None,
|
|
||||||
Font,
|
|
||||||
Element,
|
|
||||||
Buffer
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RenderElementScalingOwning {
|
|
||||||
std::vector<Pixel_BU8_GU8_RU8_AU8> scalingBuffer;
|
|
||||||
std::uint32_t bufferWidth;
|
|
||||||
std::uint32_t bufferHeight;
|
|
||||||
bool bufferUpdated = true;
|
|
||||||
RenderElementScalingOwning() = default;
|
|
||||||
RenderElementScalingOwning(std::uint32_t bufferWidth, std::uint32_t bufferHeight) : scalingBuffer(bufferWidth*bufferHeight), bufferWidth(bufferWidth), bufferHeight(bufferHeight) {
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RenderElementScalingNonOwning {
|
|
||||||
Pixel_BU8_GU8_RU8_AU8* scalingBuffer;
|
|
||||||
std::uint32_t bufferWidth;
|
|
||||||
std::uint32_t bufferHeight;
|
|
||||||
bool bufferUpdated = true;
|
|
||||||
RenderElementScalingNonOwning() = default;
|
|
||||||
RenderElementScalingNonOwning(Pixel_BU8_GU8_RU8_AU8* scalingBuffer, std::uint32_t bufferWidth, std::uint32_t bufferHeight) : scalingBuffer(scalingBuffer), bufferWidth(bufferWidth), bufferHeight(bufferHeight) {
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RenderElementRotating {
|
|
||||||
std::uint32_t rotation;
|
|
||||||
bool rotationUpdated = true;
|
|
||||||
RenderElementRotating() = default;
|
|
||||||
RenderElementRotating(std::uint32_t rotation) : rotation(rotation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct EmptyScalingBase {};
|
|
||||||
struct EmptyRotatingBase {};
|
|
||||||
|
|
||||||
template<bool Scaling, bool Owning>
|
|
||||||
using ScalingBase =
|
|
||||||
std::conditional_t<
|
|
||||||
Scaling,
|
|
||||||
std::conditional_t<Owning,
|
|
||||||
RenderElementScalingOwning,
|
|
||||||
RenderElementScalingNonOwning>,
|
|
||||||
EmptyScalingBase
|
|
||||||
>;
|
|
||||||
|
|
||||||
template<bool Rotating>
|
|
||||||
using RotatingBase =
|
|
||||||
std::conditional_t<
|
|
||||||
Rotating,
|
|
||||||
RenderElementRotating,
|
|
||||||
EmptyRotatingBase
|
|
||||||
>;
|
|
||||||
|
|
||||||
class RenderingElementBase : public Transform {
|
|
||||||
public:
|
|
||||||
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
|
|
||||||
OpaqueType opaque;
|
|
||||||
RenderingElementBase(Anchor anchor) : Transform(anchor) {
|
|
||||||
scaled.width = 0;
|
|
||||||
}
|
|
||||||
RenderingElementBase(Anchor anchor, OpaqueType opaque) : Transform(anchor), opaque(opaque) {
|
|
||||||
scaled.width = 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<bool Scaling, bool Owning, bool Rotating> requires ((!Rotating || Scaling) && (!Owning || Scaling))
|
|
||||||
class RenderingElement : public RenderingElementBase, public ScalingBase<Scaling, Owning>, public RotatingBase<Rotating> {
|
|
||||||
public:
|
|
||||||
RenderingElement() = default;
|
|
||||||
RenderingElement(Anchor anchor, OpaqueType opaque) : RenderingElementBase(anchor, opaque) {
|
|
||||||
|
|
||||||
}
|
|
||||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint32_t rotation) requires(Rotating) : RenderingElementBase(anchor, opaque), RotatingBase<Rotating>(rotation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderingElement(Anchor anchor, const std::string_view imagePath) : RenderingElementBase(anchor) {
|
|
||||||
LoadImage(imagePath);
|
|
||||||
}
|
|
||||||
RenderingElement(Anchor anchor, const std::string_view imagePath, std::uint32_t rotation) requires(Rotating) : RenderingElementBase(anchor), RotatingBase<Rotating>(rotation) {
|
|
||||||
LoadImage(imagePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderingElement(Anchor anchor, const std::string_view imagePath, OpaqueType opaque) : RenderingElementBase(anchor, opaque) {
|
|
||||||
LoadImageNoOpaqueCheck(imagePath);
|
|
||||||
}
|
|
||||||
RenderingElement(Anchor anchor, const std::string_view imagePath, OpaqueType opaque, std::uint32_t rotation) requires(Rotating) : RenderingElementBase(anchor, opaque), RotatingBase<Rotating>(rotation) {
|
|
||||||
LoadImageNoOpaqueCheck(imagePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint32_t bufferWidth, std::uint32_t bufferHeight, Pixel_BU8_GU8_RU8_AU8* scalingBuffer) requires(Scaling && !Owning) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight, scalingBuffer) {
|
|
||||||
|
|
||||||
}
|
|
||||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint32_t bufferWidth, std::uint32_t bufferHeight, Pixel_BU8_GU8_RU8_AU8* scalingBuffer, std::uint32_t rotation) requires(Scaling && !Owning && Rotating) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight, scalingBuffer), RotatingBase<Rotating>(rotation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderingElement(Anchor anchor, OpaqueType opaque, Image& image) requires(Scaling && !Owning) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(image.buffer.data(), image.width, image.height) {
|
|
||||||
|
|
||||||
}
|
|
||||||
RenderingElement(Anchor anchor, OpaqueType opaque, Image& image, std::uint32_t rotation) requires(Scaling && !Owning && Rotating) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(image.buffer.data(), image.width, image.height), RotatingBase<Rotating>(rotation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderingElement(Anchor anchor, Image& image) requires(Scaling && !Owning) : RenderingElementBase(anchor, image.opaque), ScalingBase<Scaling, Owning>(image.buffer.data(), image.width, image.height) {
|
|
||||||
|
|
||||||
}
|
|
||||||
RenderingElement(Anchor anchor, Image& image, std::uint32_t rotation) requires(Scaling && !Owning && Rotating) : RenderingElementBase(anchor, image.opaque), ScalingBase<Scaling, Owning>(image.buffer.data(), image.width, image.height), RotatingBase<Rotating>(rotation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint32_t bufferWidth, std::uint32_t bufferHeight) requires(Owning) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight) {
|
|
||||||
|
|
||||||
}
|
|
||||||
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t rotation) requires(Owning && Rotating) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight) , RotatingBase<Rotating>(rotation) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderingElement(RenderingElement&) = delete;
|
|
||||||
RenderingElement& operator=(RenderingElement&) = delete;
|
|
||||||
|
|
||||||
void ScaleNearestNeighbor() requires(Scaling) {
|
|
||||||
for (std::uint32_t y = 0; y < scaled.height; y++) {
|
|
||||||
std::uint32_t srcY = y * ScalingBase<true, Owning>::bufferHeight / scaled.height;
|
|
||||||
for (std::uint32_t x = 0; x < scaled.width; x++) {
|
|
||||||
std::uint32_t srcX = x * ScalingBase<true, Owning>::bufferWidth / scaled.width;
|
|
||||||
buffer[y * scaled.width + x] = ScalingBase<true, Owning>::scalingBuffer[srcY * ScalingBase<true, Owning>::bufferWidth + srcX];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ScaleRotating() requires(Scaling) {
|
|
||||||
const double rad = (static_cast<double>(RotatingBase<true>::rotation) / static_cast<double>(std::numeric_limits<std::uint32_t>::max())) * 2.0 * std::numbers::pi;
|
|
||||||
|
|
||||||
const std::uint32_t dstWidth = scaled.width;
|
|
||||||
const std::uint32_t dstHeight = scaled.height;
|
|
||||||
|
|
||||||
const double c2 = std::abs(std::cos(rad));
|
|
||||||
const double s2 = std::abs(std::sin(rad));
|
|
||||||
|
|
||||||
const double rotatedWidth = dstWidth * c2 + dstHeight * s2;
|
|
||||||
const double rotatedHeight = dstWidth * s2 + dstHeight * c2;
|
|
||||||
|
|
||||||
const std::uint32_t diffX = static_cast<std::uint32_t>(std::ceil((rotatedWidth - dstWidth) * 0.5));
|
|
||||||
const std::uint32_t diffY = static_cast<std::uint32_t>(std::ceil((rotatedHeight - dstHeight) * 0.5));
|
|
||||||
|
|
||||||
scaled.width += diffX + diffX;
|
|
||||||
scaled.height += diffY + diffY;
|
|
||||||
|
|
||||||
scaled.x -= diffX;
|
|
||||||
scaled.y -= diffY;
|
|
||||||
|
|
||||||
buffer.clear();
|
|
||||||
buffer.resize(scaled.width * scaled.height);
|
|
||||||
|
|
||||||
// Destination center
|
|
||||||
const double dstCx = (static_cast<double>(scaled.width) - 1.0) * 0.5;
|
|
||||||
const double dstCy = (static_cast<double>(scaled.height) - 1.0) * 0.5;
|
|
||||||
|
|
||||||
// Source center
|
|
||||||
const double srcCx = (static_cast<double>(ScalingBase<true, Owning>::bufferWidth) - 1.0) * 0.5;
|
|
||||||
const double srcCy = (static_cast<double>(ScalingBase<true, Owning>::bufferHeight) - 1.0) * 0.5;
|
|
||||||
|
|
||||||
const double c = std::cos(rad);
|
|
||||||
const double s = std::sin(rad);
|
|
||||||
|
|
||||||
// Scale factors (destination → source)
|
|
||||||
const double scaleX = static_cast<double>(ScalingBase<true, Owning>::bufferWidth) / dstWidth;
|
|
||||||
const double scaleY = static_cast<double>(ScalingBase<true, Owning>::bufferHeight) / dstHeight;
|
|
||||||
|
|
||||||
for (std::uint32_t yB = 0; yB < scaled.height; ++yB) {
|
|
||||||
for (std::uint32_t xB = 0; xB < scaled.width; ++xB) {
|
|
||||||
|
|
||||||
// ---- Destination pixel relative to center ----
|
|
||||||
const double dx = (static_cast<double>(xB) - dstCx) * scaleX;
|
|
||||||
const double dy = (static_cast<double>(yB) - dstCy) * scaleY;
|
|
||||||
|
|
||||||
// ---- Inverse rotation ----
|
|
||||||
const double sx = (c * dx - s * dy) + srcCx;
|
|
||||||
const double sy = (s * dx + c * dy) + srcCy;
|
|
||||||
|
|
||||||
// ---- Nearest neighbour sampling ----
|
|
||||||
const std::int32_t srcX = static_cast<std::int32_t>(std::round(sx));
|
|
||||||
const std::int32_t srcY = static_cast<std::int32_t>(std::round(sy));
|
|
||||||
|
|
||||||
if (srcX >= 0 && srcX < ScalingBase<true, Owning>::bufferWidth && srcY >= 0 && srcY < ScalingBase<true, Owning>::bufferHeight) {
|
|
||||||
buffer[yB * scaled.width + xB] = ScalingBase<true, Owning>::scalingBuffer[srcY * ScalingBase<true, Owning>::bufferWidth + srcX];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdatePosition(Window& window, ScaleData oldScale) {
|
|
||||||
if constexpr(Scaling && !Rotating) {
|
|
||||||
if(oldScale.width != scaled.width || oldScale.height != scaled.height) {
|
|
||||||
buffer.resize(scaled.width * scaled.height);
|
|
||||||
ScaleNearestNeighbor();
|
|
||||||
window.AddDirtyRect(oldScale);
|
|
||||||
window.AddDirtyRect(scaled);
|
|
||||||
} else if(oldScale.x != scaled.x || oldScale.y != scaled.y) {
|
|
||||||
window.AddDirtyRect(oldScale);
|
|
||||||
window.AddDirtyRect(scaled);
|
|
||||||
if(ScalingBase<true, Owning>::bufferUpdated) {
|
|
||||||
ScaleNearestNeighbor();
|
|
||||||
ScalingBase<true, Owning>::bufferUpdated = false;
|
|
||||||
}
|
|
||||||
} else if(ScalingBase<true, Owning>::bufferUpdated) {
|
|
||||||
ScaleNearestNeighbor();
|
|
||||||
ScalingBase<true, Owning>::bufferUpdated = false;
|
|
||||||
}
|
|
||||||
} else if constexpr(Rotating) {
|
|
||||||
if(oldScale.width != scaled.width || oldScale.height != scaled.height) {
|
|
||||||
buffer.resize(scaled.width * scaled.height);
|
|
||||||
ScaleRotating();
|
|
||||||
window.AddDirtyRect(oldScale);
|
|
||||||
window.AddDirtyRect(scaled);
|
|
||||||
} else if(oldScale.x != scaled.x || oldScale.y != scaled.y) {
|
|
||||||
window.AddDirtyRect(oldScale);
|
|
||||||
window.AddDirtyRect(scaled);
|
|
||||||
if(ScalingBase<true, Owning>::bufferUpdated || RotatingBase<true>::rotationUpdated) {
|
|
||||||
ScaleRotating();
|
|
||||||
ScalingBase<true, Owning>::bufferUpdated = false;
|
|
||||||
RotatingBase<true>::rotationUpdated = false;
|
|
||||||
}
|
|
||||||
} else if(ScalingBase<true, Owning>::bufferUpdated || RotatingBase<true>::rotationUpdated) {
|
|
||||||
ScaleRotating();
|
|
||||||
ScalingBase<true, Owning>::bufferUpdated = false;
|
|
||||||
RotatingBase<true>::rotationUpdated = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(oldScale.width != scaled.width || oldScale.height != scaled.height) {
|
|
||||||
buffer.resize(scaled.width * scaled.height);
|
|
||||||
window.AddDirtyRect(oldScale);
|
|
||||||
window.AddDirtyRect(scaled);
|
|
||||||
}
|
|
||||||
if(oldScale.x != scaled.x || oldScale.y != scaled.y) {
|
|
||||||
window.AddDirtyRect(oldScale);
|
|
||||||
window.AddDirtyRect(scaled);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdatePosition(Window& window) override {
|
|
||||||
ScaleData oldScale = scaled;
|
|
||||||
window.ScaleElement(*this);
|
|
||||||
UpdatePosition(window, oldScale);
|
|
||||||
for(Transform* child : children) {
|
|
||||||
child->UpdatePosition(window, *this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdatePosition(Window& window, Transform& parent) override {
|
|
||||||
ScaleData oldScale = scaled;
|
|
||||||
window.ScaleElement(*this, parent);
|
|
||||||
UpdatePosition(window, oldScale);
|
|
||||||
for(Transform* child : children) {
|
|
||||||
child->UpdatePosition(window, *this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LoadImage(const std::string_view imagePath) {
|
|
||||||
std::filesystem::path abs = std::filesystem::absolute(imagePath);
|
|
||||||
int xSize;
|
|
||||||
int ySize;
|
|
||||||
unsigned char* bgData = stbi_load(abs.string().c_str(), &xSize, &ySize, nullptr, 4);
|
|
||||||
|
|
||||||
if constexpr(Scaling && !Owning) {
|
|
||||||
ScalingBase<true, false>::bufferUpdated = true;
|
|
||||||
} else if constexpr(Scaling && Owning) {
|
|
||||||
ScalingBase<true, true>::bufferWidth = xSize;
|
|
||||||
ScalingBase<true, true>::bufferHeight = ySize;
|
|
||||||
ScalingBase<true, true>::bufferUpdated = true;
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer.resize(xSize*ySize);
|
|
||||||
} else {
|
|
||||||
buffer.resize(xSize*ySize);
|
|
||||||
}
|
|
||||||
|
|
||||||
opaque = OpaqueType::FullyOpaque;
|
|
||||||
|
|
||||||
if constexpr(Scaling) {
|
|
||||||
for(std::uint32_t x = 0; x < xSize; x++) {
|
|
||||||
for(std::uint32_t y = 0; y < ySize; y++) {
|
|
||||||
std::uint32_t idx = (x*ySize+y)*4;
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].r = bgData[idx];
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].g = bgData[idx+1];
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].b = bgData[idx+2];
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].a = bgData[idx+3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(std::uint32_t i = 0; i < xSize*ySize; i++) {
|
|
||||||
if(ScalingBase<true, Owning>::scalingBuffer[i].a != 255) {
|
|
||||||
opaque = OpaqueType::SemiOpaque;
|
|
||||||
for(std::uint32_t i2 = 0; i2 < xSize*ySize; i2++) {
|
|
||||||
if(ScalingBase<true, Owning>::scalingBuffer[i2].a != 0 && ScalingBase<true, Owning>::scalingBuffer[i2].a != 255) {
|
|
||||||
opaque = OpaqueType::Transparent;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for(std::uint32_t x = 0; x < xSize; x++) {
|
|
||||||
for(std::uint32_t y = 0; y < ySize; y++) {
|
|
||||||
std::uint32_t idx = (x*ySize+y)*4;
|
|
||||||
buffer[x*ySize+y].r = bgData[idx];
|
|
||||||
buffer[x*ySize+y].g = bgData[idx+1];
|
|
||||||
buffer[x*ySize+y].b = bgData[idx+2];
|
|
||||||
buffer[x*ySize+y].a = bgData[idx+3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(std::uint32_t i = 0; i < xSize*ySize; i++) {
|
|
||||||
if(buffer[i].a != 255) {
|
|
||||||
opaque = OpaqueType::SemiOpaque;
|
|
||||||
for(std::uint32_t i2 = 0; i2 < xSize*ySize; i2++) {
|
|
||||||
if(buffer[i2].a != 0 && buffer[i2].a != 255) {
|
|
||||||
opaque = OpaqueType::Transparent;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void LoadImageNoOpaqueCheck(const std::string_view imagePath) {
|
|
||||||
std::filesystem::path abs = std::filesystem::absolute(imagePath);
|
|
||||||
int xSize;
|
|
||||||
int ySize;
|
|
||||||
unsigned char* bgData = stbi_load(abs.string().c_str(), &xSize, &ySize, nullptr, 4);
|
|
||||||
|
|
||||||
|
|
||||||
if constexpr(Scaling && !Owning) {
|
|
||||||
ScalingBase<true, false>::bufferUpdated = true;
|
|
||||||
} else if constexpr(Scaling && Owning) {
|
|
||||||
ScalingBase<true, true>::bufferWidth = xSize;
|
|
||||||
ScalingBase<true, true>::bufferHeight = ySize;
|
|
||||||
ScalingBase<true, true>::bufferUpdated = true;
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer.resize(xSize*ySize);
|
|
||||||
} else {
|
|
||||||
buffer.resize(xSize*ySize);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if constexpr(Scaling) {
|
|
||||||
for(std::uint32_t x = 0; x < xSize; x++) {
|
|
||||||
for(std::uint32_t y = 0; y < ySize; y++) {
|
|
||||||
std::uint32_t idx = (x*ySize+y)*4;
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].r = bgData[idx];
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].g = bgData[idx+1];
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].b = bgData[idx+2];
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].a = bgData[idx+3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for(std::uint32_t x = 0; x < xSize; x++) {
|
|
||||||
for(std::uint32_t y = 0; y < ySize; y++) {
|
|
||||||
std::uint32_t idx = (x*ySize+y)*4;
|
|
||||||
buffer[x*ySize+y].r = bgData[idx];
|
|
||||||
buffer[x*ySize+y].g = bgData[idx+1];
|
|
||||||
buffer[x*ySize+y].b = bgData[idx+2];
|
|
||||||
buffer[x*ySize+y].a = bgData[idx+3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::vector<std::string_view> ResizeText(Window& window, const std::string_view text, float size, Font& font, TextOverflowMode overflowMode = TextOverflowMode::Clip, TextScaleMode scaleMode = TextScaleMode::None, Transform* parent = nullptr) {
|
|
||||||
float scale = stbtt_ScaleForPixelHeight(&font.font, size);
|
|
||||||
int baseline = (int)(font.ascent * scale);
|
|
||||||
|
|
||||||
std::vector<std::string_view> lines;
|
|
||||||
std::string_view remaining = text;
|
|
||||||
|
|
||||||
std::uint32_t lineHeight = (font.ascent - font.descent) * scale;
|
|
||||||
|
|
||||||
if(overflowMode == TextOverflowMode::Clip) {
|
|
||||||
while (!remaining.empty()) {
|
|
||||||
// Find next newline or end of string
|
|
||||||
auto newlinePos = remaining.find('\n');
|
|
||||||
if (newlinePos != std::string_view::npos) {
|
|
||||||
lines.emplace_back(remaining.substr(0, newlinePos));
|
|
||||||
remaining = remaining.substr(newlinePos + 1);
|
|
||||||
} else {
|
|
||||||
lines.emplace_back(remaining);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::uint32_t maxWidth = 0;
|
|
||||||
|
|
||||||
for(const std::string_view line: lines) {
|
|
||||||
std::uint32_t lineWidth = 0;
|
|
||||||
for (const char c : line) {
|
|
||||||
int advance, lsb;
|
|
||||||
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
|
|
||||||
lineWidth += (int)(advance * scale);
|
|
||||||
}
|
|
||||||
if(lineWidth > maxWidth) {
|
|
||||||
maxWidth = lineWidth;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if(scaleMode == TextScaleMode::Element) {
|
|
||||||
std::int32_t logicalPerPixelY = anchor.height / scaled.height;
|
|
||||||
std::int32_t oldHeight = anchor.height;
|
|
||||||
std::int32_t logicalPerPixelX = anchor.width / scaled.width;
|
|
||||||
std::int32_t oldwidth = anchor.width;
|
|
||||||
anchor.height = lineHeight * logicalPerPixelY;
|
|
||||||
anchor.width = maxWidth * logicalPerPixelX;
|
|
||||||
if(oldHeight != anchor.height || oldwidth != anchor.width) {
|
|
||||||
if(parent) {
|
|
||||||
UpdatePosition(window, *parent);
|
|
||||||
} else {
|
|
||||||
UpdatePosition(window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if(scaleMode == TextScaleMode::Font) {
|
|
||||||
//todo
|
|
||||||
} else if(scaleMode == TextScaleMode::Buffer) {
|
|
||||||
if constexpr(Scaling && Owning) {
|
|
||||||
std::uint32_t neededHeight = lines.size() * lineHeight;
|
|
||||||
if(neededHeight != ScalingBase<true, true>::bufferHeight || maxWidth != ScalingBase<true, true>::bufferWidth) {
|
|
||||||
ScalingBase<true, true>::bufferHeight = neededHeight;
|
|
||||||
ScalingBase<true, true>::bufferWidth = maxWidth;
|
|
||||||
ScalingBase<true, true>::bufferUpdated = true;
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer.resize(neededHeight*maxWidth);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if constexpr(Scaling) {
|
|
||||||
lines.resize(ScalingBase<true, Owning>::bufferHeight / lines.size());
|
|
||||||
} else {
|
|
||||||
lines.resize(scaled.height / lines.size());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
while (!remaining.empty()) {
|
|
||||||
std::string_view line;
|
|
||||||
auto newlinePos = remaining.find('\n');
|
|
||||||
if (newlinePos != std::string_view::npos) {
|
|
||||||
line = remaining.substr(0, newlinePos);
|
|
||||||
remaining = remaining.substr(newlinePos + 1);
|
|
||||||
} else {
|
|
||||||
line = remaining;
|
|
||||||
remaining = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::uint32_t lineWidth = 0;
|
|
||||||
std::size_t lastWrapPos = 0; // position of last space that can be used to wrap
|
|
||||||
std::size_t startPos = 0;
|
|
||||||
|
|
||||||
for (std::size_t i = 0; i < line.size(); ++i) {
|
|
||||||
char c = line[i];
|
|
||||||
|
|
||||||
// get width of this character
|
|
||||||
int advance, lsb;
|
|
||||||
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
|
|
||||||
lineWidth += (std::uint32_t)(advance * scale);
|
|
||||||
|
|
||||||
// remember last space for wrapping
|
|
||||||
if (c == ' ') {
|
|
||||||
lastWrapPos = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if line exceeds width, wrap
|
|
||||||
if (lineWidth > scaled.width) {
|
|
||||||
std::size_t wrapPos;
|
|
||||||
if (lastWrapPos > startPos) {
|
|
||||||
wrapPos = lastWrapPos; // wrap at last space
|
|
||||||
} else {
|
|
||||||
wrapPos = i; // no space, hard wrap
|
|
||||||
}
|
|
||||||
|
|
||||||
// push the line up to wrapPos
|
|
||||||
lines.push_back(line.substr(startPos, wrapPos - startPos));
|
|
||||||
|
|
||||||
// skip any spaces at the beginning of next line
|
|
||||||
startPos = wrapPos;
|
|
||||||
while (startPos < line.size() && line[startPos] == ' ') {
|
|
||||||
++startPos;
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset width and i
|
|
||||||
lineWidth = 0;
|
|
||||||
i = startPos - 1; // -1 because loop will increment i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// add the remaining part of the line
|
|
||||||
if (startPos < line.size()) {
|
|
||||||
lines.push_back(line.substr(startPos));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(scaleMode == TextScaleMode::Element) {
|
|
||||||
std::int32_t logicalPerPixelY = anchor.height / scaled.height;
|
|
||||||
std::int32_t oldHeight = anchor.height;
|
|
||||||
anchor.height = lineHeight * logicalPerPixelY;
|
|
||||||
if(oldHeight != anchor.height) {
|
|
||||||
if(parent) {
|
|
||||||
UpdatePosition(window, *parent);
|
|
||||||
} else {
|
|
||||||
UpdatePosition(window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if(scaleMode == TextScaleMode::Font) {
|
|
||||||
//todo
|
|
||||||
} else if(scaleMode == TextScaleMode::Buffer) {
|
|
||||||
if constexpr(Scaling && Owning) {
|
|
||||||
std::uint32_t neededHeight = lines.size() * lineHeight;
|
|
||||||
if(neededHeight != ScalingBase<true, true>::bufferHeight) {
|
|
||||||
ScalingBase<true, true>::bufferHeight = neededHeight;
|
|
||||||
ScalingBase<true, true>::bufferUpdated = true;
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer.resize(neededHeight*ScalingBase<true, true>::bufferWidth);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if constexpr(Scaling) {
|
|
||||||
lines.resize(ScalingBase<true, Owning>::bufferHeight / lines.size());
|
|
||||||
} else {
|
|
||||||
lines.resize(scaled.height / lines.size());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return lines;
|
|
||||||
}
|
|
||||||
void RenderText(Window& window, std::span<const std::string_view> lines, float size, Pixel_BU8_GU8_RU8_AU8 color, Font& font, TextAlignment alignment = TextAlignment::Left, std::uint32_t offsetX = 0, std::uint32_t offsetY = 0) {
|
|
||||||
float scale = stbtt_ScaleForPixelHeight(&font.font, size);
|
|
||||||
int baseline = (int)(font.ascent * scale);
|
|
||||||
std::uint32_t lineHeight = (font.ascent - font.descent) * scale;
|
|
||||||
std::uint32_t currentY = baseline;
|
|
||||||
for(std::string_view line : lines) {
|
|
||||||
|
|
||||||
std::uint32_t lineWidth = 0;
|
|
||||||
for (const char c : line) {
|
|
||||||
int advance, lsb;
|
|
||||||
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
|
|
||||||
lineWidth += (int)(advance * scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::uint32_t startX = 0;
|
|
||||||
switch (alignment) {
|
|
||||||
case TextAlignment::Left:
|
|
||||||
startX = 0;
|
|
||||||
break;
|
|
||||||
case TextAlignment::Center:
|
|
||||||
startX = (scaled.width - lineWidth) / 2;
|
|
||||||
break;
|
|
||||||
case TextAlignment::Right:
|
|
||||||
startX = scaled.width - lineWidth;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::uint32_t x = startX;
|
|
||||||
|
|
||||||
for (std::size_t i = 0; i < line.size(); ++i) {
|
|
||||||
int codepoint = line[i];
|
|
||||||
|
|
||||||
int ax;
|
|
||||||
int lsb;
|
|
||||||
stbtt_GetCodepointHMetrics(&font.font, codepoint, &ax, &lsb);
|
|
||||||
|
|
||||||
int c_x1, c_y1, c_x2, c_y2;
|
|
||||||
stbtt_GetCodepointBitmapBox(&font.font, codepoint, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
|
|
||||||
|
|
||||||
int w = c_x2 - c_x1;
|
|
||||||
int h = c_y2 - c_y1;
|
|
||||||
|
|
||||||
std::vector<unsigned char> bitmap(w * h);
|
|
||||||
stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint);
|
|
||||||
|
|
||||||
// Only render characters that fit within the scaled bounds
|
|
||||||
for (int j = 0; j < h; j++) {
|
|
||||||
for (int i = 0; i < w; i++) {
|
|
||||||
int bufferX = x + i + c_x1 + offsetX;
|
|
||||||
int bufferY = currentY + j + c_y1 + offsetY;
|
|
||||||
|
|
||||||
// Only draw pixels that are within our scaled buffer bounds
|
|
||||||
if constexpr(Scaling) {
|
|
||||||
if (bufferX >= 0 && bufferX < ScalingBase<true, Owning>::bufferWidth && bufferY >= 0 && bufferY < ScalingBase<true, Owning>::bufferHeight) {
|
|
||||||
ScalingBase<true, Owning>::scalingBuffer[bufferY * ScalingBase<true, Owning>::bufferWidth + bufferX] = {color.r, color.g, color.b, bitmap[j * w + i]};
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (bufferX >= 0 && bufferX < (int)scaled.width && bufferY >= 0 && bufferY < (int)scaled.height) {
|
|
||||||
buffer[bufferY * scaled.width + bufferX] = {color.r, color.g, color.b, bitmap[j * w + i]};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
x += (int)(ax * scale);
|
|
||||||
|
|
||||||
if (i + 1 < line.size()) {
|
|
||||||
x += (int)stbtt_GetCodepointKernAdvance(&font.font, codepoint, line[i+1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
currentY += lineHeight;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
508
interfaces/Crafter.Graphics-RenderingElement2D.cppm
Normal file
508
interfaces/Crafter.Graphics-RenderingElement2D.cppm
Normal file
|
|
@ -0,0 +1,508 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2026 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 version 3.0 as published by the Free Software Foundation;
|
||||||
|
|
||||||
|
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 "../lib/stb_truetype.h"
|
||||||
|
export module Crafter.Graphics:RenderingElement2D;
|
||||||
|
import std;
|
||||||
|
import :Transform2D;
|
||||||
|
import :Font;
|
||||||
|
import :Types;
|
||||||
|
import :Window;
|
||||||
|
|
||||||
|
export namespace Crafter {
|
||||||
|
enum class TextAlignment {
|
||||||
|
Left,
|
||||||
|
Center,
|
||||||
|
Right
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class TextOverflowMode {
|
||||||
|
Clip,
|
||||||
|
Wrap
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class TextScaleMode {
|
||||||
|
None,
|
||||||
|
Font,
|
||||||
|
Element,
|
||||||
|
Buffer
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RenderElement2DScalingOwning {
|
||||||
|
std::vector<Vector<std::uint8_t, 4>> scalingBuffer;
|
||||||
|
std::uint32_t bufferWidth;
|
||||||
|
std::uint32_t bufferHeight;
|
||||||
|
bool bufferUpdated = true;
|
||||||
|
RenderElement2DScalingOwning() = default;
|
||||||
|
RenderElement2DScalingOwning(std::uint32_t bufferWidth, std::uint32_t bufferHeight) : scalingBuffer(bufferWidth*bufferHeight), bufferWidth(bufferWidth), bufferHeight(bufferHeight) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RenderElement2DScalingNonOwning {
|
||||||
|
Vector<std::uint8_t, 4>* scalingBuffer;
|
||||||
|
std::uint32_t bufferWidth;
|
||||||
|
std::uint32_t bufferHeight;
|
||||||
|
bool bufferUpdated = true;
|
||||||
|
RenderElement2DScalingNonOwning() = default;
|
||||||
|
RenderElement2DScalingNonOwning(Vector<std::uint8_t, 4>* scalingBuffer, std::uint32_t bufferWidth, std::uint32_t bufferHeight) : scalingBuffer(scalingBuffer), bufferWidth(bufferWidth), bufferHeight(bufferHeight) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RenderElement2DRotating {
|
||||||
|
float rotation;
|
||||||
|
bool rotationUpdated = true;
|
||||||
|
RenderElement2DRotating() = default;
|
||||||
|
RenderElement2DRotating(float rotation) : rotation(rotation) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct EmptyScalingBase {};
|
||||||
|
struct EmptyRotatingBase {};
|
||||||
|
|
||||||
|
template<bool Scaling, bool Owning>
|
||||||
|
using ScalingBase =
|
||||||
|
std::conditional_t<
|
||||||
|
Scaling,
|
||||||
|
std::conditional_t<Owning,
|
||||||
|
RenderElement2DScalingOwning,
|
||||||
|
RenderElement2DScalingNonOwning>,
|
||||||
|
EmptyScalingBase
|
||||||
|
>;
|
||||||
|
|
||||||
|
template<bool Rotating>
|
||||||
|
using RotatingBase =
|
||||||
|
std::conditional_t<
|
||||||
|
Rotating,
|
||||||
|
RenderElement2DRotating,
|
||||||
|
EmptyRotatingBase
|
||||||
|
>;
|
||||||
|
|
||||||
|
struct RenderingElement2DBase : Transform2D {
|
||||||
|
std::vector<Vector<std::uint8_t, 4>> buffer;
|
||||||
|
OpaqueType opaque;
|
||||||
|
RenderingElement2DBase(Anchor2D anchor) : Transform2D(anchor) {
|
||||||
|
scaled.size.x = 0;
|
||||||
|
}
|
||||||
|
RenderingElement2DBase(Anchor2D anchor, OpaqueType opaque) : Transform2D(anchor), opaque(opaque) {
|
||||||
|
scaled.size.x = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<bool Scaling, bool Owning, bool Rotating> requires ((!Rotating || Scaling) && (!Owning || Scaling))
|
||||||
|
struct RenderingElement2D : RenderingElement2DBase, ScalingBase<Scaling, Owning>, RotatingBase<Rotating> {
|
||||||
|
RenderingElement2D() = default;
|
||||||
|
RenderingElement2D(Anchor2D anchor, OpaqueType opaque) : RenderingElement2DBase(anchor, opaque) {
|
||||||
|
|
||||||
|
}
|
||||||
|
RenderingElement2D(Anchor2D anchor, OpaqueType opaque, std::uint32_t rotation) requires(Rotating) : RenderingElement2DBase(anchor, opaque), RotatingBase<Rotating>(rotation) {
|
||||||
|
|
||||||
|
}
|
||||||
|
RenderingElement2D(Anchor2D anchor, OpaqueType opaque, std::uint32_t bufferWidth, std::uint32_t bufferHeight, Vector<std::uint8_t, 4>* scalingBuffer) requires(Scaling && !Owning) : RenderingElement2DBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight, scalingBuffer) {
|
||||||
|
|
||||||
|
}
|
||||||
|
RenderingElement2D(Anchor2D anchor, OpaqueType opaque, std::uint32_t bufferWidth, std::uint32_t bufferHeight, Vector<std::uint8_t, 4>* scalingBuffer, std::uint32_t rotation) requires(Scaling && !Owning && Rotating) : RenderingElement2DBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight, scalingBuffer), RotatingBase<Rotating>(rotation) {
|
||||||
|
|
||||||
|
}
|
||||||
|
RenderingElement2D(Anchor2D anchor, OpaqueType opaque, std::uint32_t bufferWidth, std::uint32_t bufferHeight) requires(Owning) : RenderingElement2DBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight) {
|
||||||
|
|
||||||
|
}
|
||||||
|
RenderingElement2D(Anchor2D anchor, OpaqueType opaque, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t rotation) requires(Owning && Rotating) : RenderingElement2DBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight) , RotatingBase<Rotating>(rotation) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderingElement2D(RenderingElement2D&) = delete;
|
||||||
|
RenderingElement2D& operator=(RenderingElement2D&) = delete;
|
||||||
|
|
||||||
|
void ScaleNearestNeighbor() requires(Scaling) {
|
||||||
|
for (std::uint32_t y = 0; y < scaled.size.y; y++) {
|
||||||
|
std::uint32_t srcY = y * ScalingBase<true, Owning>::bufferHeight / scaled.size.y;
|
||||||
|
for (std::uint32_t x = 0; x < scaled.size.x; x++) {
|
||||||
|
std::uint32_t srcX = x * ScalingBase<true, Owning>::bufferWidth / scaled.size.x;
|
||||||
|
buffer[y * scaled.size.x + x] = ScalingBase<true, Owning>::scalingBuffer[srcY * ScalingBase<true, Owning>::bufferWidth + srcX];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScaleRotating() requires(Scaling) {
|
||||||
|
const std::uint32_t dstWidth = scaled.size.x;
|
||||||
|
const std::uint32_t dstHeight = scaled.size.y;
|
||||||
|
|
||||||
|
const float c2 = std::abs(std::cos(RotatingBase<true>::rotation));
|
||||||
|
const float s2 = std::abs(std::sin(RotatingBase<true>::rotation));
|
||||||
|
|
||||||
|
const float rotatedWidth = dstWidth * c2 + dstHeight * s2;
|
||||||
|
const float rotatedHeight = dstWidth * s2 + dstHeight * c2;
|
||||||
|
|
||||||
|
const std::uint32_t diffX = static_cast<std::uint32_t>(std::ceil((rotatedWidth - dstWidth) * 0.5));
|
||||||
|
const std::uint32_t diffY = static_cast<std::uint32_t>(std::ceil((rotatedHeight - dstHeight) * 0.5));
|
||||||
|
|
||||||
|
scaled.size.x += diffX + diffX;
|
||||||
|
scaled.size.y += diffY + diffY;
|
||||||
|
|
||||||
|
scaled.position.x -= diffX;
|
||||||
|
scaled.position.y -= diffY;
|
||||||
|
|
||||||
|
buffer.clear();
|
||||||
|
buffer.resize(scaled.size.x * scaled.size.y);
|
||||||
|
|
||||||
|
// Destination center
|
||||||
|
const float dstCx = (static_cast<float>(scaled.size.x) - 1.0) * 0.5;
|
||||||
|
const float dstCy = (static_cast<float>(scaled.size.y) - 1.0) * 0.5;
|
||||||
|
|
||||||
|
// Source center
|
||||||
|
const float srcCx = (static_cast<float>(ScalingBase<true, Owning>::bufferWidth) - 1.0) * 0.5;
|
||||||
|
const float srcCy = (static_cast<float>(ScalingBase<true, Owning>::bufferHeight) - 1.0) * 0.5;
|
||||||
|
|
||||||
|
const float c = std::cos(RotatingBase<true>::rotation);
|
||||||
|
const float s = std::sin(RotatingBase<true>::rotation);
|
||||||
|
|
||||||
|
// Scale factors (destination → source)
|
||||||
|
const float scaleX = static_cast<float>(ScalingBase<true, Owning>::bufferWidth) / dstWidth;
|
||||||
|
const float scaleY = static_cast<float>(ScalingBase<true, Owning>::bufferHeight) / dstHeight;
|
||||||
|
|
||||||
|
for (std::uint32_t yB = 0; yB < scaled.size.y; ++yB) {
|
||||||
|
for (std::uint32_t xB = 0; xB < scaled.size.x; ++xB) {
|
||||||
|
|
||||||
|
// ---- Destination pixel relative to center ----
|
||||||
|
const float dx = (static_cast<float>(xB) - dstCx) * scaleX;
|
||||||
|
const float dy = (static_cast<float>(yB) - dstCy) * scaleY;
|
||||||
|
|
||||||
|
// ---- Inverse rotation ----
|
||||||
|
const float sx = (c * dx - s * dy) + srcCx;
|
||||||
|
const float sy = (s * dx + c * dy) + srcCy;
|
||||||
|
|
||||||
|
// ---- Nearest neighbour sampling ----
|
||||||
|
const std::int32_t srcX = static_cast<std::int32_t>(std::round(sx));
|
||||||
|
const std::int32_t srcY = static_cast<std::int32_t>(std::round(sy));
|
||||||
|
|
||||||
|
if (srcX >= 0 && srcX < ScalingBase<true, Owning>::bufferWidth && srcY >= 0 && srcY < ScalingBase<true, Owning>::bufferHeight) {
|
||||||
|
buffer[yB * scaled.size.x + xB] = ScalingBase<true, Owning>::scalingBuffer[srcY * ScalingBase<true, Owning>::bufferWidth + srcX];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdatePosition(Rendertarget& renderer, ScaleData2D oldScale) {
|
||||||
|
if constexpr(Scaling && !Rotating) {
|
||||||
|
if(oldScale.size.x != scaled.size.x || oldScale.size.y != scaled.size.y) {
|
||||||
|
buffer.resize(scaled.size.x * scaled.size.y);
|
||||||
|
ScaleNearestNeighbor();
|
||||||
|
renderer.AddDirtyRect(oldScale);
|
||||||
|
renderer.AddDirtyRect(scaled);
|
||||||
|
} else if(oldScale.position.x != scaled.position.x || oldScale.position.y != scaled.position.y) {
|
||||||
|
renderer.AddDirtyRect(oldScale);
|
||||||
|
renderer.AddDirtyRect(scaled);
|
||||||
|
if(ScalingBase<true, Owning>::bufferUpdated) {
|
||||||
|
ScaleNearestNeighbor();
|
||||||
|
ScalingBase<true, Owning>::bufferUpdated = false;
|
||||||
|
}
|
||||||
|
} else if(ScalingBase<true, Owning>::bufferUpdated) {
|
||||||
|
ScaleNearestNeighbor();
|
||||||
|
ScalingBase<true, Owning>::bufferUpdated = false;
|
||||||
|
}
|
||||||
|
} else if constexpr(Rotating) {
|
||||||
|
if(oldScale.size.x != scaled.size.x || oldScale.size.y != scaled.size.y) {
|
||||||
|
buffer.resize(scaled.size.x * scaled.size.y);
|
||||||
|
ScaleRotating();
|
||||||
|
renderer.AddDirtyRect(oldScale);
|
||||||
|
renderer.AddDirtyRect(scaled);
|
||||||
|
} else if(oldScale.position.x != scaled.position.x || oldScale.position.y != scaled.position.y) {
|
||||||
|
renderer.AddDirtyRect(oldScale);
|
||||||
|
renderer.AddDirtyRect(scaled);
|
||||||
|
if(ScalingBase<true, Owning>::bufferUpdated || RotatingBase<true>::rotationUpdated) {
|
||||||
|
ScaleRotating();
|
||||||
|
ScalingBase<true, Owning>::bufferUpdated = false;
|
||||||
|
RotatingBase<true>::rotationUpdated = false;
|
||||||
|
}
|
||||||
|
} else if(ScalingBase<true, Owning>::bufferUpdated || RotatingBase<true>::rotationUpdated) {
|
||||||
|
ScaleRotating();
|
||||||
|
ScalingBase<true, Owning>::bufferUpdated = false;
|
||||||
|
RotatingBase<true>::rotationUpdated = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(oldScale.size.x != scaled.size.x || oldScale.size.y != scaled.size.y) {
|
||||||
|
buffer.resize(scaled.size.x * scaled.size.y);
|
||||||
|
renderer.AddDirtyRect(oldScale);
|
||||||
|
renderer.AddDirtyRect(scaled);
|
||||||
|
}
|
||||||
|
if(oldScale.position.x != scaled.position.x || oldScale.position.y != scaled.position.y) {
|
||||||
|
renderer.AddDirtyRect(oldScale);
|
||||||
|
renderer.AddDirtyRect(scaled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdatePosition(Rendertarget& window) override {
|
||||||
|
ScaleData2D oldScale = scaled;
|
||||||
|
ScaleElement(window.transform);
|
||||||
|
UpdatePosition(window, oldScale);
|
||||||
|
for(Transform2D* child : children) {
|
||||||
|
child->UpdatePosition(window, *this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdatePosition(Rendertarget& window, Transform2D& parent) override {
|
||||||
|
ScaleData2D oldScale = scaled;
|
||||||
|
ScaleElement(parent);
|
||||||
|
UpdatePosition(window, oldScale);
|
||||||
|
for(Transform2D* child : children) {
|
||||||
|
child->UpdatePosition(window, *this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string_view> ResizeText(Window& window, const std::string_view text, float size, Font& font, TextOverflowMode overflowMode = TextOverflowMode::Clip, TextScaleMode scaleMode = TextScaleMode::None, Transform2D* parent = nullptr) {
|
||||||
|
float scale = stbtt_ScaleForPixelHeight(&font.font, size);
|
||||||
|
int baseline = (int)(font.ascent * scale);
|
||||||
|
|
||||||
|
std::vector<std::string_view> lines;
|
||||||
|
std::string_view remaining = text;
|
||||||
|
|
||||||
|
std::uint32_t lineHeight = (font.ascent - font.descent) * scale;
|
||||||
|
|
||||||
|
if(overflowMode == TextOverflowMode::Clip) {
|
||||||
|
while (!remaining.empty()) {
|
||||||
|
// Find next newline or end of string
|
||||||
|
auto newlinePos = remaining.find('\n');
|
||||||
|
if (newlinePos != std::string_view::npos) {
|
||||||
|
lines.emplace_back(remaining.substr(0, newlinePos));
|
||||||
|
remaining = remaining.substr(newlinePos + 1);
|
||||||
|
} else {
|
||||||
|
lines.emplace_back(remaining);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::uint32_t maxWidth = 0;
|
||||||
|
|
||||||
|
for(const std::string_view line: lines) {
|
||||||
|
std::uint32_t lineWidth = 0;
|
||||||
|
for (const char c : line) {
|
||||||
|
int advance, lsb;
|
||||||
|
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
|
||||||
|
lineWidth += (int)(advance * scale);
|
||||||
|
}
|
||||||
|
if(lineWidth > maxWidth) {
|
||||||
|
maxWidth = lineWidth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(scaleMode == TextScaleMode::Element) {
|
||||||
|
std::int32_t logicalPerPixelY = anchor.height / scaled.size.y;
|
||||||
|
std::int32_t oldHeight = anchor.height;
|
||||||
|
std::int32_t logicalPerPixelX = anchor.width / scaled.size.x;
|
||||||
|
std::int32_t oldwidth = anchor.width;
|
||||||
|
anchor.height = lineHeight * logicalPerPixelY;
|
||||||
|
anchor.width = maxWidth * logicalPerPixelX;
|
||||||
|
if(oldHeight != anchor.height || oldwidth != anchor.width) {
|
||||||
|
if(parent) {
|
||||||
|
UpdatePosition(window, *parent);
|
||||||
|
} else {
|
||||||
|
UpdatePosition(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if(scaleMode == TextScaleMode::Font) {
|
||||||
|
//todo
|
||||||
|
} else if(scaleMode == TextScaleMode::Buffer) {
|
||||||
|
if constexpr(Scaling && Owning) {
|
||||||
|
std::uint32_t neededHeight = lines.size() * lineHeight;
|
||||||
|
if(neededHeight != ScalingBase<true, true>::bufferHeight || maxWidth != ScalingBase<true, true>::bufferWidth) {
|
||||||
|
ScalingBase<true, true>::bufferHeight = neededHeight;
|
||||||
|
ScalingBase<true, true>::bufferWidth = maxWidth;
|
||||||
|
ScalingBase<true, true>::bufferUpdated = true;
|
||||||
|
ScalingBase<true, Owning>::scalingBuffer.resize(neededHeight*maxWidth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if constexpr(Scaling) {
|
||||||
|
lines.resize(ScalingBase<true, Owning>::bufferHeight / lines.size());
|
||||||
|
} else {
|
||||||
|
lines.resize(scaled.size.y / lines.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (!remaining.empty()) {
|
||||||
|
std::string_view line;
|
||||||
|
auto newlinePos = remaining.find('\n');
|
||||||
|
if (newlinePos != std::string_view::npos) {
|
||||||
|
line = remaining.substr(0, newlinePos);
|
||||||
|
remaining = remaining.substr(newlinePos + 1);
|
||||||
|
} else {
|
||||||
|
line = remaining;
|
||||||
|
remaining = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint32_t lineWidth = 0;
|
||||||
|
std::size_t lastWrapPos = 0; // position of last space that can be used to wrap
|
||||||
|
std::size_t startPos = 0;
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < line.size(); ++i) {
|
||||||
|
char c = line[i];
|
||||||
|
|
||||||
|
// get width of this character
|
||||||
|
int advance, lsb;
|
||||||
|
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
|
||||||
|
lineWidth += (std::uint32_t)(advance * scale);
|
||||||
|
|
||||||
|
// remember last space for wrapping
|
||||||
|
if (c == ' ') {
|
||||||
|
lastWrapPos = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if line exceeds width, wrap
|
||||||
|
if (lineWidth > scaled.size.x) {
|
||||||
|
std::size_t wrapPos;
|
||||||
|
if (lastWrapPos > startPos) {
|
||||||
|
wrapPos = lastWrapPos; // wrap at last space
|
||||||
|
} else {
|
||||||
|
wrapPos = i; // no space, hard wrap
|
||||||
|
}
|
||||||
|
|
||||||
|
// push the line up to wrapPos
|
||||||
|
lines.push_back(line.substr(startPos, wrapPos - startPos));
|
||||||
|
|
||||||
|
// skip any spaces at the beginning of next line
|
||||||
|
startPos = wrapPos;
|
||||||
|
while (startPos < line.size() && line[startPos] == ' ') {
|
||||||
|
++startPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset width and i
|
||||||
|
lineWidth = 0;
|
||||||
|
i = startPos - 1; // -1 because loop will increment i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add the remaining part of the line
|
||||||
|
if (startPos < line.size()) {
|
||||||
|
lines.push_back(line.substr(startPos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(scaleMode == TextScaleMode::Element) {
|
||||||
|
float logicalPerPixelY = anchor.height / scaled.size.y;
|
||||||
|
float oldHeight = anchor.height;
|
||||||
|
anchor.height = lineHeight * logicalPerPixelY;
|
||||||
|
if(oldHeight != anchor.height) {
|
||||||
|
if(parent) {
|
||||||
|
UpdatePosition(window, *parent);
|
||||||
|
} else {
|
||||||
|
UpdatePosition(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if(scaleMode == TextScaleMode::Font) {
|
||||||
|
//todo
|
||||||
|
} else if(scaleMode == TextScaleMode::Buffer) {
|
||||||
|
if constexpr(Scaling && Owning) {
|
||||||
|
float neededHeight = lines.size() * lineHeight;
|
||||||
|
if(neededHeight != ScalingBase<true, true>::bufferHeight) {
|
||||||
|
ScalingBase<true, true>::bufferHeight = neededHeight;
|
||||||
|
ScalingBase<true, true>::bufferUpdated = true;
|
||||||
|
ScalingBase<true, Owning>::scalingBuffer.resize(neededHeight*ScalingBase<true, true>::bufferWidth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if constexpr(Scaling) {
|
||||||
|
lines.resize(ScalingBase<true, Owning>::bufferHeight / lines.size());
|
||||||
|
} else {
|
||||||
|
lines.resize(scaled.size.y / lines.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
void RenderText(Window& window, std::span<const std::string_view> lines, float size, Vector<std::uint8_t, 4> color, Font& font, TextAlignment alignment = TextAlignment::Left, std::uint32_t offsetX = 0, std::uint32_t offsetY = 0) {
|
||||||
|
float scale = stbtt_ScaleForPixelHeight(&font.font, size);
|
||||||
|
int baseline = (int)(font.ascent * scale);
|
||||||
|
std::uint32_t lineHeight = (font.ascent - font.descent) * scale;
|
||||||
|
std::uint32_t currentY = baseline;
|
||||||
|
for(std::string_view line : lines) {
|
||||||
|
|
||||||
|
std::uint32_t lineWidth = 0;
|
||||||
|
for (const char c : line) {
|
||||||
|
int advance, lsb;
|
||||||
|
stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb);
|
||||||
|
lineWidth += (int)(advance * scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint32_t startX = 0;
|
||||||
|
switch (alignment) {
|
||||||
|
case TextAlignment::Left:
|
||||||
|
startX = 0;
|
||||||
|
break;
|
||||||
|
case TextAlignment::Center:
|
||||||
|
startX = (scaled.size.x - lineWidth) / 2;
|
||||||
|
break;
|
||||||
|
case TextAlignment::Right:
|
||||||
|
startX = scaled.size.x - lineWidth;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::uint32_t x = startX;
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < line.size(); ++i) {
|
||||||
|
int codepoint = line[i];
|
||||||
|
|
||||||
|
int ax;
|
||||||
|
int lsb;
|
||||||
|
stbtt_GetCodepointHMetrics(&font.font, codepoint, &ax, &lsb);
|
||||||
|
|
||||||
|
int c_x1, c_y1, c_x2, c_y2;
|
||||||
|
stbtt_GetCodepointBitmapBox(&font.font, codepoint, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
|
||||||
|
|
||||||
|
int w = c_x2 - c_x1;
|
||||||
|
int h = c_y2 - c_y1;
|
||||||
|
|
||||||
|
std::vector<unsigned char> bitmap(w * h);
|
||||||
|
stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint);
|
||||||
|
|
||||||
|
// Only render characters that fit within the scaled bounds
|
||||||
|
for (int j = 0; j < h; j++) {
|
||||||
|
for (int i = 0; i < w; i++) {
|
||||||
|
int bufferX = x + i + c_x1 + offsetX;
|
||||||
|
int bufferY = currentY + j + c_y1 + offsetY;
|
||||||
|
|
||||||
|
// Only draw pixels that are within our scaled buffer bounds
|
||||||
|
if constexpr(Scaling) {
|
||||||
|
if (bufferX >= 0 && bufferX < ScalingBase<true, Owning>::bufferWidth && bufferY >= 0 && bufferY < ScalingBase<true, Owning>::bufferHeight) {
|
||||||
|
ScalingBase<true, Owning>::scalingBuffer[bufferY * ScalingBase<true, Owning>::bufferWidth + bufferX] = {color.r, color.g, color.b, bitmap[j * w + i]};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (bufferX >= 0 && bufferX < (int)scaled.size.x && bufferY >= 0 && bufferY < (int)scaled.size.y) {
|
||||||
|
buffer[bufferY * scaled.size.x + bufferX] = {color.r, color.g, color.b, bitmap[j * w + i]};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
x += (int)(ax * scale);
|
||||||
|
|
||||||
|
if (i + 1 < line.size()) {
|
||||||
|
x += (int)stbtt_GetCodepointKernAdvance(&font.font, codepoint, line[i+1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentY += lineHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -18,28 +18,27 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module;
|
module;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
export module Crafter.Graphics:RenderingElement3DVulkan;
|
export module Crafter.Graphics:RenderingElement3D;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import std;
|
import std;
|
||||||
import :Mesh;
|
import :Mesh;
|
||||||
import :VulkanBuffer;
|
import :VulkanBuffer;
|
||||||
import Crafter.Math;
|
import Crafter.Math;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
|
|
||||||
struct TlasWithBuffer {
|
struct TlasWithBuffer {
|
||||||
VulkanBuffer<char, false, true, false> buffer;
|
VulkanBuffer<char, false, true, false> buffer;
|
||||||
VkAccelerationStructureKHR accelerationStructure;
|
VkAccelerationStructureKHR accelerationStructure;
|
||||||
VulkanBuffer<VkAccelerationStructureInstanceKHR, true, true, false> instanceBuffer;
|
VulkanBuffer<VkAccelerationStructureInstanceKHR, true, true, false> instanceBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
class RenderingElement3DVulkan {
|
class RenderingElement3D {
|
||||||
public:
|
public:
|
||||||
VkAccelerationStructureInstanceKHR instance;
|
VkAccelerationStructureInstanceKHR instance;
|
||||||
static std::vector<RenderingElement3DVulkan*> elements;
|
static std::vector<RenderingElement3D*> elements;
|
||||||
inline static VulkanBuffer<char, false, true, false> scratchBuffer;
|
inline static VulkanBuffer<char, false, true, false> scratchBuffer;
|
||||||
inline static std::vector<TlasWithBuffer> tlases;
|
inline static std::vector<TlasWithBuffer> tlases;
|
||||||
static void BuildTLAS(VkCommandBuffer cmd, std::uint32_t index);
|
static void BuildTLAS(VkCommandBuffer cmd, std::uint32_t index);
|
||||||
43
interfaces/Crafter.Graphics-Rendertarget.cppm
Normal file
43
interfaces/Crafter.Graphics-Rendertarget.cppm
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2026 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 version 3.0 as published by the Free Software Foundation;
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
export module Crafter.Graphics:Rendertarget;
|
||||||
|
import Crafter.Math;
|
||||||
|
import std;
|
||||||
|
import :Types;
|
||||||
|
import :Transform2D;
|
||||||
|
|
||||||
|
export namespace Crafter {
|
||||||
|
struct RenderingElement2DBase;
|
||||||
|
struct Window;
|
||||||
|
struct Rendertarget {
|
||||||
|
Vector<std::uint8_t, 4>* buffer;
|
||||||
|
#ifdef CRAFTER_TIMING
|
||||||
|
std::vector<std::tuple<const Transform*, std::uint32_t, std::uint32_t, std::chrono::nanoseconds>> renderTimings;
|
||||||
|
#endif
|
||||||
|
Transform2D transform;
|
||||||
|
std::int32_t sizeX;
|
||||||
|
std::int32_t sizeY;
|
||||||
|
std::vector<RenderingElement2DBase*> elements;
|
||||||
|
std::vector<ClipRect> dirtyRects;
|
||||||
|
Rendertarget(std::int32_t sizeX, std::int32_t sizeY);
|
||||||
|
void RenderElement(RenderingElement2DBase* transform);
|
||||||
|
void AddDirtyRect(ScaleData2D rect);
|
||||||
|
void Render();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
module;
|
module;
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -29,7 +29,7 @@ import :VulkanBuffer;
|
||||||
import :ImageVulkan;
|
import :ImageVulkan;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
template <typename PixelType>
|
template <typename PixelType>
|
||||||
class SamplerVulkan {
|
class SamplerVulkan {
|
||||||
public:
|
public:
|
||||||
|
|
@ -53,7 +53,7 @@ export namespace Crafter {
|
||||||
samplerInfo.mipLodBias = 0.0f;
|
samplerInfo.mipLodBias = 0.0f;
|
||||||
samplerInfo.minLod = 0.0f;
|
samplerInfo.minLod = 0.0f;
|
||||||
samplerInfo.maxLod = VK_LOD_CLAMP_NONE;
|
samplerInfo.maxLod = VK_LOD_CLAMP_NONE;
|
||||||
VulkanDevice::CheckVkResult(vkCreateSampler(VulkanDevice::device, &samplerInfo, nullptr, &textureSampler));
|
Device::CheckVkResult(vkCreateSampler(Device::device, &samplerInfo, nullptr, &textureSampler));
|
||||||
|
|
||||||
imageInfo = {
|
imageInfo = {
|
||||||
.sampler = textureSampler
|
.sampler = textureSampler
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module;
|
module;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
export module Crafter.Graphics:ShaderBindingTableVulkan;
|
export module Crafter.Graphics:ShaderBindingTableVulkan;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import std;
|
import std;
|
||||||
import :VulkanDevice;
|
import :Device;
|
||||||
import :VulkanBuffer;
|
import :VulkanBuffer;
|
||||||
import :ShaderVulkan;
|
import :ShaderVulkan;
|
||||||
import :Types;
|
import :Types;
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module;
|
module;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
export module Crafter.Graphics:ShaderVulkan;
|
export module Crafter.Graphics:ShaderVulkan;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import std;
|
import std;
|
||||||
import :VulkanDevice;
|
import :Device;
|
||||||
import :Types;
|
import :Types;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
|
|
@ -66,7 +66,7 @@ export namespace Crafter {
|
||||||
module_info.codeSize = spirv.size() * sizeof(uint32_t);
|
module_info.codeSize = spirv.size() * sizeof(uint32_t);
|
||||||
module_info.pCode = spirv.data();
|
module_info.pCode = spirv.data();
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateShaderModule(VulkanDevice::device, &module_info, nullptr, &shader));
|
Device::CheckVkResult(vkCreateShaderModule(Device::device, &module_info, nullptr, &shader));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -104,7 +104,7 @@ export namespace Crafter {
|
||||||
module_info.codeSize = spirv.size() * sizeof(uint32_t);
|
module_info.codeSize = spirv.size() * sizeof(uint32_t);
|
||||||
module_info.pCode = spirv.data();
|
module_info.pCode = spirv.data();
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkCreateShaderModule(VulkanDevice::device, &module_info, nullptr, &shader));
|
Device::CheckVkResult(vkCreateShaderModule(Device::device, &module_info, nullptr, &shader));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,33 +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 version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
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 <stdio.h>
|
|
||||||
|
|
||||||
export module Crafter.Graphics:Shm;
|
|
||||||
import std;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
#ifdef CRAFTER_BUILD_CONFIGURATION_TARGET_x86_64_pc_linux_gnu
|
|
||||||
void randname(char *buf);
|
|
||||||
int anonymous_shm_open();
|
|
||||||
int create_shm_file(off_t size);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
@ -1,51 +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 version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
|
|
||||||
export module Crafter.Graphics:Transform;
|
|
||||||
import std;
|
|
||||||
import :Types;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
class Window;
|
|
||||||
struct Anchor {
|
|
||||||
std::int32_t x;
|
|
||||||
std::int32_t y;
|
|
||||||
std::uint32_t width;
|
|
||||||
std::uint32_t height;
|
|
||||||
std::int32_t offsetX;
|
|
||||||
std::int32_t offsetY;
|
|
||||||
std::int32_t z;
|
|
||||||
bool maintainAspectRatio;
|
|
||||||
Anchor() = default;
|
|
||||||
Anchor(std::int32_t x, std::int32_t y, std::uint32_t width, std::uint32_t height, std::int32_t offsetX, std::int32_t offsetY, std::int32_t z, bool maintainAspectRatio = false);
|
|
||||||
};
|
|
||||||
class Transform {
|
|
||||||
public:
|
|
||||||
Anchor anchor;
|
|
||||||
ScaleData scaled;
|
|
||||||
std::vector<Transform*> children;
|
|
||||||
Transform(Anchor anchor);
|
|
||||||
Transform(Transform&) = delete;
|
|
||||||
Transform(Transform&&) = delete;
|
|
||||||
Transform& operator=(Transform&) = delete;
|
|
||||||
virtual ~Transform() = default;
|
|
||||||
virtual void UpdatePosition(Window& window);
|
|
||||||
virtual void UpdatePosition(Window& window, Transform& parent);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
51
interfaces/Crafter.Graphics-Transform2D.cppm
Normal file
51
interfaces/Crafter.Graphics-Transform2D.cppm
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
Crafter®.Graphics
|
||||||
|
Copyright (C) 2026 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 version 3.0 as published by the Free Software Foundation;
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
|
||||||
|
export module Crafter.Graphics:Transform2D;
|
||||||
|
import std;
|
||||||
|
import :Types;
|
||||||
|
|
||||||
|
export namespace Crafter {
|
||||||
|
struct Rendertarget;
|
||||||
|
struct Anchor2D {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float width;
|
||||||
|
float height;
|
||||||
|
float offsetX;
|
||||||
|
float offsetY;
|
||||||
|
std::int32_t z;
|
||||||
|
bool maintainAspectRatio;
|
||||||
|
Anchor2D() = default;
|
||||||
|
Anchor2D(float x, float y, float width, float height, float offsetX, float offsetY, std::int32_t z, bool maintainAspectRatio = false);
|
||||||
|
};
|
||||||
|
struct Transform2D {
|
||||||
|
Anchor2D anchor;
|
||||||
|
ScaleData2D scaled;
|
||||||
|
std::vector<Transform2D*> children;
|
||||||
|
Transform2D(Anchor2D anchor);
|
||||||
|
Transform2D(Transform2D&) = delete;
|
||||||
|
Transform2D(Transform2D&&) = delete;
|
||||||
|
Transform2D& operator=(Transform2D&) = delete;
|
||||||
|
virtual ~Transform2D() = default;
|
||||||
|
void ScaleElement(Transform2D& parent);
|
||||||
|
virtual void UpdatePosition(Rendertarget& window);
|
||||||
|
virtual void UpdatePosition(Rendertarget& window, Transform2D& parent);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -18,41 +18,23 @@ License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
module;
|
module;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
export module Crafter.Graphics:Types;
|
export module Crafter.Graphics:Types;
|
||||||
import std;
|
import std;
|
||||||
|
import Crafter.Math;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
struct MousePoint {
|
|
||||||
std::uint32_t x;
|
|
||||||
std::uint32_t y;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MouseDelta {
|
|
||||||
std::int64_t x;
|
|
||||||
std::int64_t y;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MouseMoveEvent {
|
struct MouseMoveEvent {
|
||||||
MousePoint lastMousePos;
|
Vector<float, 2> lastMousePos;
|
||||||
MousePoint currentMousePos;
|
Vector<float, 2> currentMousePos;
|
||||||
MouseDelta mouseDelta;
|
Vector<float, 2> mouseDelta;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ScaleData {
|
struct ScaleData2D {
|
||||||
std::int32_t x;
|
Vector<std::int32_t, 2> position;
|
||||||
std::int32_t y;
|
Vector<std::int32_t, 2> size;
|
||||||
std::int32_t width;
|
|
||||||
std::int32_t height;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ScaleDataBoundless {
|
|
||||||
std::uint32_t x;
|
|
||||||
std::uint32_t y;
|
|
||||||
std::uint32_t width;
|
|
||||||
std::uint32_t height;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ClipRect {
|
struct ClipRect {
|
||||||
|
|
@ -62,67 +44,6 @@ export namespace Crafter {
|
||||||
std::int32_t bottom;
|
std::int32_t bottom;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct __attribute__((packed)) Pixel_BU8_GU8_RU8_AU8 {
|
|
||||||
std::uint8_t b;
|
|
||||||
std::uint8_t g;
|
|
||||||
std::uint8_t r;
|
|
||||||
std::uint8_t a;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct __attribute__((packed)) Pixel_RU8_GU8_BU8_AU8 {
|
|
||||||
std::uint8_t r;
|
|
||||||
std::uint8_t g;
|
|
||||||
std::uint8_t b;
|
|
||||||
std::uint8_t a;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct __attribute__((packed)) Pixel_RU8_GU8_BU8 {
|
|
||||||
std::uint8_t r;
|
|
||||||
std::uint8_t g;
|
|
||||||
std::uint8_t b;
|
|
||||||
};
|
|
||||||
|
|
||||||
// struct __attribute__((packed)) Vertex {
|
|
||||||
// float x;
|
|
||||||
// float y;
|
|
||||||
// float z;
|
|
||||||
// float w;
|
|
||||||
// };
|
|
||||||
// struct __attribute__((packed)) VertexUV {
|
|
||||||
// float x;
|
|
||||||
// float y;
|
|
||||||
// float z;
|
|
||||||
// float w;
|
|
||||||
|
|
||||||
// float u;
|
|
||||||
// float v;
|
|
||||||
|
|
||||||
// float pad[2];
|
|
||||||
// };
|
|
||||||
|
|
||||||
// struct __attribute__((packed)) VertexRGBA {
|
|
||||||
// float x;
|
|
||||||
// float y;
|
|
||||||
// float z;
|
|
||||||
// float w;
|
|
||||||
|
|
||||||
// float r;
|
|
||||||
// float g;
|
|
||||||
// float b;
|
|
||||||
// float a;
|
|
||||||
// };
|
|
||||||
|
|
||||||
// struct __attribute__((packed)) HeightRGBA {
|
|
||||||
// float height;
|
|
||||||
|
|
||||||
// float pad[3];
|
|
||||||
|
|
||||||
// float r;
|
|
||||||
// float g;
|
|
||||||
// float b;
|
|
||||||
// float a;
|
|
||||||
// };
|
|
||||||
|
|
||||||
struct FrameTime {
|
struct FrameTime {
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> now;
|
std::chrono::time_point<std::chrono::high_resolution_clock> now;
|
||||||
std::chrono::duration<double> delta;
|
std::chrono::duration<double> delta;
|
||||||
|
|
@ -134,191 +55,6 @@ export namespace Crafter {
|
||||||
Transparent // Color blending is used
|
Transparent // Color blending is used
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
constexpr std::int8_t BOUND8 = 9;
|
|
||||||
constexpr std::int8_t SCALE8 = std::numeric_limits<std::int8_t>::max() / BOUND8;
|
|
||||||
constexpr std::uint8_t SCALEBOUNDLESS8 = std::numeric_limits<std::uint8_t>::max();
|
|
||||||
|
|
||||||
constexpr double SCALEDOUBLE8 = static_cast<double>(std::numeric_limits<std::int8_t>::max() / BOUND8);
|
|
||||||
constexpr double SCALEBOUNDLESSDOUBLE8 = static_cast<double>(std::numeric_limits<std::uint8_t>::max());
|
|
||||||
|
|
||||||
constexpr std::int16_t BOUND16 = 9;
|
|
||||||
constexpr std::int16_t SCALE16 = std::numeric_limits<std::int16_t>::max() / BOUND16;
|
|
||||||
constexpr std::uint16_t SCALEBOUNDLESS16 = std::numeric_limits<std::uint16_t>::max();
|
|
||||||
|
|
||||||
constexpr double SCALEDOUBLE16 = static_cast<double>(std::numeric_limits<std::int16_t>::max() / BOUND16);
|
|
||||||
constexpr double SCALEBOUNDLESSDOUBLE16 = static_cast<double>(std::numeric_limits<std::uint16_t>::max());
|
|
||||||
|
|
||||||
constexpr std::int32_t BOUND32 = 9;
|
|
||||||
constexpr std::int32_t SCALE32 = std::numeric_limits<std::int32_t>::max() / BOUND32;
|
|
||||||
constexpr std::uint32_t SCALEBOUNDLESS32 = std::numeric_limits<std::uint32_t>::max();
|
|
||||||
|
|
||||||
constexpr double SCALEDOUBLE32 = static_cast<double>(std::numeric_limits<std::int32_t>::max() / BOUND32);
|
|
||||||
constexpr double SCALEBOUNDLESSDOUBLE32 = static_cast<double>(std::numeric_limits<std::uint32_t>::max());
|
|
||||||
|
|
||||||
constexpr std::int64_t BOUND64 = 9;
|
|
||||||
constexpr std::int64_t SCALE64 = std::numeric_limits<std::int64_t>::max() / BOUND64;
|
|
||||||
constexpr std::uint64_t SCALEBOUNDLESS64 = std::numeric_limits<std::uint64_t>::max();
|
|
||||||
|
|
||||||
constexpr double SCALEDOUBLE64 = static_cast<double>(std::numeric_limits<std::int64_t>::max() / BOUND64);
|
|
||||||
constexpr double SCALEBOUNDLESSDOUBLE64 = static_cast<double>(std::numeric_limits<std::uint64_t>::max());
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr T FractionalToMapped(double f) requires(std::is_integral_v<T>) {
|
|
||||||
if constexpr (std::is_same_v<T, std::int8_t> || std::is_same_v<T, std::uint8_t>) {
|
|
||||||
return T(f * SCALEDOUBLE8);
|
|
||||||
} else if constexpr (std::is_same_v<T, std::int16_t> || std::is_same_v<T, std::uint16_t>) {
|
|
||||||
return T(f * SCALEDOUBLE16);
|
|
||||||
} else if constexpr (std::is_same_v<T, std::int32_t> || std::is_same_v<T, std::uint32_t>) {
|
|
||||||
return T(f * SCALEDOUBLE32);
|
|
||||||
} else {
|
|
||||||
return T(f * SCALEDOUBLE64);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr T FractionalToMappedBoundless(double f) requires(std::is_integral_v<T>) {
|
|
||||||
if constexpr (std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::int8_t>) {
|
|
||||||
return T(f * SCALEBOUNDLESSDOUBLE8);
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint16_t> || std::is_same_v<T, std::int16_t>) {
|
|
||||||
return T(f * SCALEBOUNDLESSDOUBLE16);
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::int32_t>) {
|
|
||||||
return T(f * SCALEBOUNDLESSDOUBLE32);
|
|
||||||
} else {
|
|
||||||
return T(f * SCALEBOUNDLESSDOUBLE64);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr double MappedToFractional(T mapped) requires(std::is_integral_v<T>) {
|
|
||||||
if constexpr (std::is_same_v<T, std::int8_t> || std::is_same_v<T, std::uint8_t>) {
|
|
||||||
return mapped / SCALEDOUBLE8;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::int16_t> || std::is_same_v<T, std::uint16_t>) {
|
|
||||||
return mapped / SCALEDOUBLE16;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::int32_t> || std::is_same_v<T, std::uint32_t>) {
|
|
||||||
return mapped / SCALEDOUBLE32;
|
|
||||||
} else {
|
|
||||||
return mapped / SCALEDOUBLE64;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr double MappedToFractionalBoundless(T mapped) requires(std::is_integral_v<T>) {
|
|
||||||
if constexpr (std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::int8_t>) {
|
|
||||||
return mapped / SCALEBOUNDLESSDOUBLE8;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint16_t> || std::is_same_v<T, std::int16_t>) {
|
|
||||||
return mapped / SCALEBOUNDLESSDOUBLE16;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::int32_t>) {
|
|
||||||
return mapped / SCALEBOUNDLESSDOUBLE32;
|
|
||||||
} else {
|
|
||||||
return mapped / SCALEBOUNDLESSDOUBLE64;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr double MappedToNDCBoundless(T mapped, std::uint32_t size) requires(std::is_integral_v<T>) {
|
|
||||||
if constexpr (std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::int8_t>) {
|
|
||||||
return mapped / SCALEBOUNDLESSDOUBLE8 - 0.5;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint16_t> || std::is_same_v<T, std::int16_t>) {
|
|
||||||
return mapped / SCALEBOUNDLESSDOUBLE16 - 0.5;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::int32_t>) {
|
|
||||||
return mapped / SCALEBOUNDLESSDOUBLE32 - 0.5;
|
|
||||||
} else {
|
|
||||||
return mapped / SCALEBOUNDLESSDOUBLE64 - 0.5;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, typename T2>
|
|
||||||
constexpr T MappedToAbsolute(T mapped, T2 absolute) requires(std::is_integral_v<T>) {
|
|
||||||
if constexpr (std::is_same_v<T, std::int8_t> || std::is_same_v<T, std::uint8_t>) {
|
|
||||||
return static_cast<std::int16_t>(mapped) * absolute / SCALE8;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::int16_t> || std::is_same_v<T, std::uint16_t>) {
|
|
||||||
return static_cast<std::int32_t>(mapped) * absolute / SCALE16;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::int32_t> || std::is_same_v<T, std::uint32_t>) {
|
|
||||||
return static_cast<std::int64_t>(mapped) * absolute / SCALE32;
|
|
||||||
} else {
|
|
||||||
return static_cast<__int128>(mapped) * absolute / SCALE64;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, typename T2>
|
|
||||||
constexpr T MappedToAbsoluteBoundless(T mapped, T2 absolute) requires(std::is_integral_v<T>) {
|
|
||||||
if constexpr (std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::int8_t>) {
|
|
||||||
return static_cast<std::uint16_t>(mapped) * absolute / SCALEBOUNDLESS8;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint16_t> || std::is_same_v<T, std::int16_t>) {
|
|
||||||
return static_cast<std::uint32_t>(mapped) * absolute / SCALEBOUNDLESS16;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::int32_t>) {
|
|
||||||
return static_cast<std::uint64_t>(mapped) * absolute / SCALEBOUNDLESS32;
|
|
||||||
} else {
|
|
||||||
return static_cast<unsigned __int128>(mapped) * absolute / SCALEBOUNDLESS64;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// template <typename T, typename T2>
|
|
||||||
// constexpr T PixelToMappedBoundless(T pixel, T2 screen) requires(std::is_integral_v<T>) {
|
|
||||||
// if constexpr (std::is_same_v<T, std::uint8_t>) {
|
|
||||||
// return (static_cast<std::uint16_t>(relative) * SCALE8) / (static_cast<std::uint16_t>(absolute) * SCALE8);
|
|
||||||
// } else if constexpr (std::is_same_v<T, std::uint16_t>) {
|
|
||||||
// return (static_cast<std::uint32_t>(relative) * SCALE16) / (static_cast<std::uint32_t>(absolute) * SCALE16);
|
|
||||||
// } else if constexpr (std::is_same_v<T, std::uint32_t>) {
|
|
||||||
// return (static_cast<std::uint64_t>(relative) * SCALE32) / (static_cast<std::uint64_t>(absolute) * SCALE32);
|
|
||||||
// } else {
|
|
||||||
// return (static_cast<unsigned __int128>(relative) * SCALE32) / (static_cast<unsigned __int128>(absolute) * SCALE32);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
template <typename T, typename T2>
|
|
||||||
constexpr T AbsoluteToMapped(T absolute, T2 mapped) {
|
|
||||||
if constexpr (std::is_same_v<T, std::int8_t> || std::is_same_v<T, std::uint8_t>) {
|
|
||||||
return static_cast<std::int16_t>(absolute) * SCALE8 / mapped;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::int16_t> || std::is_same_v<T, std::uint16_t> ) {
|
|
||||||
return static_cast<std::int32_t>(absolute) * SCALE16 / mapped;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::int32_t>|| std::is_same_v<T, std::uint32_t>) {
|
|
||||||
return static_cast<std::int64_t>(absolute) * SCALE32 / mapped;
|
|
||||||
} else {
|
|
||||||
return static_cast<__int128>(absolute) * SCALE64 / mapped;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, typename T2>
|
|
||||||
constexpr T AbsoluteToMappedBoundless(T absolute, T2 mapped) {
|
|
||||||
if constexpr (std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::int8_t> ) {
|
|
||||||
return static_cast<std::uint16_t>(absolute) * SCALEBOUNDLESS8 / mapped;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint16_t> || std::is_same_v<T, std::int16_t>) {
|
|
||||||
return static_cast<std::uint32_t>(absolute) * SCALEBOUNDLESS16 / mapped;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::int32_t>) {
|
|
||||||
return static_cast<std::uint64_t>(absolute) * SCALEBOUNDLESS32 / mapped;
|
|
||||||
} else {
|
|
||||||
return static_cast<unsigned __int128>(absolute) * SCALEBOUNDLESS64 / mapped;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr T BoundToBoundless(T mapped) {
|
|
||||||
if constexpr (std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::int8_t>) {
|
|
||||||
return mapped * BOUND8 * 2;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint16_t> || std::is_same_v<T, std::int16_t>) {
|
|
||||||
return mapped * BOUND16 * 2;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::int32_t>) {
|
|
||||||
return mapped * BOUND32 * 2;
|
|
||||||
} else {
|
|
||||||
return mapped * BOUND64 * 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
constexpr T BoundlessToBound(T mapped) {
|
|
||||||
if constexpr (std::is_same_v<T, std::uint8_t> || std::is_same_v<T, std::int8_t> ) {
|
|
||||||
return mapped / 2;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint16_t> || std::is_same_v<T, std::int16_t>) {
|
|
||||||
return mapped / 2;
|
|
||||||
} else if constexpr (std::is_same_v<T, std::uint32_t> || std::is_same_v<T, std::int32_t>) {
|
|
||||||
return mapped / 2;
|
|
||||||
} else {
|
|
||||||
return mapped / 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class CrafterKeys {
|
enum class CrafterKeys {
|
||||||
// Alphabetic keys
|
// Alphabetic keys
|
||||||
A, B, C, D, E, F, G, H, I, J, K, L, M,
|
A, B, C, D, E, F, G, H, I, J, K, L, M,
|
||||||
|
|
@ -372,7 +108,7 @@ export namespace Crafter {
|
||||||
return std::tan(fov * std::numbers::pi / 360.0);
|
return std::tan(fov * std::numbers::pi / 360.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
struct DescriptorBinding {
|
struct DescriptorBinding {
|
||||||
VkDescriptorType type;
|
VkDescriptorType type;
|
||||||
std::uint32_t slot;
|
std::uint32_t slot;
|
||||||
|
|
|
||||||
|
|
@ -19,14 +19,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
module;
|
module;
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
export module Crafter.Graphics:VulkanBuffer;
|
export module Crafter.Graphics:VulkanBuffer;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import std;
|
import std;
|
||||||
import :VulkanDevice;
|
import :Device;
|
||||||
|
|
||||||
namespace Crafter {
|
namespace Crafter {
|
||||||
export class VulkanBufferBase {
|
export class VulkanBufferBase {
|
||||||
|
|
@ -95,14 +95,14 @@ namespace Crafter {
|
||||||
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||||
bufferCreateInfo.usage = usageFlags;
|
bufferCreateInfo.usage = usageFlags;
|
||||||
bufferCreateInfo.size = sizeof(T)*count;
|
bufferCreateInfo.size = sizeof(T)*count;
|
||||||
VulkanDevice::CheckVkResult(vkCreateBuffer(VulkanDevice::device, &bufferCreateInfo, nullptr, &buffer));
|
Device::CheckVkResult(vkCreateBuffer(Device::device, &bufferCreateInfo, nullptr, &buffer));
|
||||||
|
|
||||||
VkMemoryRequirements memReqs;
|
VkMemoryRequirements memReqs;
|
||||||
vkGetBufferMemoryRequirements(VulkanDevice::device, buffer, &memReqs);
|
vkGetBufferMemoryRequirements(Device::device, buffer, &memReqs);
|
||||||
VkMemoryAllocateInfo memAlloc {
|
VkMemoryAllocateInfo memAlloc {
|
||||||
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
||||||
.allocationSize = memReqs.size,
|
.allocationSize = memReqs.size,
|
||||||
.memoryTypeIndex = VulkanDevice::GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags)
|
.memoryTypeIndex = Device::GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags)
|
||||||
};
|
};
|
||||||
if constexpr(Adressable) {
|
if constexpr(Adressable) {
|
||||||
VkMemoryAllocateFlagsInfoKHR allocFlagsInfo {
|
VkMemoryAllocateFlagsInfoKHR allocFlagsInfo {
|
||||||
|
|
@ -110,36 +110,36 @@ namespace Crafter {
|
||||||
.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR,
|
.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR,
|
||||||
};
|
};
|
||||||
memAlloc.pNext = &allocFlagsInfo;
|
memAlloc.pNext = &allocFlagsInfo;
|
||||||
VulkanDevice::CheckVkResult(vkAllocateMemory(VulkanDevice::device, &memAlloc, nullptr, &memory));
|
Device::CheckVkResult(vkAllocateMemory(Device::device, &memAlloc, nullptr, &memory));
|
||||||
} else {
|
} else {
|
||||||
VulkanDevice::CheckVkResult(vkAllocateMemory(VulkanDevice::device, &memAlloc, nullptr, &memory));
|
Device::CheckVkResult(vkAllocateMemory(Device::device, &memAlloc, nullptr, &memory));
|
||||||
}
|
}
|
||||||
|
|
||||||
descriptor.offset = 0;
|
descriptor.offset = 0;
|
||||||
descriptor.buffer = buffer;
|
descriptor.buffer = buffer;
|
||||||
descriptor.range = VK_WHOLE_SIZE;
|
descriptor.range = VK_WHOLE_SIZE;
|
||||||
|
|
||||||
VulkanDevice::CheckVkResult(vkBindBufferMemory(VulkanDevice::device, buffer, memory, 0));
|
Device::CheckVkResult(vkBindBufferMemory(Device::device, buffer, memory, 0));
|
||||||
|
|
||||||
if constexpr(Adressable) {
|
if constexpr(Adressable) {
|
||||||
VkBufferDeviceAddressInfo addressInfo = {
|
VkBufferDeviceAddressInfo addressInfo = {
|
||||||
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
|
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
|
||||||
.buffer = buffer
|
.buffer = buffer
|
||||||
};
|
};
|
||||||
VulkanBufferAdressableConditional<true>::address = vkGetBufferDeviceAddress(VulkanDevice::device, &addressInfo);
|
VulkanBufferAdressableConditional<true>::address = vkGetBufferDeviceAddress(Device::device, &addressInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
if constexpr(Mapped) {
|
if constexpr(Mapped) {
|
||||||
VulkanDevice::CheckVkResult(vkMapMemory(VulkanDevice::device, memory, 0, memReqs.size, 0, reinterpret_cast<void**>(&(VulkanBufferMappedConditional<T, true>::value))));
|
Device::CheckVkResult(vkMapMemory(Device::device, memory, 0, memReqs.size, 0, reinterpret_cast<void**>(&(VulkanBufferMappedConditional<T, true>::value))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
if constexpr(Mapped) {
|
if constexpr(Mapped) {
|
||||||
vkUnmapMemory(VulkanDevice::device, memory);
|
vkUnmapMemory(Device::device, memory);
|
||||||
}
|
}
|
||||||
vkDestroyBuffer(VulkanDevice::device, buffer, nullptr);
|
vkDestroyBuffer(Device::device, buffer, nullptr);
|
||||||
vkFreeMemory(VulkanDevice::device, memory, nullptr);
|
vkFreeMemory(Device::device, memory, nullptr);
|
||||||
buffer = VK_NULL_HANDLE;
|
buffer = VK_NULL_HANDLE;
|
||||||
if constexpr(Staged) {
|
if constexpr(Staged) {
|
||||||
delete VulkanBufferMappedConditional<T, true>::stagingBuffer;
|
delete VulkanBufferMappedConditional<T, true>::stagingBuffer;
|
||||||
|
|
@ -201,7 +201,7 @@ namespace Crafter {
|
||||||
.offset = 0,
|
.offset = 0,
|
||||||
.size = VK_WHOLE_SIZE
|
.size = VK_WHOLE_SIZE
|
||||||
};
|
};
|
||||||
vkFlushMappedMemoryRanges(VulkanDevice::device, 1, &range);
|
vkFlushMappedMemoryRanges(Device::device, 1, &range);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FlushDevice(VkCommandBuffer cmd, VkAccessFlags dstAccessMask, VkPipelineStageFlags dstStageMask) requires(Mapped) {
|
void FlushDevice(VkCommandBuffer cmd, VkAccessFlags dstAccessMask, VkPipelineStageFlags dstStageMask) requires(Mapped) {
|
||||||
|
|
@ -235,7 +235,7 @@ namespace Crafter {
|
||||||
.offset = 0,
|
.offset = 0,
|
||||||
.size = VK_WHOLE_SIZE
|
.size = VK_WHOLE_SIZE
|
||||||
};
|
};
|
||||||
vkInvalidateMappedMemoryRanges(VulkanDevice::device, 1, &range);
|
vkInvalidateMappedMemoryRanges(Device::device, 1, &range);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FlushDevice(VkCommandBuffer cmd) requires(Staged) {
|
void FlushDevice(VkCommandBuffer cmd) requires(Staged) {
|
||||||
|
|
|
||||||
|
|
@ -1,59 +0,0 @@
|
||||||
/*
|
|
||||||
Crafter®.Graphics
|
|
||||||
Copyright (C) 2026 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 version 3.0 as published by the Free Software Foundation;
|
|
||||||
|
|
||||||
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;
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
|
||||||
#include "vulkan/vulkan.h"
|
|
||||||
#endif
|
|
||||||
export module Crafter.Graphics:VulkanDevice;
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
|
||||||
import std;
|
|
||||||
|
|
||||||
export namespace Crafter {
|
|
||||||
class VulkanDevice {
|
|
||||||
public:
|
|
||||||
inline static VkInstance instance = VK_NULL_HANDLE;
|
|
||||||
inline static VkDebugUtilsMessengerEXT debugMessenger = VK_NULL_HANDLE;
|
|
||||||
inline static VkPhysicalDevice physDevice = VK_NULL_HANDLE;
|
|
||||||
inline static VkDevice device = VK_NULL_HANDLE;
|
|
||||||
inline static std::uint32_t queueFamilyIndex = 0;
|
|
||||||
inline static VkQueue queue = VK_NULL_HANDLE;
|
|
||||||
inline static VkCommandPool commandPool = VK_NULL_HANDLE;
|
|
||||||
inline static VkSwapchainKHR swapchain = VK_NULL_HANDLE;
|
|
||||||
inline static PFN_vkCmdDrawMeshTasksEXT vkCmdDrawMeshTasksEXTProc;
|
|
||||||
inline static PFN_vkCmdBeginRenderingKHR vkCmdBeginRenderingKHRProc;
|
|
||||||
inline static PFN_vkCmdEndRenderingKHR vkCmdEndRenderingKHRProc;
|
|
||||||
inline static PFN_vkGetAccelerationStructureBuildSizesKHR vkGetAccelerationStructureBuildSizesKHR;
|
|
||||||
inline static PFN_vkCreateAccelerationStructureKHR vkCreateAccelerationStructureKHR;
|
|
||||||
inline static PFN_vkCmdBuildAccelerationStructuresKHR vkCmdBuildAccelerationStructuresKHR;
|
|
||||||
inline static PFN_vkGetAccelerationStructureDeviceAddressKHR vkGetAccelerationStructureDeviceAddressKHR;
|
|
||||||
inline static PFN_vkCreateRayTracingPipelinesKHR vkCreateRayTracingPipelinesKHR;
|
|
||||||
inline static PFN_vkGetRayTracingShaderGroupHandlesKHR vkGetRayTracingShaderGroupHandlesKHR;
|
|
||||||
inline static PFN_vkCmdTraceRaysKHR vkCmdTraceRaysKHR;
|
|
||||||
inline static VkPhysicalDeviceMemoryProperties memoryProperties;
|
|
||||||
inline static VkPhysicalDeviceRayTracingPipelinePropertiesKHR rayTracingProperties = {
|
|
||||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR
|
|
||||||
};
|
|
||||||
static void CreateDevice();
|
|
||||||
static void CheckVkResult(VkResult result);
|
|
||||||
static std::uint32_t GetMemoryType(std::uint32_t typeBits, VkMemoryPropertyFlags properties);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
@ -18,12 +18,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module;
|
module;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#endif
|
#endif
|
||||||
export module Crafter.Graphics:VulkanTransition;
|
export module Crafter.Graphics:VulkanTransition;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import std;
|
import std;
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
catcrafts.net
|
catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -18,7 +18,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
module;
|
module;
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
@ -32,95 +32,46 @@ module;
|
||||||
#include <linux/input.h>
|
#include <linux/input.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <wayland-cursor.h>
|
#include <wayland-cursor.h>
|
||||||
#include <xkbcommon/xkbcommon.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <wayland-client.h>
|
#include <wayland-client.h>
|
||||||
#include <wayland-client-protocol.h>
|
#include <wayland-client-protocol.h>
|
||||||
#endif
|
#endif
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
#ifndef CRAFTER_GRAPHICS_WINDOWS
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <linux/input-event-codes.h>
|
|
||||||
#include <xkbcommon/xkbcommon.h>
|
|
||||||
#include "../lib/xdg-shell-client-protocol.h"
|
|
||||||
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
|
|
||||||
#include "../lib/fractional-scale-v1.h"
|
|
||||||
#include "../lib/viewporter.h"
|
|
||||||
#include <string.h>
|
|
||||||
#include <linux/input.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <wayland-cursor.h>
|
|
||||||
#include <xkbcommon/xkbcommon.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <print>
|
|
||||||
#include <wayland-client.h>
|
|
||||||
#include <wayland-client-protocol.h>
|
|
||||||
#include "vulkan/vulkan.h"
|
#include "vulkan/vulkan.h"
|
||||||
#include "vulkan/vulkan_wayland.h"
|
|
||||||
#else
|
|
||||||
#include "vulkan/vulkan.h"
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
export module Crafter.Graphics:Window;
|
export module Crafter.Graphics:Window;
|
||||||
import std;
|
import std;
|
||||||
import :Types;
|
import :Types;
|
||||||
|
import :Rendertarget;
|
||||||
|
import :Transform2D;
|
||||||
import Crafter.Event;
|
import Crafter.Event;
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
import :PipelineRTVulkan;
|
import :PipelineRTVulkan;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
export namespace Crafter {
|
export namespace Crafter {
|
||||||
class Transform;
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
class MouseElement;
|
struct Semaphores {
|
||||||
class Window {
|
// Swap chain image presentation
|
||||||
public:
|
VkSemaphore presentComplete;
|
||||||
|
// Command buffer submission and execution
|
||||||
|
VkSemaphore renderComplete;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct MouseElement;
|
||||||
|
struct Window {
|
||||||
FrameTime currentFrameTime;
|
FrameTime currentFrameTime;
|
||||||
std::int32_t width;
|
std::uint32_t width;
|
||||||
std::int32_t height;
|
std::uint32_t height;
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> lastFrameBegin;
|
std::chrono::time_point<std::chrono::high_resolution_clock> lastFrameBegin;
|
||||||
std::vector<Transform*> elements;
|
|
||||||
Event<void> onClose;
|
Event<void> onClose;
|
||||||
Event<FrameTime> onUpdate;
|
Event<FrameTime> onUpdate;
|
||||||
bool open = true;
|
bool open = true;
|
||||||
bool updating = false;
|
bool updating = false;
|
||||||
std::vector<ClipRect> dirtyRects;
|
|
||||||
float scale;
|
|
||||||
|
|
||||||
Window() = default;
|
|
||||||
Window(std::int32_t width, std::int32_t height);
|
|
||||||
Window(Window&) = delete;
|
|
||||||
Window(Window&&) = delete;
|
|
||||||
virtual ~Window() = default;
|
|
||||||
Window& operator=(const Window&) = delete;
|
|
||||||
void AddDirtyRect(ScaleData rect);
|
|
||||||
virtual void StartSync() = 0;
|
|
||||||
virtual void StartUpdate() = 0;
|
|
||||||
virtual void StopUpdate() = 0;
|
|
||||||
void ScaleElement(Transform& element, Transform& parent);
|
|
||||||
void ScaleElement(Transform& element);
|
|
||||||
void ScaleMouse(MouseElement& element, Transform& parent);
|
|
||||||
void ScaleMouse(MouseElement& element);
|
|
||||||
#ifdef CRAFTER_TIMING
|
|
||||||
std::chrono::nanoseconds totalUpdate;
|
|
||||||
std::vector<std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>> updateTimings;
|
|
||||||
std::chrono::nanoseconds totalRender;
|
|
||||||
std::vector<std::tuple<const Transform*, std::uint32_t, std::uint32_t, std::chrono::nanoseconds>> renderTimings;
|
|
||||||
std::chrono::nanoseconds vblank;
|
|
||||||
std::chrono::nanoseconds totalFrame;
|
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> frameEnd;
|
|
||||||
std::vector<std::chrono::nanoseconds> frameTimes;
|
|
||||||
void LogTiming();
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
class WindowKeyboard {
|
|
||||||
public:
|
|
||||||
bool heldkeys[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)] = {};
|
bool heldkeys[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)] = {};
|
||||||
Event<void> onKeyDown[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
|
Event<void> onKeyDown[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
|
||||||
Event<void> onKeyHold[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
|
Event<void> onKeyHold[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
|
||||||
|
|
@ -128,134 +79,69 @@ export namespace Crafter {
|
||||||
Event<CrafterKeys> onAnyKeyDown;
|
Event<CrafterKeys> onAnyKeyDown;
|
||||||
Event<CrafterKeys> onAnyKeyHold;
|
Event<CrafterKeys> onAnyKeyHold;
|
||||||
Event<CrafterKeys> onAnyKeyUp;
|
Event<CrafterKeys> onAnyKeyUp;
|
||||||
};
|
Event<void> onMouseRightClick;
|
||||||
|
Event<void> onMouseLeftClick;
|
||||||
class MouseElement;
|
Event<void> onMouseRightHold;
|
||||||
class WindowMouse {
|
Event<void> onMouseLeftHold;
|
||||||
public:
|
Event<void> onMouseRightRelease;
|
||||||
Event<MousePoint> onMouseRightClick;
|
Event<void> onMouseLeftRelease;
|
||||||
Event<MousePoint> onMouseLeftClick;
|
Event<void> onMouseMove;
|
||||||
Event<MousePoint> onMouseRightHold;
|
Event<void> onMouseEnter;
|
||||||
Event<MousePoint> onMouseLeftHold;
|
Event<void> onMouseLeave;
|
||||||
Event<MousePoint> onMouseRightRelease;
|
|
||||||
Event<MousePoint> onMouseLeftRelease;
|
|
||||||
Event<MouseMoveEvent> onMouseMove;
|
|
||||||
Event<MouseMoveEvent> onMouseEnter;
|
|
||||||
Event<MouseMoveEvent> onMouseLeave;
|
|
||||||
Event<std::uint32_t> onMouseScroll;
|
Event<std::uint32_t> onMouseScroll;
|
||||||
MousePoint currentMousePos;
|
Vector<float, 2> currentMousePos;
|
||||||
MousePoint lastMousePos;
|
Vector<float, 2> lastMousePos;
|
||||||
MouseDelta mouseDelta;
|
Vector<float, 2> mouseDelta;
|
||||||
bool mouseLeftHeld = false;
|
bool mouseLeftHeld = false;
|
||||||
bool mouseRightHeld = false;
|
bool mouseRightHeld = false;
|
||||||
std::vector<MouseElement*> mouseElements;
|
std::vector<MouseElement*> mouseElements;
|
||||||
std::vector<MouseElement*> pendingMouseElements;
|
std::vector<MouseElement*> pendingMouseElements;
|
||||||
};
|
|
||||||
|
|
||||||
class WindowTitle {
|
Window(std::uint32_t width, std::uint32_t height);
|
||||||
public:
|
Window(std::uint32_t width, std::uint32_t height, const std::string_view title);
|
||||||
virtual void SetTitle(const std::string_view title) = 0;
|
Window(Window&) = delete;
|
||||||
};
|
Window(Window&&) = delete;
|
||||||
|
Window& operator=(const Window&) = delete;
|
||||||
|
|
||||||
class Transform;
|
void StartSync();
|
||||||
class WindowFramebuffer : public Window {
|
void StartUpdate();
|
||||||
public:
|
void StopUpdate();
|
||||||
WindowFramebuffer() = default;
|
void SetTitle(const std::string_view title);
|
||||||
WindowFramebuffer(std::uint32_t width, std::uint32_t height);
|
void Resize(std::uint32_t width, std::uint32_t height);
|
||||||
virtual void Resize(std::uint32_t width, std::uint32_t height) = 0;
|
void Render();
|
||||||
virtual void Write(Pixel_BU8_GU8_RU8_AU8* pixels) = 0;
|
void Update();
|
||||||
virtual void Write(std::uint32_t x, std::uint32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) = 0;
|
|
||||||
virtual Pixel_BU8_GU8_RU8_AU8 Read(std::uint32_t x, std::uint32_t y) const = 0;
|
|
||||||
virtual const Pixel_BU8_GU8_RU8_AU8* Read() const = 0;
|
|
||||||
virtual Pixel_BU8_GU8_RU8_AU8* Get() = 0;
|
|
||||||
virtual void Store() = 0;
|
|
||||||
virtual void Render() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_WAYLAND
|
#ifdef CRAFTER_TIMING
|
||||||
class WindowWayland final : public WindowKeyboard, public WindowMouse, public WindowFramebuffer, public WindowTitle {
|
std::chrono::nanoseconds totalUpdate;
|
||||||
public:
|
std::vector<std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>> updateTimings;
|
||||||
Pixel_BU8_GU8_RU8_AU8* framebuffer = nullptr;
|
std::chrono::nanoseconds totalRender;
|
||||||
WindowWayland(std::uint32_t width, std::uint32_t height);
|
std::chrono::nanoseconds vblank;
|
||||||
WindowWayland(std::uint32_t width, std::uint32_t height, const std::string_view title);
|
std::chrono::nanoseconds totalFrame;
|
||||||
~WindowWayland();
|
std::chrono::time_point<std::chrono::high_resolution_clock> frameEnd;
|
||||||
|
std::vector<std::chrono::nanoseconds> frameTimes;
|
||||||
|
void LogTiming();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
|
||||||
|
float scale;
|
||||||
|
#ifdef CRAFTER_GRAPHICS_RENDERER_SOFTWARE
|
||||||
|
Rendertarget renderer;
|
||||||
|
#endif
|
||||||
bool configured = false;
|
bool configured = false;
|
||||||
wl_shm* shm = nullptr;
|
|
||||||
wl_seat* seat = nullptr;
|
|
||||||
wp_fractional_scale_v1* wp_scale = nullptr;
|
|
||||||
xdg_toplevel* xdgToplevel = nullptr;
|
xdg_toplevel* xdgToplevel = nullptr;
|
||||||
wp_viewport* wpViewport = nullptr;
|
wp_viewport* wpViewport = nullptr;
|
||||||
wp_viewporter* wpViewporter = nullptr;
|
|
||||||
xdg_wm_base* xdgWmBase = nullptr;
|
|
||||||
zxdg_decoration_manager_v1* manager = nullptr;
|
|
||||||
wp_fractional_scale_manager_v1* fractionalScaleManager = nullptr;
|
|
||||||
wl_surface* surface = nullptr;
|
wl_surface* surface = nullptr;
|
||||||
wl_buffer* buffer = nullptr;
|
wl_buffer* buffer = nullptr;
|
||||||
wl_buffer* backBuffer = nullptr;
|
wl_buffer* backBuffer = nullptr;
|
||||||
xdg_surface* xdgSurface = nullptr;
|
xdg_surface* xdgSurface = nullptr;
|
||||||
wl_display* display = nullptr;
|
|
||||||
wl_callback* cb = nullptr;
|
wl_callback* cb = nullptr;
|
||||||
xkb_keymap* xkb_keymap;
|
|
||||||
xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
||||||
xkb_state* xkb_state;
|
|
||||||
void RenderElement(Transform* transform);
|
|
||||||
void Render() override;
|
|
||||||
void QueueRender();
|
|
||||||
void StartSync() override;
|
|
||||||
void StartUpdate() override;
|
|
||||||
void StopUpdate() override;
|
|
||||||
void SetTitle(const std::string_view title) override;
|
|
||||||
void Resize(std::uint32_t width, std::uint32_t height) override;
|
|
||||||
void Write(Pixel_BU8_GU8_RU8_AU8* pixels) override;
|
|
||||||
void Write(std::uint32_t x, std::uint32_t y, Pixel_BU8_GU8_RU8_AU8 pixel) override;
|
|
||||||
Pixel_BU8_GU8_RU8_AU8 Read(std::uint32_t x, std::uint32_t y) const override;
|
|
||||||
const Pixel_BU8_GU8_RU8_AU8* Read() const override;
|
|
||||||
Pixel_BU8_GU8_RU8_AU8* Get() override;
|
|
||||||
void Store() override;
|
|
||||||
inline static wl_compositor* compositor = nullptr;
|
|
||||||
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
|
|
||||||
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
|
||||||
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value);
|
|
||||||
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
|
||||||
static void PointerListenerHandleLeave(void*, wl_pointer*, std::uint32_t, wl_surface*);
|
|
||||||
static void xdg_toplevel_handle_close(void* data, xdg_toplevel*);
|
|
||||||
static void handle_global(void* data, wl_registry* registry, std::uint32_t name, const char* interface, std::uint32_t version);
|
|
||||||
static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state);
|
|
||||||
static void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities);
|
|
||||||
static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial);
|
|
||||||
static void xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale);
|
static void xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale);
|
||||||
static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial);
|
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
|
||||||
static void keyboard_keymap(void* data, wl_keyboard* keyboard, uint32_t format, int fd, uint32_t size);
|
static void xdg_toplevel_handle_close(void* data, xdg_toplevel*);
|
||||||
static void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys);
|
static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial);
|
||||||
static void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface);
|
|
||||||
static void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
|
|
||||||
static void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
|
|
||||||
static void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay);
|
|
||||||
static void handle_global_remove(void* data, wl_registry* registry, uint32_t name);
|
|
||||||
static void xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*);
|
static void xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*);
|
||||||
|
|
||||||
constexpr static wl_pointer_listener pointer_listener = {
|
|
||||||
.enter = PointerListenerHandleEnter,
|
|
||||||
.leave = PointerListenerHandleLeave,
|
|
||||||
.motion = PointerListenerHandleMotion,
|
|
||||||
.button = pointer_handle_button,
|
|
||||||
.axis = PointerListenerHandleAxis,
|
|
||||||
};
|
|
||||||
constexpr static wl_keyboard_listener keyboard_listener = {
|
|
||||||
.keymap = keyboard_keymap,
|
|
||||||
.enter = keyboard_enter,
|
|
||||||
.leave = keyboard_leave,
|
|
||||||
.key = keyboard_key,
|
|
||||||
.modifiers = keyboard_modifiers,
|
|
||||||
.repeat_info = keyboard_repeat_info,
|
|
||||||
};
|
|
||||||
constexpr static wl_seat_listener seat_listener = {
|
|
||||||
.capabilities = seat_handle_capabilities,
|
|
||||||
};
|
|
||||||
constexpr static wl_registry_listener registry_listener = {
|
|
||||||
.global = handle_global,
|
|
||||||
.global_remove = handle_global_remove,
|
|
||||||
};
|
|
||||||
constexpr static xdg_toplevel_listener xdg_toplevel_listener = {
|
constexpr static xdg_toplevel_listener xdg_toplevel_listener = {
|
||||||
.configure = xdg_toplevel_configure,
|
.configure = xdg_toplevel_configure,
|
||||||
.close = xdg_toplevel_handle_close,
|
.close = xdg_toplevel_handle_close,
|
||||||
|
|
@ -263,60 +149,17 @@ export namespace Crafter {
|
||||||
constexpr static wl_callback_listener wl_callback_listener = {
|
constexpr static wl_callback_listener wl_callback_listener = {
|
||||||
.done = wl_surface_frame_done,
|
.done = wl_surface_frame_done,
|
||||||
};
|
};
|
||||||
constexpr static xdg_wm_base_listener xdgWmBaseListener = {
|
|
||||||
.ping = xdg_wm_base_handle_ping,
|
|
||||||
};
|
|
||||||
constexpr static xdg_surface_listener xdg_surface_listener = {
|
constexpr static xdg_surface_listener xdg_surface_listener = {
|
||||||
.configure = xdg_surface_handle_configure,
|
.configure = xdg_surface_handle_configure,
|
||||||
};
|
};
|
||||||
constexpr static wp_fractional_scale_v1_listener wp_fractional_scale_v1_listener = {
|
constexpr static wp_fractional_scale_v1_listener wp_fractional_scale_v1_listener = {
|
||||||
.preferred_scale = xdg_surface_handle_preferred_scale,
|
.preferred_scale = xdg_surface_handle_preferred_scale,
|
||||||
};
|
};
|
||||||
};
|
inline static wp_fractional_scale_v1* wp_scale = nullptr;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
struct Semaphores {
|
|
||||||
// Swap chain image presentation
|
|
||||||
VkSemaphore presentComplete;
|
|
||||||
// Command buffer submission and execution
|
|
||||||
VkSemaphore renderComplete;
|
|
||||||
};
|
|
||||||
#ifndef CRAFTER_GRAPHICS_WINDOWS
|
|
||||||
class WindowVulkan final : public Window, public WindowKeyboard, public WindowMouse, public WindowTitle {
|
|
||||||
public:
|
|
||||||
WindowVulkan(std::uint32_t width, std::uint32_t height);
|
|
||||||
WindowVulkan(std::uint32_t width, std::uint32_t height, const std::string_view title);
|
|
||||||
~WindowVulkan();
|
|
||||||
bool configured = false;
|
|
||||||
wl_shm* shm = nullptr;
|
|
||||||
wl_seat* seat = nullptr;
|
|
||||||
wp_fractional_scale_v1* wp_scale = nullptr;
|
|
||||||
xdg_toplevel* xdgToplevel = nullptr;
|
|
||||||
wp_viewport* wpViewport = nullptr;
|
|
||||||
wp_viewporter* wpViewporter = nullptr;
|
|
||||||
xdg_wm_base* xdgWmBase = nullptr;
|
|
||||||
zxdg_decoration_manager_v1* manager = nullptr;
|
|
||||||
wp_fractional_scale_manager_v1* fractionalScaleManager = nullptr;
|
|
||||||
wl_surface* surface = nullptr;
|
|
||||||
xdg_surface* xdgSurface = nullptr;
|
|
||||||
wl_display* display = nullptr;
|
|
||||||
wl_callback* cb = nullptr;
|
|
||||||
xkb_keymap* xkb_keymap;
|
|
||||||
xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
||||||
xkb_state* xkb_state;
|
|
||||||
Event<void> onRender;
|
|
||||||
std::vector<VkDescriptorSet> descriptorsRt;
|
std::vector<VkDescriptorSet> descriptorsRt;
|
||||||
void Render();
|
|
||||||
void QueueRender();
|
|
||||||
void Resize(std::uint32_t width, std::uint32_t height);
|
|
||||||
void StartSync() override;
|
|
||||||
void StartUpdate() override;
|
|
||||||
void StopUpdate() override;
|
|
||||||
void SetTitle(const std::string_view title) override;
|
|
||||||
VkCommandBuffer StartInit();
|
|
||||||
void FinishInit();
|
|
||||||
|
|
||||||
template <typename Pipeline>
|
template <typename Pipeline>
|
||||||
void SetPipelineRT() {
|
void SetPipelineRT() {
|
||||||
rtPipeline = Pipeline::pipeline;
|
rtPipeline = Pipeline::pipeline;
|
||||||
|
|
@ -326,77 +169,11 @@ export namespace Crafter {
|
||||||
hitRegion = Pipeline::hitRegion;
|
hitRegion = Pipeline::hitRegion;
|
||||||
callableRegion = Pipeline::callableRegion;
|
callableRegion = Pipeline::callableRegion;
|
||||||
}
|
}
|
||||||
|
void SetPipelineRT(PipelineRTVulkan& pipeline);
|
||||||
void SetPipelineRT(PipelineRTVulkan& pipeline) {
|
VkCommandBuffer StartInit();
|
||||||
rtPipeline = pipeline.pipeline;
|
void FinishInit();
|
||||||
rtPipelineLayout = pipeline.pipelineLayout;
|
|
||||||
raygenRegion = pipeline.raygenRegion;
|
|
||||||
missRegion = pipeline.missRegion;
|
|
||||||
hitRegion = pipeline.hitRegion;
|
|
||||||
callableRegion = pipeline.callableRegion;
|
|
||||||
}
|
|
||||||
inline static wl_compositor* compositor = nullptr;
|
|
||||||
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
|
|
||||||
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
|
||||||
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value);
|
|
||||||
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
|
|
||||||
static void PointerListenerHandleLeave(void*, wl_pointer*, std::uint32_t, wl_surface*);
|
|
||||||
static void xdg_toplevel_handle_close(void* data, xdg_toplevel*);
|
|
||||||
static void handle_global(void* data, wl_registry* registry, std::uint32_t name, const char* interface, std::uint32_t version);
|
|
||||||
static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state);
|
|
||||||
static void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities);
|
|
||||||
static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial);
|
|
||||||
static void xdg_surface_handle_preferred_scale(void* data, wp_fractional_scale_v1*, std::uint32_t scale);
|
|
||||||
static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial);
|
|
||||||
static void keyboard_keymap(void* data, wl_keyboard* keyboard, uint32_t format, int fd, uint32_t size);
|
|
||||||
static void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys);
|
|
||||||
static void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface);
|
|
||||||
static void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
|
|
||||||
static void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group);
|
|
||||||
static void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay);
|
|
||||||
static void handle_global_remove(void* data, wl_registry* registry, uint32_t name);
|
|
||||||
static void xdg_toplevel_configure(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*);
|
|
||||||
|
|
||||||
constexpr static wl_pointer_listener pointer_listener = {
|
|
||||||
.enter = PointerListenerHandleEnter,
|
|
||||||
.leave = PointerListenerHandleLeave,
|
|
||||||
.motion = PointerListenerHandleMotion,
|
|
||||||
.button = pointer_handle_button,
|
|
||||||
.axis = PointerListenerHandleAxis,
|
|
||||||
};
|
|
||||||
constexpr static wl_keyboard_listener keyboard_listener = {
|
|
||||||
.keymap = keyboard_keymap,
|
|
||||||
.enter = keyboard_enter,
|
|
||||||
.leave = keyboard_leave,
|
|
||||||
.key = keyboard_key,
|
|
||||||
.modifiers = keyboard_modifiers,
|
|
||||||
.repeat_info = keyboard_repeat_info,
|
|
||||||
};
|
|
||||||
constexpr static wl_seat_listener seat_listener = {
|
|
||||||
.capabilities = seat_handle_capabilities,
|
|
||||||
};
|
|
||||||
constexpr static wl_registry_listener registry_listener = {
|
|
||||||
.global = handle_global,
|
|
||||||
.global_remove = handle_global_remove,
|
|
||||||
};
|
|
||||||
constexpr static xdg_toplevel_listener xdg_toplevel_listener = {
|
|
||||||
.configure = xdg_toplevel_configure,
|
|
||||||
.close = xdg_toplevel_handle_close,
|
|
||||||
};
|
|
||||||
constexpr static wl_callback_listener wl_callback_listener = {
|
|
||||||
.done = wl_surface_frame_done,
|
|
||||||
};
|
|
||||||
constexpr static xdg_wm_base_listener xdgWmBaseListener = {
|
|
||||||
.ping = xdg_wm_base_handle_ping,
|
|
||||||
};
|
|
||||||
constexpr static xdg_surface_listener xdg_surface_listener = {
|
|
||||||
.configure = xdg_surface_handle_configure,
|
|
||||||
};
|
|
||||||
constexpr static wp_fractional_scale_v1_listener wp_fractional_scale_v1_listener = {
|
|
||||||
.preferred_scale = xdg_surface_handle_preferred_scale,
|
|
||||||
};
|
|
||||||
void CreateSwapchain();
|
void CreateSwapchain();
|
||||||
static constexpr std::uint32_t numFrames = 3;
|
static constexpr std::uint8_t numFrames = 3;
|
||||||
VkSurfaceKHR vulkanSurface = VK_NULL_HANDLE;
|
VkSurfaceKHR vulkanSurface = VK_NULL_HANDLE;
|
||||||
VkSwapchainKHR swapChain = VK_NULL_HANDLE;
|
VkSwapchainKHR swapChain = VK_NULL_HANDLE;
|
||||||
VkFormat colorFormat;
|
VkFormat colorFormat;
|
||||||
|
|
@ -407,7 +184,7 @@ export namespace Crafter {
|
||||||
VkCommandBuffer drawCmdBuffers[numFrames];
|
VkCommandBuffer drawCmdBuffers[numFrames];
|
||||||
VkSubmitInfo submitInfo;
|
VkSubmitInfo submitInfo;
|
||||||
Semaphores semaphores;
|
Semaphores semaphores;
|
||||||
uint32_t currentBuffer = 0;
|
std::uint32_t currentBuffer = 0;
|
||||||
VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||||
VkPipeline rtPipeline;
|
VkPipeline rtPipeline;
|
||||||
VkPipelineLayout rtPipelineLayout;
|
VkPipelineLayout rtPipelineLayout;
|
||||||
|
|
@ -415,64 +192,6 @@ export namespace Crafter {
|
||||||
VkStridedDeviceAddressRegionKHR missRegion;
|
VkStridedDeviceAddressRegionKHR missRegion;
|
||||||
VkStridedDeviceAddressRegionKHR hitRegion;
|
VkStridedDeviceAddressRegionKHR hitRegion;
|
||||||
VkStridedDeviceAddressRegionKHR callableRegion;
|
VkStridedDeviceAddressRegionKHR callableRegion;
|
||||||
};
|
|
||||||
#else
|
|
||||||
class WindowVulkan final : public Window, public WindowKeyboard, public WindowMouse, public WindowTitle {
|
|
||||||
public:
|
|
||||||
WindowVulkan(std::uint32_t width, std::uint32_t height);
|
|
||||||
WindowVulkan(std::uint32_t width, std::uint32_t height, const std::string_view title);
|
|
||||||
~WindowVulkan();
|
|
||||||
Event<void> onRender;
|
|
||||||
std::vector<VkDescriptorSet> descriptorsRt;
|
|
||||||
void Render();
|
|
||||||
void QueueRender();
|
|
||||||
void Resize(std::uint32_t width, std::uint32_t height);
|
|
||||||
void StartSync() override;
|
|
||||||
void StartUpdate() override;
|
|
||||||
void StopUpdate() override;
|
|
||||||
void SetTitle(const std::string_view title) override;
|
|
||||||
VkCommandBuffer StartInit();
|
|
||||||
void FinishInit();
|
|
||||||
|
|
||||||
template <typename Pipeline>
|
|
||||||
void SetPipelineRT() {
|
|
||||||
rtPipeline = Pipeline::pipeline;
|
|
||||||
rtPipelineLayout = Pipeline::pipelineLayout;
|
|
||||||
raygenRegion = Pipeline::raygenRegion;
|
|
||||||
missRegion = Pipeline::missRegion;
|
|
||||||
hitRegion = Pipeline::hitRegion;
|
|
||||||
callableRegion = Pipeline::callableRegion;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetPipelineRT(PipelineRTVulkan& pipeline) {
|
|
||||||
rtPipeline = pipeline.pipeline;
|
|
||||||
rtPipelineLayout = pipeline.pipelineLayout;
|
|
||||||
raygenRegion = pipeline.raygenRegion;
|
|
||||||
missRegion = pipeline.missRegion;
|
|
||||||
hitRegion = pipeline.hitRegion;
|
|
||||||
callableRegion = pipeline.callableRegion;
|
|
||||||
}
|
|
||||||
void CreateSwapchain();
|
|
||||||
static constexpr std::uint32_t numFrames = 3;
|
|
||||||
VkSurfaceKHR vulkanSurface = VK_NULL_HANDLE;
|
|
||||||
VkSwapchainKHR swapChain = VK_NULL_HANDLE;
|
|
||||||
VkFormat colorFormat;
|
|
||||||
VkColorSpaceKHR colorSpace;
|
|
||||||
VkImage images[numFrames];
|
|
||||||
VkImageView imageViews[numFrames];
|
|
||||||
std::thread thread;
|
|
||||||
VkCommandBuffer drawCmdBuffers[numFrames];
|
|
||||||
VkSubmitInfo submitInfo;
|
|
||||||
Semaphores semaphores;
|
|
||||||
uint32_t currentBuffer = 0;
|
|
||||||
VkPipelineStageFlags submitPipelineStages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
||||||
VkPipeline rtPipeline;
|
|
||||||
VkPipelineLayout rtPipelineLayout;
|
|
||||||
VkStridedDeviceAddressRegionKHR raygenRegion;
|
|
||||||
VkStridedDeviceAddressRegionKHR missRegion;
|
|
||||||
VkStridedDeviceAddressRegionKHR hitRegion;
|
|
||||||
VkStridedDeviceAddressRegionKHR callableRegion;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Crafter®.Graphics
|
Crafter®.Graphics
|
||||||
Copyright (C) 2025 Catcrafts®
|
Copyright (C) 2026 Catcrafts®
|
||||||
Catcrafts.net
|
Catcrafts.net
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
|
|
@ -21,26 +21,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
export module Crafter.Graphics;
|
export module Crafter.Graphics;
|
||||||
|
|
||||||
export import :Window;
|
export import :Window;
|
||||||
export import :Transform;
|
export import :Transform2D;
|
||||||
export import :RenderingElement;
|
export import :RenderingElement2D;
|
||||||
export import :MouseElement;
|
export import :MouseElement;
|
||||||
export import :GridElement;
|
export import :GridElement;
|
||||||
export import :Types;
|
export import :Types;
|
||||||
|
export import :Device;
|
||||||
export import :Font;
|
export import :Font;
|
||||||
export import :Image;
|
|
||||||
export import :Shm;
|
|
||||||
export import :Animation;
|
export import :Animation;
|
||||||
export import :Mesh;
|
export import :Mesh;
|
||||||
|
export import :Rendertarget;
|
||||||
|
|
||||||
#ifdef CRAFTER_GRAPHICS_VULKAN
|
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
|
||||||
export import :VulkanDevice;
|
export import :Device;
|
||||||
export import :VulkanTransition;
|
export import :VulkanTransition;
|
||||||
export import :VulkanBuffer;
|
export import :VulkanBuffer;
|
||||||
export import :DescriptorPoolVulkan;
|
export import :DescriptorPoolVulkan;
|
||||||
export import :ShaderVulkan;
|
export import :ShaderVulkan;
|
||||||
export import :ShaderBindingTableVulkan;
|
export import :ShaderBindingTableVulkan;
|
||||||
export import :PipelineRTVulkan;
|
export import :PipelineRTVulkan;
|
||||||
export import :RenderingElement3DVulkan;
|
export import :RenderingElement3D;
|
||||||
export import :ImageVulkan;
|
export import :ImageVulkan;
|
||||||
export import :SamplerVulkan;
|
export import :SamplerVulkan;
|
||||||
export import :DescriptorSetLayoutVulkan;
|
export import :DescriptorSetLayoutVulkan;
|
||||||
|
|
@ -55,6 +55,6 @@ export import :DescriptorSetLayoutVulkan;
|
||||||
// export import :TextureShader;
|
// export import :TextureShader;
|
||||||
// export import :DescriptorSet;
|
// export import :DescriptorSet;
|
||||||
// export import :HeightmapShader;
|
// export import :HeightmapShader;
|
||||||
// export import :VulkanDevice;
|
// export import :Device;
|
||||||
// export import :VulkanPipeline;
|
// export import :VulkanPipeline;
|
||||||
// export import :VulkanShader;
|
// export import :VulkanShader;
|
||||||
142
project.json
142
project.json
|
|
@ -3,33 +3,68 @@
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "base",
|
"name": "base",
|
||||||
"implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Window", "implementations/Crafter.Graphics-MouseElement", "implementations/Crafter.Graphics-Transform", "implementations/Crafter.Graphics-GridElement", "implementations/Crafter.Graphics-Image"],
|
"implementations": [
|
||||||
"interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-Image", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-Animation", "interfaces/Crafter.Graphics-RenderingElement", "interfaces/Crafter.Graphics-MouseElement", "interfaces/Crafter.Graphics-Transform", "interfaces/Crafter.Graphics-GridElement", "interfaces/Crafter.Graphics-VulkanDevice", "interfaces/Crafter.Graphics-VulkanTransition", "interfaces/Crafter.Graphics-Mesh", "interfaces/Crafter.Graphics-VulkanBuffer", "interfaces/Crafter.Graphics-RenderingElement3DVulkan", "interfaces/Crafter.Graphics-DescriptorPoolVulkan", "interfaces/Crafter.Graphics-ShaderVulkan", "interfaces/Crafter.Graphics-PipelineRTVulkan", "interfaces/Crafter.Graphics-ShaderBindingTableVulkan", "interfaces/Crafter.Graphics-ImageVulkan", "interfaces/Crafter.Graphics-SamplerVulkan", "interfaces/Crafter.Graphics-DescriptorSetLayoutVulkan"],
|
"implementations/Crafter.Graphics-Font",
|
||||||
|
"implementations/Crafter.Graphics-Window",
|
||||||
|
"implementations/Crafter.Graphics-MouseElement",
|
||||||
|
"implementations/Crafter.Graphics-Transform2D",
|
||||||
|
"implementations/Crafter.Graphics-GridElement",
|
||||||
|
"implementations/Crafter.Graphics-Rendertarget",
|
||||||
|
"implementations/Crafter.Graphics-Device"
|
||||||
|
],
|
||||||
|
"interfaces": [
|
||||||
|
"interfaces/Crafter.Graphics-Window",
|
||||||
|
"interfaces/Crafter.Graphics",
|
||||||
|
"interfaces/Crafter.Graphics-Types",
|
||||||
|
"interfaces/Crafter.Graphics-Font",
|
||||||
|
"interfaces/Crafter.Graphics-Animation",
|
||||||
|
"interfaces/Crafter.Graphics-RenderingElement2D",
|
||||||
|
"interfaces/Crafter.Graphics-MouseElement",
|
||||||
|
"interfaces/Crafter.Graphics-Transform2D",
|
||||||
|
"interfaces/Crafter.Graphics-GridElement",
|
||||||
|
"interfaces/Crafter.Graphics-Device",
|
||||||
|
"interfaces/Crafter.Graphics-VulkanTransition",
|
||||||
|
"interfaces/Crafter.Graphics-Mesh",
|
||||||
|
"interfaces/Crafter.Graphics-VulkanBuffer",
|
||||||
|
"interfaces/Crafter.Graphics-RenderingElement3D",
|
||||||
|
"interfaces/Crafter.Graphics-DescriptorPoolVulkan",
|
||||||
|
"interfaces/Crafter.Graphics-ShaderVulkan",
|
||||||
|
"interfaces/Crafter.Graphics-PipelineRTVulkan",
|
||||||
|
"interfaces/Crafter.Graphics-ShaderBindingTableVulkan",
|
||||||
|
"interfaces/Crafter.Graphics-ImageVulkan",
|
||||||
|
"interfaces/Crafter.Graphics-SamplerVulkan",
|
||||||
|
"interfaces/Crafter.Graphics-DescriptorSetLayoutVulkan",
|
||||||
|
"interfaces/Crafter.Graphics-Rendertarget"
|
||||||
|
],
|
||||||
"type": "library"
|
"type": "library"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "wayland",
|
"name": "wayland",
|
||||||
"implementations": ["implementations/Crafter.Graphics-Window_wayland", "implementations/Crafter.Graphics-Shm"],
|
|
||||||
"interfaces": [],
|
|
||||||
"libs": ["wayland-client", "xkbcommon"],
|
"libs": ["wayland-client", "xkbcommon"],
|
||||||
"c_files": ["lib/xdg-shell-protocol", "lib/wayland-xdg-decoration-unstable-v1-client-protocol", "lib/fractional-scale-v1", "lib/viewporter"],
|
"c_files": ["lib/xdg-shell-protocol", "lib/wayland-xdg-decoration-unstable-v1-client-protocol", "lib/fractional-scale-v1", "lib/viewporter"],
|
||||||
"extends": ["base"],
|
"extends": ["base"],
|
||||||
"defines": [
|
"defines": [
|
||||||
{
|
{
|
||||||
"name": "CRAFTER_GRAPHICS_WAYLAND"
|
"name": "CRAFTER_GRAPHICS_WINDOW_WAYLAND"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "win32",
|
||||||
|
"libs": ["kernel32", "user32"],
|
||||||
|
"extends": ["base"],
|
||||||
|
"defines": [
|
||||||
|
{
|
||||||
|
"name": "CRAFTER_GRAPHICS_WINDOW_WIN32"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "vulkan",
|
"name": "vulkan",
|
||||||
"implementations": ["implementations/Crafter.Graphics-VulkanDevice", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-Window_vulkan", "implementations/Crafter.Graphics-Mesh_vulkan", "implementations/Crafter.Graphics-RenderingElement3DVulkan"],
|
"implementations": ["implementations/Crafter.Graphics-Mesh", "implementations/Crafter.Graphics-RenderingElement3D"],
|
||||||
"interfaces": [],
|
|
||||||
"libs": ["wayland-client", "xkbcommon", "vulkan"],
|
|
||||||
"c_files": ["lib/xdg-shell-protocol", "lib/wayland-xdg-decoration-unstable-v1-client-protocol", "lib/fractional-scale-v1", "lib/viewporter"],
|
|
||||||
"extends": ["base"],
|
|
||||||
"defines": [
|
"defines": [
|
||||||
{
|
{
|
||||||
"name": "CRAFTER_GRAPHICS_VULKAN"
|
"name": "CRAFTER_GRAPHICS_RENDERER_VULKAN"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
|
|
@ -44,27 +79,10 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "vulkan-windows",
|
"name": "software",
|
||||||
"implementations": ["implementations/Crafter.Graphics-VulkanDevice", "implementations/Crafter.Graphics-Window_vulkan_windows", "implementations/Crafter.Graphics-Mesh_vulkan", "implementations/Crafter.Graphics-RenderingElement3DVulkan"],
|
|
||||||
"interfaces": [],
|
|
||||||
"libs": ["kernel32", "user32"],
|
|
||||||
"extends": ["base"],
|
|
||||||
"defines": [
|
"defines": [
|
||||||
{
|
{
|
||||||
"name": "CRAFTER_GRAPHICS_VULKAN"
|
"name": "CRAFTER_GRAPHICS_RENDERER_SOFTWARE"
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "CRAFTER_GRAPHICS_WINDOWS"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"dependencies": [
|
|
||||||
{
|
|
||||||
"path":"https://github.com/KhronosGroup/Vulkan-Headers.git",
|
|
||||||
"type":"include"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"path":"https://github.com/KhronosGroup/Vulkan-Utility-Libraries.git",
|
|
||||||
"type":"include"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -92,7 +110,8 @@
|
||||||
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Math.git",
|
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Math.git",
|
||||||
"configuration":"lib"
|
"configuration":"lib"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"defines": [{ "name": "CRAFTER_TIMING" }]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "deps-debug",
|
"name": "deps-debug",
|
||||||
|
|
@ -112,52 +131,57 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-wayland",
|
"name": "lib-wayland",
|
||||||
"extends": ["wayland", "deps"],
|
"extends": ["wayland", "software", "deps"]
|
||||||
"type": "library"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-wayland-timing",
|
"name": "lib-wayland-timing",
|
||||||
"extends": ["wayland", "deps-timing"],
|
"extends": ["wayland", "software", "deps-timing"]
|
||||||
"type": "library",
|
|
||||||
"defines": [{ "name": "CRAFTER_TIMING" }]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-wayland-debug",
|
"name": "lib-wayland-debug",
|
||||||
"type": "library",
|
"extends": ["wayland", "software", "deps-debug"]
|
||||||
"extends": ["wayland", "deps-debug"]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-vulkan",
|
"name": "lib-wayland-vulkan",
|
||||||
"extends": ["vulkan", "deps"],
|
"extends": ["wayland", "vulkan", "deps"],
|
||||||
"type": "library"
|
"libs": ["vulkan"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-vulkan-windows",
|
"name": "lib-wayland-vulkan-timing",
|
||||||
"extends": ["vulkan-windows", "deps"],
|
"extends": ["wayland", "vulkan", "deps-timing"],
|
||||||
"type": "library"
|
"libs": ["vulkan"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-vulkan-windows-debug",
|
"name": "lib-wayland-vulkan-debug",
|
||||||
"extends": ["vulkan-windows", "deps-debug"],
|
"extends": ["wayland", "vulkan", "deps-debug"],
|
||||||
"type": "library",
|
"libs": ["vulkan"]
|
||||||
"debug": true
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-vulkan-windows-timing",
|
"name": "lib-win32",
|
||||||
"extends": ["vulkan-windows", "deps-timing"],
|
"extends": ["win32", "software", "deps"]
|
||||||
"type": "library",
|
|
||||||
"defines": [{ "name": "CRAFTER_TIMING" }]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-vulkan-timing",
|
"name": "lib-win32-timing",
|
||||||
"extends": ["vulkan", "deps-timing"],
|
"extends": ["win32", "software", "deps-timing"]
|
||||||
"type": "library",
|
|
||||||
"defines": [{ "name": "CRAFTER_TIMING" }]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "lib-vulkan-debug",
|
"name": "lib-win32-debug",
|
||||||
"type": "library",
|
"extends": ["win32", "software", "deps-debug"]
|
||||||
"extends": ["vulkan", "deps-debug"]
|
},
|
||||||
|
{
|
||||||
|
"name": "lib-win32-vulkan",
|
||||||
|
"extends": ["win32", "vulkan", "deps"],
|
||||||
|
"libs": ["vulkan-1"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "lib-win32-vulkan-timing",
|
||||||
|
"extends": ["win32", "vulkan", "deps-timing"],
|
||||||
|
"libs": ["vulkan-1"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "lib-win32-vulkan-debug",
|
||||||
|
"extends": ["win32", "vulkan", "deps-debug"],
|
||||||
|
"libs": ["vulkan-1"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue