OpenGL: Handle Spir-V
This commit is contained in:
parent
f63d045676
commit
bd6924d66d
|
|
@ -19,13 +19,11 @@ namespace Nz
|
|||
class NAZARA_OPENGLRENDERER_API OpenGLShaderStage : public ShaderStageImpl
|
||||
{
|
||||
public:
|
||||
OpenGLShaderStage() = default;
|
||||
OpenGLShaderStage(OpenGLDevice& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize);
|
||||
OpenGLShaderStage(const OpenGLShaderStage&) = delete;
|
||||
OpenGLShaderStage(OpenGLShaderStage&&) noexcept = default;
|
||||
~OpenGLShaderStage() = default;
|
||||
|
||||
bool Create(OpenGLDevice& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize);
|
||||
|
||||
OpenGLShaderStage& operator=(const OpenGLShaderStage&) = delete;
|
||||
OpenGLShaderStage& operator=(OpenGLShaderStage&&) noexcept = default;
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@
|
|||
#define NAZARA_OPENGLRENDERER_CONTEXTIMPL_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/CoreFunctions.hpp>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
|
|
@ -20,6 +22,22 @@ namespace Nz::GL
|
|||
OpenGL_ES
|
||||
};
|
||||
|
||||
enum class Extension
|
||||
{
|
||||
SpirV,
|
||||
|
||||
Max = SpirV
|
||||
};
|
||||
|
||||
enum class ExtensionStatus
|
||||
{
|
||||
NotSupported,
|
||||
|
||||
ARB,
|
||||
EXT,
|
||||
KHR
|
||||
};
|
||||
|
||||
struct ContextParams
|
||||
{
|
||||
ContextType type = ContextType::OpenGL_ES;
|
||||
|
|
@ -44,8 +62,10 @@ namespace Nz::GL
|
|||
|
||||
virtual void EnableVerticalSync(bool enabled) = 0;
|
||||
|
||||
inline ExtensionStatus GetExtensionStatus(Extension extension) const;
|
||||
inline const ContextParams& GetParams() const;
|
||||
|
||||
inline bool IsExtensionSupported(Extension extension) const;
|
||||
inline bool IsExtensionSupported(const std::string& extension) const;
|
||||
|
||||
bool Initialize(const ContextParams& params);
|
||||
|
|
@ -53,7 +73,7 @@ namespace Nz::GL
|
|||
virtual void SwapBuffers() = 0;
|
||||
|
||||
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) sig name = nullptr;
|
||||
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC)
|
||||
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_FUNC)
|
||||
#undef NAZARA_OPENGLRENDERER_FUNC
|
||||
|
||||
static const Context* GetCurrentContext();
|
||||
|
|
@ -73,6 +93,7 @@ namespace Nz::GL
|
|||
private:
|
||||
void GL_APIENTRY HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message) const;
|
||||
|
||||
std::array<ExtensionStatus, UnderlyingCast(Extension::Max) + 1> m_extensionStatus;
|
||||
std::unordered_set<std::string> m_supportedExtensions;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,11 +7,22 @@
|
|||
|
||||
namespace Nz::GL
|
||||
{
|
||||
|
||||
inline ExtensionStatus Context::GetExtensionStatus(Extension extension) const
|
||||
{
|
||||
return m_extensionStatus[UnderlyingCast(extension)];
|
||||
}
|
||||
|
||||
inline const ContextParams& Context::GetParams() const
|
||||
{
|
||||
return m_params;
|
||||
}
|
||||
|
||||
inline bool Context::IsExtensionSupported(Extension extension) const
|
||||
{
|
||||
return GetExtensionStatus(extension) != ExtensionStatus::NotSupported;
|
||||
}
|
||||
|
||||
inline bool Context::IsExtensionSupported(const std::string& extension) const
|
||||
{
|
||||
return m_supportedExtensions.find(extension) != m_supportedExtensions.end();
|
||||
|
|
|
|||
|
|
@ -11,6 +11,10 @@
|
|||
#include <GLES3/gl32.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
// Define some OpenGL (not ES) extensions
|
||||
#define GL_SHADER_BINARY_FORMAT_SPIR_V_ARB 0x9551
|
||||
#define GL_SPIR_V_BINARY_ARB 0x9552
|
||||
typedef void (GL_APIENTRYP PFNGLSPECIALIZESHADERARBPROC) (GLuint shader, const GLchar* pEntryPoint, GLuint numSpecializationConstants, const GLuint* pConstantIndex, const GLuint* pConstantValue);
|
||||
|
||||
// OpenGL core
|
||||
#define NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(cb, extCb) \
|
||||
|
|
@ -143,5 +147,7 @@
|
|||
cb(glViewport, PFNGLVIEWPORTPROC) \
|
||||
\
|
||||
extCb(glDebugMessageCallback, PFNGLDEBUGMESSAGECALLBACKPROC) \
|
||||
\
|
||||
extCb(glSpecializeShaderARB, PFNGLSPECIALIZESHADERARBPROC) \
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -21,14 +21,19 @@ namespace Nz::GL
|
|||
Shader(Shader&&) noexcept = default;
|
||||
inline ~Shader();
|
||||
|
||||
inline bool Compile(std::string* error = nullptr);
|
||||
inline void Compile();
|
||||
|
||||
inline bool Create(OpenGLDevice& device, GLenum type);
|
||||
inline void Destroy();
|
||||
|
||||
inline bool GetCompilationStatus(std::string* error = nullptr);
|
||||
|
||||
inline void SetBinarySource(GLenum binaryFormat, const void* binary, GLsizei length);
|
||||
inline void SetSource(const char* source, GLint length);
|
||||
|
||||
// GL_ARB_gl_spirv
|
||||
inline void SpecializeShader(const GLchar* pEntryPoint, GLuint numSpecializationConstants, const GLuint* pConstantIndex, const GLuint* pConstantValue);
|
||||
|
||||
Shader& operator=(const Shader&) = delete;
|
||||
Shader& operator=(Shader&&) noexcept = default;
|
||||
|
||||
|
|
|
|||
|
|
@ -13,11 +13,37 @@ namespace Nz::GL
|
|||
Destroy();
|
||||
}
|
||||
|
||||
inline bool Shader::Compile(std::string* error)
|
||||
inline void Shader::Compile()
|
||||
{
|
||||
assert(m_shader);
|
||||
m_device->GetReferenceContext().glCompileShader(m_shader);
|
||||
}
|
||||
|
||||
inline bool Shader::Create(OpenGLDevice& device, GLenum type)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
m_device = &device;
|
||||
m_shader = device.GetReferenceContext().glCreateShader(type);
|
||||
if (!m_shader)
|
||||
return false; //< TODO: Handle error messages
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void Shader::Destroy()
|
||||
{
|
||||
if (m_shader)
|
||||
{
|
||||
m_device->GetReferenceContext().glDeleteShader(m_shader);
|
||||
m_shader = 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool Shader::GetCompilationStatus(std::string* error)
|
||||
{
|
||||
assert(m_shader);
|
||||
const GL::Context& context = m_device->GetReferenceContext();
|
||||
context.glCompileShader(m_shader);
|
||||
|
||||
GLint success;
|
||||
context.glGetShaderiv(m_shader, GL_COMPILE_STATUS, &success);
|
||||
|
|
@ -43,27 +69,6 @@ namespace Nz::GL
|
|||
return true;
|
||||
}
|
||||
|
||||
inline bool Shader::Create(OpenGLDevice& device, GLenum type)
|
||||
{
|
||||
Destroy();
|
||||
|
||||
m_device = &device;
|
||||
m_shader = device.GetReferenceContext().glCreateShader(type);
|
||||
if (!m_shader)
|
||||
return false; //< TODO: Handle error messages
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void Shader::Destroy()
|
||||
{
|
||||
if (m_shader)
|
||||
{
|
||||
m_device->GetReferenceContext().glDeleteShader(m_shader);
|
||||
m_shader = 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline void Shader::SetBinarySource(GLenum binaryFormat, const void* binary, GLsizei length)
|
||||
{
|
||||
assert(m_shader);
|
||||
|
|
@ -77,6 +82,15 @@ namespace Nz::GL
|
|||
|
||||
m_device->GetReferenceContext().glShaderSource(m_shader, 1U, &source, &length);
|
||||
}
|
||||
|
||||
inline void Shader::SpecializeShader(const GLchar* pEntryPoint, GLuint numSpecializationConstants, const GLuint* pConstantIndex, const GLuint* pConstantValue)
|
||||
{
|
||||
assert(m_shader);
|
||||
const GL::Context& context = m_device->GetReferenceContext();
|
||||
assert(context.glSpecializeShaderARB);
|
||||
|
||||
context.glSpecializeShaderARB(m_shader, pEntryPoint, numSpecializationConstants, pConstantIndex, pConstantValue);
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/OpenGLRenderer/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -53,11 +53,7 @@ namespace Nz
|
|||
|
||||
std::shared_ptr<ShaderStageImpl> OpenGLDevice::InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
{
|
||||
auto shaderStage = std::make_shared<OpenGLShaderStage>();
|
||||
if (!shaderStage->Create(*this, type, lang, source, sourceSize))
|
||||
return {};
|
||||
|
||||
return shaderStage;
|
||||
return std::make_shared<OpenGLShaderStage>(*this, type, lang, source, sourceSize);
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> OpenGLDevice::InstantiateTexture(const TextureInfo& params)
|
||||
|
|
|
|||
|
|
@ -5,47 +5,39 @@
|
|||
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Utils.hpp>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
bool OpenGLShaderStage::Create(OpenGLDevice& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
OpenGLShaderStage::OpenGLShaderStage(OpenGLDevice& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
{
|
||||
if (!m_shader.Create(device, ToOpenGL(type)))
|
||||
return false; //< TODO: Handle error message
|
||||
throw std::runtime_error("failed to create shader"); //< TODO: Handle error message
|
||||
|
||||
switch (lang)
|
||||
{
|
||||
case ShaderLanguage::GLSL:
|
||||
m_shader.SetSource(reinterpret_cast<const char*>(source), GLint(sourceSize));
|
||||
m_shader.Compile();
|
||||
break;
|
||||
|
||||
case ShaderLanguage::SpirV:
|
||||
{
|
||||
if (!device.GetReferenceContext().IsExtensionSupported("GL_ARB_gl_spirv"))
|
||||
{
|
||||
NazaraError("Spir-V is not supported");
|
||||
return false;
|
||||
}
|
||||
if (!device.GetReferenceContext().IsExtensionSupported(GL::Extension::SpirV))
|
||||
throw std::runtime_error("SpirV is not supported by this OpenGL implementation");
|
||||
|
||||
constexpr GLenum SHADER_BINARY_FORMAT_SPIR_V = 0x9551;
|
||||
|
||||
m_shader.SetBinarySource(SHADER_BINARY_FORMAT_SPIR_V, source, GLsizei(sourceSize));
|
||||
m_shader.SetBinarySource(GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, source, GLsizei(sourceSize));
|
||||
m_shader.SpecializeShader("main", 0U, nullptr, nullptr);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
NazaraError("Unsupported shader language");
|
||||
return false;
|
||||
throw std::runtime_error("Unsupported shader language");
|
||||
}
|
||||
|
||||
std::string errorLog;
|
||||
if (!m_shader.Compile(&errorLog))
|
||||
{
|
||||
NazaraError("Failed to compile shader: " + errorLog);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (!m_shader.GetCompilationStatus(&errorLog))
|
||||
throw std::runtime_error("Failed to compile shader: " + errorLog);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@
|
|||
|
||||
namespace Nz::GL
|
||||
{
|
||||
Context::~Context() = default;
|
||||
thread_local const Context* s_currentContext = nullptr;
|
||||
|
||||
|
||||
bool Context::Initialize(const ContextParams& params)
|
||||
{
|
||||
|
|
@ -70,6 +71,10 @@ namespace Nz::GL
|
|||
return true;
|
||||
});
|
||||
|
||||
m_extensionStatus.fill(ExtensionStatus::NotSupported);
|
||||
if (m_supportedExtensions.count("GL_ARB_gl_spirv"))
|
||||
m_extensionStatus[UnderlyingCast(Extension::SpirV)] = ExtensionStatus::ARB;
|
||||
|
||||
#define NAZARA_OPENGLRENDERER_FUNC(name, sig)
|
||||
#define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) LoadSymbol(name, #name, false);
|
||||
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC)
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue