OpenGLRenderer: Implement TextureSampler (and texture units)

This commit is contained in:
Lynix
2020-04-26 16:29:31 +02:00
parent cbd81e3abf
commit e9f0b01e02
13 changed files with 244 additions and 66 deletions

View File

@@ -30,8 +30,9 @@ namespace Nz::GL
enum class Extension
{
SpirV,
TextureFilterAnisotropic,
Max = SpirV
Max = TextureFilterAnisotropic
};
enum class ExtensionStatus
@@ -73,7 +74,9 @@ namespace Nz::GL
inline Context(const OpenGLDevice* device);
virtual ~Context();
void BindSampler(UInt32 textureUnit, GLuint sampler) const;
void BindTexture(TextureTarget target, GLuint texture) const;
void BindTexture(UInt32 textureUnit, TextureTarget target, GLuint texture) const;
virtual void EnableVerticalSync(bool enabled) = 0;
@@ -86,8 +89,11 @@ namespace Nz::GL
bool Initialize(const ContextParams& params);
inline void NotifySamplerDestruction(GLuint sampler) const;
inline void NotifyTextureDestruction(GLuint texture) const;
inline void SetCurrentTextureUnit(UInt32 textureUnit) const;
virtual void SwapBuffers() = 0;
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) sig name = nullptr;
@@ -113,7 +119,14 @@ namespace Nz::GL
struct State
{
std::array<GLuint, UnderlyingCast(TextureTarget::Max) + 1> boundTextures;
struct TextureUnit
{
GLuint sampler = 0;
std::array<GLuint, UnderlyingCast(TextureTarget::Max) + 1> textureTargets = { 0 };
};
std::vector<TextureUnit> textureUnits;
UInt32 currentTextureUnit = 0;
};
std::array<ExtensionStatus, UnderlyingCast(Extension::Max) + 1> m_extensionStatus;

View File

@@ -37,12 +37,33 @@ namespace Nz::GL
return m_supportedExtensions.find(extension) != m_supportedExtensions.end();
}
inline void Context::NotifySamplerDestruction(GLuint sampler) const
{
for (auto& unit : m_state.textureUnits)
{
if (unit.sampler == sampler)
unit.sampler = 0;
}
}
inline void Context::NotifyTextureDestruction(GLuint texture) const
{
for (GLuint& boundTexture : m_state.boundTextures)
for (auto& unit : m_state.textureUnits)
{
if (boundTexture == texture)
boundTexture = 0;
for (GLuint& boundTexture : unit.textureTargets)
{
if (boundTexture == texture)
boundTexture = 0;
}
}
}
inline void Context::SetCurrentTextureUnit(UInt32 textureUnit) const
{
if (m_state.currentTextureUnit != textureUnit)
{
glActiveTexture(GL_TEXTURE0 + textureUnit);
m_state.currentTextureUnit = textureUnit;
}
}
}

View File

@@ -110,7 +110,9 @@ typedef void (GL_APIENTRYP PFNGLSPECIALIZESHADERARBPROC) (GLuint shader, const G
cb(glReadPixels, PFNGLREADPIXELSPROC) \
cb(glRenderbufferStorage, PFNGLRENDERBUFFERSTORAGEPROC) \
cb(glSamplerParameterf, PFNGLSAMPLERPARAMETERFPROC) \
cb(glSamplerParameterfv, PFNGLSAMPLERPARAMETERFVPROC) \
cb(glSamplerParameteri, PFNGLSAMPLERPARAMETERIPROC) \
cb(glSamplerParameteriv, PFNGLSAMPLERPARAMETERIVPROC) \
cb(glScissor, PFNGLSCISSORPROC) \
cb(glShaderBinary, PFNGLSHADERBINARYPROC) \
cb(glShaderSource, PFNGLSHADERSOURCEPROC) \

View File

@@ -4,36 +4,40 @@
#pragma once
#ifndef NAZARA_OPENGLRENDERER_VKSAMPLER_HPP
#define NAZARA_OPENGLRENDERER_VKSAMPLER_HPP
#ifndef NAZARA_OPENGLRENDERER_GLSAMPLER_HPP
#define NAZARA_OPENGLRENDERER_GLSAMPLER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/MovableValue.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/DeviceObject.hpp>
namespace Nz
namespace Nz::GL
{
namespace Vk
class Sampler : public DeviceObject<Sampler, GL_SAMPLER>
{
class Sampler : public DeviceObject<Sampler, VkSampler, VkSamplerCreateInfo, VK_OBJECT_TYPE_SAMPLER>
{
friend DeviceObject;
friend DeviceObject;
public:
Sampler() = default;
Sampler(const Sampler&) = delete;
Sampler(Sampler&&) = default;
~Sampler() = default;
public:
Sampler() = default;
Sampler(const Sampler&) = delete;
Sampler(Sampler&&) noexcept = default;
~Sampler() = default;
Sampler& operator=(const Sampler&) = delete;
Sampler& operator=(Sampler&&) = delete;
inline void SetParameterf(GLenum pname, GLfloat param);
inline void SetParameteri(GLenum pname, GLint param);
inline void SetParameterfv(GLenum pname, const GLfloat* param);
inline void SetParameteriv(GLenum pname, const GLint* param);
private:
static inline VkResult CreateHelper(Device& device, const VkSamplerCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSampler* handle);
static inline void DestroyHelper(Device& device, VkSampler handle, const VkAllocationCallbacks* allocator);
};
}
Sampler& operator=(const Sampler&) = delete;
Sampler& operator=(Sampler&&) noexcept = default;
private:
static inline GLuint CreateHelper(OpenGLDevice& device, const Context& context);
static inline void DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId);
};
}
#include <Nazara/OpenGLRenderer/Wrapper/Sampler.inl>
#endif // NAZARA_OPENGLRENDERER_VKSAMPLER_HPP
#endif

View File

@@ -5,19 +5,53 @@
#include <Nazara/OpenGLRenderer/Wrapper/Sampler.hpp>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
namespace Nz::GL
{
namespace Vk
inline void Sampler::SetParameterf(GLenum pname, GLfloat param)
{
inline VkResult Sampler::CreateHelper(Device& device, const VkSamplerCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkSampler* handle)
{
return device.vkCreateSampler(device, createInfo, allocator, handle);
}
assert(m_objectId);
inline void Sampler::DestroyHelper(Device& device, VkSampler handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroySampler(device, handle, allocator);
}
const Context& context = EnsureDeviceContext();
context.glSamplerParameterf(m_objectId, pname, param);
}
inline void Sampler::SetParameteri(GLenum pname, GLint param)
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glSamplerParameteri(m_objectId, pname, param);
}
inline void Sampler::SetParameterfv(GLenum pname, const GLfloat* param)
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glSamplerParameterfv(m_objectId, pname, param);
}
inline void Sampler::SetParameteriv(GLenum pname, const GLint* param)
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glSamplerParameteriv(m_objectId, pname, param);
}
inline GLuint Sampler::CreateHelper(OpenGLDevice& device, const Context& context)
{
GLuint sampler = 0;
context.glGenSamplers(1U, &sampler);
return sampler;
}
inline void Sampler::DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId)
{
context.glDeleteSamplers(1U, &objectId);
device.NotifySamplerDestruction(objectId);
}
}