OpenGLRenderer: Fix explicit texture/block binding (fixes GLSL ES 3.0 support)

This commit is contained in:
SirLynix 2022-08-12 23:01:58 +02:00
parent c4a3b3f18a
commit 099528758c
6 changed files with 67 additions and 9 deletions

View File

@ -22,17 +22,28 @@ namespace Nz
class NAZARA_OPENGLRENDERER_API OpenGLShaderModule : public ShaderModule class NAZARA_OPENGLRENDERER_API OpenGLShaderModule : public ShaderModule
{ {
public: public:
struct ExplicitBinding;
OpenGLShaderModule(OpenGLDevice& device, nzsl::ShaderStageTypeFlags shaderStages, const nzsl::Ast::Module& shaderModule, const nzsl::ShaderWriter::States& states = {}); OpenGLShaderModule(OpenGLDevice& device, nzsl::ShaderStageTypeFlags shaderStages, const nzsl::Ast::Module& shaderModule, const nzsl::ShaderWriter::States& states = {});
OpenGLShaderModule(OpenGLDevice& device, nzsl::ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const nzsl::ShaderWriter::States& states = {}); OpenGLShaderModule(OpenGLDevice& device, nzsl::ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const nzsl::ShaderWriter::States& states = {});
OpenGLShaderModule(const OpenGLShaderModule&) = delete; OpenGLShaderModule(const OpenGLShaderModule&) = delete;
OpenGLShaderModule(OpenGLShaderModule&&) noexcept = default; OpenGLShaderModule(OpenGLShaderModule&&) noexcept = default;
~OpenGLShaderModule() = default; ~OpenGLShaderModule() = default;
nzsl::ShaderStageTypeFlags Attach(GL::Program& program, const nzsl::GlslWriter::BindingMapping& bindingMapping) const; nzsl::ShaderStageTypeFlags Attach(GL::Program& program, const nzsl::GlslWriter::BindingMapping& bindingMapping, std::vector<ExplicitBinding>* explicitBindings) const;
inline const std::vector<ExplicitBinding>& GetExplicitBindings() const;
OpenGLShaderModule& operator=(const OpenGLShaderModule&) = delete; OpenGLShaderModule& operator=(const OpenGLShaderModule&) = delete;
OpenGLShaderModule& operator=(OpenGLShaderModule&&) noexcept = default; OpenGLShaderModule& operator=(OpenGLShaderModule&&) noexcept = default;
struct ExplicitBinding
{
std::string name;
unsigned int binding;
bool isBlock;
};
private: private:
void Create(OpenGLDevice& device, nzsl::ShaderStageTypeFlags shaderStages, const nzsl::Ast::Module& shaderModule, const nzsl::ShaderWriter::States& states); void Create(OpenGLDevice& device, nzsl::ShaderStageTypeFlags shaderStages, const nzsl::Ast::Module& shaderModule, const nzsl::ShaderWriter::States& states);
@ -56,6 +67,7 @@ namespace Nz
OpenGLDevice& m_device; OpenGLDevice& m_device;
nzsl::ShaderWriter::States m_states; nzsl::ShaderWriter::States m_states;
std::vector<ExplicitBinding> m_explicitBindings;
std::vector<Shader> m_shaders; std::vector<Shader> m_shaders;
}; };
} }

View File

@ -7,6 +7,10 @@
namespace Nz namespace Nz
{ {
inline auto OpenGLShaderModule::GetExplicitBindings() const -> const std::vector<ExplicitBinding>&
{
return m_explicitBindings;
}
} }
#include <Nazara/OpenGLRenderer/DebugOff.hpp> #include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@ -44,6 +44,7 @@ namespace Nz::GL
inline void Link(); inline void Link();
inline void Uniform(GLint uniformLocation, float value) const; inline void Uniform(GLint uniformLocation, float value) const;
inline void Uniform(GLint uniformLocation, int value) const;
inline void UniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) const; inline void UniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) const;
Program& operator=(const Program&) = delete; Program& operator=(const Program&) = delete;

View File

