Commit for 2020.08.15.7z

This commit is contained in:
mrq 2020-08-15 00:00:00 -05:00
parent ffc432b137
commit d3d5465ba7
37 changed files with 372 additions and 835 deletions

View File

@ -3,7 +3,7 @@
#include <uf/config.h>
#include <uf/utils/math/matrix.h>
#include <uf/utils/math/quaternion.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/serialize/serializer.h>
#ifdef USE_OPENVR_MINGW
#include <openvr/openvr_mingw.h>
@ -63,8 +63,8 @@ namespace ext {
bool UF_API controllerActive( vr::Controller_Hand );
bool UF_API requestRenderModel( const std::string& );
uf::Mesh& UF_API getRenderModel( const std::string& );
uf::Mesh& UF_API controllerRenderModel( vr::Controller_Hand );
uf::Graphic& UF_API getRenderModel( const std::string& );
uf::Graphic& UF_API controllerRenderModel( vr::Controller_Hand );
void UF_API resetPosition();
/*

View File

@ -4,11 +4,10 @@
#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/utils/graphic/mesh.h>
namespace ext {
namespace vulkan {
struct RenderMode;
struct Graphic;
struct UF_API Shader : public Buffers {
@ -63,16 +62,11 @@ namespace ext {
void initializeShaders( const std::vector<std::pair<std::string, VkShaderStageFlagBits>>& );
};
struct UF_API Graphic : public Buffers {
struct UF_API VertexDescriptor {
VkFormat format; // VK_FORMAT_R32G32B32_SFLOAT
std::size_t offset; // offsetof(Vertex, position)
};
struct Descriptor {
std::string renderMode = "";
uint32_t subpass = 0;
std::size_t size; // sizeof(Vertex)
std::vector<VertexDescriptor> attributes;
uf::BaseGeometry geometry;
size_t indices = 0;
VkPrimitiveTopology topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
@ -95,14 +89,20 @@ namespace ext {
std::unordered_map<std::string, Pipeline> pipelines;
void initialize( const std::string& = "" );
void initializePipeline();
void destroy();
Pipeline& initializePipeline( Descriptor& descriptor, bool update = true );
template<typename T, typename U>
void initializeGeometry( uf::BaseMesh<T, U>& mesh );
bool hasPipeline( Descriptor& descriptor );
void initializePipeline();
Pipeline& initializePipeline( Descriptor& descriptor, bool update = true );
Pipeline& getPipeline( Descriptor& descriptor );
void record( VkCommandBuffer commandBuffer );
};
}
}
#include "graphic.inl"

View File

@ -0,0 +1,49 @@
template<typename T, typename U>
void ext::vulkan::Graphic::initializeGeometry( uf::BaseMesh<T, U>& mesh ) {
if ( mesh.indices.empty() ) mesh.initialize();
// already generated, check if we can just update
if ( descriptor.indices > 0 ) {
if ( descriptor.geometry.sizes.vertex == mesh.sizes.vertex && descriptor.geometry.sizes.indices == mesh.sizes.indices && descriptor.indices == mesh.indices.size() ) {
// too lazy to check if this equals, only matters in pipeline creation anyways
descriptor.geometry = mesh;
updateBuffer(
(void*) mesh.vertices.data(),
mesh.vertices.size() * mesh.sizes.vertex,
0,
false
);
updateBuffer(
(void*) mesh.indices.data(),
mesh.indices.size() * mesh.sizes.indices,
0,
false
);
return;
}
// can't reuse buffers, re-create buffers
{
for ( auto& buffer : buffers ) buffer.destroy();
buffers.clear();
}
}
descriptor.geometry = mesh;
descriptor.indices = mesh.indices.size();
initializeBuffer(
(void*) mesh.vertices.data(),
mesh.vertices.size() * mesh.sizes.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*) mesh.indices.data(),
mesh.indices.size() * mesh.sizes.indices,
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
);
}

View File

@ -11,10 +11,6 @@
namespace ext {
namespace vulkan {
struct UF_API VertexDescriptor {
VkFormat format; // VK_FORMAT_R32G32B32_SFLOAT
std::size_t offset; // offsetof(Vertex, position)
};
struct UF_API GraphicDescriptor {
std::size_t size; // sizeof(Vertex)
std::vector<VertexDescriptor> attributes;

View File

@ -3,7 +3,6 @@
#include <uf/ext/vulkan.h>
#include <uf/ext/vulkan/device.h>
#include <uf/ext/vulkan/swapchain.h>
#include <uf/ext/vulkan/graphic.h>
#include <uf/ext/vulkan/rendermodes/base.h>
#include <uf/engine/scene/scene.h>
@ -35,6 +34,10 @@ namespace ext {
uint32_t getMemoryTypeIndex(uint32_t typeBits, VkMemoryPropertyFlags properties);
struct VertexDescriptor {
VkFormat format; // VK_FORMAT_R32G32B32_SFLOAT
std::size_t offset; // offsetof(Vertex, position)
};
extern UF_API uint32_t width;
extern UF_API uint32_t height;
@ -52,7 +55,6 @@ namespace ext {
extern UF_API RenderMode* currentRenderMode;
extern UF_API std::vector<std::string> passes;
// extern UF_API std::vector<Graphic*>* graphics;
extern UF_API std::vector<RenderMode*> renderModes;
extern UF_API std::vector<uf::Scene*> scenes;

View File

@ -0,0 +1,7 @@
#pragma once
#include <uf/ext/vulkan/graphic.h>
namespace uf {
typedef ext::vulkan::Graphic Graphic;
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <uf/utils/math/vector.h>
#include <uf/ext/vulkan/graphics/base.h>
#include <uf/utils/math/matrix.h>
#include <uf/ext/vulkan/vulkan.h>
#include <functional>
@ -14,7 +14,7 @@ namespace pod {
alignas(16) pod::Vector3f normal;
alignas(16) pod::Vector4t<uint8_t> color;
static UF_API std::vector<ext::vulkan::Graphic::VertexDescriptor> descriptor;
static UF_API std::vector<ext::vulkan::VertexDescriptor> descriptor;
bool operator==( const Vertex_3F2F3F32B& that ) const {
return this->position == that.position &&
@ -30,7 +30,7 @@ namespace pod {
alignas(8) pod::Vector2f uv;
alignas(16) pod::Vector3f normal;
static UF_API std::vector<ext::vulkan::Graphic::VertexDescriptor> descriptor;
static UF_API std::vector<ext::vulkan::VertexDescriptor> descriptor;
bool operator==( const Vertex_3F2F3F& that ) const {
return this->position == that.position &&
@ -44,7 +44,7 @@ namespace pod {
alignas(16) pod::Vector3f position;
alignas(8) pod::Vector2f uv;
static UF_API std::vector<ext::vulkan::Graphic::VertexDescriptor> descriptor;
static UF_API std::vector<ext::vulkan::VertexDescriptor> descriptor;
bool operator==( const Vertex_3F2F& that ) const {
return this->position == that.position &&
@ -55,7 +55,7 @@ namespace pod {
struct /*UF_API*/ Vertex_3F {
alignas(16) pod::Vector3f position;
static UF_API std::vector<ext::vulkan::Graphic::VertexDescriptor> descriptor;
static UF_API std::vector<ext::vulkan::VertexDescriptor> descriptor;
bool operator==( const Vertex_3F& that ) const {
return this->position == that.position;
@ -104,23 +104,25 @@ namespace std {
};
}
namespace uf {
struct /*UF_API*/ MeshBase {
struct /*UF_API*/ BaseGeometry {
public:
// ext::vulkan::BaseGraphic graphic;
ext::vulkan::Graphic graphic;
bool generated = false;
struct {
size_t vertex;
size_t indices;
} sizes;
std::vector<ext::vulkan::VertexDescriptor> attributes;
};
template<typename T>
class /*UF_API*/ BaseMesh : public MeshBase {
template<typename T, typename U = uint32_t>
class /*UF_API*/ BaseMesh : public BaseGeometry {
public:
typedef T vertex_t;
typedef U indices_t;
std::vector<vertex_t> vertices;
std::vector<uint32_t> indices;
std::vector<indices_t> indices;
~BaseMesh();
void initialize( bool compress = true );
void generate();
void destroy( bool clear = true );
void destroy();
};
}
/*

View File

@ -0,0 +1,41 @@
template<typename T, typename U>
void uf::BaseMesh<T, U>::initialize( bool compress ) {
// this->destroy(false);
sizes.vertex = sizeof(vertex_t);
sizes.indices = sizeof(indices_t);
attributes = vertex_t::descriptor;
if ( compress ) {
std::unordered_map<vertex_t, indices_t> unique;
std::vector<vertex_t> _vertices = std::move( this->vertices );
this->indices.clear();
this->vertices.clear();
this->indices.reserve(_vertices.size());
this->vertices.reserve(_vertices.size());
for ( vertex_t& vertex : _vertices ) {
if ( unique.count(vertex) == 0 ) {
unique[vertex] = static_cast<indices_t>(this->vertices.size());
this->vertices.push_back( vertex );
}
this->indices.push_back( unique[vertex] );
}
} else {
this->indices.clear();
this->indices.reserve(vertices.size());
for ( size_t i = 0; i < vertices.size(); ++i ) {
this->indices.push_back(i);
}
}
}
template<typename T, typename U>
void uf::BaseMesh<T, U>::destroy() {
this->indices.clear();
this->vertices.clear();
}
template<typename T, typename U>
uf::BaseMesh<T, U>::~BaseMesh() {
this->destroy();
}

View File

@ -1,94 +0,0 @@
template<typename T>
void uf::BaseMesh<T>::initialize( bool compress ) {
// this->destroy(false);
if ( compress ) {
std::unordered_map<vertex_t, uint32_t> unique;
std::vector<vertex_t> _vertices = std::move( this->vertices );
this->indices.clear();
this->vertices.clear();
this->indices.reserve(_vertices.size());
this->vertices.reserve(_vertices.size());
for ( vertex_t& vertex : _vertices ) {
if ( unique.count(vertex) == 0 ) {
unique[vertex] = static_cast<uint32_t>(this->vertices.size());
this->vertices.push_back( vertex );
}
this->indices.push_back( unique[vertex] );
}
} else {
this->indices.clear();
this->indices.reserve(vertices.size());
for ( size_t i = 0; i < vertices.size(); ++i ) {
this->indices.push_back(i);
}
}
this->generate();
}
template<typename T>
void uf::BaseMesh<T>::generate() {
// already generated, check if we can just update
if ( graphic.descriptor.indices > 0 ) {
if ( graphic.descriptor.size == sizeof(vertex_t) && graphic.descriptor.indices == indices.size() ) {
// too lazy to check if this equals, only matters in pipeline creation anyways
graphic.descriptor.attributes = vertex_t::descriptor;
graphic.updateBuffer(
(void*) vertices.data(),
vertices.size() * sizeof(vertex_t),
0,
false
);
graphic.updateBuffer(
(void*) indices.data(),
indices.size() * sizeof(uint32_t),
0,
false
);
return;
}
// can't reuse buffers, re-create buffers
this->destroy(false);
}
graphic.device = &ext::vulkan::device;
graphic.descriptor.size = sizeof(vertex_t);
graphic.descriptor.attributes = vertex_t::descriptor;
graphic.descriptor.indices = indices.size();
graphic.initializeBuffer(
(void*) vertices.data(),
vertices.size() * sizeof(vertex_t),
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
);
graphic.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->generated = true;
// wait for shaders and uniforms
}
template<typename T>
void uf::BaseMesh<T>::destroy( bool clear ) {
// if ( this->generated ) this->graphic.destroy();
if ( this->generated ) {
for ( auto& buffer : graphic.buffers ) buffer.destroy();
graphic.buffers.clear();
}
this->generated = false;
if ( clear ) {
this->indices.clear();
this->vertices.clear();
}
}
template<typename T>
uf::BaseMesh<T>::~BaseMesh() {
this->destroy();
}

View File

@ -71,7 +71,7 @@ namespace {
uf::Serializer state;
pod::Matrix4t<> matrix;
pod::Matrix4t<> tip;
uf::Mesh mesh;
uf::Graphic mesh;
} left, right;
} controllers;
} devices;
@ -88,7 +88,7 @@ namespace {
vr::RenderModel_TextureMap_t* texture;
};
std::unordered_map<std::string, QueuedRenderModel> queuedRenderModels;
std::unordered_map<std::string, uf::Mesh> renderModels;
std::unordered_map<std::string, uf::Graphic> renderModels;
std::vector<std::string> renderModelNames;
}
@ -240,10 +240,12 @@ bool ext::openvr::initialize( int stage ) {
return true;
}
void ext::openvr::terminate() {
/*
::devices.controllers.left.mesh.graphic.destroy();
::devices.controllers.right.mesh.graphic.destroy();
::devices.controllers.left.mesh.destroy();
::devices.controllers.right.mesh.destroy();
*/
vr::VR_Shutdown();
ext::openvr::context = NULL;
}
@ -283,7 +285,10 @@ void ext::openvr::tick() {
}
// loaded texture, process
{
uf::Mesh& mesh = renderModels[name];
// uf::Mesh& mesh = renderModels[name];
uf::Graphic& graphic = renderModels[name];
uf::Mesh mesh;
mesh.vertices.reserve(queued.model->unVertexCount);
for ( size_t i = 0; i < queued.model->unVertexCount; ++i ) {
auto& v = queued.model->rVertexData[i];
@ -301,12 +306,10 @@ void ext::openvr::tick() {
}
// grab texture
size_t len = queued.texture->unWidth * queued.texture->unHeight * 4;
// mesh.graphic.texture.fromBuffers( (void*) queued.texture->rubTextureMapData, len, VK_FORMAT_R8G8B8A8_UNORM, queued.texture->unWidth, queued.texture->unHeight, ext::vulkan::device, ext::vulkan::device.graphicsQueue );
mesh.initialize(true);
mesh.graphic.process = false;
mesh.graphic.initialize();
auto& texture = mesh.graphic.material.textures.emplace_back();
mesh.initialize();
graphic.initializeGeometry(mesh);
auto& texture = graphic.material.textures.emplace_back();
texture.fromBuffers( (void*) queued.texture->rubTextureMapData, len, VK_FORMAT_R8G8B8A8_UNORM, queued.texture->unWidth, queued.texture->unHeight, ext::vulkan::device, ext::vulkan::device.graphicsQueue );
}
// clear
@ -666,10 +669,10 @@ bool ext::openvr::controllerActive( vr::Controller_Hand hand ) {
else if ( hand == vr::Controller_Hand::Hand_Right ) return ::devices.controllers.right.active;
return false;
}
uf::Mesh& ext::openvr::getRenderModel( const std::string& name ) {
uf::Graphic& ext::openvr::getRenderModel( const std::string& name ) {
return ::renderModels[name];
}
uf::Mesh& ext::openvr::controllerRenderModel( vr::Controller_Hand hand ) {
uf::Graphic& ext::openvr::controllerRenderModel( vr::Controller_Hand hand ) {
if ( hand == vr::Controller_Hand::Hand_Left ) return renderModels["{indexcontroller}valve_controller_knu_1_0_left"]; //return ::devices.controllers.left.mesh;
else if ( hand == vr::Controller_Hand::Hand_Right ) return renderModels["{indexcontroller}valve_controller_knu_1_0_right"]; //return ::devices.controllers.right.mesh;
throw false; //std::exception("error");

View File

@ -262,14 +262,14 @@ void ext::vulkan::Pipeline::initialize( Graphic& graphic ) {
std::vector<VkVertexInputBindingDescription> vertexBindingDescriptions = {
ext::vulkan::initializers::vertexInputBindingDescription(
VERTEX_BUFFER_BIND_ID,
graphic.descriptor.size,
graphic.descriptor.geometry.sizes.vertex,
VK_VERTEX_INPUT_RATE_VERTEX
)
};
// Attribute descriptions
// Describes memory layout and shader positions
std::vector<VkVertexInputAttributeDescription> vertexAttributeDescriptions = {};
for ( auto& attribute : graphic.descriptor.attributes ) {
for ( auto& attribute : graphic.descriptor.geometry.attributes ) {
auto d = ext::vulkan::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
vertexAttributeDescriptions.size(),
@ -486,7 +486,16 @@ void ext::vulkan::Graphic::record( VkCommandBuffer commandBuffer ) {
VkDeviceSize offsets[1] = { 0 };
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer.buffer, offsets);
// Bind triangle index buffer
vkCmdBindIndexBuffer(commandBuffer, indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
VkIndexType indicesType = VK_INDEX_TYPE_UINT32;
switch ( descriptor.geometry.sizes.indices * 8 ) {
case 8: indicesType = VK_INDEX_TYPE_UINT8_EXT; break;
case 16: indicesType = VK_INDEX_TYPE_UINT16; break;
case 32: indicesType = VK_INDEX_TYPE_UINT32; break;
default:
throw std::runtime_error("invalid indices size of " + std::to_string((int) descriptor.geometry.sizes.indices));
break;
}
vkCmdBindIndexBuffer(commandBuffer, indexBuffer.buffer, 0, indicesType);
// Draw indexed triangle
vkCmdDrawIndexed(commandBuffer, descriptor.indices, 1, 0, 0, 1);
}
@ -506,13 +515,14 @@ std::string ext::vulkan::Graphic::Descriptor::hash() const {
uf::Serializer serializer;
serializer["subpass"] = subpass;
serializer["size"] = size;
serializer["geometry"]["sizes"]["vertex"] = geometry.sizes.vertex;
serializer["geometry"]["sizes"]["indices"] = geometry.sizes.indices;
{
int i = 0;
for ( auto& attribute : attributes ) {
serializer["attributes"][i]["format"] = attribute.format;
serializer["attributes"][i]["offset"] = attribute.offset;
for ( auto& attribute : geometry.attributes ) {
serializer["geometry"]["attributes"][i]["format"] = attribute.format;
serializer["geometry"]["attributes"][i]["offset"] = attribute.offset;
++i;
}
}

View File

@ -1,4 +1,5 @@
#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>

View File

@ -1,438 +0,0 @@
#include <uf/ext/vulkan/graphics/compute.h>
#include <uf/ext/vulkan/initializers.h>
#include <uf/ext/vulkan/vulkan.h>
#include <algorithm>
std::string ext::vulkan::ComputeGraphic::name() const {
return "ComputeGraphic";
}
void ext::vulkan::ComputeGraphic::setStorageBuffers( Device& device, std::vector<Cube>& cubes, std::vector<Light>& lights, std::vector<Tree>& trees ) {
this->device = &device;
// Cubes
if ( !cubes.empty() ) {
uniforms.ssbo.cubes.start = 0;
uniforms.ssbo.cubes.end = cubes.size();
} {
initializeBuffer(
(void*) cubes.data(),
cubes.size() * sizeof(Cube),
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
true
);
}
// Trees
if ( !trees.empty() ) {
uniforms.ssbo.root = trees.size() - 1;
}
{
initializeBuffer(
(void*) trees.data(),
trees.size() * sizeof(Tree),
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
true
);
}
// Lights
if ( !lights.empty() ) {
uniforms.ssbo.lights.start = 0;
uniforms.ssbo.lights.end = lights.size();
} {
initializeBuffer(
(void*) lights.data(),
lights.size() * sizeof(Light),
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
true
);
}
}
void ext::vulkan::ComputeGraphic::updateStorageBuffers( Device& device, std::vector<Cube>& cubes, std::vector<Light>& lights, std::vector<Tree>& trees ) {
if ( !cubes.empty() ) {
uniforms.ssbo.cubes.start = 0;
uniforms.ssbo.cubes.end = cubes.size();
updateBuffer(
(void*) cubes.data(),
cubes.size() * sizeof(Cube),
1,
true
);
}
if ( !lights.empty() ) {
uniforms.ssbo.lights.start = 0;
uniforms.ssbo.lights.end = lights.size();
updateBuffer(
(void*) lights.data(),
lights.size() * sizeof(Light),
3,
true
);
}
if ( !trees.empty() ) {
uniforms.ssbo.root = trees.size() - 1;
updateBuffer(
(void*) trees.data(),
trees.size() * sizeof(Tree),
2,
true
);
}
}
void ext::vulkan::ComputeGraphic::initialize( Device& device, RenderMode& renderMode, uint32_t width, uint32_t height ) {
assert( buffers.size() >= 3 );
ext::vulkan::GraphicOld::initialize( device, renderMode );
// Set queue
{
VkDeviceQueueCreateInfo queueCreateInfo = {};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.pNext = NULL;
queueCreateInfo.queueFamilyIndex = device.queueFamilyIndices.compute;
queueCreateInfo.queueCount = 1;
vkGetDeviceQueue(device, device.queueFamilyIndices.compute, 0, &queue);
}
// Create uniform buffer
initializeBuffer(
(void*) &uniforms,
sizeof(uniforms),
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
false
);
// Swap buffers
buffers = {
buffers.at(3),
buffers.at(0),
buffers.at(1),
buffers.at(2),
};
// Create render target
{
if ( width == 0 ) width = ext::vulkan::width;
if ( height == 0 ) height = ext::vulkan::height;
renderTarget.asRenderTarget( device, width, height, device.graphicsQueue );
}
// Set Descriptor Layout
initializeDescriptorLayout({
// Binding 0: Storage image (raytraced output)
ext::vulkan::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
VK_SHADER_STAGE_COMPUTE_BIT,
0
),
// Binding 1: Uniform buffer block
ext::vulkan::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
VK_SHADER_STAGE_COMPUTE_BIT,
1
),
// Binding 1: Shader storage buffer for the cubes
ext::vulkan::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
VK_SHADER_STAGE_COMPUTE_BIT,
2
),
// Binding 1: Shader storage buffer for the cubes
ext::vulkan::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
VK_SHADER_STAGE_COMPUTE_BIT,
3
),
// Binding 1: Shader storage buffer for the cubes
ext::vulkan::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
VK_SHADER_STAGE_COMPUTE_BIT,
4
),
// Binding 1: Texture sampler
ext::vulkan::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_SHADER_STAGE_COMPUTE_BIT,
5
)
});
// Set descriptor pool
initializeDescriptorPool({
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), // UBO
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1), // Graphics image samplers
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1), // Storage image for ray traced image output
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 3), // Storage buffer for the scene primitives
}, 1);
// Set descriptor set
initializeDescriptorSet({
// Binding 0: Output storage image
ext::vulkan::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
0,
&renderTarget.descriptor
),
// Binding 1: Uniform buffer block
ext::vulkan::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1,
&(buffers.at(0).descriptor)
),
// Binding 2: Shader storage buffer for the cubes
ext::vulkan::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
2,
&(buffers.at(1).descriptor)
),
// Binding 2: Shader storage buffer for the lights
ext::vulkan::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
3,
&(buffers.at(2).descriptor)
),
// Binding 2: Shader storage buffer for the trees
ext::vulkan::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
4,
&(buffers.at(3).descriptor)
),
// Binding 3 : Texture sampler
ext::vulkan::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
5,
&diffuseTexture.descriptor
)
});
// Create pipeline
{
// Create compute shader pipelines
VkComputePipelineCreateInfo computePipelineCreateInfo = ext::vulkan::initializers::computePipelineCreateInfo(
pipelineLayout,
0
);
computePipelineCreateInfo.stage = shader.stages.at(0);
VK_CHECK_RESULT(vkCreateComputePipelines(device, device.pipelineCache, 1, &computePipelineCreateInfo, nullptr, &pipeline));
}
// Create command pool
{
// Separate command pool as queue family for compute may be different than graphics
VkCommandPoolCreateInfo cmdPoolInfo = {};
cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmdPoolInfo.queueFamilyIndex = device.queueFamilyIndices.compute;
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
VK_CHECK_RESULT(vkCreateCommandPool(device, &cmdPoolInfo, nullptr, &commandPool));
}
// Create command buffer
{
// Create a command buffer for compute operations
VkCommandBufferAllocateInfo cmdBufAllocateInfo = ext::vulkan::initializers::commandBufferAllocateInfo(
commandPool,
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1
);
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &commandBuffer));
}
// Create fence
{
// Fence for compute CB sync
VkFenceCreateInfo fenceCreateInfo = ext::vulkan::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &fence));
}
// Build command buffer
this->createCommandBuffer(commandBuffer);
}
void ext::vulkan::ComputeGraphic::updateUniformBuffer() {
updateBuffer( (void*) &uniforms, sizeof(uniforms), 0, false );
}
void ext::vulkan::ComputeGraphic::createCommandBuffer( VkCommandBuffer commandBuffer ) {
VkCommandBufferBeginInfo cmdBufInfo = ext::vulkan::initializers::commandBufferBeginInfo();
VK_CHECK_RESULT(vkBeginCommandBuffer(commandBuffer, &cmdBufInfo));
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0, 1, &descriptorSet, 0, 0);
vkCmdDispatch(commandBuffer, renderTarget.width / 32, renderTarget.height / 32, 1);
vkEndCommandBuffer(commandBuffer);
}
void ext::vulkan::ComputeGraphic::destroy() {
renderTarget.destroy();
diffuseTexture.destroy();
if ( fence != VK_NULL_HANDLE ) {
vkDestroyFence(*device, fence, nullptr);
fence = VK_NULL_HANDLE;
}
if ( commandPool != VK_NULL_HANDLE ) {
vkDestroyCommandPool(*device, commandPool, nullptr);
commandPool = VK_NULL_HANDLE;
}
ext::vulkan::GraphicOld::destroy();
}
////////////////////////////////////////////////////////////////
bool ext::vulkan::RTGraphic::autoAssignable() const {
return true;
}
std::string ext::vulkan::RTGraphic::name() const {
return "RTGraphic";
}
void ext::vulkan::RTGraphic::updateUniformBuffer() {
compute.updateUniformBuffer();
}
void ext::vulkan::RTGraphic::initialize( const std::string& renderMode ) {
return initialize(this->device ? *device : ext::vulkan::device, ext::vulkan::getRenderMode(renderMode));
}
void ext::vulkan::RTGraphic::initialize( Device& device, RenderMode& renderMode ) {
ext::vulkan::GraphicOld::initialize( device, renderMode );
compute.initialize( device, renderMode, this->width, this->height );
// Set Descriptor Layout
initializeDescriptorLayout({
ext::vulkan::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_SHADER_STAGE_FRAGMENT_BIT,
0
)
});
// Load shaders
initializeShaders({
{"./data/shaders/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
{"./data/shaders/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT},
});
// Create 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_FRONT_BIT,
VK_FRONT_FACE_COUNTER_CLOCKWISE,
0
);
VkPipelineColorBlendAttachmentState blendAttachmentState = ext::vulkan::initializers::pipelineColorBlendAttachmentState(
0xf,
VK_FALSE
);
VkPipelineColorBlendStateCreateInfo colorBlendState = ext::vulkan::initializers::pipelineColorBlendStateCreateInfo(
1,
&blendAttachmentState
);
VkPipelineDepthStencilStateCreateInfo depthStencilState = ext::vulkan::initializers::pipelineDepthStencilStateCreateInfo(
VK_FALSE,
VK_FALSE,
VK_COMPARE_OP_LESS_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
);
VkGraphicsPipelineCreateInfo pipelineCreateInfo = ext::vulkan::initializers::pipelineCreateInfo(
pipelineLayout,
renderMode.renderTarget.renderPass,
0
);
VkPipelineVertexInputStateCreateInfo emptyInputState = {};
emptyInputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
emptyInputState.vertexAttributeDescriptionCount = 0;
emptyInputState.pVertexAttributeDescriptions = nullptr;
emptyInputState.vertexBindingDescriptionCount = 0;
emptyInputState.pVertexBindingDescriptions = nullptr;
pipelineCreateInfo.pVertexInputState = &emptyInputState;
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();
initializePipeline( pipelineCreateInfo );
}
// Set descriptor pool
initializeDescriptorPool({
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2), // Compute UBO
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4), // Graphics image samplers
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1), // Storage image for ray traced image output
ext::vulkan::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2), // // Storage buffer for the scene primitives
}, 3);
// Set descriptor set
initializeDescriptorSet({
// Binding 0 : Fragment shader texture sampler
ext::vulkan::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
0,
&compute.renderTarget.descriptor
),
});
}
void ext::vulkan::RTGraphic::destroy() {
compute.destroy();
ext::vulkan::GraphicOld::destroy();
}
void ext::vulkan::RTGraphic::createCommandBuffer( VkCommandBuffer commandBuffer ) {
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
}
void ext::vulkan::RTGraphic::render() {
// Submit compute commands
// Use a fence to ensure that compute command buffer has finished executing before using it again
vkWaitForFences( *device, 1, &compute.fence, VK_TRUE, UINT64_MAX );
vkResetFences( *device, 1, &compute.fence );
VkSubmitInfo computeSubmitInfo = ext::vulkan::initializers::submitInfo();
computeSubmitInfo.commandBufferCount = 1;
computeSubmitInfo.pCommandBuffers = &compute.commandBuffer;
VK_CHECK_RESULT(vkQueueSubmit(compute.queue, 1, &computeSubmitInfo, compute.fence));
}
void ext::vulkan::RTGraphic::createImageMemoryBarrier( VkCommandBuffer commandBuffer ) {
// Image memory barrier to make sure that compute shader writes are finished before sampling from the texture
VkImageMemoryBarrier imageMemoryBarrier = {};
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
imageMemoryBarrier.image = compute.renderTarget.image;
imageMemoryBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
imageMemoryBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
vkCmdPipelineBarrier(
commandBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
VK_FLAGS_NONE,
0, nullptr,
0, nullptr,
1, &imageMemoryBarrier
);
}

View File

@ -1,9 +1,10 @@
#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/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
namespace {
uint32_t VERTEX_BUFFER_BIND_ID = 0;

View File

@ -2,8 +2,8 @@
#include <uf/ext/vulkan/graphics/rendertarget.h>
#include <uf/ext/vulkan/vulkan.h>
#include <uf/ext/vulkan/texture.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/ext/vulkan/graphic.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/ext/openvr/openvr.h>
namespace {

View File

@ -1,10 +1,11 @@
#include <uf/ext/vulkan/vulkan.h>
#include <uf/ext/vulkan/rendermode.h>
#include <uf/ext/vulkan/initializers.h>
#include <uf/ext/vulkan/graphic.h>
#include <uf/utils/window/window.h>
#include <uf/ext/vulkan/rendertarget.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/serialize/serializer.h>
#include <uf/engine/scene/scene.h>
@ -23,12 +24,18 @@ const std::string& ext::vulkan::RenderMode::getName() const {
void ext::vulkan::RenderMode::createCommandBuffers() {
std::vector<ext::vulkan::Graphic*> graphics;
std::function<void(uf::Entity*)> filter = [&]( uf::Entity* entity ) {
if ( !entity->hasComponent<uf::Graphic>() ) return;
ext::vulkan::Graphic& graphic = entity->getComponent<uf::Graphic>();
if ( !graphic.initialized ) return;
if ( !graphic.process ) return;
/*
if ( !entity->hasComponent<uf::Mesh>() ) return;
uf::MeshBase& mesh = entity->getComponent<uf::Mesh>();
if ( !mesh.generated ) return;
ext::vulkan::Graphic& graphic = mesh.graphic;
if ( !graphic.initialized ) return;
if ( !graphic.process ) return;
*/
graphics.push_back(&graphic);
};
for ( uf::Scene* scene : ext::vulkan::scenes ) {

View File

@ -2,9 +2,9 @@
#include <uf/ext/vulkan/rendermodes/base.h>
#include <uf/ext/vulkan/initializers.h>
#include <uf/utils/window/window.h>
#include <uf/ext/vulkan/graphic.h>
#include <uf/ext/vulkan/rendertarget.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
namespace {
std::vector<VkImage> images;

View File

@ -3,8 +3,8 @@
#include <uf/ext/vulkan/rendermodes/rendertarget.h>
#include <uf/ext/vulkan/initializers.h>
#include <uf/utils/window/window.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/ext/vulkan/graphic.h>
#include <uf/engine/scene/scene.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/math/transform.h>

View File

@ -2,7 +2,8 @@
#include <uf/ext/vulkan/rendermodes/rendertarget.h>
#include <uf/ext/vulkan/initializers.h>
#include <uf/utils/window/window.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/ext/vulkan/graphic.h>
std::string ext::vulkan::RenderTargetRenderMode::getType() const {
return "RenderTarget";

View File

@ -4,8 +4,8 @@
#include <uf/ext/vulkan/rendermodes/rendertarget.h>
#include <uf/ext/vulkan/initializers.h>
#include <uf/utils/window/window.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/ext/vulkan/graphic.h>
#include <uf/engine/scene/scene.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/math/transform.h>

View File

@ -1,8 +1,9 @@
#include <uf/ext/glfw/glfw.h>
#include <uf/ext/vulkan/vulkan.h>
#include <uf/ext/vulkan/initializers.h>
#include <uf/ext/vulkan/graphic.h>
#include <uf/ext/vulkan/rendermode.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <ostream>
#include <fstream>
@ -230,12 +231,17 @@ void ext::vulkan::initialize( uint8_t stage ) {
} break;
case 1: {
std::function<void(uf::Entity*)> filter = [&]( uf::Entity* entity ) {
if ( !entity->hasComponent<uf::Graphic>() ) return;
ext::vulkan::Graphic& graphic = entity->getComponent<uf::Graphic>();
if ( graphic.initialized ) return;
/*
if ( !entity->hasComponent<uf::Mesh>() ) return;
uf::MeshBase& mesh = entity->getComponent<uf::Mesh>();
ext::vulkan::Graphic& graphic = mesh.graphic;
if ( !mesh.generated ) return;
if ( !graphic.process ) return;
if ( graphic.initialized ) return;
*/
graphic.initializePipeline();
ext::vulkan::rebuild = true;
};
@ -260,10 +266,15 @@ void ext::vulkan::tick() {
if ( ext::vulkan::resized ) ext::vulkan::rebuild = true;
std::function<void(uf::Entity*)> filter = [&]( uf::Entity* entity ) {
if ( !entity->hasComponent<uf::Graphic>() ) return;
ext::vulkan::Graphic& graphic = entity->getComponent<uf::Graphic>();
if ( graphic.initialized ) return;
/*
if ( !entity->hasComponent<uf::Mesh>() ) return;
uf::MeshBase& mesh = entity->getComponent<uf::Mesh>();
ext::vulkan::Graphic& graphic = mesh.graphic;
if ( !mesh.generated ) return;
*/
if ( !graphic.process ) return;
if ( graphic.initialized ) return;
graphic.initializePipeline();
@ -310,9 +321,8 @@ void ext::vulkan::destroy() {
vkDeviceWaitIdle( device );
std::function<void(uf::Entity*)> filter = [&]( uf::Entity* entity ) {
if ( !entity->hasComponent<uf::Mesh>() ) return;
uf::MeshBase& mesh = entity->getComponent<uf::Mesh>();
ext::vulkan::Graphic& graphic = mesh.graphic;
if ( !entity->hasComponent<uf::Graphic>() ) return;
uf::Graphic& graphic = entity->getComponent<uf::Graphic>();
graphic.destroy();
};
for ( uf::Scene* scene : ext::vulkan::scenes ) {

View File

@ -1,4 +1,4 @@
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/mesh.h>
/*
uf::Graphic::~Graphic() {
this->destroy();
@ -7,7 +7,7 @@ void uf::Graphic::destroy( bool clear ) {
}
*/
// Used for terrain
std::vector<ext::vulkan::Graphic::VertexDescriptor> pod::Vertex_3F2F3F32B::descriptor = {
std::vector<ext::vulkan::VertexDescriptor> pod::Vertex_3F2F3F32B::descriptor = {
{
VK_FORMAT_R32G32B32_SFLOAT,
offsetof(pod::Vertex_3F2F3F32B, position)
@ -26,7 +26,7 @@ std::vector<ext::vulkan::Graphic::VertexDescriptor> pod::Vertex_3F2F3F32B::descr
}
};
// Used for normal meshses
std::vector<ext::vulkan::Graphic::VertexDescriptor> pod::Vertex_3F2F3F::descriptor = {
std::vector<ext::vulkan::VertexDescriptor> pod::Vertex_3F2F3F::descriptor = {
{
VK_FORMAT_R32G32B32_SFLOAT,
offsetof(pod::Vertex_3F2F3F, position)
@ -41,7 +41,7 @@ std::vector<ext::vulkan::Graphic::VertexDescriptor> pod::Vertex_3F2F3F::descript
}
};
// (Typically) used for displaying textures
std::vector<ext::vulkan::Graphic::VertexDescriptor> pod::Vertex_3F2F::descriptor = {
std::vector<ext::vulkan::VertexDescriptor> pod::Vertex_3F2F::descriptor = {
{
VK_FORMAT_R32G32B32_SFLOAT,
offsetof(pod::Vertex_3F2F, position)
@ -52,7 +52,7 @@ std::vector<ext::vulkan::Graphic::VertexDescriptor> pod::Vertex_3F2F::descriptor
}
};
// Basic
std::vector<ext::vulkan::Graphic::VertexDescriptor> pod::Vertex_3F::descriptor = {
std::vector<ext::vulkan::VertexDescriptor> pod::Vertex_3F::descriptor = {
{
VK_FORMAT_R32G32B32_SFLOAT,
offsetof(pod::Vertex_3F, position)

View File

@ -6,7 +6,8 @@
#include <uf/utils/userdata/userdata.h>
#include <uf/utils/window/window.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/string/ext.h>
#include <uf/utils/text/glyph.h>
@ -332,6 +333,7 @@ namespace {
gui.addAlias<uf::GuiMesh, uf::Mesh>();
}
uf::GuiMesh& mesh = gui.getComponent<uf::GuiMesh>();
uf::Graphic& graphic = gui.getComponent<uf::Graphic>();
/* get original image size (before padding) */ {
metadata["original size"]["x"] = image.getDimensions().x;
metadata["original size"]["y"] = image.getDimensions().y;
@ -402,8 +404,9 @@ namespace {
vertex.position.x /= ::size.reference.x;
vertex.position.y /= ::size.reference.y;
}
mesh.initialize(true);
mesh.graphic.initialize( "Gui" );
graphic.initialize( "Gui" );
graphic.initializeGeometry( mesh );
struct {
std::string vertex = "./data/shaders/gui.text.vert.spv";
std::string fragment = "./data/shaders/gui.text.frag.spv";
@ -412,7 +415,7 @@ namespace {
if ( metadata["shaders"]["fragment"].isString() ) filenames.fragment = metadata["shaders"]["fragment"].asString();
else if ( suffix != "" ) filenames.fragment = "./data/shaders/gui.text."+suffix+"frag.spv";
mesh.graphic.material.initializeShaders({
graphic.material.initializeShaders({
{filenames.vertex, VK_SHADER_STAGE_VERTEX_BIT},
{filenames.fragment, VK_SHADER_STAGE_FRAGMENT_BIT},
});
@ -449,8 +452,8 @@ namespace {
{ {1.0f, -1.0f}, {1.0f, 1.0f}, },
{ {1.0f, 1.0f}, {1.0f, 0.0f}, }
};
mesh.initialize(true);
mesh.graphic.initialize( "Gui" );
graphic.initialize( "Gui" );
graphic.initializeGeometry( mesh );
struct {
std::string vertex = "./data/shaders/gui.vert.spv";
std::string fragment = "./data/shaders/gui.frag.spv";
@ -459,19 +462,19 @@ namespace {
if ( metadata["shaders"]["fragment"].isString() ) filenames.fragment = metadata["shaders"]["fragment"].asString();
else if ( suffix != "" ) filenames.fragment = "./data/shaders/gui."+suffix+"frag.spv";
mesh.graphic.material.initializeShaders({
graphic.material.initializeShaders({
{filenames.vertex, VK_SHADER_STAGE_VERTEX_BIT},
{filenames.fragment, VK_SHADER_STAGE_FRAGMENT_BIT},
});
}
auto& texture = mesh.graphic.material.textures.emplace_back();
auto& texture = graphic.material.textures.emplace_back();
texture.loadFromImage( image );
{
pod::Transform<>& transform = gui.getComponent<pod::Transform<>>();
uf::GuiMesh& mesh = gui.getComponent<uf::GuiMesh>();
auto& texture = mesh.graphic.material.textures.front();
auto& texture = graphic.material.textures.front();
pod::Vector2f textureSize = {
metadata["original size"]["x"].asFloat(),
@ -615,7 +618,8 @@ void ext::Gui::initialize() {
}
pod::Transform<>& transform = this->getComponent<pod::Transform<>>();
uf::GuiMesh& mesh = this->getComponent<uf::GuiMesh>();
auto& texture = mesh.graphic.material.textures.front();
uf::Graphic& graphic = this->getComponent<uf::Graphic>();
auto& texture = graphic.material.textures.front();
pod::Vector2f textureSize = {
metadata["original size"]["x"].asFloat(),
metadata["original size"]["y"].asFloat()
@ -813,9 +817,10 @@ void ext::Gui::render() {
/* Update uniforms */ if ( this->hasComponent<uf::GuiMesh>() ) {
auto& scene = this->getRootParent<uf::Scene>();
auto& mesh = this->getComponent<uf::GuiMesh>();
auto& graphic = this->getComponent<uf::Graphic>();
auto& camera = scene.getController()->getComponent<uf::Camera>();
auto& transform = this->getComponent<pod::Transform<>>();
if ( !mesh.generated ) return;
if ( !graphic.initialized ) return;
pod::Vector4 offset = {
metadata["uv"][0].asFloat(),
@ -828,8 +833,8 @@ void ext::Gui::render() {
if ( this->m_name == "Gui: Text" ) {
// ::GlyphDescriptor uniforms;
// auto& uniforms = mesh.graphic.uniforms<::GlyphDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<::GlyphDescriptor>();
// auto& uniforms = graphic.uniforms<::GlyphDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<::GlyphDescriptor>();
if ( !metadata["text settings"]["color"].isArray() ) {
metadata["text settings"]["color"][0] = 1.0f;
@ -912,8 +917,8 @@ void ext::Gui::render() {
}
}
uniforms.gui.depth = 1.0f - uniforms.gui.depth;
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// calculate click box
{
auto& model = uniforms.matrices.model[0];
@ -944,8 +949,8 @@ void ext::Gui::render() {
metadata["color"][3].asFloat()
};
// uf::StereoGuiMeshDescriptor uniforms;
// auto& uniforms = mesh.graphic.uniforms<uf::StereoGuiMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoGuiMeshDescriptor>();
// auto& uniforms = graphic.uniforms<uf::StereoGuiMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoGuiMeshDescriptor>();
uniforms.gui.offset = offset;
uniforms.gui.color = color;
uniforms.gui.mode = mode;
@ -980,8 +985,8 @@ void ext::Gui::render() {
}
}
uniforms.gui.depth = 1.0f - uniforms.gui.depth;
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// calculate click box
{
auto& model = uniforms.matrices.model[0];
@ -1135,10 +1140,7 @@ void ext::Gui::render() {
}
void ext::Gui::destroy() {
if ( this->hasComponent<uf::GuiMesh>() ) {
auto& mesh = this->getComponent<uf::GuiMesh>();
mesh.graphic.destroy();
mesh.destroy();
}
auto& graphic = this->getComponent<uf::Graphic>();
graphic.destroy();
uf::Object::destroy();
}

View File

@ -19,7 +19,7 @@
#include <uf/utils/userdata/userdata.h>
#include <uf/utils/image/image.h>
#include <uf/utils/thread/thread.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/http/http.h>

View File

@ -1,6 +1,7 @@
#include "heightmap.h"
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/graphic/mesh.h>
#include <uf/utils/math/transform.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/noise/noise.h>
@ -19,6 +20,7 @@ void ext::Heightmap::initialize() {
this->m_name = "Heightmap";
auto& mesh = this->getComponent<MESH_TYPE>();
auto& graphic = this->getComponent<uf::Graphic>();
auto& transform = this->getComponent<pod::Transform<>>();
// generate heightmap
@ -79,11 +81,11 @@ void ext::Heightmap::initialize() {
}
}
{
// mesh.graphic.texture.loadFromFile( "./data/textures/texture.png" );
mesh.graphic.initialize();
// graphic.texture.loadFromFile( "./data/textures/texture.png" );
graphic.initialize();
mesh.initialize(true);
auto& texture = mesh.graphic.material.textures.emplace_back();
auto& texture = graphic.material.textures.emplace_back();
texture.loadFromFile( "./data/textures/texture.png" );
std::string suffix = ""; {
@ -91,52 +93,50 @@ void ext::Heightmap::initialize() {
if ( _ != "" ) suffix = _ + ".";
}
mesh.graphic.material.attachShader("./data/shaders/heightmap.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
mesh.graphic.material.attachShader("./data/shaders/heightmap."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
graphic.material.attachShader("./data/shaders/heightmap.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
graphic.material.attachShader("./data/shaders/heightmap."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
/*
mesh.graphic.initializeShaders({
graphic.initializeShaders({
{"./data/shaders/heightmap.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
{"./data/shaders/heightmap."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT}
});
mesh.initialize(true);
mesh.graphic.bindUniform<uf::StereoMeshDescriptor>();
mesh.graphic.initialize();
mesh.graphic.autoAssign();
graphic.bindUniform<uf::StereoMeshDescriptor>();
graphic.initialize();
graphic.autoAssign();
*/
mesh.graphic.process = true;
graphic.process = true;
}
}
void ext::Heightmap::tick() {
uf::Object::tick();
}
void ext::Heightmap::destroy() {
if ( this->hasComponent<MESH_TYPE>() ) {
auto& mesh = this->getComponent<MESH_TYPE>();
mesh.graphic.destroy();
mesh.destroy();
}
auto& graphic = this->getComponent<uf::Graphic>();
graphic.destroy();
uf::Object::destroy();
}
void ext::Heightmap::render() {
uf::Object::render();
/* Update uniforms */ if ( this->hasComponent<MESH_TYPE>() ) {
auto& mesh = this->getComponent<MESH_TYPE>();
auto& graphic = this->getComponent<uf::Graphic>();
auto& root = this->getRootParent<uf::Scene>();
auto& player = *root.getController();
auto& camera = player.getComponent<uf::Camera>();
auto& transform = player.getComponent<pod::Transform<>>();
auto& model = this->getComponent<pod::Transform<>>();
if ( !mesh.generated ) return;
if ( !graphic.initialized ) return;
uf::Serializer& metadata = this->getComponent<uf::Serializer>();
//auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
// auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
//auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
// auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
uniforms.matrices.model = uf::transform::model( this->getComponent<pod::Transform<>>() );
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
uniforms.matrices.projection[i] = camera.getProjection( i );
}
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
};
}

View File

@ -1,6 +1,7 @@
#include "marching.h"
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/math/transform.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/noise/noise.h>
@ -309,6 +310,7 @@ void ext::Marching::initialize() {
this->m_name = "Marching";
auto& mesh = this->getComponent<MESH_TYPE>();
auto& graphic = this->getComponent<uf::Graphic>();
auto& transform = this->getComponent<pod::Transform<>>();
auto& metadata = this->getComponent<uf::Serializer>();
@ -468,18 +470,18 @@ void ext::Marching::initialize() {
}
}
{
std::cout << mesh.graphic.initialized << std::endl;
if ( mesh.graphic.initialized ) {
mesh.graphic.destroy();
std::cout << graphic.initialized << std::endl;
if ( graphic.initialized ) {
graphic.destroy();
mesh.destroy();
}
std::cout << "Done?" << std::endl;
mesh.vertices = std::move(vertices);
mesh.initialize(true);
mesh.graphic.initialize();
graphic.initialize();
graphic.initializeGeometry( mesh );
auto& texture = mesh.graphic.material.textures.emplace_back();
auto& texture = graphic.material.textures.emplace_back();
texture.loadFromFile( "./data/textures/texture.png" );
std::string suffix = ""; {
@ -487,23 +489,8 @@ void ext::Marching::initialize() {
if ( _ != "" ) suffix = _ + ".";
}
mesh.graphic.material.attachShader("./data/shaders/heightmap.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
mesh.graphic.material.attachShader("./data/shaders/heightmap."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
/*
mesh.graphic.texture.loadFromFile( "./data/textures/texture.png" );
std::string suffix = ""; {
std::string _ = this->getRootParent<uf::Scene>().getComponent<uf::Serializer>()["shaders"]["region"]["suffix"].asString();
if ( _ != "" ) suffix = _ + ".";
}
mesh.graphic.initializeShaders({
{"./data/shaders/heightmap.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
{"./data/shaders/heightmap."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT}
});
mesh.initialize(true);
mesh.graphic.bindUniform<uf::StereoMeshDescriptor>();
mesh.graphic.initialize();
mesh.graphic.autoAssign();
*/
graphic.material.attachShader("./data/shaders/heightmap.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
graphic.material.attachShader("./data/shaders/heightmap."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
}
return "true";
});
@ -521,32 +508,30 @@ void ext::Marching::tick() {
*/
}
void ext::Marching::destroy() {
if ( this->hasComponent<MESH_TYPE>() ) {
auto& mesh = this->getComponent<MESH_TYPE>();
mesh.graphic.destroy();
mesh.destroy();
}
auto& graphic = this->getComponent<uf::Graphic>();
graphic.destroy();
uf::Object::destroy();
}
void ext::Marching::render() {
uf::Object::render();
/* Update uniforms */ if ( this->hasComponent<MESH_TYPE>() ) {
auto& mesh = this->getComponent<MESH_TYPE>();
auto& graphic = this->getComponent<uf::Graphic>();
auto& root = this->getRootParent<uf::Scene>();
auto& player = *root.getController();
auto& camera = player.getComponent<uf::Camera>();
auto& transform = player.getComponent<pod::Transform<>>();
auto& model = this->getComponent<pod::Transform<>>();
if ( !mesh.generated ) return;
if ( !graphic.initialized ) return;
uf::Serializer& metadata = this->getComponent<uf::Serializer>();
// auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
// auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
uniforms.matrices.model = uf::transform::model( this->getComponent<pod::Transform<>>() );
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
uniforms.matrices.projection[i] = camera.getProjection( i );
}
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
};
}

View File

@ -6,7 +6,6 @@
#include <uf/utils/userdata/userdata.h>
#include <uf/utils/window/window.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/string/ext.h>
// #include <uf/gl/glyph/glyph.h>
#include "../world.h"

View File

@ -6,7 +6,6 @@
#include <uf/utils/userdata/userdata.h>
#include <uf/utils/window/window.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/string/ext.h>
// #include <uf/gl/glyph/glyph.h>
#include <uf/engine/asset/asset.h>

View File

@ -6,7 +6,6 @@
#include <uf/utils/userdata/userdata.h>
#include <uf/utils/window/window.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/string/ext.h>
// #include <uf/gl/glyph/glyph.h>
#include <uf/engine/asset/asset.h>

View File

@ -6,7 +6,7 @@
#include <uf/utils/time/time.h>
#include <uf/utils/serialize/serializer.h>
#include <uf/utils/userdata/userdata.h>
#include <uf/utils/mesh/mesh.h>
#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>

View File

@ -6,7 +6,7 @@
#include <uf/utils/time/time.h>
#include <uf/utils/serialize/serializer.h>
#include <uf/utils/userdata/userdata.h>
#include <uf/utils/mesh/mesh.h>
#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>

View File

@ -4,7 +4,8 @@
#include <uf/utils/time/time.h>
#include <uf/utils/serialize/serializer.h>
#include <uf/utils/userdata/userdata.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/mesh.h>
#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>
@ -68,6 +69,7 @@ void ext::HousamoSprite::initialize() {
uf::Image image = *imagePointer;
uf::Mesh& mesh = this->getComponent<uf::Mesh>();
auto& graphic = this->getComponent<uf::Graphic>();
mesh.vertices = {
{{-1*-0.5f, 0.0f, 0.0f}, {1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f } },
{{-1*0.5f, 0.0f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, -1.0f } },
@ -83,23 +85,16 @@ void ext::HousamoSprite::initialize() {
{{-1*-0.5f, 1.0f, 0.0f}, {1.0f, 1.0f}, { 0.0f, 0.0f, 1.0f } },
{{-1*0.5f, 1.0f, 0.0f}, {0.0f, 1.0f}, { 0.0f, 0.0f, 1.0f } },
};
mesh.initialize(true);
mesh.graphic.initialize();
auto& texture = mesh.graphic.material.textures.emplace_back();
graphic.initialize();
graphic.initializeGeometry( mesh );
auto& texture = graphic.material.textures.emplace_back();
texture.loadFromImage( image );
mesh.graphic.material.attachShader("./data/shaders/base.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
mesh.graphic.material.attachShader("./data/shaders/base.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
/*
mesh.graphic.texture.loadFromImage( image );
mesh.graphic.bindUniform<uf::StereoMeshDescriptor>();
mesh.graphic.initializeShaders({
{"./data/shaders/base.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
{"./data/shaders/base.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT}
});
mesh.graphic.initialize();
mesh.graphic.autoAssign();
*/
graphic.material.attachShader("./data/shaders/base.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
graphic.material.attachShader("./data/shaders/base.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
metadata["system"]["control"] = true;
metadata["system"]["loaded"] = true;
return "true";
@ -131,11 +126,8 @@ void ext::HousamoSprite::tick() {
}
void ext::HousamoSprite::destroy() {
if ( this->hasComponent<uf::Mesh>() ) {
auto& mesh = this->getComponent<uf::Mesh>();
mesh.graphic.destroy();
mesh.destroy();
}
auto& graphic = this->getComponent<uf::Graphic>();
graphic.destroy();
ext::Craeture::destroy();
}
@ -146,14 +138,15 @@ void ext::HousamoSprite::render() {
ext::Craeture::render();
/* Update uniforms */ if ( this->hasComponent<uf::Mesh>() ) {
auto& mesh = this->getComponent<uf::Mesh>();
auto& graphic = this->getComponent<uf::Graphic>();
auto& scene = uf::scene::getCurrentScene();
auto& controller = *scene.getController();
auto& camera = controller.getComponent<uf::Camera>();
auto& transform = this->getComponent<pod::Transform<>>();
if ( !mesh.generated ) return;
//auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
// auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
if ( !graphic.initialized ) return;
//auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
// auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
uniforms.matrices.model = uf::transform::model( transform );
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
@ -163,7 +156,7 @@ void ext::HousamoSprite::render() {
uniforms.color[1] = metadata["color"][1].asFloat();
uniforms.color[2] = metadata["color"][2].asFloat();
uniforms.color[3] = metadata["color"][3].asFloat();
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// mesh.graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
};
}

View File

@ -1,6 +1,7 @@
#include "hands.h"
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/math/transform.h>
#include <uf/utils/math/physics.h>
@ -55,21 +56,13 @@ void ext::Hands::initialize() {
{
uf::Object& hand = *pointer;
uf::Mesh& mesh = (hand.getComponent<uf::Mesh>() = ext::openvr::getRenderModel( name ));
mesh.graphic.process = true;
mesh.graphic.material.attachShader("./data/shaders/base.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
mesh.graphic.material.attachShader("./data/shaders/base.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
/*
mesh.graphic.initializeShaders({
{"./data/shaders/base.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
{"./data/shaders/base.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT}
});
mesh.graphic.description.rasterMode.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
mesh.generate();
mesh.graphic.bindUniform<uf::StereoMeshDescriptor>();
mesh.graphic.initialize();
mesh.graphic.autoAssign();
*/
uf::Graphic& graphic = (hand.getComponent<uf::Graphic>() = ext::openvr::getRenderModel( name ));
graphic.process = true;
graphic.material.attachShader("./data/shaders/base.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
graphic.material.attachShader("./data/shaders/base.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
hand.initialize();
}
{
@ -93,30 +86,32 @@ void ext::Hands::initialize() {
};
auto& mesh = line.getComponent<uf::LineMesh>();
auto& graphic = line.getComponent<uf::Graphic>();
mesh.vertices = {
{ {0.0f, 0.0f, 0.0f} },
{ {0.0f, 0.0f, metadata["hands"][hand]["pointer"]["length"].asFloat()} },
};
mesh.initialize(true);
graphic.initialize();
graphic.initializeGeometry(mesh);
mesh.graphic.initialize();
mesh.graphic.material.attachShader("./data/shaders/line.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
mesh.graphic.material.attachShader("./data/shaders/line.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
mesh.graphic.descriptor.topology = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
mesh.graphic.descriptor.fill = VK_POLYGON_MODE_LINE;
mesh.graphic.descriptor.lineWidth = metadata["hands"][hand]["pointer"]["width"].asFloat();
graphic.material.attachShader("./data/shaders/line.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
graphic.material.attachShader("./data/shaders/line.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
graphic.descriptor.topology = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
graphic.descriptor.fill = VK_POLYGON_MODE_LINE;
graphic.descriptor.lineWidth = metadata["hands"][hand]["pointer"]["width"].asFloat();
/*
mesh.graphic.initializeShaders({
graphic.initializeShaders({
{"./data/shaders/line.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
{"./data/shaders/line.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT}
});
mesh.generate();
mesh.graphic.bindUniform<uf::StereoMeshDescriptor>();
mesh.graphic.description.rasterMode.topology = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
mesh.graphic.description.rasterMode.fill = VK_POLYGON_MODE_LINE;
mesh.graphic.description.rasterMode.lineWidth = metadata["hands"][hand]["pointer"]["width"].asFloat();
mesh.graphic.initialize();
mesh.graphic.autoAssign();
graphic.bindUniform<uf::StereoMeshDescriptor>();
graphic.description.rasterMode.topology = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
graphic.description.rasterMode.fill = VK_POLYGON_MODE_LINE;
graphic.description.rasterMode.lineWidth = metadata["hands"][hand]["pointer"]["width"].asFloat();
graphic.initialize();
graphic.autoAssign();
*/
line.initialize();
}
@ -381,12 +376,13 @@ void ext::Hands::render() {
pod::Matrix4f cameraModel = uf::matrix::translate( uf::matrix::identity(), camera.getTransform().position + controller.getComponent<pod::Transform<>>().position ) * uf::quaternion::matrix( controller.getComponent<pod::Transform<>>().orientation * pod::Vector4f{1,1,1,-1} );
if ( hands.left.hasComponent<uf::Mesh>() ) {
auto& mesh = hands.left.getComponent<uf::Mesh>();
auto& graphic = hands.left.getComponent<uf::Graphic>();
auto& transform = hands.left.getComponent<pod::Transform<>>();
mesh.graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Left );
graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Left );
pod::Matrix4f model = cameraModel * ext::openvr::controllerModelMatrix( vr::Controller_Hand::Hand_Left, false );
if ( mesh.generated ) {
// auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
if ( graphic.initialized ) {
// auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
uniforms.matrices.model = model;
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
@ -396,18 +392,19 @@ void ext::Hands::render() {
uniforms.color[1] = metadata["hands"]["left"]["controller"]["color"][1].asFloat();
uniforms.color[2] = metadata["hands"]["left"]["controller"]["color"][2].asFloat();
uniforms.color[3] = metadata["hands"]["left"]["controller"]["color"][3].asFloat();
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
}
}
if ( hands.right.hasComponent<uf::Mesh>() ) {
auto& mesh = hands.right.getComponent<uf::Mesh>();
auto& graphic = hands.right.getComponent<uf::Graphic>();
auto& transform = hands.right.getComponent<pod::Transform<>>();
mesh.graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Right );
graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Right );
pod::Matrix4f model = cameraModel * ext::openvr::controllerModelMatrix( vr::Controller_Hand::Hand_Right, false );
if ( mesh.generated ) {
// auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
if ( graphic.initialized ) {
// auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
uniforms.matrices.model = model;
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
@ -417,18 +414,19 @@ void ext::Hands::render() {
uniforms.color[1] = metadata["hands"]["right"]["controller"]["color"][1].asFloat();
uniforms.color[2] = metadata["hands"]["right"]["controller"]["color"][2].asFloat();
uniforms.color[3] = metadata["hands"]["right"]["controller"]["color"][3].asFloat();
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
}
}
if ( lines.left.hasComponent<uf::Mesh>() ) {
auto& mesh = lines.left.getComponent<uf::Mesh>();
auto& graphic = lines.left.getComponent<uf::Graphic>();
auto& transform = lines.left.getComponent<pod::Transform<>>();
mesh.graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Left );
graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Left );
pod::Matrix4f model = cameraModel * ext::openvr::controllerModelMatrix( vr::Controller_Hand::Hand_Left, true );
if ( mesh.generated ) {
// auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
if ( graphic.initialized ) {
// auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
uniforms.matrices.model = model;
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
@ -438,18 +436,19 @@ void ext::Hands::render() {
uniforms.color[1] = metadata["hands"]["left"]["pointer"]["color"][1].asFloat();
uniforms.color[2] = metadata["hands"]["left"]["pointer"]["color"][2].asFloat();
uniforms.color[3] = metadata["hands"]["left"]["pointer"]["color"][3].asFloat();
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
}
}
if ( lines.right.hasComponent<uf::Mesh>() ) {
auto& mesh = lines.right.getComponent<uf::Mesh>();
auto& graphic = lines.right.getComponent<uf::Graphic>();
auto& transform = lines.right.getComponent<pod::Transform<>>();
mesh.graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Right );
graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Right );
pod::Matrix4f model = cameraModel * ext::openvr::controllerModelMatrix( vr::Controller_Hand::Hand_Right, true );
if ( mesh.generated ) {
// auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
if ( graphic.initialized ) {
// auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
uniforms.matrices.model = model;
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
@ -459,29 +458,8 @@ void ext::Hands::render() {
uniforms.color[1] = metadata["hands"]["right"]["pointer"]["color"][1].asFloat();
uniforms.color[2] = metadata["hands"]["right"]["pointer"]["color"][2].asFloat();
uniforms.color[3] = metadata["hands"]["right"]["pointer"]["color"][3].asFloat();
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
}
}
/*
if ( lines.right.hasComponent<uf::Mesh>() ) {
auto& mesh = lines.right.getComponent<uf::Mesh>();
// mesh.graphic.process = ext::openvr::controllerActive( vr::Controller_Hand::Hand_Right );
auto& transform = lines.right.getComponent<pod::Transform<>>();
if ( mesh.generated ) {
auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
uniforms.matrices.model = cameraModel * ext::openvr::controllerMatrix( vr::Controller_Hand::Hand_Right, true );//uf::transform::model( transform );
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
uniforms.matrices.projection[i] = camera.getProjection( i );
}
uniforms.color[0] = metadata["hands"]["right"]["pointer"]["color"][0].asFloat();
uniforms.color[1] = metadata["hands"]["right"]["pointer"]["color"][1].asFloat();
uniforms.color[2] = metadata["hands"]["right"]["pointer"]["color"][2].asFloat();
uniforms.color[3] = metadata["hands"]["right"]["pointer"]["color"][3].asFloat();
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
}
}
*/
}

View File

@ -1,6 +1,7 @@
#include "portal.h"
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/mesh.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/math/transform.h>
#include <uf/utils/math/physics.h>

View File

@ -11,7 +11,7 @@
#include <uf/utils/noise/noise.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/mesh.h>
#include <uf/ext/vulkan/graphics/compute.h>
#include <uf/utils/string/rle.h>

View File

@ -7,6 +7,8 @@
#include <uf/engine/asset/asset.h>
#include <uf/utils/camera/camera.h>
#include <uf/utils/math/collision.h>
#include <uf/utils/graphic/graphic.h>
#include <uf/utils/graphic/mesh.h>
#include <uf/utils/string/ext.h>
namespace {
@ -46,11 +48,12 @@ void ext::Region::initialize() {
}
}
ext::TerrainGenerator::mesh_t& mesh = this->getComponent<ext::TerrainGenerator::mesh_t>();
auto& graphic = this->getComponent<uf::Graphic>();
mesh.graphic.initialize();
mesh.graphic.process = false;
graphic.initialize();
graphic.process = false;
auto& texture = mesh.graphic.material.textures.emplace_back();
auto& texture = graphic.material.textures.emplace_back();
texture.sampler.filter = VK_FILTER_NEAREST;
texture.loadFromFile( textureFilename );
@ -58,20 +61,8 @@ void ext::Region::initialize() {
std::string _ = this->getRootParent<uf::Scene>().getComponent<uf::Serializer>()["shaders"]["region"]["suffix"].asString();
if ( _ != "" ) suffix = _ + ".";
}
mesh.graphic.material.attachShader("./data/shaders/terrain.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
mesh.graphic.material.attachShader("./data/shaders/terrain."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
/*
mesh.graphic.texture.sampler.filter = VK_FILTER_NEAREST;
mesh.graphic.texture.loadFromFile( texture );
std::string suffix = ""; {
std::string _ = this->getRootParent<uf::Scene>().getComponent<uf::Serializer>()["shaders"]["region"]["suffix"].asString();
if ( _ != "" ) suffix = _ + ".";
}
mesh.graphic.initializeShaders({
{"./data/shaders/terrain.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT},
{"./data/shaders/terrain."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT}
});
*/
graphic.material.attachShader("./data/shaders/terrain.stereo.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
graphic.material.attachShader("./data/shaders/terrain."+suffix+"frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
}
this->addHook( "region:Generate.%UID%", [&](const std::string& event)->std::string{
@ -131,18 +122,11 @@ void ext::Region::initialize() {
ext::TerrainGenerator& generator = this->getComponent<ext::TerrainGenerator>();
ext::TerrainGenerator::mesh_t& mesh = this->getComponent<ext::TerrainGenerator::mesh_t>();
/*
if ( !mesh.vertices.empty() ) {
mesh.graphic.destroy();
mesh.destroy();
}
*/
auto& graphic = this->getComponent<uf::Graphic>();
generator.rasterize(mesh.vertices, *this);
mesh.initialize(true);
/*
mesh.graphic.bindUniform<uf::StereoMeshDescriptor>();
mesh.graphic.initialize();
*/
graphic.initializeGeometry( mesh );
this->queueHook("region:Finalize.%UID%", "");
this->queueHook("region:Populate.%UID%", "");
return "true";
@ -151,10 +135,9 @@ void ext::Region::initialize() {
uf::Serializer json = event;
ext::TerrainGenerator::mesh_t& mesh = this->getComponent<ext::TerrainGenerator::mesh_t>();
auto& graphic = this->getComponent<uf::Graphic>();
// mesh.graphic.autoAssign();
mesh.graphic.process = true;
// mesh.graphic.initializePipeline();
graphic.process = true;
metadata["region"]["rasterized"] = true;
return "true";
@ -292,11 +275,9 @@ void ext::Region::tick() {
uf::Object::tick();
}
void ext::Region::destroy() {
if ( this->hasComponent<ext::TerrainGenerator::mesh_t>() ) {
auto& mesh = this->getComponent<ext::TerrainGenerator::mesh_t>();
mesh.graphic.destroy();
mesh.destroy();
}
auto& graphic = this->getComponent<uf::Graphic>();
graphic.destroy();
uf::Object::destroy();
}
void ext::Region::render( ) {
@ -311,17 +292,18 @@ void ext::Region::render( ) {
/* Update uniforms */ if ( this->hasComponent<ext::TerrainGenerator::mesh_t>() ) {
auto& world = this->getRootParent<uf::Scene>();
auto& mesh = this->getComponent<ext::TerrainGenerator::mesh_t>();
auto& graphic = this->getComponent<uf::Graphic>();
auto& camera = world.getController()->getComponent<uf::Camera>();
if ( !mesh.generated ) return;
// auto& uniforms = mesh.graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = mesh.graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
if ( !graphic.initialized ) return;
// auto& uniforms = graphic.uniforms<uf::StereoMeshDescriptor>();
auto& uniforms = graphic.material.shaders.front().uniforms.front().get<uf::StereoMeshDescriptor>();
uniforms.matrices.model = uf::matrix::identity();
for ( std::size_t i = 0; i < 2; ++i ) {
uniforms.matrices.view[i] = camera.getView( i );
uniforms.matrices.projection[i] = camera.getProjection( i );
}
// mesh.graphic.updateBuffer( uniforms, 0, false );
mesh.graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
// graphic.updateBuffer( uniforms, 0, false );
graphic.material.shaders.front().updateBuffer( uniforms, 0, false );
}
}

View File

@ -7,7 +7,8 @@
#include "region.h"
#include <uf/utils/window/window.h>
#include <uf/utils/mesh/mesh.h>
#include <uf/utils/graphic/mesh.h>
#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>