OpenGL: Implement textures

This commit is contained in:
Lynix
2020-04-19 15:33:56 +02:00
parent 349e915e10
commit b4b15f826d
12 changed files with 227 additions and 298 deletions

View File

@@ -43,6 +43,16 @@ namespace Nz::GL
KHR
};
enum class TextureTarget
{
Cubemap,
Target2D,
Target2D_Array,
Target3D,
Max = Target3D
};
struct ContextParams
{
ContextType type = ContextType::OpenGL_ES;
@@ -63,6 +73,7 @@ namespace Nz::GL
inline Context(const OpenGLDevice* device);
virtual ~Context();
void BindTexture(TextureTarget target, GLuint texture) const;
virtual void EnableVerticalSync(bool enabled) = 0;
@@ -75,6 +86,8 @@ namespace Nz::GL
bool Initialize(const ContextParams& params);
inline void NotifyTextureDestruction(GLuint texture) const;
virtual void SwapBuffers() = 0;
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) sig name = nullptr;
@@ -98,9 +111,15 @@ namespace Nz::GL
private:
void GL_APIENTRY HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message) const;
struct State
{
std::array<GLuint, UnderlyingCast(TextureTarget::Max) + 1> boundTextures;
};
std::array<ExtensionStatus, UnderlyingCast(Extension::Max) + 1> m_extensionStatus;
std::unordered_set<std::string> m_supportedExtensions;
const OpenGLDevice* m_device;
mutable State m_state;
};
}

View File

@@ -36,6 +36,15 @@ namespace Nz::GL
{
return m_supportedExtensions.find(extension) != m_supportedExtensions.end();
}
inline void Context::NotifyTextureDestruction(GLuint texture) const
{
for (GLuint& boundTexture : m_state.boundTextures)
{
if (boundTexture == texture)
boundTexture = 0;
}
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@@ -4,40 +4,41 @@
#pragma once
#ifndef NAZARA_OPENGLRENDERER_VKPIPELINELAYOUT_HPP
#define NAZARA_OPENGLRENDERER_VKPIPELINELAYOUT_HPP
#ifndef NAZARA_OPENGLRENDERER_GLTEXTURE_HPP
#define NAZARA_OPENGLRENDERER_GLTEXTURE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/DeviceObject.hpp>
#include <Nazara/Core/MovableValue.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
namespace Nz
namespace Nz::GL
{
namespace Vk
class Texture
{
class PipelineLayout : public DeviceObject<PipelineLayout, VkPipelineLayout, VkPipelineLayoutCreateInfo, VK_OBJECT_TYPE_PIPELINE_LAYOUT>
{
friend DeviceObject;
public:
Texture() = default;
Texture(const Texture&) = delete;
Texture(Texture&&) noexcept = default;
inline ~Texture();
public:
PipelineLayout() = default;
PipelineLayout(const PipelineLayout&) = delete;
PipelineLayout(PipelineLayout&&) = default;
~PipelineLayout() = default;
inline bool Create(OpenGLDevice& device);
inline void Destroy();
using DeviceObject::Create;
bool Create(Device& device, VkDescriptorSetLayout layout, VkPipelineLayoutCreateFlags flags = 0);
bool Create(Device& device, UInt32 layoutCount, const VkDescriptorSetLayout* layouts, VkPipelineLayoutCreateFlags flags = 0);
inline void TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border);
inline void TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* data);
inline void TexSubImage2D(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void* data);
PipelineLayout& operator=(const PipelineLayout&) = delete;
PipelineLayout& operator=(PipelineLayout&&) = delete;
Texture& operator=(const Texture&) = delete;
Texture& operator=(Texture&&) noexcept = default;
private:
static inline VkResult CreateHelper(Device& device, const VkPipelineLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineLayout* handle);
static inline void DestroyHelper(Device& device, VkPipelineLayout handle, const VkAllocationCallbacks* allocator);
};
}
private:
const Context& EnsureDeviceContext();
MovablePtr<OpenGLDevice> m_device;
MovableValue<GLuint> m_texture;
};
}
#include <Nazara/OpenGLRenderer/Wrapper/PipelineLayout.inl>
#include <Nazara/OpenGLRenderer/Wrapper/Texture.inl>
#endif // NAZARA_OPENGLRENDERER_VKPIPELINELAYOUT_HPP
#endif

View File

@@ -2,42 +2,82 @@
// This file is part of the "Nazara Engine - OpenGL Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/OpenGLRenderer/Wrapper/PipelineLayout.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Texture.hpp>
#include <cassert>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
namespace Nz::GL
{
namespace Vk
inline Texture::~Texture()
{
inline bool PipelineLayout::Create(Device& device, VkDescriptorSetLayout layout, VkPipelineLayoutCreateFlags flags)
Destroy();
}
inline bool Texture::Create(OpenGLDevice& device)
{
Destroy();
m_device = &device;
const Context& context = EnsureDeviceContext();
context.glGenTextures(1U, &m_texture.Get());
if (!m_texture)
return false; //< TODO: Handle error messages
return true;
}
inline void Texture::Destroy()
{
if (m_texture)
{
return Create(device, 1U, &layout, flags);
const Context& context = EnsureDeviceContext();
context.glDeleteTextures(1U, &m_texture.Get());
m_device->NotifyTextureDestruction(m_texture);
m_texture = 0;
}
}
inline void Texture::TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border)
{
return TexImage2D(level, internalFormat, width, height, border, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
}
inline void Texture::TexImage2D(GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* data)
{
const Context& context = EnsureDeviceContext();
context.BindTexture(TextureTarget::Target2D, m_texture);
context.glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width, height, border, format, type, data);
//< TODO: Handle errors
}
inline void Texture::TexSubImage2D(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void* data)
{
const Context& context = EnsureDeviceContext();
context.BindTexture(TextureTarget::Target2D, m_texture);
context.glTexSubImage2D(GL_TEXTURE_2D, level, xoffset, yoffset, width, height, format, type, data);
//< TODO: Handle errors
}
inline const Context& Texture::EnsureDeviceContext()
{
assert(m_device);
const Context* activeContext = Context::GetCurrentContext();
if (!activeContext || activeContext->GetDevice() != m_device)
{
const Context& referenceContext = m_device->GetReferenceContext();
if (!Context::SetCurrentContext(&referenceContext))
throw std::runtime_error("failed to activate context");
return referenceContext;
}
inline bool PipelineLayout::Create(Device& device, UInt32 layoutCount, const VkDescriptorSetLayout* layouts, VkPipelineLayoutCreateFlags flags)
{
VkPipelineLayoutCreateInfo createInfo = {
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
nullptr,
flags,
layoutCount,
layouts,
0U,
nullptr
};
return Create(device, createInfo);
}
inline VkResult PipelineLayout::CreateHelper(Device& device, const VkPipelineLayoutCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkPipelineLayout* handle)
{
return device.vkCreatePipelineLayout(device, createInfo, allocator, handle);
}
inline void PipelineLayout::DestroyHelper(Device& device, VkPipelineLayout handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroyPipelineLayout(device, handle, allocator);
}
return *activeContext;
}
}