Commit for 2020.08.29.7z
This commit is contained in:
parent
1435868244
commit
1313677a56
@ -3,6 +3,7 @@
|
||||
layout (input_attachment_index = 0, binding = 1) uniform subpassInput samplerAlbedo;
|
||||
layout (input_attachment_index = 0, binding = 2) uniform subpassInput samplerPosition;
|
||||
layout (input_attachment_index = 0, binding = 3) uniform subpassInput samplerNormal;
|
||||
layout (input_attachment_index = 0, binding = 4) uniform subpassInput samplerDepth;
|
||||
|
||||
layout (location = 0) in vec2 inUv;
|
||||
layout (location = 1) in flat uint inPushConstantPass;
|
||||
@ -82,6 +83,16 @@ void main() {
|
||||
position.eye = subpassLoad(samplerPosition).rgb;
|
||||
normal.eye = subpassLoad(samplerNormal).rgb;
|
||||
|
||||
{
|
||||
float depth = subpassLoad(samplerDepth).r;
|
||||
mat4 iProj = inverse( ubo.matrices.projection[inPushConstantPass] );
|
||||
vec4 positionClip = vec4(inUv * 2.0 - 1.0, depth, 1.0);
|
||||
positionClip.y *= -1;
|
||||
vec4 positionEye = iProj * positionClip;
|
||||
positionEye /= positionEye.w;
|
||||
position.eye = positionEye.xyz;
|
||||
}
|
||||
|
||||
vec3 fragColor = albedoSpecular.rgb * ubo.ambient.rgb;
|
||||
bool lit = false;
|
||||
for ( uint i = 0; i < LIGHTS; ++i ) {
|
||||
|
@ -1,86 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/ext/vulkan/device.h>
|
||||
#include <uf/ext/vulkan/swapchain.h>
|
||||
#include <uf/ext/vulkan/initializers.h>
|
||||
#include <uf/ext/vulkan/texture.h>
|
||||
#include <uf/ext/vulkan/rendermodes/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
struct RenderMode;
|
||||
struct UF_API GraphicOld {
|
||||
VkPipeline pipeline;
|
||||
VkPipelineLayout pipelineLayout;
|
||||
VkDescriptorSet descriptorSet;
|
||||
VkDescriptorSetLayout descriptorSetLayout;
|
||||
VkDescriptorPool descriptorPool;
|
||||
|
||||
struct {
|
||||
std::vector<VkShaderModule> modules;
|
||||
std::vector<VkPipelineShaderStageCreateInfo> stages;
|
||||
} shader;
|
||||
std::vector<Buffer> buffers;
|
||||
|
||||
Device* device = VK_NULL_HANDLE;
|
||||
RenderMode* renderMode = VK_NULL_HANDLE;
|
||||
|
||||
bool autoAssigned = false;
|
||||
bool initialized = false;
|
||||
bool process = true;
|
||||
uint32_t subpass = 0;
|
||||
static VkFrontFace DEFAULT_WINDING_ORDER;
|
||||
|
||||
virtual ~GraphicOld();
|
||||
//
|
||||
template<typename T>
|
||||
inline size_t initializeBuffer( T data, VkDeviceSize length, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, bool stage = true ) {
|
||||
return initializeBuffer( (void*) &data, length, usageFlags, memoryPropertyFlags, stage );
|
||||
}
|
||||
template<typename T>
|
||||
inline void updateBuffer( T data, VkDeviceSize length, size_t index = 0, bool stage = true ) {
|
||||
return updateBuffer( (void*) &data, length, index, stage );
|
||||
}
|
||||
template<typename T>
|
||||
inline size_t initializeBuffer( T data, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, bool stage = true ) {
|
||||
return initializeBuffer( (void*) &data, static_cast<VkDeviceSize>(sizeof(T)), usageFlags, memoryPropertyFlags, stage );
|
||||
}
|
||||
template<typename T>
|
||||
inline void updateBuffer( T data, size_t index = 0, bool stage = true ) {
|
||||
return updateBuffer( (void*) &data, static_cast<VkDeviceSize>(sizeof(T)), index, stage );
|
||||
}
|
||||
template<typename T>
|
||||
inline void updateBuffer( T data, VkDeviceSize length, Buffer& buffer, bool stage = true ) {
|
||||
return updateBuffer( (void*) &data, length, buffer, stage );
|
||||
}
|
||||
size_t initializeBuffer( void* data, VkDeviceSize length, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, bool stage = true );
|
||||
void updateBuffer( void* data, VkDeviceSize length, size_t index = 0, bool stage = true );
|
||||
void updateBuffer( void* data, VkDeviceSize length, Buffer& buffer, bool stage = true );
|
||||
|
||||
void initializeShaders( const std::vector<std::pair<std::string, VkShaderStageFlagBits>>& );
|
||||
|
||||
template<typename T>
|
||||
inline void initializeDescriptorLayout( const std::vector<VkDescriptorSetLayoutBinding>& setLayoutBindings, const T& t ) {
|
||||
return initializeDescriptorLayout( setLayoutBindings, sizeof(t) );
|
||||
}
|
||||
void initializeDescriptorLayout( const std::vector<VkDescriptorSetLayoutBinding>& setLayoutBindings, std::size_t = 0 );
|
||||
void initializePipeline( VkGraphicsPipelineCreateInfo pipelineCreateInfo );
|
||||
void initializeDescriptorPool( const std::vector<VkDescriptorPoolSize>& poolSize, size_t length );
|
||||
void initializeDescriptorSet( const std::vector<VkWriteDescriptorSet>& writeDescriptorSets );
|
||||
|
||||
// RAII
|
||||
virtual void initialize( const std::string& = "" );
|
||||
virtual void initialize( Device& device, RenderMode& renderMode );
|
||||
virtual void destroy();
|
||||
virtual void autoAssign();
|
||||
virtual bool autoAssignable() const;
|
||||
virtual std::string name() const;
|
||||
//
|
||||
virtual void render();
|
||||
virtual void createCommandBuffer( VkCommandBuffer );
|
||||
virtual void createImageMemoryBarrier( VkCommandBuffer );
|
||||
virtual void updateDynamicUniformBuffer(bool = false);
|
||||
};
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/ext/vulkan/device.h>
|
||||
#include <uf/ext/vulkan/swapchain.h>
|
||||
#include <uf/ext/vulkan/initializers.h>
|
||||
#include <uf/ext/vulkan/graphic.old.h>
|
||||
#include <uf/ext/vulkan/texture.h>
|
||||
#include <uf/utils/userdata/userdata.h>
|
||||
|
||||
#include <uf/utils/math/matrix.h>
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
struct UF_API GraphicDescriptor {
|
||||
std::size_t size; // sizeof(Vertex)
|
||||
std::vector<VertexDescriptor> attributes;
|
||||
uf::Userdata uniforms;
|
||||
|
||||
bool defaultedPushConstants = false;
|
||||
uf::Userdata pushConstants;
|
||||
struct {
|
||||
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||
VkPolygonMode fill = VK_POLYGON_MODE_FILL;
|
||||
float lineWidth = 1.0f;
|
||||
VkFrontFace frontFace = ext::vulkan::GraphicOld::DEFAULT_WINDING_ORDER;
|
||||
} rasterMode;
|
||||
|
||||
};
|
||||
struct UF_API BaseGraphic : public GraphicOld {
|
||||
uint32_t indices = 0;
|
||||
ext::vulkan::GraphicDescriptor description;
|
||||
ext::vulkan::Texture2D texture;
|
||||
//
|
||||
void describeVertex( const std::vector<VertexDescriptor>& );
|
||||
template<typename U>
|
||||
void bindUniform() {
|
||||
description.uniforms.create<U>();
|
||||
}
|
||||
template<typename U>
|
||||
void bindPushConstants() {
|
||||
description.pushConstants.create<U>();
|
||||
}
|
||||
template<typename U>
|
||||
U& uniforms() {
|
||||
return description.uniforms.get<U>();
|
||||
}
|
||||
template<typename U>
|
||||
U& pushConstants() {
|
||||
return description.pushConstants.get<U>();
|
||||
}
|
||||
|
||||
virtual void createCommandBuffer( VkCommandBuffer );
|
||||
virtual bool autoAssignable() const;
|
||||
virtual std::string name() const;
|
||||
// RAII
|
||||
virtual void initialize( const std::string& = "" );
|
||||
virtual void initialize( Device& device, RenderMode& renderMode );
|
||||
virtual void destroy();
|
||||
};
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/ext/vulkan/buffer.h>
|
||||
#include <uf/ext/vulkan/texture.h>
|
||||
#include <uf/ext/vulkan/device.h>
|
||||
#include <uf/ext/vulkan/swapchain.h>
|
||||
#include <uf/ext/vulkan/graphic.old.h>
|
||||
#include <uf/utils/math/rayt.h>
|
||||
#include <uf/utils/math/vector.h>
|
||||
#include <uf/utils/math/matrix.h>
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
struct UF_API ComputeGraphic : public GraphicOld {
|
||||
// alias typedefs
|
||||
typedef pod::Primitive Cube;
|
||||
typedef pod::Light Light;
|
||||
typedef pod::Tree Tree;
|
||||
|
||||
// Uniform buffer
|
||||
struct {
|
||||
struct {
|
||||
alignas(16) pod::Vector3f position = { 0.0f, 0.0f, 0.0f };
|
||||
alignas(16) pod::Matrix4f view;
|
||||
float aspectRatio; // Aspect ratio of the viewport
|
||||
} camera;
|
||||
struct {
|
||||
struct {
|
||||
uint start = 0;
|
||||
uint end = std::numeric_limits<uint>::max();
|
||||
} cubes;
|
||||
struct {
|
||||
uint start = 0;
|
||||
uint end = std::numeric_limits<uint>::max();
|
||||
} lights;
|
||||
uint root = std::numeric_limits<uint>::max();
|
||||
} ssbo;
|
||||
} uniforms;
|
||||
|
||||
VkQueue queue; // Separate queue for compute commands (queue family may differ from the one used for graphics)
|
||||
VkCommandPool commandPool; // Use a separate command pool (queue family may differ from the one used for graphics)
|
||||
VkCommandBuffer commandBuffer; // Command buffer storing the dispatch commands and barriers
|
||||
VkFence fence; // Synchronization fence to avoid rewriting compute CB if still in use
|
||||
Texture2D renderTarget; //
|
||||
Texture2D diffuseTexture; //
|
||||
|
||||
void setStorageBuffers( Device& device, std::vector<Cube>& cubes, std::vector<Light>& lights, std::vector<Tree>& );
|
||||
void updateStorageBuffers( Device& device, std::vector<Cube>& cubes, std::vector<Light>& lights, std::vector<Tree>& );
|
||||
void updateUniformBuffer();
|
||||
virtual void createCommandBuffer( VkCommandBuffer );
|
||||
virtual std::string name() const;
|
||||
// RAII
|
||||
void initialize( Device& device, RenderMode& renderMode, uint32_t width = 0, uint32_t height = 0 );
|
||||
virtual void destroy();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
struct UF_API RTGraphic : public GraphicOld {
|
||||
ComputeGraphic compute;
|
||||
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
|
||||
virtual void createCommandBuffer( VkCommandBuffer );
|
||||
virtual void createImageMemoryBarrier( VkCommandBuffer );
|
||||
virtual void updateUniformBuffer();
|
||||
virtual bool autoAssignable() const;
|
||||
virtual std::string name() const;
|
||||
// RAII
|
||||
virtual void initialize( const std::string& = "" );
|
||||
virtual void initialize( Device& device, RenderMode& renderMode );
|
||||
virtual void render();
|
||||
virtual void destroy();
|
||||
};
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/ext/vulkan/device.h>
|
||||
#include <uf/ext/vulkan/swapchain.h>
|
||||
#include <uf/ext/vulkan/initializers.h>
|
||||
#include <uf/ext/vulkan/graphic.old.h>
|
||||
#include <uf/ext/vulkan/texture.h>
|
||||
#include <uf/ext/vulkan/rendertarget.h>
|
||||
|
||||
#include <uf/utils/math/matrix.h>
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
struct UF_API DeferredRenderingGraphic : public GraphicOld {
|
||||
struct Vertex {
|
||||
pod::Vector2f position;
|
||||
pod::Vector2f uv;
|
||||
};
|
||||
|
||||
static size_t maxLights;
|
||||
uf::Userdata uniforms;
|
||||
|
||||
struct {
|
||||
uint32_t pass = 0;
|
||||
} pushConstants;
|
||||
|
||||
uint32_t indices = 0;
|
||||
ext::vulkan::RenderTarget* renderTarget;
|
||||
|
||||
virtual void createCommandBuffer( VkCommandBuffer );
|
||||
virtual bool autoAssignable() const;
|
||||
virtual std::string name() const;
|
||||
// RAII
|
||||
virtual void initialize( const std::string& = "" );
|
||||
virtual void initialize( Device& device, RenderMode& renderMode );
|
||||
virtual void destroy();
|
||||
};
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/ext/vulkan/device.h>
|
||||
#include <uf/ext/vulkan/swapchain.h>
|
||||
#include <uf/ext/vulkan/initializers.h>
|
||||
#include <uf/ext/vulkan/graphic.old.h>
|
||||
#include <uf/ext/vulkan/texture.h>
|
||||
#include <uf/ext/vulkan/rendertarget.h>
|
||||
|
||||
#include <uf/utils/math/matrix.h>
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
struct UF_API RenderTargetGraphic : public GraphicOld {
|
||||
struct Vertex {
|
||||
alignas(8) pod::Vector2f position;
|
||||
alignas(8) pod::Vector2f uv;
|
||||
};
|
||||
|
||||
struct {
|
||||
struct {
|
||||
alignas(16) pod::Matrix4f models[2];
|
||||
} matrices;
|
||||
struct {
|
||||
alignas(8) pod::Vector2f position = { 0.5f, 0.5f };
|
||||
alignas(8) pod::Vector2f radius = { 0.1f, 0.1f };
|
||||
alignas(16) pod::Vector4f color = { 1, 1, 1, 1 };
|
||||
} cursor;
|
||||
alignas(4) float alpha;
|
||||
} uniforms;
|
||||
struct {
|
||||
uint32_t pass = 0;
|
||||
} pushConstants;
|
||||
|
||||
uint32_t indices = 0;
|
||||
VkSampler sampler;
|
||||
ext::vulkan::RenderTarget* renderTarget;
|
||||
|
||||
virtual void createCommandBuffer( VkCommandBuffer );
|
||||
virtual bool autoAssignable() const;
|
||||
virtual std::string name() const;
|
||||
// RAII
|
||||
virtual void initialize( const std::string& = "" );
|
||||
virtual void initialize( Device& device, RenderMode& renderMode );
|
||||
virtual void destroy();
|
||||
};
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/ext/vulkan/rendermode.h>
|
||||
#include <uf/ext/vulkan/graphics/deferredrendering.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/ext/vulkan/rendermode.h>
|
||||
#include <uf/ext/vulkan/graphics/rendertarget.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
|
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/ext/vulkan/rendermode.h>
|
||||
#include <uf/ext/vulkan/graphics/deferredrendering.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
|
||||
namespace ext {
|
||||
namespace vulkan {
|
||||
|
@ -1,245 +0,0 @@
|
||||
#include <uf/ext/vulkan/graphic.old.h>
|
||||
#include <uf/ext/vulkan/initializers.h>
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
#include <uf/ext/openvr/openvr.h>
|
||||
|
||||
VkFrontFace ext::vulkan::GraphicOld::DEFAULT_WINDING_ORDER = VK_FRONT_FACE_CLOCKWISE;
|
||||
|
||||
// Buffers
|
||||
size_t ext::vulkan::GraphicOld::initializeBuffer( void* data, VkDeviceSize length, VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, bool stage ) {
|
||||
Buffer buffer;
|
||||
|
||||
if ( autoAssignable() ) autoAssign();
|
||||
|
||||
// Stage
|
||||
if ( !stage ) {
|
||||
VK_CHECK_RESULT(device->createBuffer(
|
||||
usageFlags,
|
||||
memoryPropertyFlags,
|
||||
buffer,
|
||||
length
|
||||
));
|
||||
size_t index = buffers.size();
|
||||
buffers.push_back( std::move(buffer) );
|
||||
this->updateBuffer( data, length, index, stage );
|
||||
return index;
|
||||
}
|
||||
|
||||
VkQueue queue = device->graphicsQueue;
|
||||
Buffer staging;
|
||||
VkDeviceSize storageBufferSize = length;
|
||||
device->createBuffer(
|
||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
staging,
|
||||
storageBufferSize,
|
||||
data
|
||||
);
|
||||
|
||||
device->createBuffer(
|
||||
usageFlags,
|
||||
memoryPropertyFlags,
|
||||
buffer,
|
||||
storageBufferSize
|
||||
);
|
||||
|
||||
// Copy to staging buffer
|
||||
VkCommandBuffer copyCmd = device->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||
VkBufferCopy copyRegion = {};
|
||||
copyRegion.size = storageBufferSize;
|
||||
vkCmdCopyBuffer(copyCmd, staging.buffer, buffer.buffer, 1, ©Region);
|
||||
device->flushCommandBuffer(copyCmd, queue, true);
|
||||
staging.destroy();
|
||||
|
||||
size_t index = buffers.size();
|
||||
buffers.push_back( std::move(buffer) );
|
||||
return index;
|
||||
}
|
||||
void ext::vulkan::GraphicOld::updateBuffer( void* data, VkDeviceSize length, size_t index, bool stage ) {
|
||||
Buffer& buffer = buffers.at(index);
|
||||
return updateBuffer( data, length, buffer, stage );
|
||||
}
|
||||
void ext::vulkan::GraphicOld::updateBuffer( void* data, VkDeviceSize length, Buffer& buffer, bool stage ) {
|
||||
if ( !stage ) {
|
||||
VK_CHECK_RESULT(buffer.map());
|
||||
memcpy(buffer.mapped, data, length);
|
||||
buffer.unmap();
|
||||
return;
|
||||
}
|
||||
VkQueue queue = device->graphicsQueue;
|
||||
Buffer staging;
|
||||
device->createBuffer(
|
||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
staging,
|
||||
length,
|
||||
data
|
||||
);
|
||||
|
||||
// Copy to staging buffer
|
||||
VkCommandBuffer copyCmd = device->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||
VkBufferCopy copyRegion = {};
|
||||
copyRegion.size = length;
|
||||
vkCmdCopyBuffer(copyCmd, staging.buffer, buffer.buffer, 1, ©Region);
|
||||
device->flushCommandBuffer(copyCmd, queue, true);
|
||||
staging.destroy();
|
||||
}
|
||||
// Set shaders
|
||||
void ext::vulkan::GraphicOld::initializeShaders( const std::vector<std::pair<std::string, VkShaderStageFlagBits>>& layout ) {
|
||||
if ( !device ) device = &ext::vulkan::device;
|
||||
shader.stages.clear();
|
||||
shader.modules.clear();
|
||||
shader.stages.reserve( layout.size() );
|
||||
for ( auto& pair : layout ) {
|
||||
shader.stages.push_back(loadShader(pair.first, pair.second, device->logicalDevice, shader.modules));
|
||||
}
|
||||
}
|
||||
// Set Descriptor Layout
|
||||
void ext::vulkan::GraphicOld::initializeDescriptorLayout( const std::vector<VkDescriptorSetLayoutBinding>& setLayoutBindings, std::size_t pushConstantSize ) {
|
||||
VkDescriptorSetLayoutCreateInfo descriptorLayout = ext::vulkan::initializers::descriptorSetLayoutCreateInfo(
|
||||
setLayoutBindings.data(),
|
||||
static_cast<uint32_t>(setLayoutBindings.size())
|
||||
);
|
||||
|
||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(*device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
||||
|
||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = ext::vulkan::initializers::pipelineLayoutCreateInfo(
|
||||
&descriptorSetLayout,
|
||||
1
|
||||
);
|
||||
|
||||
VkPushConstantRange pushConstantRange = {};
|
||||
if ( pushConstantSize > 0 ) {
|
||||
pushConstantRange = ext::vulkan::initializers::pushConstantRange(
|
||||
VK_SHADER_STAGE_VERTEX_BIT,
|
||||
pushConstantSize,
|
||||
0
|
||||
);
|
||||
// Push constant ranges are part of the pipeline layout
|
||||
pPipelineLayoutCreateInfo.pushConstantRangeCount = 1;
|
||||
pPipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
|
||||
}
|
||||
|
||||
VK_CHECK_RESULT(vkCreatePipelineLayout(*device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
||||
}
|
||||
// Create pipeline
|
||||
void ext::vulkan::GraphicOld::initializePipeline( VkGraphicsPipelineCreateInfo pipelineCreateInfo ) {
|
||||
// ext::vulkan::mutex.lock();
|
||||
|
||||
pipelineCreateInfo.subpass = this->subpass;
|
||||
VK_CHECK_RESULT(vkCreateGraphicsPipelines(*device, device->pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipeline));
|
||||
|
||||
// ext::vulkan::mutex.unlock();
|
||||
}
|
||||
// Set descriptor pool
|
||||
void ext::vulkan::GraphicOld::initializeDescriptorPool( const std::vector<VkDescriptorPoolSize>& poolSizes, size_t length ) {
|
||||
VkDescriptorPoolCreateInfo descriptorPoolInfo = ext::vulkan::initializers::descriptorPoolCreateInfo(
|
||||
static_cast<uint32_t>(poolSizes.size()),
|
||||
const_cast<VkDescriptorPoolSize*>(poolSizes.data()),
|
||||
length
|
||||
);
|
||||
|
||||
VK_CHECK_RESULT(vkCreateDescriptorPool(*device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||||
|
||||
VkDescriptorSetAllocateInfo allocInfo = ext::vulkan::initializers::descriptorSetAllocateInfo(
|
||||
descriptorPool,
|
||||
&descriptorSetLayout,
|
||||
1
|
||||
);
|
||||
|
||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(*device, &allocInfo, &descriptorSet));
|
||||
}
|
||||
// Set descriptor set
|
||||
void ext::vulkan::GraphicOld::initializeDescriptorSet( const std::vector<VkWriteDescriptorSet>& writeDescriptorSets ) {
|
||||
vkUpdateDescriptorSets(
|
||||
*device,
|
||||
static_cast<uint32_t>(writeDescriptorSets.size()),
|
||||
writeDescriptorSets.data(),
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
}
|
||||
|
||||
bool ext::vulkan::GraphicOld::autoAssignable() const {
|
||||
return false;
|
||||
}
|
||||
void ext::vulkan::GraphicOld::autoAssign() {
|
||||
if ( !autoAssigned ) {
|
||||
ext::vulkan::rebuild = true;
|
||||
/*
|
||||
ext::vulkan::graphicOlds->push_back(this);
|
||||
*/
|
||||
}
|
||||
autoAssigned = true;
|
||||
}
|
||||
std::string ext::vulkan::GraphicOld::name() const {
|
||||
return "Graphic";
|
||||
}
|
||||
|
||||
void ext::vulkan::GraphicOld::initialize( const std::string& renderMode ) {
|
||||
return initialize(this->device ? *device : ext::vulkan::device, ext::vulkan::getRenderMode(renderMode));
|
||||
}
|
||||
void ext::vulkan::GraphicOld::initialize( Device& device, RenderMode& renderMode ) {
|
||||
this->device = &device;
|
||||
this->renderMode = &renderMode;
|
||||
this->initialized = true;
|
||||
if ( autoAssignable() ) autoAssign();
|
||||
}
|
||||
ext::vulkan::GraphicOld::~GraphicOld() {
|
||||
this->destroy();
|
||||
}
|
||||
void ext::vulkan::GraphicOld::destroy() {
|
||||
for (auto& buffer : buffers) {
|
||||
buffer.destroy();
|
||||
}
|
||||
if ( autoAssigned ) {
|
||||
/*
|
||||
ext::vulkan::graphicOlds->erase(
|
||||
std::remove(ext::vulkan::graphicOlds->begin(), ext::vulkan::graphicOlds->end(), this),
|
||||
ext::vulkan::graphicOlds->end()
|
||||
);
|
||||
*/
|
||||
autoAssigned = false;
|
||||
ext::vulkan::rebuild = true;
|
||||
}
|
||||
initialized = false;
|
||||
if ( !device || device == VK_NULL_HANDLE ) return;
|
||||
if ( descriptorPool != VK_NULL_HANDLE) {
|
||||
vkDestroyDescriptorPool( *device, descriptorPool, nullptr );
|
||||
descriptorPool = VK_NULL_HANDLE;
|
||||
}
|
||||
if ( pipelineLayout != VK_NULL_HANDLE ) {
|
||||
vkDestroyPipelineLayout( *device, pipelineLayout, nullptr );
|
||||
pipelineLayout = VK_NULL_HANDLE;
|
||||
}
|
||||
if ( pipeline != VK_NULL_HANDLE ) {
|
||||
vkDestroyPipeline( *device, pipeline, nullptr );
|
||||
pipeline = VK_NULL_HANDLE;
|
||||
}
|
||||
if ( descriptorSetLayout != VK_NULL_HANDLE ) {
|
||||
vkDestroyDescriptorSetLayout( *device, descriptorSetLayout, nullptr );
|
||||
descriptorSetLayout = VK_NULL_HANDLE;
|
||||
}
|
||||
for ( auto& module : shader.modules ) {
|
||||
if ( module != VK_NULL_HANDLE ) {
|
||||
vkDestroyShaderModule( *device, module, nullptr );
|
||||
module = VK_NULL_HANDLE;
|
||||
}
|
||||
}
|
||||
shader.modules.clear();
|
||||
shader.stages.clear();
|
||||
buffers.clear();
|
||||
}
|
||||
/*
|
||||
bool ext::vulkan::GraphicOld::autoAssignable() {
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
void ext::vulkan::GraphicOld::render() {
|
||||
}
|
||||
void ext::vulkan::GraphicOld::createCommandBuffer( VkCommandBuffer commandBuffer ) {
|
||||
}
|
||||
void ext::vulkan::GraphicOld::createImageMemoryBarrier( VkCommandBuffer commandBuffer ) {
|
||||
}
|
||||
void ext::vulkan::GraphicOld::updateDynamicUniformBuffer( bool force ) {
|
||||
}
|
@ -1,316 +0,0 @@
|
||||
#include <uf/ext/vulkan/initializers.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
#include <uf/ext/openvr/openvr.h>
|
||||
|
||||
namespace {
|
||||
uint32_t VERTEX_BUFFER_BIND_ID = 0;
|
||||
struct PushConstants {
|
||||
uint32_t pass = 0;
|
||||
};
|
||||
}
|
||||
bool ext::vulkan::BaseGraphic::autoAssignable() const {
|
||||
return false;
|
||||
}
|
||||
std::string ext::vulkan::BaseGraphic::name() const {
|
||||
return "BaseGraphic";
|
||||
}
|
||||
void ext::vulkan::BaseGraphic::createCommandBuffer( VkCommandBuffer commandBuffer ) {
|
||||
assert( buffers.size() >= 2 );
|
||||
Buffer& vertexBuffer = buffers.at(1);
|
||||
Buffer& indexBuffer = buffers.at(2);
|
||||
|
||||
if ( description.pushConstants.initialized() ) {
|
||||
// Bind push constants
|
||||
pod::Userdata& userdata = description.pushConstants.data();
|
||||
if ( description.defaultedPushConstants ) pushConstants<PushConstants>() = { ext::openvr::renderPass };
|
||||
vkCmdPushConstants( commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, userdata.len, userdata.data );
|
||||
}
|
||||
|
||||
// Bind descriptor sets describing shader binding points
|
||||
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
|
||||
// Bind the rendering pipeline
|
||||
// The pipeline (state object) contains all states of the rendering pipeline, binding it will set all the states specified at pipeline creation time
|
||||
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||
// Bind triangle vertex buffer (contains position and colors)
|
||||
VkDeviceSize offsets[1] = { 0 };
|
||||
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer.buffer, offsets);
|
||||
// Bind triangle index buffer
|
||||
vkCmdBindIndexBuffer(commandBuffer, indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
|
||||
// Draw indexed triangle
|
||||
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices), 1, 0, 0, 1);
|
||||
|
||||
// std::cout << this << ": " << descriptorSet << std::endl;
|
||||
}
|
||||
void ext::vulkan::BaseGraphic::describeVertex( const std::vector<VertexDescriptor>& descriptor ) {
|
||||
description.attributes = descriptor;
|
||||
}
|
||||
void ext::vulkan::BaseGraphic::initialize( const std::string& renderMode ) {
|
||||
return initialize(this->device ? *device : ext::vulkan::device, ext::vulkan::getRenderMode(renderMode));
|
||||
}
|
||||
void ext::vulkan::BaseGraphic::initialize( Device& device, RenderMode& renderMode ) {
|
||||
// asset correct buffer sizes
|
||||
assert( buffers.size() >= 2 );
|
||||
ext::vulkan::GraphicOld::initialize( device, renderMode );
|
||||
|
||||
// create default push constant
|
||||
if ( !description.pushConstants.initialized() ) {
|
||||
// || description.pushConstants.data().len > device.properties.limits.maxPushConstantsSize ) {
|
||||
description.defaultedPushConstants = true;
|
||||
bindPushConstants<PushConstants>();
|
||||
pushConstants<PushConstants>() = { 0 };
|
||||
|
||||
}
|
||||
if ( description.pushConstants.initialized() ) {
|
||||
// set descriptor layout
|
||||
assert( description.pushConstants.data().len <= device.properties.limits.maxPushConstantsSize );
|
||||
}
|
||||
|
||||
initializeDescriptorLayout({
|
||||
// Vertex shader
|
||||
ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
VK_SHADER_STAGE_VERTEX_BIT,
|
||||
0
|
||||
),
|
||||
// Fragment shader
|
||||
ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
1
|
||||
)
|
||||
}, description.pushConstants.data().len);
|
||||
// Create uniform buffer
|
||||
pod::Userdata& userdata = description.uniforms.data();
|
||||
initializeBuffer(
|
||||
(void*) userdata.data,
|
||||
userdata.len,
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
false
|
||||
);
|
||||
// Move uniform buffer to the front
|
||||
{
|
||||
for ( auto it = buffers.begin(); it != buffers.end(); ++it ) {
|
||||
Buffer& buffer = *it;
|
||||
if ( !(buffer.usageFlags & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) ) continue;
|
||||
Buffer uniformBuffer = std::move(buffer);
|
||||
buffers.erase(it);
|
||||
buffers.insert( buffers.begin(), std::move(uniformBuffer) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*
|
||||
buffers = {
|
||||
std::move(buffers.at(2)),
|
||||
std::move(buffers.at(0)),
|
||||
std::move(buffers.at(1)),
|
||||
};
|
||||
*/
|
||||
// set pipeline
|
||||
{
|
||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = ext::vulkan::initializers::pipelineInputAssemblyStateCreateInfo(
|
||||
this->description.rasterMode.topology,
|
||||
0,
|
||||
VK_FALSE
|
||||
);
|
||||
VkPipelineRasterizationStateCreateInfo rasterizationState = ext::vulkan::initializers::pipelineRasterizationStateCreateInfo(
|
||||
this->description.rasterMode.fill,
|
||||
VK_CULL_MODE_BACK_BIT,
|
||||
// VK_CULL_MODE_NONE,
|
||||
this->description.rasterMode.frontFace,
|
||||
0
|
||||
);
|
||||
rasterizationState.lineWidth = this->description.rasterMode.lineWidth;
|
||||
/*
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
|
||||
VK_FALSE
|
||||
);
|
||||
blendAttachmentState.blendEnable = VK_TRUE;
|
||||
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
|
||||
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
|
||||
1,
|
||||
&blendAttachmentState
|
||||
);
|
||||
*/
|
||||
/*
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
0xf,
|
||||
VK_FALSE
|
||||
);
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
|
||||
1,
|
||||
&blendAttachmentState
|
||||
);
|
||||
*/
|
||||
|
||||
std::vector<VkPipelineColorBlendAttachmentState> blendAttachmentStates;
|
||||
if ( renderMode.getType() == "Swapchain" ) {
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
0xf,
|
||||
VK_TRUE
|
||||
);
|
||||
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
|
||||
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||
blendAttachmentStates.push_back(blendAttachmentState);
|
||||
} else {
|
||||
auto& subpass = renderMode.renderTarget.passes[this->subpass];
|
||||
for ( auto& input : subpass.colors ) {
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
0xf,
|
||||
VK_TRUE
|
||||
);
|
||||
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
|
||||
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||
blendAttachmentStates.push_back(blendAttachmentState);
|
||||
}
|
||||
}
|
||||
// std::cout << blendAttachmentStates.size() << ": " << renderMode.getType() << std::endl;
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
|
||||
blendAttachmentStates.size(),
|
||||
blendAttachmentStates.data()
|
||||
);
|
||||
/*
|
||||
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||||
colorBlendState.logicOpEnable = VK_FALSE;
|
||||
colorBlendState.logicOp = VK_LOGIC_OP_COPY;
|
||||
colorBlendState.blendConstants[0] = 0.0f;
|
||||
colorBlendState.blendConstants[1] = 0.0f;
|
||||
colorBlendState.blendConstants[2] = 0.0f;
|
||||
colorBlendState.blendConstants[3] = 0.0f;
|
||||
*/
|
||||
VkPipelineDepthStencilStateCreateInfo depthStencilState = ext::vulkan::initializers::pipelineDepthStencilStateCreateInfo(
|
||||
VK_TRUE,
|
||||
VK_TRUE,
|
||||
//VK_COMPARE_OP_LESS_OR_EQUAL
|
||||
VK_COMPARE_OP_GREATER_OR_EQUAL
|
||||
);
|
||||
VkPipelineViewportStateCreateInfo viewportState = ext::vulkan::initializers::pipelineViewportStateCreateInfo(
|
||||
1, 1, 0
|
||||
);
|
||||
VkPipelineMultisampleStateCreateInfo multisampleState = ext::vulkan::initializers::pipelineMultisampleStateCreateInfo(
|
||||
VK_SAMPLE_COUNT_1_BIT,
|
||||
0
|
||||
);
|
||||
std::vector<VkDynamicState> dynamicStateEnables = {
|
||||
VK_DYNAMIC_STATE_VIEWPORT,
|
||||
VK_DYNAMIC_STATE_SCISSOR
|
||||
};
|
||||
VkPipelineDynamicStateCreateInfo dynamicState = ext::vulkan::initializers::pipelineDynamicStateCreateInfo(
|
||||
dynamicStateEnables.data(),
|
||||
static_cast<uint32_t>(dynamicStateEnables.size()),
|
||||
0
|
||||
);
|
||||
|
||||
// Binding description
|
||||
std::vector<VkVertexInputBindingDescription> vertexBindingDescriptions = {
|
||||
ext::vulkan::initializers::vertexInputBindingDescription(
|
||||
VERTEX_BUFFER_BIND_ID,
|
||||
description.size,
|
||||
VK_VERTEX_INPUT_RATE_VERTEX
|
||||
)
|
||||
};
|
||||
// Attribute descriptions
|
||||
// Describes memory layout and shader positions
|
||||
std::vector<VkVertexInputAttributeDescription> vertexAttributeDescriptions = {};
|
||||
for ( auto& descriptor : description.attributes ) {
|
||||
auto d = ext::vulkan::initializers::vertexInputAttributeDescription(
|
||||
VERTEX_BUFFER_BIND_ID,
|
||||
vertexAttributeDescriptions.size(),
|
||||
descriptor.format,
|
||||
descriptor.offset
|
||||
);
|
||||
vertexAttributeDescriptions.push_back(d);
|
||||
}
|
||||
|
||||
VkPipelineVertexInputStateCreateInfo vertexInputState = ext::vulkan::initializers::pipelineVertexInputStateCreateInfo();
|
||||
vertexInputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertexBindingDescriptions.size());
|
||||
vertexInputState.pVertexBindingDescriptions = vertexBindingDescriptions.data();
|
||||
vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexAttributeDescriptions.size());
|
||||
vertexInputState.pVertexAttributeDescriptions = vertexAttributeDescriptions.data();
|
||||
|
||||
VkGraphicsPipelineCreateInfo pipelineCreateInfo = ext::vulkan::initializers::pipelineCreateInfo(
|
||||
pipelineLayout,
|
||||
renderMode.renderTarget.renderPass,
|
||||
0
|
||||
);
|
||||
|
||||
pipelineCreateInfo.pVertexInputState = &vertexInputState;
|
||||
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
||||
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
||||
pipelineCreateInfo.pColorBlendState = &colorBlendState;
|
||||
pipelineCreateInfo.pMultisampleState = &multisampleState;
|
||||
pipelineCreateInfo.pViewportState = &viewportState;
|
||||
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
|
||||
pipelineCreateInfo.pDynamicState = &dynamicState;
|
||||
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shader.stages.size());
|
||||
pipelineCreateInfo.pStages = shader.stages.data();
|
||||
pipelineCreateInfo.subpass = this->subpass;
|
||||
|
||||
initializePipeline(pipelineCreateInfo);
|
||||
}
|
||||
// Set descriptor pool
|
||||
initializeDescriptorPool({
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1)
|
||||
}, 1);
|
||||
// Set descriptor set
|
||||
{
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||
// Binding 0 : Projection/View matrix uniform buffer
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&(buffers.at(0).descriptor)
|
||||
)
|
||||
};
|
||||
if ( texture.generated() )
|
||||
writeDescriptorSets.push_back(ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
1,
|
||||
&texture.descriptor
|
||||
));
|
||||
initializeDescriptorSet(writeDescriptorSets);
|
||||
}
|
||||
/*
|
||||
initializeDescriptorSet({
|
||||
// Binding 0 : Projection/View matrix uniform buffer
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&(buffers.at(0).descriptor)
|
||||
),
|
||||
// Binding 1 : Fragment shader texture sampler
|
||||
// Fragment shader: layout (binding = 1) uniform sampler2D samplerColor;
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
1,
|
||||
&texture.descriptor
|
||||
)
|
||||
});
|
||||
*/
|
||||
}
|
||||
void ext::vulkan::BaseGraphic::destroy() {
|
||||
texture.destroy();
|
||||
shader.stages.clear();
|
||||
ext::vulkan::GraphicOld::destroy();
|
||||
}
|
@ -1,333 +0,0 @@
|
||||
#include <uf/ext/vulkan/initializers.h>
|
||||
#include <uf/ext/vulkan/graphics/deferredrendering.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
#include <uf/ext/vulkan/texture.h>
|
||||
#include <uf/ext/openvr/openvr.h>
|
||||
#include <uf/utils/graphic/graphic.h>
|
||||
|
||||
namespace {
|
||||
uint32_t VERTEX_BUFFER_BIND_ID = 0;
|
||||
float r() {
|
||||
return (rand() % 100) / 100.0;
|
||||
}
|
||||
}
|
||||
|
||||
size_t ext::vulkan::DeferredRenderingGraphic::maxLights = 32;
|
||||
|
||||
bool ext::vulkan::DeferredRenderingGraphic::autoAssignable() const {
|
||||
return false;
|
||||
}
|
||||
std::string ext::vulkan::DeferredRenderingGraphic::name() const {
|
||||
return "DeferredRenderingGraphic";
|
||||
}
|
||||
void ext::vulkan::DeferredRenderingGraphic::createCommandBuffer( VkCommandBuffer commandBuffer ) {
|
||||
assert( buffers.size() >= 2 );
|
||||
Buffer& vertexBuffer = buffers.at(1);
|
||||
Buffer& indexBuffer = buffers.at(2);
|
||||
|
||||
pushConstants = { ext::openvr::renderPass };
|
||||
vkCmdPushConstants( commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(pushConstants), &pushConstants );
|
||||
// Bind descriptor sets describing shader binding points
|
||||
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
|
||||
// Bind the rendering pipeline
|
||||
// The pipeline (state object) contains all states of the rendering pipeline, binding it will set all the states specified at pipeline creation time
|
||||
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||
// Bind triangle vertex buffer (contains position and colors)
|
||||
VkDeviceSize offsets[1] = { 0 };
|
||||
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer.buffer, offsets);
|
||||
// Bind triangle index buffer
|
||||
vkCmdBindIndexBuffer(commandBuffer, indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
|
||||
// Draw indexed triangle
|
||||
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices), 1, 0, 0, 1);
|
||||
}
|
||||
void ext::vulkan::DeferredRenderingGraphic::initialize( const std::string& renderMode ) {
|
||||
return initialize(this->device ? *device : ext::vulkan::device, ext::vulkan::getRenderMode(renderMode));
|
||||
}
|
||||
void ext::vulkan::DeferredRenderingGraphic::initialize( Device& device, RenderMode& renderMode ) {
|
||||
this->device = &device;
|
||||
|
||||
std::vector<Vertex> vertices = {
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {-1.0f, -1.0f}, {0.0f, 1.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
{ {1.0f, 1.0f}, {1.0f, 0.0f}, }
|
||||
|
||||
/*
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {-1.0f, -1.0f}, {0.0f, 1.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
{ {1.0f, 1.0f}, {1.0f, 0.0f}, }
|
||||
*/
|
||||
};
|
||||
|
||||
std::vector<uint32_t> indices = {
|
||||
0, 1, 2, 0, 2, 3
|
||||
// 0, 1, 2, 3, 4, 5
|
||||
};
|
||||
|
||||
initializeBuffer(
|
||||
(void*) vertices.data(),
|
||||
vertices.size() * sizeof(Vertex),
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, //VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||
false
|
||||
);
|
||||
initializeBuffer(
|
||||
(void*) indices.data(),
|
||||
indices.size() * sizeof(uint32_t),
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, //VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||
false
|
||||
);
|
||||
this->indices = indices.size();
|
||||
// asset correct buffer sizes
|
||||
assert( buffers.size() >= 2 );
|
||||
ext::vulkan::GraphicOld::initialize( device, renderMode );
|
||||
|
||||
// set descriptor layout
|
||||
{
|
||||
std::vector<VkDescriptorSetLayoutBinding> bindings = {
|
||||
// Uniforms
|
||||
ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
0
|
||||
)
|
||||
};
|
||||
auto& subpass = renderMode.renderTarget.passes[this->subpass];
|
||||
for ( auto& input : subpass.inputs ) {
|
||||
bindings.push_back(ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
bindings.size()
|
||||
));
|
||||
}
|
||||
initializeDescriptorLayout(bindings, sizeof(pushConstants));
|
||||
}
|
||||
|
||||
// Create uniform buffer
|
||||
// max kludge
|
||||
{
|
||||
size_t MAX_LIGHTS = ext::vulkan::DeferredRenderingGraphic::maxLights;
|
||||
struct UniformDescriptor {
|
||||
struct Matrices {
|
||||
alignas(16) pod::Matrix4f view[2];
|
||||
alignas(16) pod::Matrix4f projection[2];
|
||||
} matrices;
|
||||
alignas(16) pod::Vector4f ambient;
|
||||
struct Light {
|
||||
alignas(16) pod::Vector4f position;
|
||||
alignas(16) pod::Vector4f color;
|
||||
} lights;
|
||||
};
|
||||
size_t size = sizeof(UniformDescriptor) + sizeof(UniformDescriptor::Light) * (MAX_LIGHTS - 1);
|
||||
uint8_t buffer[size]; for ( size_t i = 0; i < size; ++i ) buffer[i] = 0;
|
||||
{
|
||||
UniformDescriptor* uniforms = (UniformDescriptor*) &buffer[0];
|
||||
uniforms->ambient = { 1, 1, 1, 1 };
|
||||
UniformDescriptor::Light* lights = (UniformDescriptor::Light*) &buffer[sizeof(UniformDescriptor) - sizeof(UniformDescriptor::Light)];
|
||||
for ( size_t i = 0; i < MAX_LIGHTS; ++i ) {
|
||||
UniformDescriptor::Light& light = lights[i];
|
||||
light.position = { 0, 0, 0, 0 };
|
||||
light.color = { 0, 0, 0, 0 };
|
||||
}
|
||||
}
|
||||
uniforms.create( size, (void*) &buffer );
|
||||
}
|
||||
pod::Userdata& userdata = uniforms.data();
|
||||
initializeBuffer(
|
||||
(void*) userdata.data,
|
||||
userdata.len,
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
false
|
||||
);
|
||||
/*
|
||||
initializeBuffer(
|
||||
(void*) &uniforms,
|
||||
sizeof(uniforms),
|
||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
false
|
||||
);
|
||||
*/
|
||||
// Move uniform buffer to the front
|
||||
{
|
||||
for ( auto it = buffers.begin(); it != buffers.end(); ++it ) {
|
||||
Buffer& buffer = *it;
|
||||
if ( !(buffer.usageFlags & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) ) continue;
|
||||
Buffer uniformBuffer = std::move(buffer);
|
||||
buffers.erase(it);
|
||||
buffers.insert( buffers.begin(), std::move(uniformBuffer) );
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
// set pipeline
|
||||
{
|
||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = ext::vulkan::initializers::pipelineInputAssemblyStateCreateInfo(
|
||||
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
||||
0,
|
||||
VK_FALSE
|
||||
);
|
||||
VkPipelineRasterizationStateCreateInfo rasterizationState = ext::vulkan::initializers::pipelineRasterizationStateCreateInfo(
|
||||
VK_POLYGON_MODE_FILL,
|
||||
VK_CULL_MODE_BACK_BIT,
|
||||
// VK_CULL_MODE_NONE,
|
||||
VK_FRONT_FACE_CLOCKWISE,
|
||||
0
|
||||
);
|
||||
std::vector<VkPipelineColorBlendAttachmentState> blendAttachmentStates;
|
||||
auto& subpass = renderMode.renderTarget.passes[this->subpass];
|
||||
for ( auto& input : subpass.colors ) {
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
0xf,
|
||||
VK_FALSE
|
||||
);
|
||||
blendAttachmentStates.push_back(blendAttachmentState);
|
||||
}
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
|
||||
blendAttachmentStates.size(),
|
||||
blendAttachmentStates.data()
|
||||
);
|
||||
VkPipelineDepthStencilStateCreateInfo depthStencilState = ext::vulkan::initializers::pipelineDepthStencilStateCreateInfo(
|
||||
VK_FALSE,
|
||||
VK_FALSE,
|
||||
//VK_COMPARE_OP_LESS_OR_EQUAL
|
||||
VK_COMPARE_OP_GREATER_OR_EQUAL
|
||||
);
|
||||
VkPipelineViewportStateCreateInfo viewportState = ext::vulkan::initializers::pipelineViewportStateCreateInfo(
|
||||
1, 1, 0
|
||||
);
|
||||
VkPipelineMultisampleStateCreateInfo multisampleState = ext::vulkan::initializers::pipelineMultisampleStateCreateInfo(
|
||||
VK_SAMPLE_COUNT_1_BIT,
|
||||
0
|
||||
);
|
||||
std::vector<VkDynamicState> dynamicStateEnables = {
|
||||
VK_DYNAMIC_STATE_VIEWPORT,
|
||||
VK_DYNAMIC_STATE_SCISSOR
|
||||
};
|
||||
VkPipelineDynamicStateCreateInfo dynamicState = ext::vulkan::initializers::pipelineDynamicStateCreateInfo(
|
||||
dynamicStateEnables.data(),
|
||||
static_cast<uint32_t>(dynamicStateEnables.size()),
|
||||
0
|
||||
);
|
||||
|
||||
// Binding description
|
||||
std::vector<VkVertexInputBindingDescription> vertexBindingDescriptions = {
|
||||
ext::vulkan::initializers::vertexInputBindingDescription(
|
||||
VERTEX_BUFFER_BIND_ID,
|
||||
sizeof(Vertex),
|
||||
VK_VERTEX_INPUT_RATE_VERTEX
|
||||
)
|
||||
};
|
||||
// Attribute descriptions
|
||||
// Describes memory layout and shader positions
|
||||
std::vector<VkVertexInputAttributeDescription> vertexAttributeDescriptions = {
|
||||
// Location 0 : Position
|
||||
ext::vulkan::initializers::vertexInputAttributeDescription(
|
||||
VERTEX_BUFFER_BIND_ID,
|
||||
0,
|
||||
VK_FORMAT_R32G32_SFLOAT,
|
||||
offsetof(Vertex, position)
|
||||
),
|
||||
// Location 1 : Texture coordinates
|
||||
ext::vulkan::initializers::vertexInputAttributeDescription(
|
||||
VERTEX_BUFFER_BIND_ID,
|
||||
1,
|
||||
VK_FORMAT_R32G32_SFLOAT,
|
||||
offsetof(Vertex, uv)
|
||||
)
|
||||
};
|
||||
|
||||
// Specialization Constants
|
||||
|
||||
struct SpecializationConstants {
|
||||
uint32_t lights;
|
||||
} specializationConstants;
|
||||
// grab requested light count
|
||||
specializationConstants.lights = ext::vulkan::DeferredRenderingGraphic::maxLights;
|
||||
|
||||
std::vector<VkSpecializationMapEntry> specializationMapEntries;
|
||||
{
|
||||
VkSpecializationMapEntry specializationMapEntry;
|
||||
specializationMapEntry.constantID = 0;
|
||||
specializationMapEntry.size = sizeof(specializationConstants.lights);
|
||||
specializationMapEntry.offset = offsetof(SpecializationConstants, lights);
|
||||
specializationMapEntries.push_back(specializationMapEntry);
|
||||
}
|
||||
VkSpecializationInfo specializationInfo{};
|
||||
specializationInfo.dataSize = sizeof(specializationConstants);
|
||||
specializationInfo.mapEntryCount = static_cast<uint32_t>(specializationMapEntries.size());
|
||||
specializationInfo.pMapEntries = specializationMapEntries.data();
|
||||
specializationInfo.pData = &specializationConstants;
|
||||
// fragment shader
|
||||
shader.stages[1].pSpecializationInfo = &specializationInfo;
|
||||
|
||||
VkPipelineVertexInputStateCreateInfo vertexInputState = ext::vulkan::initializers::pipelineVertexInputStateCreateInfo();
|
||||
vertexInputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertexBindingDescriptions.size());
|
||||
vertexInputState.pVertexBindingDescriptions = vertexBindingDescriptions.data();
|
||||
vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexAttributeDescriptions.size());
|
||||
vertexInputState.pVertexAttributeDescriptions = vertexAttributeDescriptions.data();
|
||||
|
||||
VkGraphicsPipelineCreateInfo pipelineCreateInfo = ext::vulkan::initializers::pipelineCreateInfo(
|
||||
pipelineLayout,
|
||||
renderMode.renderTarget.renderPass,
|
||||
0
|
||||
);
|
||||
pipelineCreateInfo.pVertexInputState = &vertexInputState;
|
||||
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
||||
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
||||
pipelineCreateInfo.pColorBlendState = &colorBlendState;
|
||||
pipelineCreateInfo.pMultisampleState = &multisampleState;
|
||||
pipelineCreateInfo.pViewportState = &viewportState;
|
||||
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
|
||||
pipelineCreateInfo.pDynamicState = &dynamicState;
|
||||
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shader.stages.size());
|
||||
pipelineCreateInfo.pStages = shader.stages.data();
|
||||
pipelineCreateInfo.subpass = this->subpass;
|
||||
|
||||
initializePipeline(pipelineCreateInfo);
|
||||
}
|
||||
{
|
||||
std::vector<VkDescriptorImageInfo> inputDescriptors;
|
||||
auto& subpass = renderMode.renderTarget.passes[this->subpass];
|
||||
for ( auto& input : subpass.inputs ) {
|
||||
inputDescriptors.push_back(ext::vulkan::initializers::descriptorImageInfo(
|
||||
renderTarget->attachments[input.attachment].view,
|
||||
input.layout
|
||||
));
|
||||
}
|
||||
// Set descriptor pool
|
||||
initializeDescriptorPool({
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, inputDescriptors.size()),
|
||||
}, 1);
|
||||
// Set descriptor set
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||
// Binding 0 : Projection/View matrix uniform buffer
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&(buffers.at(0).descriptor)
|
||||
)
|
||||
};
|
||||
for ( size_t i = 0; i < inputDescriptors.size(); ++i ) {
|
||||
writeDescriptorSets.push_back(ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
|
||||
i + 1,
|
||||
&inputDescriptors[i]
|
||||
));
|
||||
}
|
||||
initializeDescriptorSet(writeDescriptorSets);
|
||||
}
|
||||
}
|
||||
void ext::vulkan::DeferredRenderingGraphic::destroy() {
|
||||
ext::vulkan::GraphicOld::destroy();
|
||||
}
|
@ -1,453 +0,0 @@
|
||||
#include <uf/ext/vulkan/initializers.h>
|
||||
#include <uf/ext/vulkan/graphics/rendertarget.h>
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
#include <uf/ext/vulkan/texture.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
#include <uf/utils/graphic/graphic.h>
|
||||
#include <uf/ext/openvr/openvr.h>
|
||||
|
||||
namespace {
|
||||
uint32_t VERTEX_BUFFER_BIND_ID = 0;
|
||||
}
|
||||
bool ext::vulkan::RenderTargetGraphic::autoAssignable() const {
|
||||
return false;
|
||||
}
|
||||
std::string ext::vulkan::RenderTargetGraphic::name() const {
|
||||
return "RenderTargetGraphic";
|
||||
}
|
||||
void ext::vulkan::RenderTargetGraphic::createCommandBuffer( VkCommandBuffer commandBuffer ) {
|
||||
assert( buffers.size() >= 2 );
|
||||
Buffer& vertexBuffer = buffers.at(1);
|
||||
Buffer& indexBuffer = buffers.at(2);
|
||||
|
||||
pushConstants = { ext::openvr::renderPass };
|
||||
vkCmdPushConstants( commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(pushConstants), &pushConstants );
|
||||
// Bind descriptor sets describing shader binding points
|
||||
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
|
||||
// Bind the rendering pipeline
|
||||
// The pipeline (state object) contains all states of the rendering pipeline, binding it will set all the states specified at pipeline creation time
|
||||
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||
// Bind triangle vertex buffer (contains position and colors)
|
||||
VkDeviceSize offsets[1] = { 0 };
|
||||
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer.buffer, offsets);
|
||||
// Bind triangle index buffer
|
||||
vkCmdBindIndexBuffer(commandBuffer, indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
|
||||
// Draw indexed triangle
|
||||
vkCmdDrawIndexed(commandBuffer, static_cast<uint32_t>(indices), 1, 0, 0, 1);
|
||||
}
|
||||
void ext::vulkan::RenderTargetGraphic::initialize( const std::string& renderMode ) {
|
||||
return initialize(this->device ? *device : ext::vulkan::device, ext::vulkan::getRenderMode(renderMode));
|
||||
}
|
||||
void ext::vulkan::RenderTargetGraphic::initialize( Device& device, RenderMode& renderMode ) {
|
||||
this->device = &device;
|
||||
|
||||
std::vector<Vertex> vertices = {
|
||||
|
||||
{ {-1.0f, 1.0f}, {0.0f, 1.0f}, },
|
||||
{ {-1.0f, -1.0f}, {0.0f, 0.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 0.0f}, },
|
||||
{ {1.0f, 1.0f}, {1.0f, 1.0f}, }
|
||||
|
||||
/*
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {-1.0f, -1.0f}, {0.0f, 1.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
{ {1.0f, 1.0f}, {1.0f, 0.0f}, }
|
||||
*/
|
||||
};
|
||||
|
||||
std::vector<uint32_t> indices = {
|
||||
// 0, 1, 2, 0, 2, 3
|
||||
0, 1, 2, 2, 3, 0
|
||||
// 2, 1, 0, 0, 3, 2
|
||||
// 0, 3, 2, 2, 1, 0
|
||||
// 0, 1, 2, 3, 4, 5
|
||||
};
|
||||
|
||||
initializeBuffer(
|
||||
(void*) vertices.data(),
|
||||
vertices.size() * sizeof(Vertex),
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, //VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||
false
|
||||
);
|
||||
initializeBuffer(
|
||||
(void*) indices.data(),
|
||||
indices.size() * sizeof(uint32_t),
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, //VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||
false
|
||||
);
|
||||
this->indices = indices.size();
|
||||
// asset correct buffer sizes
|
||||
assert( buffers.size() >= 2 );
|
||||
ext::vulkan::GraphicOld::initialize( device, renderMode );
|
||||
// set descriptor layout
|
||||
{
|
||||
std::vector<VkDescriptorSetLayoutBinding> bindings = {
|
||||
// Uniforms
|
||||
ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
VK_SHADER_STAGE_VERTEX_BIT,
|
||||
0
|
||||
),
|
||||
ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_SAMPLER,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
1
|
||||
),
|
||||
};
|
||||
/*
|
||||
bindings.push_back(ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
bindings.size()
|
||||
));
|
||||
*/
|
||||
for ( auto& attachment : renderTarget->attachments ) {
|
||||
if ( !(attachment.usage & VK_IMAGE_USAGE_SAMPLED_BIT) ) continue;
|
||||
bindings.push_back(ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
bindings.size()
|
||||
));
|
||||
//break;
|
||||
}
|
||||
/*
|
||||
auto& subpass = renderMode.renderTarget.passes[this->subpass];
|
||||
for ( auto& color : subpass.colors ) {
|
||||
bindings.push_back(ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
bindings.size()
|
||||
));
|
||||
}
|
||||
*/
|
||||
initializeDescriptorLayout(bindings, sizeof(pushConstants));
|
||||
}
|
||||
/*
|
||||
initializeDescriptorLayout({
|
||||
// Vertex shader
|
||||
ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
VK_SHADER_STAGE_VERTEX_BIT,
|
||||
0
|
||||
),
|
||||
// Fragment shader
|
||||
ext::vulkan::initializers::descriptorSetLayoutBinding(
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
1
|
||||
),
|
||||
}, sizeof(pushConstants));
|
||||
*/
|
||||
// Create sampler
|
||||
{
|
||||
VkSamplerCreateInfo samplerInfo = {};
|
||||
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
||||
samplerInfo.magFilter = VK_FILTER_NEAREST;
|
||||
samplerInfo.minFilter = VK_FILTER_NEAREST;
|
||||
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
||||
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||
samplerInfo.addressModeV = samplerInfo.addressModeU;
|
||||
samplerInfo.addressModeW = samplerInfo.addressModeU;
|
||||
samplerInfo.mipLodBias = 0.0f;
|
||||
samplerInfo.maxAnisotropy = 1.0f;
|
||||
samplerInfo.minLod = 0.0f;
|
||||
samplerInfo.maxLod = 1.0f;
|
||||
samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
||||
VK_CHECK_RESULT(vkCreateSampler(device, &samplerInfo, nullptr, &sampler));
|
||||
}
|
||||
// Create uniform buffer
|
||||
initializeBuffer(
|
||||
(void*) &uniforms,
|
||||
sizeof(uniforms),
|
||||
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
false
|
||||
);
|
||||
// Swap buffers
|
||||
// Move uniform buffer to the front
|
||||
{
|
||||
for ( auto it = buffers.begin(); it != buffers.end(); ++it ) {
|
||||
Buffer& buffer = *it;
|
||||
if ( !(buffer.usageFlags & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) ) continue;
|
||||
Buffer uniformBuffer = std::move(buffer);
|
||||
buffers.erase(it);
|
||||
buffers.insert( buffers.begin(), std::move(uniformBuffer) );
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*
|
||||
buffers = {
|
||||
std::move(buffers.at(2)),
|
||||
std::move(buffers.at(0)),
|
||||
std::move(buffers.at(1)),
|
||||
};
|
||||
*/
|
||||
// set pipeline
|
||||
{
|
||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = ext::vulkan::initializers::pipelineInputAssemblyStateCreateInfo(
|
||||
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
||||
0,
|
||||
VK_FALSE
|
||||
);
|
||||
VkPipelineRasterizationStateCreateInfo rasterizationState = ext::vulkan::initializers::pipelineRasterizationStateCreateInfo(
|
||||
VK_POLYGON_MODE_FILL,
|
||||
VK_CULL_MODE_BACK_BIT,
|
||||
// VK_CULL_MODE_NONE,
|
||||
ext::vulkan::GraphicOld::DEFAULT_WINDING_ORDER,
|
||||
0
|
||||
);
|
||||
|
||||
std::vector<VkPipelineColorBlendAttachmentState> blendAttachmentStates;
|
||||
for ( auto& attachment : renderMode.renderTarget.attachments ) {
|
||||
if ( attachment.usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT ) {
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
0xf,
|
||||
VK_FALSE
|
||||
);
|
||||
blendAttachmentState.blendEnable = VK_TRUE;
|
||||
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
|
||||
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||
blendAttachmentStates.push_back(blendAttachmentState);
|
||||
}
|
||||
}
|
||||
auto& subpass = renderMode.renderTarget.passes[this->subpass];
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
|
||||
// renderMode.getType() == "Swapchain" ? 1 : blendAttachmentStates.size(),
|
||||
subpass.colors.size(),
|
||||
blendAttachmentStates.data()
|
||||
);
|
||||
/*
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
0xf,
|
||||
VK_FALSE
|
||||
);
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
|
||||
1,
|
||||
&blendAttachmentState
|
||||
);
|
||||
*/
|
||||
/*
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
// VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
|
||||
0xf,
|
||||
VK_FALSE
|
||||
);
|
||||
blendAttachmentState.blendEnable = VK_TRUE;
|
||||
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
|
||||
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
|
||||
1,
|
||||
&blendAttachmentState
|
||||
);
|
||||
*/
|
||||
/*
|
||||
std::vector<VkPipelineColorBlendAttachmentState> blendAttachmentStates;
|
||||
for ( auto& attachment : renderMode.renderTarget.attachments ) {
|
||||
if ( attachment.layout == VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT ) {
|
||||
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
|
||||
0xf,
|
||||
VK_FALSE
|
||||
);
|
||||
blendAttachmentStates.push_back(blendAttachmentState);
|
||||
}
|
||||
}
|
||||
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
|
||||
blendAttachmentStates.size(),
|
||||
blendAttachmentStates.data()
|
||||
);
|
||||
colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||||
colorBlendState.logicOpEnable = VK_FALSE;
|
||||
colorBlendState.logicOp = VK_LOGIC_OP_COPY;
|
||||
colorBlendState.blendConstants[0] = 0.0f;
|
||||
colorBlendState.blendConstants[1] = 0.0f;
|
||||
colorBlendState.blendConstants[2] = 0.0f;
|
||||
colorBlendState.blendConstants[3] = 0.0f;
|
||||
*/
|
||||
VkPipelineDepthStencilStateCreateInfo depthStencilState = ext::vulkan::initializers::pipelineDepthStencilStateCreateInfo(
|
||||
VK_TRUE,
|
||||
VK_TRUE,
|
||||
//VK_COMPARE_OP_LESS_OR_EQUAL
|
||||
VK_COMPARE_OP_GREATER_OR_EQUAL
|
||||
);
|
||||
VkPipelineViewportStateCreateInfo viewportState = ext::vulkan::initializers::pipelineViewportStateCreateInfo(
|
||||
1, 1, 0
|
||||
);
|
||||
VkPipelineMultisampleStateCreateInfo multisampleState = ext::vulkan::initializers::pipelineMultisampleStateCreateInfo(
|
||||
VK_SAMPLE_COUNT_1_BIT,
|
||||
0
|
||||
);
|
||||
std::vector<VkDynamicState> dynamicStateEnables = {
|
||||
VK_DYNAMIC_STATE_VIEWPORT,
|
||||
VK_DYNAMIC_STATE_SCISSOR
|
||||
};
|
||||
VkPipelineDynamicStateCreateInfo dynamicState = ext::vulkan::initializers::pipelineDynamicStateCreateInfo(
|
||||
dynamicStateEnables.data(),
|
||||
static_cast<uint32_t>(dynamicStateEnables.size()),
|
||||
0
|
||||
);
|
||||
|
||||
// Binding description
|
||||
std::vector<VkVertexInputBindingDescription> vertexBindingDescriptions = {
|
||||
ext::vulkan::initializers::vertexInputBindingDescription(
|
||||
VERTEX_BUFFER_BIND_ID,
|
||||
sizeof(Vertex),
|
||||
VK_VERTEX_INPUT_RATE_VERTEX
|
||||
)
|
||||
};
|
||||
// Attribute descriptions
|
||||
// Describes memory layout and shader positions
|
||||
std::vector<VkVertexInputAttributeDescription> vertexAttributeDescriptions = {
|
||||
// Location 0 : Position
|
||||
ext::vulkan::initializers::vertexInputAttributeDescription(
|
||||
VERTEX_BUFFER_BIND_ID,
|
||||
0,
|
||||
VK_FORMAT_R32G32_SFLOAT,
|
||||
offsetof(Vertex, position)
|
||||
),
|
||||
// Location 1 : Texture coordinates
|
||||
ext::vulkan::initializers::vertexInputAttributeDescription(
|
||||
VERTEX_BUFFER_BIND_ID,
|
||||
1,
|
||||
VK_FORMAT_R32G32_SFLOAT,
|
||||
offsetof(Vertex, uv)
|
||||
)
|
||||
};
|
||||
|
||||
VkPipelineVertexInputStateCreateInfo vertexInputState = ext::vulkan::initializers::pipelineVertexInputStateCreateInfo();
|
||||
vertexInputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertexBindingDescriptions.size());
|
||||
vertexInputState.pVertexBindingDescriptions = vertexBindingDescriptions.data();
|
||||
vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexAttributeDescriptions.size());
|
||||
vertexInputState.pVertexAttributeDescriptions = vertexAttributeDescriptions.data();
|
||||
|
||||
VkGraphicsPipelineCreateInfo pipelineCreateInfo = ext::vulkan::initializers::pipelineCreateInfo(
|
||||
pipelineLayout,
|
||||
renderMode.renderTarget.renderPass,
|
||||
0
|
||||
);
|
||||
pipelineCreateInfo.pVertexInputState = &vertexInputState;
|
||||
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
||||
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
||||
pipelineCreateInfo.pColorBlendState = &colorBlendState;
|
||||
pipelineCreateInfo.pMultisampleState = &multisampleState;
|
||||
pipelineCreateInfo.pViewportState = &viewportState;
|
||||
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
|
||||
pipelineCreateInfo.pDynamicState = &dynamicState;
|
||||
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shader.stages.size());
|
||||
pipelineCreateInfo.pStages = shader.stages.data();
|
||||
pipelineCreateInfo.subpass = this->subpass;
|
||||
|
||||
initializePipeline(pipelineCreateInfo);
|
||||
}
|
||||
|
||||
{
|
||||
VkDescriptorImageInfo samplerDescriptor; samplerDescriptor.sampler = sampler;
|
||||
std::vector<VkDescriptorImageInfo> colorDescriptors;
|
||||
/*
|
||||
colorDescriptors.push_back(ext::vulkan::initializers::descriptorImageInfo(
|
||||
renderTarget->attachments[0].view,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
|
||||
));
|
||||
*/
|
||||
// auto& subpass = renderMode.renderTarget.passes[this->subpass];
|
||||
for ( auto& attachment : renderTarget->attachments ) {
|
||||
if ( !(attachment.usage & VK_IMAGE_USAGE_SAMPLED_BIT) ) continue;
|
||||
colorDescriptors.push_back(ext::vulkan::initializers::descriptorImageInfo(
|
||||
attachment.view,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
|
||||
));
|
||||
//break;
|
||||
}
|
||||
/*
|
||||
auto& subpass = renderMode.renderTarget.passes[this->subpass];
|
||||
for ( auto& color : subpass.colors ) {
|
||||
colorDescriptors.push_back(ext::vulkan::initializers::descriptorImageInfo(
|
||||
renderTarget->attachments[color].view,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||
));
|
||||
}
|
||||
*/
|
||||
// Set descriptor pool
|
||||
initializeDescriptorPool({
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_SAMPLER, 1),
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, colorDescriptors.size()),
|
||||
}, 1);
|
||||
// Set descriptor set
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||
// Binding 0 : Projection/View matrix uniform buffer
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&(buffers.at(0).descriptor)
|
||||
),
|
||||
// Binding 1 : Sampler
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_SAMPLER,
|
||||
1,
|
||||
&samplerDescriptor
|
||||
)
|
||||
};
|
||||
for ( size_t i = 0; i < colorDescriptors.size(); ++i ) {
|
||||
writeDescriptorSets.push_back(ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
i + 2,
|
||||
&colorDescriptors[i]
|
||||
));
|
||||
}
|
||||
initializeDescriptorSet(writeDescriptorSets);
|
||||
}
|
||||
/*
|
||||
// Set descriptor pool
|
||||
initializeDescriptorPool({
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
|
||||
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1),
|
||||
}, 2);
|
||||
// Set descriptor set
|
||||
{
|
||||
VkDescriptorImageInfo renderTargetDescriptor = ext::vulkan::initializers::descriptorImageInfo(
|
||||
renderTarget->attachments[0].view,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||
sampler
|
||||
);
|
||||
|
||||
initializeDescriptorSet({
|
||||
// Binding 0 : Projection/View matrix uniform buffer
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&(buffers.at(0).descriptor)
|
||||
),
|
||||
// Binding 1 : Fragment shader texture sampler
|
||||
// Fragment shader: layout (binding = 1) uniform sampler2D samplerColor;
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
1,
|
||||
&renderTargetDescriptor
|
||||
)
|
||||
});
|
||||
}
|
||||
*/
|
||||
}
|
||||
void ext::vulkan::RenderTargetGraphic::destroy() {
|
||||
vkDestroySampler( *device, sampler, nullptr );
|
||||
ext::vulkan::GraphicOld::destroy();
|
||||
}
|
@ -53,7 +53,7 @@ void ext::vulkan::DeferredRenderMode::initialize( Device& device ) {
|
||||
renderTarget.addPass(
|
||||
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
|
||||
{ attachments.output },
|
||||
{ attachments.albedo, attachments.position, attachments.normals },
|
||||
{ attachments.albedo, attachments.position, attachments.normals, attachments.depth },
|
||||
attachments.depth
|
||||
);
|
||||
}
|
||||
@ -62,17 +62,6 @@ void ext::vulkan::DeferredRenderMode::initialize( Device& device ) {
|
||||
|
||||
{
|
||||
uf::BaseMesh<pod::Vertex_2F2F, uint16_t> mesh;
|
||||
/*
|
||||
mesh.vertices = {
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {-1.0f, -1.0f}, {0.0f, 1.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
{ {1.0f, 1.0f}, {1.0f, 0.0f}, }
|
||||
};
|
||||
*/
|
||||
mesh.vertices = {
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {-1.0f, -1.0f}, {0.0f, 1.0f}, },
|
||||
@ -94,17 +83,6 @@ void ext::vulkan::DeferredRenderMode::initialize( Device& device ) {
|
||||
});
|
||||
blitter.initializePipeline();
|
||||
}
|
||||
// update layer rendertargets descriptor sets
|
||||
/*
|
||||
{
|
||||
std::vector<RenderMode*> layers = ext::vulkan::getRenderModes("RenderTarget", false); //{ &ext::vulkan::getRenderMode("Gui") };
|
||||
for ( auto _ : layers ) {
|
||||
RenderTargetRenderMode* layer = (RenderTargetRenderMode*) _;
|
||||
auto& blitter = layer->blitter;
|
||||
blitter.initialize( this->getName() );
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
void ext::vulkan::DeferredRenderMode::tick() {
|
||||
ext::vulkan::RenderMode::tick();
|
||||
@ -113,21 +91,10 @@ void ext::vulkan::DeferredRenderMode::tick() {
|
||||
renderTarget.initialize( *renderTarget.device );
|
||||
// update blitter descriptor set
|
||||
if ( blitter.initialized ) {
|
||||
// blitter.material.textures.clear();
|
||||
auto& pipeline = blitter.getPipeline();
|
||||
pipeline.update( blitter );
|
||||
}
|
||||
}
|
||||
/*
|
||||
std::vector<RenderMode*> layers = ext::vulkan::getRenderModes("RenderTarget", false);
|
||||
for ( auto _ : layers ) {
|
||||
RenderTargetRenderMode* layer = (RenderTargetRenderMode*) _;
|
||||
auto& blitter = layer->blitter;
|
||||
if ( blitter.initialized ) continue;
|
||||
blitter.initialize( this->getName() );
|
||||
blitter.initializePipeline();
|
||||
}
|
||||
*/
|
||||
}
|
||||
void ext::vulkan::DeferredRenderMode::destroy() {
|
||||
ext::vulkan::RenderMode::destroy();
|
||||
|
@ -16,15 +16,6 @@ void ext::vulkan::RenderTargetRenderMode::initialize( Device& device ) {
|
||||
|
||||
{
|
||||
renderTarget.device = &device;
|
||||
// attach targets
|
||||
/*
|
||||
struct {
|
||||
size_t color, depth;
|
||||
} attachments;
|
||||
|
||||
attachments.color = renderTarget.attach( VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL ); // albedo
|
||||
attachments.depth = renderTarget.attach( device.formats.depth, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ); // depth
|
||||
*/
|
||||
struct {
|
||||
size_t albedo, position, normals, depth, output;
|
||||
} attachments;
|
||||
@ -58,8 +49,6 @@ void ext::vulkan::RenderTargetRenderMode::initialize( Device& device ) {
|
||||
{
|
||||
renderTarget.addPass(
|
||||
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
|
||||
// { attachments.output },
|
||||
// { attachments.albedo, attachments.position, attachments.normals },
|
||||
{ attachments.output },
|
||||
{ attachments.albedo, attachments.position, attachments.normals },
|
||||
attachments.depth
|
||||
@ -79,22 +68,6 @@ void ext::vulkan::RenderTargetRenderMode::initialize( Device& device ) {
|
||||
mesh.indices = {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
/*
|
||||
mesh.vertices = {
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {-1.0f, -1.0f}, {0.0f, 1.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
|
||||
{ {-1.0f, 1.0f}, {0.0f, 0.0f}, },
|
||||
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
|
||||
{ {1.0f, 1.0f}, {1.0f, 0.0f}, }
|
||||
};
|
||||
*/
|
||||
// blitter.descriptor.subpass = 1;
|
||||
// blitter.descriptor.depthTest.test = false;
|
||||
// blitter.descriptor.depthTest.write = false;
|
||||
// blitter.initialize(this->getName());
|
||||
|
||||
blitter.device = &device;
|
||||
blitter.material.device = &device;
|
||||
blitter.initializeGeometry( mesh );
|
||||
|
@ -121,40 +121,7 @@ void ext::vulkan::StereoscopicDeferredRenderMode::initialize( Device& device ) {
|
||||
});
|
||||
blitter.initializePipeline();
|
||||
}
|
||||
/*
|
||||
blitter.renderTarget = &renderTarget;
|
||||
blitter.initializeShaders({
|
||||
{"./data/shaders/display.subpass.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
|
||||
{"./data/shaders/display.subpass.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT}
|
||||
});
|
||||
|
||||
blitter.subpass = 1;
|
||||
blitter.initialize( device, *this );
|
||||
*/
|
||||
}
|
||||
/*
|
||||
{
|
||||
blitter.renderTarget = &renderTarget;
|
||||
blitter.initializeShaders({
|
||||
{"./data/shaders/display.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
|
||||
{"./data/shaders/display.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT}
|
||||
});
|
||||
blitter.subpass = 2;
|
||||
blitter.initialize( device, *this );
|
||||
}
|
||||
*/
|
||||
// update layer rendertargets descriptor sets
|
||||
/*
|
||||
{
|
||||
std::vector<RenderMode*> layers = ext::vulkan::getRenderModes("RenderTarget", false); //{ &ext::vulkan::getRenderMode("Gui") };
|
||||
for ( auto layer : layers ) {
|
||||
RenderTargetRenderMode* rtLayer = (RenderTargetRenderMode*) layer;
|
||||
auto& blitter = rtLayer->blitter;
|
||||
// blitter.subpass = 1;
|
||||
blitter.initialize( device, *this );
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
void ext::vulkan::StereoscopicDeferredRenderMode::tick() {
|
||||
ext::vulkan::RenderMode::tick();
|
||||
@ -178,144 +145,6 @@ void ext::vulkan::StereoscopicDeferredRenderMode::tick() {
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
std::vector<RenderMode*> layers = ext::vulkan::getRenderModes("RenderTarget", false);
|
||||
for ( auto layer : layers ) {
|
||||
RenderTargetRenderMode* rtLayer = (RenderTargetRenderMode*) layer;
|
||||
auto& blitter = rtLayer->blitter;
|
||||
// update descriptor set
|
||||
if ( !blitter.initialized ) {
|
||||
// blitter.subpass = 1;
|
||||
if ( blitter.renderTarget ) blitter.initialize( *device, *this );
|
||||
}
|
||||
}
|
||||
if ( ext::vulkan::resized ) {
|
||||
struct EYES {
|
||||
RenderTarget* renderTarget;
|
||||
Graphic* blitter;
|
||||
};
|
||||
std::vector<EYES> eyes = {
|
||||
{ &renderTargets.left, &blitters.left },
|
||||
{ &renderTargets.right, &blitters.right },
|
||||
};
|
||||
for ( auto& eye : eyes ) {
|
||||
auto& renderTarget = *eye.renderTarget;
|
||||
auto& blitter = *eye.blitter;
|
||||
// destroy if exist
|
||||
{
|
||||
renderTarget.initialize( *renderTarget.device );
|
||||
}
|
||||
// update blitter descriptor set
|
||||
if ( blitter.initialized ) {
|
||||
std::vector<VkDescriptorImageInfo> inputDescriptors;
|
||||
auto& subpass = renderTarget.passes[blitter.subpass];
|
||||
for ( auto& input : subpass.inputs ) {
|
||||
inputDescriptors.push_back(ext::vulkan::initializers::descriptorImageInfo(
|
||||
renderTarget.attachments[input.attachment].view,
|
||||
input.layout
|
||||
));
|
||||
}
|
||||
// Set descriptor set
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||
// Binding 0 : Projection/View matrix uniform buffer
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
blitter.descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&(blitter.buffers.at(0).descriptor)
|
||||
)
|
||||
};
|
||||
for ( size_t i = 0; i < inputDescriptors.size(); ++i ) {
|
||||
writeDescriptorSets.push_back(ext::vulkan::initializers::writeDescriptorSet(
|
||||
blitter.descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
|
||||
i + 1,
|
||||
&inputDescriptors[i]
|
||||
));
|
||||
}
|
||||
blitter.initializeDescriptorSet( writeDescriptorSets );
|
||||
}
|
||||
}
|
||||
{
|
||||
// update blitter descriptor set
|
||||
if ( blitter.initialized ) {
|
||||
std::vector<VkDescriptorImageInfo> inputDescriptors;
|
||||
auto& subpass = renderTarget.passes[blitter.subpass];
|
||||
for ( auto& input : subpass.inputs ) {
|
||||
inputDescriptors.push_back(ext::vulkan::initializers::descriptorImageInfo(
|
||||
renderTarget.attachments[input.attachment].view,
|
||||
input.layout
|
||||
));
|
||||
}
|
||||
// Set descriptor set
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||
// Binding 0 : Projection/View matrix uniform buffer
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
blitter.descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&(blitter.buffers.at(0).descriptor)
|
||||
)
|
||||
};
|
||||
for ( size_t i = 0; i < inputDescriptors.size(); ++i ) {
|
||||
writeDescriptorSets.push_back(ext::vulkan::initializers::writeDescriptorSet(
|
||||
blitter.descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
|
||||
i + 1,
|
||||
&inputDescriptors[i]
|
||||
));
|
||||
}
|
||||
blitter.initializeDescriptorSet( writeDescriptorSets );
|
||||
}
|
||||
}
|
||||
// update layer rendertargets descriptor sets
|
||||
for ( auto layer : layers ) {
|
||||
RenderTargetRenderMode* rtLayer = (RenderTargetRenderMode*) layer;
|
||||
auto& blitter = rtLayer->blitter;
|
||||
auto& renderTarget = rtLayer->renderTarget;
|
||||
// update descriptor set
|
||||
if ( blitter.initialized ) {
|
||||
VkDescriptorImageInfo samplerDescriptor; samplerDescriptor.sampler = blitter.sampler;
|
||||
std::vector<VkDescriptorImageInfo> colorDescriptors;
|
||||
|
||||
for ( auto& attachment : renderTarget.attachments ) {
|
||||
colorDescriptors.push_back(ext::vulkan::initializers::descriptorImageInfo(
|
||||
attachment.view,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
|
||||
));
|
||||
//break;
|
||||
}
|
||||
|
||||
// Set descriptor set
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||
// Binding 0 : Projection/View matrix uniform buffer
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
blitter.descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
0,
|
||||
&(blitter.buffers.at(0).descriptor)
|
||||
),
|
||||
// Binding 1 : Sampler
|
||||
ext::vulkan::initializers::writeDescriptorSet(
|
||||
blitter.descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_SAMPLER,
|
||||
1,
|
||||
&samplerDescriptor
|
||||
),
|
||||
};
|
||||
for ( size_t i = 0; i < colorDescriptors.size(); ++i ) {
|
||||
writeDescriptorSets.push_back(ext::vulkan::initializers::writeDescriptorSet(
|
||||
blitter.descriptorSet,
|
||||
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
|
||||
i + 2,
|
||||
&colorDescriptors[i]
|
||||
));
|
||||
}
|
||||
vkUpdateDescriptorSets( *device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr );
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
void ext::vulkan::StereoscopicDeferredRenderMode::destroy() {
|
||||
ext::vulkan::RenderMode::destroy();
|
||||
|
@ -211,7 +211,7 @@ void ext::vulkan::RenderTarget::initialize( Device& device ) {
|
||||
|
||||
VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass));
|
||||
|
||||
std::cout << renderPass << ": " << attachments.size() << std::endl;
|
||||
// std::cout << renderPass << ": " << attachments.size() << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -46,14 +46,32 @@ std::string UF_API uf::string::uppercase( const std::string& str ) {
|
||||
}
|
||||
std::vector<std::string> UF_API uf::string::split( const std::string& haystack, const std::string& needle ) {
|
||||
std::vector<std::string> cont;
|
||||
size_t last = 0, next = 0;
|
||||
while ((next = haystack.find(needle, last)) != std::string::npos) {
|
||||
cont.push_back(haystack.substr(last, next-last));
|
||||
last = next + 1;
|
||||
}
|
||||
cont.push_back( haystack.substr(last) );
|
||||
/*
|
||||
std::string token;
|
||||
size_t pos = 0;
|
||||
while ((pos = haystack.find(needle)) != std::string::npos) {
|
||||
cont.push_back( haystack.substr(0, pos) );
|
||||
haystack.erase(0, pos + needle.length());
|
||||
}
|
||||
cont.push_back(haystack);
|
||||
*/
|
||||
|
||||
/*
|
||||
std::size_t current, previous = 0;
|
||||
current = haystack.find_first_of(needle);
|
||||
while (current != std::string::npos) {
|
||||
cont.push_back(haystack.substr(previous, current - previous));
|
||||
previous = current + 1;
|
||||
previous = current + needle.size();
|
||||
current = haystack.find_first_of(needle, previous);
|
||||
}
|
||||
cont.push_back(haystack.substr(previous, current - previous));
|
||||
*/
|
||||
return cont;
|
||||
}
|
||||
std::string UF_API uf::string::replace( const std::string& string, const std::string& search, const std::string& replace ) {
|
||||
|
@ -19,7 +19,7 @@
|
||||
#include <codecvt>
|
||||
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
#include <uf/ext/vulkan/rendermodes/rendertarget.h>
|
||||
#include <uf/ext/openvr/openvr.h>
|
||||
|
||||
|
@ -104,7 +104,7 @@ void EXT_API ext::initialize() {
|
||||
// Enable valiation layer
|
||||
ext::vulkan::validation = ::config["engine"]["ext"]["vulkan"]["validation"].asBool();
|
||||
//
|
||||
ext::vulkan::DeferredRenderingGraphic::maxLights = ::config["engine"]["scenes"]["max lights"].asUInt();
|
||||
// ext::vulkan::DeferredRenderingGraphic::maxLights = ::config["engine"]["scenes"]["max lights"].asUInt();
|
||||
//
|
||||
ext::openvr::enabled = ::config["engine"]["ext"]["vr"]["enable"].asBool();
|
||||
ext::openvr::swapEyes = ::config["engine"]["ext"]["vr"]["swap eyes"].asBool();
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
|
||||
#include <uf/utils/http/http.h>
|
||||
#include <uf/utils/audio/audio.h>
|
||||
|
@ -13,7 +13,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
|
||||
#include <uf/utils/http/http.h>
|
||||
#include <uf/utils/audio/audio.h>
|
||||
|
@ -13,7 +13,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
|
||||
#include <uf/utils/http/http.h>
|
||||
#include <uf/utils/audio/audio.h>
|
||||
|
@ -9,7 +9,7 @@
|
||||
#include <uf/utils/graphic/mesh.h>
|
||||
#include <uf/utils/window/window.h>
|
||||
#include <uf/utils/camera/camera.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
#include <uf/ext/vulkan/device.h>
|
||||
#include <uf/ext/vulkan/swapchain.h>
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
|
@ -9,7 +9,7 @@
|
||||
#include <uf/utils/graphic/mesh.h>
|
||||
#include <uf/utils/window/window.h>
|
||||
#include <uf/utils/camera/camera.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
#include <uf/ext/vulkan/device.h>
|
||||
#include <uf/ext/vulkan/swapchain.h>
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include <uf/utils/graphic/graphic.h>
|
||||
#include <uf/utils/window/window.h>
|
||||
#include <uf/utils/camera/camera.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
#include <uf/ext/vulkan/device.h>
|
||||
#include <uf/ext/vulkan/swapchain.h>
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
|
96
ext/scenes/world/light/light.cpp
Normal file
96
ext/scenes/world/light/light.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
#include "light.h"
|
||||
|
||||
#include <uf/ext/vulkan/rendertarget.h>
|
||||
#include <uf/ext/vulkan/rendermodes/rendertarget.h>
|
||||
#include <uf/utils/math/transform.h>
|
||||
#include <uf/utils/camera/camera.h>
|
||||
|
||||
EXT_OBJECT_REGISTER_CPP(Light)
|
||||
void ext::Light::initialize() {
|
||||
uf::Object::initialize();
|
||||
auto& metadata = this->getComponent<uf::Serializer>();
|
||||
auto& transform = this->getComponent<pod::Transform<>>();
|
||||
auto& camera = this->getComponent<uf::Camera>();
|
||||
{
|
||||
auto& scene = uf::scene::getCurrentScene();
|
||||
auto& controller = *scene.getController();
|
||||
camera = controller.getComponent<uf::Camera>();
|
||||
|
||||
camera.setFov( metadata["light"]["fov"].asFloat() );
|
||||
}
|
||||
{
|
||||
auto& renderMode = this->getComponent<ext::vulkan::RenderTargetRenderMode>();
|
||||
renderMode.width = 512;
|
||||
renderMode.height = 512;
|
||||
std::string name = "Render Target: " + std::to_string((int) this->getUid());
|
||||
ext::vulkan::addRenderMode( &renderMode, name );
|
||||
}
|
||||
}
|
||||
void ext::Light::tick() {
|
||||
uf::Object::tick();
|
||||
|
||||
auto& renderMode = this->getComponent<ext::vulkan::RenderTargetRenderMode>();
|
||||
renderMode.target = "";
|
||||
|
||||
auto& camera = this->getComponent<uf::Camera>();
|
||||
auto& transform = this->getComponent<pod::Transform<>>();
|
||||
for ( std::size_t i = 0; i < 2; ++i ) {
|
||||
camera.setView( uf::matrix::inverse( uf::transform::model( transform ) ), i );
|
||||
}
|
||||
}
|
||||
void ext::Light::render() {
|
||||
uf::Object::render();
|
||||
/*
|
||||
{
|
||||
auto& renderMode = this->getComponent<ext::vulkan::RenderTargetRenderMode>();
|
||||
auto& blitter = renderMode.blitter;
|
||||
|
||||
auto& transform = this->getComponent<pod::Transform<>>();
|
||||
auto& scene = uf::scene::getCurrentScene();
|
||||
auto& controller = *scene.getController();
|
||||
auto& camera = this->getComponent<uf::Camera>();
|
||||
auto& controllerCamera = controller.getComponent<uf::Camera>();
|
||||
if ( !blitter.initialized ) return;
|
||||
|
||||
struct UniformDescriptor {
|
||||
struct {
|
||||
alignas(16) pod::Matrix4f models[2];
|
||||
} matrices;
|
||||
struct {
|
||||
alignas(8) pod::Vector2f position = { 0.5f, 0.5f };
|
||||
alignas(8) pod::Vector2f radius = { 0.1f, 0.1f };
|
||||
alignas(16) pod::Vector4f color = { 1, 1, 1, 1 };
|
||||
} cursor;
|
||||
alignas(4) float alpha;
|
||||
};
|
||||
|
||||
auto& shader = blitter.material.shaders.front();
|
||||
auto& uniforms = shader.uniforms.front().get<UniformDescriptor>();
|
||||
|
||||
for ( std::size_t i = 0; i < 2; ++i ) {
|
||||
pod::Matrix4f model = uf::transform::model( transform );
|
||||
|
||||
uniforms.matrices.models[i] = controllerCamera.getProjection(i) * controllerCamera.getView(i) * model;
|
||||
|
||||
uniforms.alpha = 1.0f;
|
||||
|
||||
uniforms.cursor.position.x = -1.0f;
|
||||
uniforms.cursor.position.y = -1.0f;
|
||||
|
||||
uniforms.cursor.radius.x = 0.0f;
|
||||
uniforms.cursor.radius.y = 0.0f;
|
||||
|
||||
uniforms.cursor.color.x = 0.0f;
|
||||
uniforms.cursor.color.y = 0.0f;
|
||||
uniforms.cursor.color.z = 0.0f;
|
||||
uniforms.cursor.color.w = 0.0f;
|
||||
}
|
||||
shader.updateBuffer( (void*) &uniforms, sizeof(uniforms), 0 );
|
||||
}
|
||||
*/
|
||||
}
|
||||
void ext::Light::destroy() {
|
||||
auto& renderMode = this->getComponent<ext::vulkan::RenderTargetRenderMode>();
|
||||
ext::vulkan::removeRenderMode( &renderMode, false );
|
||||
uf::Object::destroy();
|
||||
}
|
16
ext/scenes/world/light/light.h
Normal file
16
ext/scenes/world/light/light.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <uf/config.h>
|
||||
#include <uf/ext/ext.h>
|
||||
#include <uf/engine/object/object.h>
|
||||
#include <uf/engine/scene/scene.h>
|
||||
|
||||
namespace ext {
|
||||
class EXT_API Light : public uf::Object {
|
||||
public:
|
||||
virtual void initialize();
|
||||
virtual void tick();
|
||||
virtual void render();
|
||||
virtual void destroy();
|
||||
};
|
||||
}
|
@ -69,7 +69,7 @@ void ext::Portal::initialize() {
|
||||
}
|
||||
{
|
||||
auto& renderMode = this->getComponent<ext::vulkan::RenderTargetRenderMode>();
|
||||
std::string name = this->getName() + ": " + std::to_string((int) this->getUid());
|
||||
std::string name = "Render Target: " + std::to_string((int) this->getUid());
|
||||
ext::vulkan::addRenderMode( &renderMode, name );
|
||||
if ( ext::openvr::enabled ) {
|
||||
ext::openvr::initialize();
|
||||
|
@ -1430,116 +1430,4 @@ void ext::TerrainGenerator::rasterize( std::vector<ext::TerrainGenerator::mesh_t
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ext::TerrainGenerator::vectorize( std::vector<ext::vulkan::ComputeGraphic::Cube>& buffer, const ext::Region& region ) {
|
||||
struct {
|
||||
pod::Vector3f position;
|
||||
struct { float u, v, x, y; } uv;
|
||||
} offset;
|
||||
|
||||
const ext::Terrain& terrain = region.getParent<ext::Terrain>();
|
||||
const pod::Transform<>& transform = region.getComponent<pod::Transform<>>();
|
||||
const pod::Vector3i location = {
|
||||
transform.position.x / this->m_size.x,
|
||||
transform.position.y / this->m_size.y,
|
||||
transform.position.z / this->m_size.z,
|
||||
};
|
||||
|
||||
struct { ext::Region* right = NULL; ext::Region* left = NULL; ext::Region* top = NULL; ext::Region* bottom = NULL; ext::Region* front = NULL; ext::Region* back = NULL; } regions;
|
||||
|
||||
regions.right = terrain.at( { location.x + 1, location.y, location.z } );
|
||||
regions.left = terrain.at( { location.x - 1, location.y, location.z } );
|
||||
regions.top = terrain.at( { location.x, location.y + 1, location.z } );
|
||||
regions.bottom = terrain.at( { location.x, location.y - 1, location.z } );
|
||||
regions.front = terrain.at( { location.x, location.y, location.z + 1 } );
|
||||
regions.back = terrain.at( { location.x, location.y, location.z - 1 } );
|
||||
/*
|
||||
for ( uint x = 0; x < this->m_size.x; ++x ) {
|
||||
for ( uint y = 0; y < this->m_size.y; ++y ) {
|
||||
for ( uint z = 0; z < this->m_size.z; ++z ) {
|
||||
ext::TerrainVoxel voxel = ext::TerrainVoxel::atlas(this->m_voxels.id.raw[x][y][z]);
|
||||
*/
|
||||
std::size_t i = 0;
|
||||
for ( auto& _ : this->m_voxels.id.rle ) {
|
||||
for ( std::size_t __ = 0; __ < _.length; ++__ ) {
|
||||
{
|
||||
std::size_t x = 0, y = 0, z = 0;
|
||||
{
|
||||
auto v = this->unwrapIndex( i++, this->m_voxels.id.swizzle );
|
||||
x = v.x;
|
||||
y = v.y;
|
||||
z = v.z;
|
||||
}
|
||||
|
||||
ext::TerrainVoxel voxel = ext::TerrainVoxel::atlas( _.value );
|
||||
offset.position.x = x - (this->m_size.x / 2.0f);
|
||||
offset.position.y = y - (this->m_size.y / 2.0f);
|
||||
offset.position.z = z - (this->m_size.z / 2.0f);
|
||||
|
||||
const ext::TerrainVoxel::Model& model = voxel.model();
|
||||
if ( !voxel.opaque() ) continue;
|
||||
|
||||
struct {
|
||||
bool top = true, bottom = true, left = true, right = true, front = true, back = true;
|
||||
} should;
|
||||
struct {
|
||||
ext::TerrainVoxel top = ext::TerrainVoxel::atlas(0), bottom = ext::TerrainVoxel::atlas(0), left = ext::TerrainVoxel::atlas(0), right = ext::TerrainVoxel::atlas(0), front = ext::TerrainVoxel::atlas(0), back = ext::TerrainVoxel::atlas(0);
|
||||
} neighbor;
|
||||
|
||||
if ( x > 0 ) neighbor.left = ext::TerrainVoxel::atlas(this->getVoxel(x-1,y,z)); else if ( regions.left != NULL ) {
|
||||
const ext::Region& left = *regions.left;
|
||||
const ext::TerrainGenerator& t = left.getComponent<ext::TerrainGenerator>();
|
||||
neighbor.left = ext::TerrainVoxel::atlas(t.getVoxel(t.m_size.x-1,y,z));
|
||||
}
|
||||
if ( y > 0 ) neighbor.bottom = ext::TerrainVoxel::atlas(this->getVoxel(x,y-1,z)); else if ( regions.bottom != NULL ) {
|
||||
const ext::Region& bottom = *regions.bottom;
|
||||
const ext::TerrainGenerator& t = bottom.getComponent<ext::TerrainGenerator>();
|
||||
neighbor.bottom =ext::TerrainVoxel::atlas( t.getVoxel(x,t.m_size.y-1,z));
|
||||
}
|
||||
if ( z > 0 ) neighbor.back = ext::TerrainVoxel::atlas(this->getVoxel(x,y,z-1)); else if ( regions.back != NULL ) {
|
||||
const ext::Region& back = *regions.back;
|
||||
const ext::TerrainGenerator& t = back.getComponent<ext::TerrainGenerator>();
|
||||
neighbor.back = ext::TerrainVoxel::atlas(t.getVoxel(x,y,t.m_size.z-1));
|
||||
}
|
||||
|
||||
if ( x + 1 < this->m_size.x ) neighbor.right = ext::TerrainVoxel::atlas(this->getVoxel(x+1,y,z)); else if ( regions.right != NULL ) {
|
||||
const ext::Region& right = *regions.right;
|
||||
const ext::TerrainGenerator& t = right.getComponent<ext::TerrainGenerator>();
|
||||
neighbor.right = ext::TerrainVoxel::atlas(t.getVoxel(0,y,z));
|
||||
}
|
||||
if ( y + 1 < this->m_size.y ) neighbor.top = ext::TerrainVoxel::atlas(this->getVoxel(x,y+1,z)); else if ( regions.top != NULL ) {
|
||||
const ext::Region& top = *regions.top;
|
||||
const ext::TerrainGenerator& t = top.getComponent<ext::TerrainGenerator>();
|
||||
neighbor.top = ext::TerrainVoxel::atlas(t.getVoxel(x,0,z));
|
||||
}
|
||||
if ( z + 1 < this->m_size.z ) neighbor.front = ext::TerrainVoxel::atlas(this->getVoxel(x,y,z+1)); else if ( regions.front != NULL ) {
|
||||
const ext::Region& front = *regions.front;
|
||||
const ext::TerrainGenerator& t = front.getComponent<ext::TerrainGenerator>();
|
||||
neighbor.front = ext::TerrainVoxel::atlas(t.getVoxel(x,y,0));
|
||||
}
|
||||
|
||||
should.right = !neighbor.right.opaque();
|
||||
should.left = !neighbor.left.opaque();
|
||||
should.top = !neighbor.top.opaque();
|
||||
should.bottom = !neighbor.bottom.opaque();
|
||||
should.front = !neighbor.front.opaque();
|
||||
should.back = !neighbor.back.opaque();
|
||||
|
||||
// occluded
|
||||
if ( !should.right && !should.left && !should.top && !should.bottom && !should.front && !should.back ) continue;
|
||||
if ( !voxel.opaque() ) continue;
|
||||
|
||||
ext::vulkan::ComputeGraphic::Cube cube;
|
||||
pod::Vector3f location = offset.position + transform.position;
|
||||
cube.position.x = location.x;
|
||||
cube.position.y = location.y;
|
||||
cube.position.z = location.z;
|
||||
cube.position.z *= -1;
|
||||
cube.position.w = 0.5f;
|
||||
cube.type = voxel.opaque() ? pod::Primitive::CUBE : pod::Primitive::EMPTY;
|
||||
buffer.push_back(cube);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -12,7 +12,6 @@
|
||||
#include <uf/utils/noise/noise.h>
|
||||
|
||||
#include <uf/utils/graphic/mesh.h>
|
||||
#include <uf/ext/vulkan/graphics/compute.h>
|
||||
|
||||
#include <uf/utils/string/rle.h>
|
||||
|
||||
@ -56,7 +55,8 @@ namespace ext {
|
||||
|
||||
void generate( ext::Region& );
|
||||
void rasterize( std::vector<TerrainGenerator::mesh_t::vertex_t>& vertices, const ext::Region& );
|
||||
void vectorize( std::vector<ext::vulkan::ComputeGraphic::Cube>&, const ext::Region& );
|
||||
// void vectorize( std::vector<ext::vulkan::ComputeGraphic::Cube>&, const ext::Region& );
|
||||
|
||||
std::vector<ext::TerrainVoxel::uid_t> getRawVoxels();
|
||||
const pod::RLE<ext::TerrainVoxel::uid_t>::string_t& getVoxels() const;
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
#include <uf/utils/graphic/graphic.h>
|
||||
#include <uf/utils/camera/camera.h>
|
||||
#include <uf/utils/thread/thread.h>
|
||||
#include <uf/ext/vulkan/graphics/base.h>
|
||||
#include <uf/ext/vulkan/graphic.h>
|
||||
#include <uf/ext/vulkan/vulkan.h>
|
||||
|
||||
#include <uf/utils/string/hash.h>
|
||||
|
@ -424,10 +424,10 @@ uf::Entity* ext::World::getController() {
|
||||
auto& renderMode = *ext::vulkan::currentRenderMode;
|
||||
std::string name = renderMode.name;
|
||||
auto split = uf::string::split( name, ": " );
|
||||
if ( split.front() == "Portal" ) {
|
||||
if ( split.front() == "Render Target" ) {
|
||||
uint64_t uid = std::stoi( split.back() );
|
||||
uf::Entity* portal = this->findByUid( uid );
|
||||
if ( portal ) return portal;
|
||||
uf::Entity* ent = this->findByUid( uid );
|
||||
if ( ent ) return ent;
|
||||
}
|
||||
}
|
||||
return uf::Scene::getController();
|
||||
|
Loading…
Reference in New Issue
Block a user