@ -203,6 +203,15 @@ namespace Nz::GL
context.glUniform1f(uniformLocation, value); context.glUniform1f(uniformLocation, value);
} }
inline void Program::Uniform(GLint uniformLocation, int value) const
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.BindProgram(m_objectId);
context.glUniform1i(uniformLocation, value);
}
inline void Program::UniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) const inline void Program::UniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) const
{ {
assert(m_objectId); assert(m_objectId);

View File

@ -34,11 +34,12 @@ namespace Nz
activeContext->UpdateStates(m_pipelineInfo, false); activeContext->UpdateStates(m_pipelineInfo, false);
nzsl::ShaderStageTypeFlags stageFlags; nzsl::ShaderStageTypeFlags stageFlags;
std::vector<OpenGLShaderModule::ExplicitBinding> explicitBindings;
for (const auto& shaderModulePtr : m_pipelineInfo.shaderModules) for (const auto& shaderModulePtr : m_pipelineInfo.shaderModules)
{ {
OpenGLShaderModule& shaderModule = static_cast<OpenGLShaderModule&>(*shaderModulePtr); OpenGLShaderModule& shaderModule = static_cast<OpenGLShaderModule&>(*shaderModulePtr);
stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping()); stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping(), &explicitBindings);
} }
// OpenGL ES programs must have both vertex and fragment shaders or a compute shader or a mesh and fragment shader. // OpenGL ES programs must have both vertex and fragment shaders or a compute shader or a mesh and fragment shader.
@ -53,7 +54,7 @@ namespace Nz
dummyModule.rootNode->statements.push_back(nzsl::ShaderBuilder::DeclareFunction(stage, "main", {}, {})); dummyModule.rootNode->statements.push_back(nzsl::ShaderBuilder::DeclareFunction(stage, "main", {}, {}));
OpenGLShaderModule shaderModule(device, stage, dummyModule); OpenGLShaderModule shaderModule(device, stage, dummyModule);
stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping()); stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping(), &explicitBindings);
} }
}; };
@ -70,6 +71,26 @@ namespace Nz
m_flipYUniformLocation = m_program.GetUniformLocation(nzsl::GlslWriter::GetFlipYUniformName().data()); m_flipYUniformLocation = m_program.GetUniformLocation(nzsl::GlslWriter::GetFlipYUniformName().data());
if (m_flipYUniformLocation != -1) if (m_flipYUniformLocation != -1)
m_program.Uniform(m_flipYUniformLocation, 1.f); m_program.Uniform(m_flipYUniformLocation, 1.f);
for (const auto& explicitBinding : explicitBindings)
{
if (explicitBinding.isBlock)
{
GLuint blockIndex = m_program.GetUniformBlockIndex(explicitBinding.name);
if (blockIndex == GL_INVALID_INDEX)
continue;
m_program.UniformBlockBinding(blockIndex, explicitBinding.binding);
}
else
{
int location = m_program.GetUniformLocation(explicitBinding.name);
if (location == -1)
continue;
m_program.Uniform(location, static_cast<int>(explicitBinding.binding)); //< FIXME: Use SafeCast
}
}
} }
void OpenGLRenderPipeline::Apply(const GL::Context& context, bool flipViewport) const void OpenGLRenderPipeline::Apply(const GL::Context& context, bool flipViewport) const

View File

@ -83,7 +83,7 @@ namespace Nz
} }
} }
nzsl::ShaderStageTypeFlags OpenGLShaderModule::Attach(GL::Program& program, const nzsl::GlslWriter::BindingMapping& bindingMapping) const nzsl::ShaderStageTypeFlags OpenGLShaderModule::Attach(GL::Program& program, const nzsl::GlslWriter::BindingMapping& bindingMapping, std::vector<ExplicitBinding>* explicitBindings) const
{ {
const auto& context = m_device.GetReferenceContext(); const auto& context = m_device.GetReferenceContext();
const auto& contextParams = context.GetParams(); const auto& contextParams = context.GetParams();
@ -120,13 +120,24 @@ namespace Nz
nzsl::GlslWriter::Output output = writer.Generate(shaderEntry.stage, *arg.ast, bindingMapping, m_states); nzsl::GlslWriter::Output output = writer.Generate(shaderEntry.stage, *arg.ast, bindingMapping, m_states);
shader.SetSource(output.code.data(), GLint(output.code.size())); shader.SetSource(output.code.data(), GLint(output.code.size()));
if (explicitBindings)
{
explicitBindings->reserve(explicitBindings->size() + output.explicitTextureBinding.size() + output.explicitUniformBlockBinding.size());
for (const auto& [name, bindingPoint] : output.explicitTextureBinding)
{
auto& explicitBinding = explicitBindings->emplace_back();
explicitBinding.name = name;
explicitBinding.binding = bindingPoint;
explicitBinding.isBlock = false;
}
for (const auto& [name, bindingPoint] : output.explicitUniformBlockBinding) for (const auto& [name, bindingPoint] : output.explicitUniformBlockBinding)
{ {
GLuint blockIndex = program.GetUniformBlockIndex(name); auto& explicitBinding = explicitBindings->emplace_back();
if (blockIndex == GL_INVALID_INDEX) explicitBinding.name = name;
continue; explicitBinding.binding = bindingPoint;
explicitBinding.isBlock = true;
program.UniformBlockBinding(blockIndex, bindingPoint); }
} }
} }
else else