Uses core GL functions for GLSL shaders.

Tested By: nwtour, Stan
Differential Revision: https://code.wildfiregames.com/D4416
This was SVN commit r26175.
This commit is contained in:
vladislavbelov
2022-01-06 11:41:04 +00:00
parent cee0ce48eb
commit 912202ff0c
5 changed files with 53 additions and 47 deletions
+31 -31
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games. /* Copyright (C) 2022 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@@ -270,8 +270,8 @@ public:
m_VertexAttribs(vertexAttribs) m_VertexAttribs(vertexAttribs)
{ {
m_Program = 0; m_Program = 0;
m_VertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER); m_VertexShader = glCreateShader(GL_VERTEX_SHADER);
m_FragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER); m_FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
m_FileDependencies = {m_VertexFile, m_FragmentFile}; m_FileDependencies = {m_VertexFile, m_FragmentFile};
} }
@@ -291,9 +291,9 @@ public:
const char* code_string = code.c_str(); const char* code_string = code.c_str();
GLint code_length = code.length(); GLint code_length = code.length();
glShaderSourceARB(shader, 1, &code_string, &code_length); glShaderSource(shader, 1, &code_string, &code_length);
glCompileShaderARB(shader); glCompileShader(shader);
GLint ok = 0; GLint ok = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok); glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
@@ -329,20 +329,20 @@ public:
TIMER_ACCRUE(tc_ShaderGLSLLink); TIMER_ACCRUE(tc_ShaderGLSLLink);
ENSURE(!m_Program); ENSURE(!m_Program);
m_Program = glCreateProgramObjectARB(); m_Program = glCreateProgram();
glAttachObjectARB(m_Program, m_VertexShader); glAttachShader(m_Program, m_VertexShader);
ogl_WarnIfError(); ogl_WarnIfError();
glAttachObjectARB(m_Program, m_FragmentShader); glAttachShader(m_Program, m_FragmentShader);
ogl_WarnIfError(); ogl_WarnIfError();
// Set up the attribute bindings explicitly, since apparently drivers // Set up the attribute bindings explicitly, since apparently drivers
// don't always pick the most efficient bindings automatically, // don't always pick the most efficient bindings automatically,
// and also this lets us hardcode indexes into VertexPointer etc // and also this lets us hardcode indexes into VertexPointer etc
for (std::map<CStrIntern, int>::iterator it = m_VertexAttribs.begin(); it != m_VertexAttribs.end(); ++it) for (std::map<CStrIntern, int>::iterator it = m_VertexAttribs.begin(); it != m_VertexAttribs.end(); ++it)
glBindAttribLocationARB(m_Program, it->second, it->first.c_str()); glBindAttribLocation(m_Program, it->second, it->first.c_str());
glLinkProgramARB(m_Program); glLinkProgram(m_Program);
GLint ok = 0; GLint ok = 0;
glGetProgramiv(m_Program, GL_LINK_STATUS, &ok); glGetProgramiv(m_Program, GL_LINK_STATUS, &ok);
@@ -387,10 +387,10 @@ public:
GLsizei nameLength = 0; GLsizei nameLength = 0;
GLint size = 0; GLint size = 0;
GLenum type = 0; GLenum type = 0;
glGetActiveUniformARB(m_Program, i, ARRAY_SIZE(name), &nameLength, &size, &type, name); glGetActiveUniform(m_Program, i, ARRAY_SIZE(name), &nameLength, &size, &type, name);
ogl_WarnIfError(); ogl_WarnIfError();
GLint loc = glGetUniformLocationARB(m_Program, name); GLint loc = glGetUniformLocation(m_Program, name);
CStrIntern nameIntern(name); CStrIntern nameIntern(name);
m_Uniforms[nameIntern] = std::make_pair(loc, type); m_Uniforms[nameIntern] = std::make_pair(loc, type);
@@ -406,7 +406,7 @@ public:
int unit = (int)m_Samplers.size(); int unit = (int)m_Samplers.size();
m_Samplers[nameIntern].first = (type == GL_SAMPLER_CUBE ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D); m_Samplers[nameIntern].first = (type == GL_SAMPLER_CUBE ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D);
m_Samplers[nameIntern].second = unit; m_Samplers[nameIntern].second = unit;
glUniform1iARB(loc, unit); // link uniform to unit glUniform1i(loc, unit); // link uniform to unit
ogl_WarnIfError(); ogl_WarnIfError();
} }
} }
@@ -502,18 +502,18 @@ public:
void Bind() override void Bind() override
{ {
glUseProgramObjectARB(m_Program); glUseProgram(m_Program);
for (std::map<CStrIntern, int>::iterator it = m_VertexAttribs.begin(); it != m_VertexAttribs.end(); ++it) for (std::map<CStrIntern, int>::iterator it = m_VertexAttribs.begin(); it != m_VertexAttribs.end(); ++it)
glEnableVertexAttribArrayARB(it->second); glEnableVertexAttribArray(it->second);
} }
void Unbind() override void Unbind() override
{ {
glUseProgramObjectARB(0); glUseProgram(0);
for (std::map<CStrIntern, int>::iterator it = m_VertexAttribs.begin(); it != m_VertexAttribs.end(); ++it) for (std::map<CStrIntern, int>::iterator it = m_VertexAttribs.begin(); it != m_VertexAttribs.end(); ++it)
glDisableVertexAttribArrayARB(it->second); glDisableVertexAttribArray(it->second);
// TODO: should unbind textures, probably // TODO: should unbind textures, probably
} }
@@ -533,7 +533,7 @@ public:
if (it == m_Samplers.end()) if (it == m_Samplers.end())
return; return;
glActiveTextureARB(GL_TEXTURE0 + it->second.second); glActiveTexture(GL_TEXTURE0 + it->second.second);
glBindTexture(it->second.first, tex); glBindTexture(it->second.first, tex);
} }
@@ -542,7 +542,7 @@ public:
if (id.second == -1) if (id.second == -1)
return; return;
glActiveTextureARB(GL_TEXTURE0 + id.second); glActiveTexture(GL_TEXTURE0 + id.second);
glBindTexture(id.first, tex); glBindTexture(id.first, tex);
} }
@@ -560,13 +560,13 @@ public:
if (id.first != -1) if (id.first != -1)
{ {
if (id.second == GL_FLOAT) if (id.second == GL_FLOAT)
glUniform1fARB(id.first, v0); glUniform1f(id.first, v0);
else if (id.second == GL_FLOAT_VEC2) else if (id.second == GL_FLOAT_VEC2)
glUniform2fARB(id.first, v0, v1); glUniform2f(id.first, v0, v1);
else if (id.second == GL_FLOAT_VEC3) else if (id.second == GL_FLOAT_VEC3)
glUniform3fARB(id.first, v0, v1, v2); glUniform3f(id.first, v0, v1, v2);
else if (id.second == GL_FLOAT_VEC4) else if (id.second == GL_FLOAT_VEC4)
glUniform4fARB(id.first, v0, v1, v2, v3); glUniform4f(id.first, v0, v1, v2, v3);
else else
LOGERROR("CShaderProgramGLSL::Uniform(): Invalid uniform type (expected float, vec2, vec3, vec4)"); LOGERROR("CShaderProgramGLSL::Uniform(): Invalid uniform type (expected float, vec2, vec3, vec4)");
} }
@@ -577,7 +577,7 @@ public:
if (id.first != -1) if (id.first != -1)
{ {
if (id.second == GL_FLOAT_MAT4) if (id.second == GL_FLOAT_MAT4)
glUniformMatrix4fvARB(id.first, 1, GL_FALSE, &v._11); glUniformMatrix4fv(id.first, 1, GL_FALSE, &v._11);
else else
LOGERROR("CShaderProgramGLSL::Uniform(): Invalid uniform type (expected mat4)"); LOGERROR("CShaderProgramGLSL::Uniform(): Invalid uniform type (expected mat4)");
} }
@@ -588,7 +588,7 @@ public:
if (id.first != -1) if (id.first != -1)
{ {
if (id.second == GL_FLOAT_MAT4) if (id.second == GL_FLOAT_MAT4)
glUniformMatrix4fvARB(id.first, count, GL_FALSE, &v->_11); glUniformMatrix4fv(id.first, count, GL_FALSE, &v->_11);
else else
LOGERROR("CShaderProgramGLSL::Uniform(): Invalid uniform type (expected mat4)"); LOGERROR("CShaderProgramGLSL::Uniform(): Invalid uniform type (expected mat4)");
} }
@@ -599,7 +599,7 @@ public:
if (id.first != -1) if (id.first != -1)
{ {
if (id.second == GL_FLOAT) if (id.second == GL_FLOAT)
glUniform1fvARB(id.first, count, v); glUniform1fv(id.first, count, v);
else else
LOGERROR("CShaderProgramGLSL::Uniform(): Invalid uniform type (expected float)"); LOGERROR("CShaderProgramGLSL::Uniform(): Invalid uniform type (expected float)");
} }
@@ -610,25 +610,25 @@ public:
void VertexPointer(GLint size, GLenum type, GLsizei stride, const void* pointer) override void VertexPointer(GLint size, GLenum type, GLsizei stride, const void* pointer) override
{ {
glVertexAttribPointerARB(0, size, type, GL_FALSE, stride, pointer); glVertexAttribPointer(0, size, type, GL_FALSE, stride, pointer);
m_ValidStreams |= STREAM_POS; m_ValidStreams |= STREAM_POS;
} }
void NormalPointer(GLenum type, GLsizei stride, const void* pointer) override void NormalPointer(GLenum type, GLsizei stride, const void* pointer) override
{ {
glVertexAttribPointerARB(2, 3, type, (type == GL_FLOAT ? GL_FALSE : GL_TRUE), stride, pointer); glVertexAttribPointer(2, 3, type, (type == GL_FLOAT ? GL_FALSE : GL_TRUE), stride, pointer);
m_ValidStreams |= STREAM_NORMAL; m_ValidStreams |= STREAM_NORMAL;
} }
void ColorPointer(GLint size, GLenum type, GLsizei stride, const void* pointer) override void ColorPointer(GLint size, GLenum type, GLsizei stride, const void* pointer) override
{ {
glVertexAttribPointerARB(3, size, type, (type == GL_FLOAT ? GL_FALSE : GL_TRUE), stride, pointer); glVertexAttribPointer(3, size, type, (type == GL_FLOAT ? GL_FALSE : GL_TRUE), stride, pointer);
m_ValidStreams |= STREAM_COLOR; m_ValidStreams |= STREAM_COLOR;
} }
void TexCoordPointer(GLenum texture, GLint size, GLenum type, GLsizei stride, const void* pointer) override void TexCoordPointer(GLenum texture, GLint size, GLenum type, GLsizei stride, const void* pointer) override
{ {
glVertexAttribPointerARB(8 + texture - GL_TEXTURE0, size, type, GL_FALSE, stride, pointer); glVertexAttribPointer(8 + texture - GL_TEXTURE0, size, type, GL_FALSE, stride, pointer);
m_ValidStreams |= STREAM_UV0 << (texture - GL_TEXTURE0); m_ValidStreams |= STREAM_UV0 << (texture - GL_TEXTURE0);
} }
@@ -637,7 +637,7 @@ public:
std::map<CStrIntern, int>::iterator it = m_VertexAttribs.find(id); std::map<CStrIntern, int>::iterator it = m_VertexAttribs.find(id);
if (it != m_VertexAttribs.end()) if (it != m_VertexAttribs.end())
{ {
glVertexAttribPointerARB(it->second, size, type, normalized, stride, pointer); glVertexAttribPointer(it->second, size, type, normalized, stride, pointer);
} }
} }
+8 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games. /* Copyright (C) 2022 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@@ -522,7 +522,13 @@ void CVideoMode::Shutdown()
bool CVideoMode::CreateBackendDevice(const bool createSDLContext) bool CVideoMode::CreateBackendDevice(const bool createSDLContext)
{ {
m_BackendDevice = Renderer::Backend::GL::CDevice::Create(createSDLContext ? m_Window : nullptr); m_BackendDevice = Renderer::Backend::GL::CDevice::Create(createSDLContext ? m_Window : nullptr, m_Backend == Backend::GL_ARB);
if (!m_BackendDevice && m_Backend == Backend::GL)
{
LOGERROR("Unable to create device for GL backend, switching to ARB.", static_cast<int>(m_Backend));
m_Backend = Backend::GL_ARB;
return CreateBackendDevice(createSDLContext);
}
return !!m_BackendDevice; return !!m_BackendDevice;
} }
+5 -9
View File
@@ -340,18 +340,14 @@ void CRenderer::EnumCaps()
m_Caps.m_ARBProgramShadow = true; m_Caps.m_ARBProgramShadow = true;
} }
if (0 == ogl_HaveExtensions(0, "GL_ARB_shader_objects", "GL_ARB_shading_language_100", NULL)) // GLSL shaders are in core since GL2.0.
{ if (ogl_HaveVersion(2, 0))
if (ogl_HaveExtension("GL_ARB_vertex_shader")) m_Caps.m_VertexShader = m_Caps.m_FragmentShader = true;
m_Caps.m_VertexShader = true;
if (ogl_HaveExtension("GL_ARB_fragment_shader"))
m_Caps.m_FragmentShader = true;
}
#if CONFIG2_GLES #if CONFIG2_GLES
m_Caps.m_Shadows = true; m_Caps.m_Shadows = true;
#else #else
if (0 == ogl_HaveExtensions(0, "GL_ARB_shadow", "GL_ARB_depth_texture", "GL_EXT_framebuffer_object", NULL)) if (0 == ogl_HaveExtensions(0, "GL_ARB_shadow", "GL_ARB_depth_texture", NULL))
{ {
if (ogl_max_tex_units >= 4) if (ogl_max_tex_units >= 4)
m_Caps.m_Shadows = true; m_Caps.m_Shadows = true;
@@ -361,7 +357,7 @@ void CRenderer::EnumCaps()
#if CONFIG2_GLES #if CONFIG2_GLES
m_Caps.m_PrettyWater = true; m_Caps.m_PrettyWater = true;
#else #else
if (0 == ogl_HaveExtensions(0, "GL_ARB_vertex_shader", "GL_ARB_fragment_shader", "GL_EXT_framebuffer_object", NULL)) if (m_Caps.m_VertexShader && m_Caps.m_FragmentShader)
m_Caps.m_PrettyWater = true; m_Caps.m_PrettyWater = true;
#endif #endif
} }
+7 -3
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games. /* Copyright (C) 2022 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@@ -127,7 +127,7 @@ std::vector<std::string> GetExtensionsImpl()
} // anonymous namespace } // anonymous namespace
// static // static
std::unique_ptr<CDevice> CDevice::Create(SDL_Window* window) std::unique_ptr<CDevice> CDevice::Create(SDL_Window* window, const bool arb)
{ {
std::unique_ptr<CDevice> device(new CDevice()); std::unique_ptr<CDevice> device(new CDevice());
@@ -167,8 +167,12 @@ std::unique_ptr<CDevice> CDevice::Create(SDL_Window* window)
#endif #endif
} }
// If we don't have GL2.0 then we don't have GLSL in core.
if (!arb && !ogl_HaveVersion(2, 0))
return nullptr;
if ((ogl_HaveExtensions(0, "GL_ARB_vertex_program", "GL_ARB_fragment_program", nullptr) // ARB if ((ogl_HaveExtensions(0, "GL_ARB_vertex_program", "GL_ARB_fragment_program", nullptr) // ARB
&& ogl_HaveExtensions(0, "GL_ARB_vertex_shader", "GL_ARB_fragment_shader", nullptr)) // GLSL && !ogl_HaveVersion(2, 0)) // GLSL
|| !ogl_HaveExtension("GL_ARB_vertex_buffer_object") // VBO || !ogl_HaveExtension("GL_ARB_vertex_buffer_object") // VBO
|| ogl_HaveExtensions(0, "GL_ARB_multitexture", "GL_EXT_draw_range_elements", nullptr) || ogl_HaveExtensions(0, "GL_ARB_multitexture", "GL_EXT_draw_range_elements", nullptr)
|| (!ogl_HaveExtension("GL_EXT_framebuffer_object") && !ogl_HaveExtension("GL_ARB_framebuffer_object"))) || (!ogl_HaveExtension("GL_EXT_framebuffer_object") && !ogl_HaveExtension("GL_ARB_framebuffer_object")))
+2 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games. /* Copyright (C) 2022 Wildfire Games.
* This file is part of 0 A.D. * This file is part of 0 A.D.
* *
* 0 A.D. is free software: you can redistribute it and/or modify * 0 A.D. is free software: you can redistribute it and/or modify
@@ -44,7 +44,7 @@ public:
/** /**
* Creates the GL device and the GL context for the window if it presents. * Creates the GL device and the GL context for the window if it presents.
*/ */
static std::unique_ptr<CDevice> Create(SDL_Window* window); static std::unique_ptr<CDevice> Create(SDL_Window* window, const bool arb);
const std::string& GetName() const { return m_Name; } const std::string& GetName() const { return m_Name; }
const std::string& GetVersion() const { return m_Version; } const std::string& GetVersion() const { return m_Version; }