vulkan UI
This commit is contained in:
parent
f8e142fb06
commit
177f873639
13 changed files with 455 additions and 37 deletions
Binary file not shown.
Binary file not shown.
|
|
@ -7,13 +7,9 @@
|
|||
"dependencies": [
|
||||
{
|
||||
"path":"../../project.json",
|
||||
"configuration":"lib-win32-vulkan-debug"
|
||||
"configuration":"lib-wayland-vulkan-debug"
|
||||
}
|
||||
],
|
||||
"march": "x86-64-v3",
|
||||
"mtune": "generic",
|
||||
"target": "x86_64-w64-mingw32",
|
||||
"debug": false,
|
||||
"shaders": [
|
||||
{
|
||||
"path":"raygen.glsl",
|
||||
|
|
|
|||
230
examples/VulkanUI/main.cpp
Normal file
230
examples/VulkanUI/main.cpp
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
#include "vulkan/vulkan.h"
|
||||
#include <cassert>
|
||||
|
||||
import Crafter.Graphics;
|
||||
using namespace Crafter;
|
||||
import std;
|
||||
import Crafter.Event;
|
||||
import Crafter.Math;
|
||||
|
||||
|
||||
int main() {
|
||||
Device::Initialize();
|
||||
Window window(1280, 720, "HelloVulkan");
|
||||
VkCommandBuffer cmd = window.StartInit();
|
||||
DescriptorHeapVulkan descriptorHeap;
|
||||
descriptorHeap.Initialize(1,1,0);
|
||||
|
||||
VkSpecializationMapEntry entry = {
|
||||
.constantID = 0,
|
||||
.offset = 0,
|
||||
.size = sizeof(uint16_t)
|
||||
};
|
||||
|
||||
VkSpecializationInfo specilizationInfo = {
|
||||
.mapEntryCount = 1,
|
||||
.pMapEntries = &entry,
|
||||
.dataSize = sizeof(uint16_t),
|
||||
.pData = &descriptorHeap.bufferStartElement
|
||||
};
|
||||
|
||||
std::array<VulkanShader, 1> shaders{{
|
||||
{"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR, &specilizationInfo}
|
||||
}};
|
||||
|
||||
ShaderBindingTableVulkan shaderTable;
|
||||
shaderTable.Init(shaders);
|
||||
|
||||
std::array<VkRayTracingShaderGroupCreateInfoKHR, 1> raygenGroups {{
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
|
||||
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
|
||||
.generalShader = 0,
|
||||
.closestHitShader = VK_SHADER_UNUSED_KHR,
|
||||
.anyHitShader = VK_SHADER_UNUSED_KHR,
|
||||
.intersectionShader = VK_SHADER_UNUSED_KHR,
|
||||
},
|
||||
}};
|
||||
std::array<VkRayTracingShaderGroupCreateInfoKHR, 0> missGroups;
|
||||
std::array<VkRayTracingShaderGroupCreateInfoKHR, 0> hitGroups;
|
||||
|
||||
|
||||
PipelineRTVulkan pipeline;
|
||||
pipeline.Init(cmd, raygenGroups, missGroups, hitGroups, shaderTable);
|
||||
|
||||
window.FinishInit();
|
||||
|
||||
RendertargetBase<3> rendertargetBase(1280, 720);
|
||||
RenderingElement2DVulkan<true, true> element(
|
||||
{
|
||||
0.5, //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor
|
||||
0.5, //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor
|
||||
0.5, //relativeSizeX: the relative x 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
|
||||
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, //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
|
||||
},
|
||||
2,
|
||||
1
|
||||
);
|
||||
for(std::uint8_t i = 0; i < Window::numFrames; i++) {
|
||||
element.buffers[i]->value[0] = {1, 0, 0, 1};
|
||||
element.buffers[i]->value[1] = {0, 1, 0, 1};
|
||||
element.buffers[i]->FlushDevice();
|
||||
}
|
||||
|
||||
InitializeRenderingElement2DVulkanBuffer();
|
||||
element.UpdatePosition(rendertargetBase, rendertargetBase.transform);
|
||||
|
||||
renderingElement2DVulkanTransformBuffer[0].FlushDevice();
|
||||
renderingElement2DVulkanTransformBuffer[1].FlushDevice();
|
||||
renderingElement2DVulkanTransformBuffer[2].FlushDevice();
|
||||
|
||||
VkImageDescriptorInfoEXT imageInfo0 = {
|
||||
.sType = VK_STRUCTURE_TYPE_IMAGE_DESCRIPTOR_INFO_EXT,
|
||||
.pView = &window.imageViews[0],
|
||||
.layout = VK_IMAGE_LAYOUT_GENERAL
|
||||
};
|
||||
|
||||
VkImageDescriptorInfoEXT imageInfo1 = {
|
||||
.sType = VK_STRUCTURE_TYPE_IMAGE_DESCRIPTOR_INFO_EXT,
|
||||
.pView = &window.imageViews[1],
|
||||
.layout = VK_IMAGE_LAYOUT_GENERAL
|
||||
};
|
||||
|
||||
VkImageDescriptorInfoEXT imageInfo2 = {
|
||||
.sType = VK_STRUCTURE_TYPE_IMAGE_DESCRIPTOR_INFO_EXT,
|
||||
.pView = &window.imageViews[2],
|
||||
.layout = VK_IMAGE_LAYOUT_GENERAL
|
||||
};
|
||||
|
||||
VkDeviceAddressRangeKHR transformRange0 = {
|
||||
.address = renderingElement2DVulkanTransformBuffer[0].address,
|
||||
.size = renderingElement2DVulkanTransformBuffer[0].descriptor.range
|
||||
};
|
||||
|
||||
VkDeviceAddressRangeKHR transformRange1 = {
|
||||
.address = renderingElement2DVulkanTransformBuffer[1].address,
|
||||
.size = renderingElement2DVulkanTransformBuffer[1].descriptor.range
|
||||
};
|
||||
|
||||
VkDeviceAddressRangeKHR transformRange2 = {
|
||||
.address = renderingElement2DVulkanTransformBuffer[2].address,
|
||||
.size = renderingElement2DVulkanTransformBuffer[2].descriptor.range
|
||||
};
|
||||
|
||||
VkDeviceAddressRangeKHR elRange0 = {
|
||||
.address = element.buffers[0]->address,
|
||||
.size = element.buffers[0]->descriptor.range
|
||||
};
|
||||
|
||||
VkDeviceAddressRangeKHR elRange1 = {
|
||||
.address = element.buffers[1]->address,
|
||||
.size = element.buffers[1]->descriptor.range
|
||||
};
|
||||
|
||||
VkDeviceAddressRangeKHR elRange2 = {
|
||||
.address = element.buffers[2]->address,
|
||||
.size = element.buffers[2]->descriptor.range
|
||||
};
|
||||
|
||||
VkResourceDescriptorInfoEXT resources[9] = {
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
||||
.data = { .pImage = &imageInfo0 }
|
||||
},
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
||||
.data = { .pImage = &imageInfo1 }
|
||||
},
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
|
||||
.data = { .pImage = &imageInfo2 }
|
||||
},
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.data = { .pAddressRange = &transformRange0}
|
||||
},
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.data = { .pAddressRange = &transformRange1}
|
||||
},
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.data = { .pAddressRange = &transformRange2}
|
||||
},
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.data = { .pAddressRange = &elRange0}
|
||||
},
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.data = { .pAddressRange = &elRange1}
|
||||
},
|
||||
{
|
||||
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
|
||||
.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
|
||||
.data = { .pAddressRange = &elRange2}
|
||||
},
|
||||
};
|
||||
|
||||
VkHostAddressRangeEXT destinations[9] = {
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[0].value,
|
||||
.size = Device::descriptorHeapProperties.imageDescriptorSize
|
||||
},
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[1].value,
|
||||
.size = Device::descriptorHeapProperties.imageDescriptorSize
|
||||
},
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[2].value,
|
||||
.size = Device::descriptorHeapProperties.imageDescriptorSize
|
||||
},
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[0].value + descriptorHeap.bufferStartOffset,
|
||||
.size = Device::descriptorHeapProperties.bufferDescriptorSize
|
||||
},
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[1].value + descriptorHeap.bufferStartOffset,
|
||||
.size = Device::descriptorHeapProperties.bufferDescriptorSize
|
||||
},
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[2].value + descriptorHeap.bufferStartOffset,
|
||||
.size = Device::descriptorHeapProperties.bufferDescriptorSize
|
||||
},
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[0].value + descriptorHeap.bufferStartOffset + Device::descriptorHeapProperties.bufferDescriptorSize * (renderingElement2DVulkans.size()),
|
||||
.size = Device::descriptorHeapProperties.bufferDescriptorSize
|
||||
},
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[1].value + descriptorHeap.bufferStartOffset + Device::descriptorHeapProperties.bufferDescriptorSize * (renderingElement2DVulkans.size()),
|
||||
.size = Device::descriptorHeapProperties.bufferDescriptorSize
|
||||
},
|
||||
{
|
||||
.address = descriptorHeap.resourceHeap[2].value + descriptorHeap.bufferStartOffset + Device::descriptorHeapProperties.bufferDescriptorSize * (renderingElement2DVulkans.size()),
|
||||
.size = Device::descriptorHeapProperties.bufferDescriptorSize
|
||||
},
|
||||
};
|
||||
|
||||
Device::vkWriteResourceDescriptorsEXT(Device::device, 9, resources, destinations);
|
||||
|
||||
descriptorHeap.resourceHeap[0].FlushDevice();
|
||||
descriptorHeap.resourceHeap[1].FlushDevice();
|
||||
descriptorHeap.resourceHeap[2].FlushDevice();
|
||||
|
||||
window.pipeline = &pipeline;
|
||||
window.descriptorHeap = &descriptorHeap;
|
||||
|
||||
window.Render();
|
||||
window.StartSync();
|
||||
}
|
||||
23
examples/VulkanUI/project.json
Normal file
23
examples/VulkanUI/project.json
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "crafter-graphics",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "executable",
|
||||
"implementations": ["main"],
|
||||
"dependencies": [
|
||||
{
|
||||
"path":"../../project.json",
|
||||
"configuration":"lib-wayland-vulkan-debug"
|
||||
}
|
||||
],
|
||||
"debug": true,
|
||||
"shaders": [
|
||||
{
|
||||
"path":"raygen.glsl",
|
||||
"type": 6,
|
||||
"entrypoint":"main"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
48
examples/VulkanUI/raygen.glsl
Normal file
48
examples/VulkanUI/raygen.glsl
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#version 460
|
||||
#extension GL_EXT_ray_tracing : enable
|
||||
#extension GL_EXT_shader_image_load_formatted : enable
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable
|
||||
#extension GL_EXT_descriptor_heap : enable
|
||||
#extension GL_EXT_nonuniform_qualifier : enable
|
||||
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
|
||||
|
||||
|
||||
struct UIScaledData{
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
int16_t sizeX;
|
||||
int16_t sizeY;
|
||||
uint16_t bufferX;
|
||||
uint16_t bufferY;
|
||||
};
|
||||
|
||||
layout(std430, descriptor_heap) buffer UIScaledDataBuffer {
|
||||
uint16_t count;
|
||||
uint16_t pad[5];
|
||||
UIScaledData data[];
|
||||
} UITransformBuffer[];
|
||||
|
||||
layout(std430, descriptor_heap) buffer UIPixelBufferr {
|
||||
f16vec4 pixels[];
|
||||
} UIPixelBuffer[];
|
||||
|
||||
layout(constant_id = 0) const uint16_t bufferStart = 0us;
|
||||
layout(descriptor_heap) uniform writeonly image2D image[];
|
||||
|
||||
void main()
|
||||
{
|
||||
uvec2 pixel = gl_LaunchIDEXT.xy;
|
||||
uvec2 resolution = gl_LaunchSizeEXT.xy;
|
||||
|
||||
vec4 hitValue = vec4(0);
|
||||
|
||||
for (uint16_t i = 1us; i < UITransformBuffer[bufferStart].count+1; i++) {
|
||||
if(pixel.x > UITransformBuffer[bufferStart].data[i].x && pixel.x < UITransformBuffer[bufferStart].data[i].x + UITransformBuffer[bufferStart].data[i].sizeX && pixel.y > UITransformBuffer[bufferStart].data[i].y && pixel.y < UITransformBuffer[bufferStart].data[i].y + UITransformBuffer[bufferStart].data[i].sizeY) {
|
||||
int16_t srcX = int16_t((pixel.x - UITransformBuffer[bufferStart].data[i].x) * UITransformBuffer[bufferStart].data[i].bufferX / UITransformBuffer[bufferStart].data[i].sizeX);
|
||||
int16_t srcY = int16_t((pixel.y - UITransformBuffer[bufferStart].data[i].y) * UITransformBuffer[bufferStart].data[i].bufferY / UITransformBuffer[bufferStart].data[i].sizeY);
|
||||
hitValue = vec4(UIPixelBuffer[bufferStart + UITransformBuffer[bufferStart].count].pixels[srcY * UITransformBuffer[bufferStart].data[i].bufferX + srcX]);
|
||||
}
|
||||
}
|
||||
|
||||
imageStore(image[0], ivec2(pixel), hitValue);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue