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

@@ -2,34 +2,28 @@
// This file is part of the "Nazara Engine - OpenGL Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#if 0
#include <Nazara/OpenGLRenderer/OpenGLTextureSampler.hpp>
#include <Nazara/OpenGLRenderer/Utils.hpp>
#include <stdexcept>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
{
OpenGLTextureSampler::OpenGLTextureSampler(Vk::Device& device, TextureSamplerInfo samplerInfo)
OpenGLTextureSampler::OpenGLTextureSampler(OpenGLDevice& device, TextureSamplerInfo samplerInfo)
{
VkSamplerCreateInfo createInfo = { VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO };
createInfo.magFilter = ToOpenGL(samplerInfo.magFilter);
createInfo.minFilter = ToOpenGL(samplerInfo.minFilter);
createInfo.addressModeU = ToOpenGL(samplerInfo.wrapModeU);
createInfo.addressModeV = ToOpenGL(samplerInfo.wrapModeV);
createInfo.addressModeW = ToOpenGL(samplerInfo.wrapModeW);
createInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
createInfo.mipmapMode = ToOpenGL(samplerInfo.mipmapMode);
if (!m_sampler.Create(device))
throw std::runtime_error("failed to create sampler object");
if (samplerInfo.anisotropyLevel > 0.f)
{
createInfo.anisotropyEnable = VK_TRUE;
createInfo.maxAnisotropy = samplerInfo.anisotropyLevel;
}
// In OpenGL, min and mipmap sampler are part of the same enum
if (!m_sampler.Create(device, createInfo))
throw std::runtime_error("Failed to create sampler: " + TranslateOpenGLError(m_sampler.GetLastErrorCode()));
m_sampler.SetParameteri(GL_TEXTURE_MIN_FILTER, ToOpenGL(samplerInfo.minFilter, samplerInfo.mipmapMode));
m_sampler.SetParameteri(GL_TEXTURE_MAG_FILTER, ToOpenGL(samplerInfo.magFilter));
m_sampler.SetParameteri(GL_TEXTURE_WRAP_S, ToOpenGL(samplerInfo.wrapModeU));
m_sampler.SetParameteri(GL_TEXTURE_WRAP_T, ToOpenGL(samplerInfo.wrapModeV));
m_sampler.SetParameteri(GL_TEXTURE_WRAP_R, ToOpenGL(samplerInfo.wrapModeW));
if (samplerInfo.anisotropyLevel > 1.f)
m_sampler.SetParameterf(GL_TEXTURE_MAX_ANISOTROPY_EXT, samplerInfo.anisotropyLevel);
}
}
#endif

View File

@@ -22,13 +22,40 @@ namespace Nz::GL
m_device->NotifyContextDestruction(*this);
}
void Context::BindSampler(UInt32 textureUnit, GLuint sampler) const
{
if (textureUnit >= m_state.textureUnits.size())
throw std::runtime_error("unsupported texture unit #" + std::to_string(textureUnit));
auto& unit = m_state.textureUnits[textureUnit];
if (unit.sampler != sampler)
{
if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context");
glBindSampler(textureUnit, sampler);
unit.sampler = sampler;
}
}
void Context::BindTexture(TextureTarget target, GLuint texture) const
{
if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context");
BindTexture(m_state.currentTextureUnit, target, texture);
}
if (m_state.boundTextures[UnderlyingCast(target)] != texture)
void Context::BindTexture(UInt32 textureUnit, TextureTarget target, GLuint texture) const
{
if (textureUnit >= m_state.textureUnits.size())
throw std::runtime_error("unsupported texture unit #" + std::to_string(textureUnit));
auto& unit = m_state.textureUnits[textureUnit];
if (unit.textureTargets[UnderlyingCast(target)] != texture)
{
if (!SetCurrentContext(this))
throw std::runtime_error("failed to activate context");
SetCurrentTextureUnit(textureUnit);
GLenum glTarget;
switch (target)
{
@@ -54,7 +81,7 @@ namespace Nz::GL
glBindTexture(glTarget, texture);
m_state.boundTextures[UnderlyingCast(target)] = texture;
unit.textureTargets[UnderlyingCast(target)] = texture;
}
}
@@ -66,8 +93,6 @@ namespace Nz::GL
return false;
}
m_state.boundTextures.fill(0);
const Loader& loader = GetLoader();
auto LoadSymbol = [&](auto& func, const char* funcName, bool mandatory)
@@ -118,6 +143,14 @@ namespace Nz::GL
});
m_extensionStatus.fill(ExtensionStatus::NotSupported);
// TextureFilterAnisotropic
if (m_supportedExtensions.count("GL_ARB_texture_filter_anisotropic"))
m_extensionStatus[UnderlyingCast(Extension::TextureFilterAnisotropic)] = ExtensionStatus::ARB;
else if (m_supportedExtensions.count("GL_EXT_texture_filter_anisotropic"))
m_extensionStatus[UnderlyingCast(Extension::TextureFilterAnisotropic)] = ExtensionStatus::EXT;
// SpirV
if (m_supportedExtensions.count("GL_ARB_gl_spirv"))
m_extensionStatus[UnderlyingCast(Extension::SpirV)] = ExtensionStatus::ARB;
@@ -162,6 +195,14 @@ namespace Nz::GL
}, this);
}
GLint maxTextureUnits = -1;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
if (maxTextureUnits < 32) //< OpenGL ES 3.0 requires at least 32 textures units
NazaraWarning("GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS is " + std::to_string(maxTextureUnits) + ", >= 32 expected");
assert(maxTextureUnits > 0);
m_state.textureUnits.resize(maxTextureUnits);
return true;
}