Skip to content

Commit

Permalink
Don't use texture buffer object if not supported, see #2571
Browse files Browse the repository at this point in the history
Also allow to use setTextureUnits for texture buffer
  • Loading branch information
Benau committed Feb 12, 2017
1 parent 4af2554 commit ca0a605
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 35 deletions.
11 changes: 11 additions & 0 deletions src/graphics/central_settings.cpp
Expand Up @@ -52,6 +52,7 @@ void CentralVideoSettings::init()
hasGS = false;
hasTextureFilterAnisotropic = false;
hasTextureSwizzle = false;
hasTextureBufferObject = false;

#if defined(USE_GLES2)
hasBGRA = false;
Expand Down Expand Up @@ -196,6 +197,11 @@ void CentralVideoSettings::init()
hasTextureSwizzle = true;
Log::info("GLDriver", "ARB Texture Swizzle Present");
}
if (hasGLExtension("GL_ARB_texture_buffer_object"))
{
hasTextureBufferObject = true;
Log::info("GLDriver", "ARB Texture Buffer Object Present");
}
// Only unset the high def textures if they are set as default. If the
// user has enabled them (bit 1 set), then leave them enabled.
if (GraphicsRestrictions::isDisabled(GraphicsRestrictions::GR_HIGHDEFINITION_TEXTURES) &&
Expand Down Expand Up @@ -476,4 +482,9 @@ bool CentralVideoSettings::isARBTextureSwizzleUsable() const
return m_glsl && hasTextureSwizzle;
}

bool CentralVideoSettings::isARBTextureBufferObjectUsable() const
{
return hasTextureBufferObject;
}

#endif // !SERVER_ONLY
2 changes: 2 additions & 0 deletions src/graphics/central_settings.hpp
Expand Up @@ -44,6 +44,7 @@ class CentralVideoSettings
bool hasMultiDrawIndirect;
bool hasTextureFilterAnisotropic;
bool hasTextureSwizzle;
bool hasTextureBufferObject;

#if defined(USE_GLES2)
bool hasBGRA;
Expand Down Expand Up @@ -84,6 +85,7 @@ class CentralVideoSettings
bool isARBExplicitAttribLocationUsable() const;
bool isEXTTextureFilterAnisotropicUsable() const;
bool isARBTextureSwizzleUsable() const;
bool isARBTextureBufferObjectUsable() const;

#if defined(USE_GLES2)
bool isEXTTextureFormatBGRA8888Usable() const;
Expand Down
19 changes: 9 additions & 10 deletions src/graphics/gpu_particles.cpp
Expand Up @@ -91,22 +91,21 @@ class FlipParticleRender : public TextureShader<FlipParticleRender, 2>

// ============================================================================
/** */
class HeightmapSimulationShader : public Shader <HeightmapSimulationShader,
core::matrix4, int, int,
float,float,float,float,float>
class HeightmapSimulationShader :
public TextureShader<HeightmapSimulationShader, 1,
core::matrix4, int, int,
float, float, float, float,
float>
{
public:
GLuint m_TU_heightmap;

HeightmapSimulationShader()
{
const char *varyings[] = {"new_particle_position", "new_lifetime",
"new_particle_velocity", "new_size" };
loadTFBProgram("particlesimheightmap.vert", varyings, 4);
assignUniforms("sourcematrix", "dt", "level", "size_increase_factor",
"track_x", "track_x_len", "track_z", "track_z_len");
m_TU_heightmap = 2;
assignTextureUnit(m_TU_heightmap, "heightmap");
assignSamplerNames(0, "heightmap", ST_TEXTURE_BUFFER);
} // HeightmapSimulationShader


Expand Down Expand Up @@ -203,6 +202,7 @@ void ParticleSystemProxy::setHeightmap(const std::vector<std::vector<float> > &h
glBindTexture(GL_TEXTURE_BUFFER, heightmaptexture);
glTexBuffer(GL_TEXTURE_BUFFER, GL_R32F, heighmapbuffer);
glBindBuffer(GL_TEXTURE_BUFFER, 0);
glBindTexture(GL_TEXTURE_BUFFER, 0);

delete[] hm_array;
#endif
Expand Down Expand Up @@ -475,10 +475,9 @@ void ParticleSystemProxy::simulate()
{
#if !defined(USE_GLES2)
HeightmapSimulationShader::getInstance()->use();
glActiveTexture(GL_TEXTURE0 + HeightmapSimulationShader::getInstance()->m_TU_heightmap);
glBindTexture(GL_TEXTURE_BUFFER, heightmaptexture);
HeightmapSimulationShader::getInstance()->setTextureUnits(heightmaptexture);
HeightmapSimulationShader::getInstance()->setUniforms(matrix, timediff, active_count, size_increase_factor, track_x, track_x_len, track_z, track_z_len);
#endif
#endif
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/particle_emitter.cpp
Expand Up @@ -710,7 +710,7 @@ void ParticleEmitter::setParticleType(const ParticleKind* type)
void ParticleEmitter::addHeightMapAffector(Track* t)
{

if (m_is_glsl)
if (m_is_glsl && CVS->isARBTextureBufferObjectUsable())
{
const Vec3* aabb_min;
const Vec3* aabb_max;
Expand Down
35 changes: 15 additions & 20 deletions src/graphics/skybox.cpp
Expand Up @@ -57,21 +57,18 @@ class SkyboxShader : public TextureShader<SkyboxShader,1>
}; // SkyboxShader


class SpecularIBLGenerator : public TextureShader<SpecularIBLGenerator, 1,
class SpecularIBLGenerator : public TextureShader<SpecularIBLGenerator, 2,
core::matrix4, float >
{
public:
GLuint m_tu_samples;
SpecularIBLGenerator()
{
loadProgram(OBJECT, GL_VERTEX_SHADER, "screenquad.vert",
GL_FRAGMENT_SHADER, "importance_sampling_specular.frag");
assignUniforms("PermutationMatrix", "ViewportSize");
m_tu_samples = 1;
assignSamplerNames(0, "tex", ST_TRILINEAR_CUBEMAP);
assignTextureUnit(m_tu_samples, "samples");
assignSamplerNames(0, "tex", ST_TRILINEAR_CUBEMAP,
1, "samples", ST_TEXTURE_BUFFER);
}

}; // SpecularIBLGenerator


Expand Down Expand Up @@ -231,7 +228,7 @@ void Skybox::generateSpecularCubemap()
}
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);

if (!CVS->isDefferedEnabled())
if (!CVS->isDefferedEnabled() || !CVS->isARBTextureBufferObjectUsable())
return;

#if !defined(USE_GLES2)
Expand Down Expand Up @@ -277,16 +274,15 @@ void Skybox::generateSpecularCubemap()
}

glBindVertexArray(0);
glActiveTexture(GL_TEXTURE0 +
SpecularIBLGenerator::getInstance()->m_tu_samples);
GLuint sampleTex, sampleBuffer;
glGenBuffers(1, &sampleBuffer);
glBindBuffer(GL_TEXTURE_BUFFER, sampleBuffer);
GLuint sample_texture, sample_buffer;
glGenBuffers(1, &sample_buffer);
glBindBuffer(GL_TEXTURE_BUFFER, sample_buffer);
glBufferData(GL_TEXTURE_BUFFER, 2048 * sizeof(float), tmp,
GL_STATIC_DRAW);
glGenTextures(1, &sampleTex);
glBindTexture(GL_TEXTURE_BUFFER, sampleTex);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, sampleBuffer);
glGenTextures(1, &sample_texture);
glBindTexture(GL_TEXTURE_BUFFER, sample_texture);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, sample_buffer);
glBindTexture(GL_TEXTURE_BUFFER, 0);
glBindVertexArray(SharedGPUObjects::getFullScreenQuadVAO());

for (unsigned face = 0; face < 6; face++)
Expand All @@ -298,20 +294,19 @@ void Skybox::generateSpecularCubemap()
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
assert(status == GL_FRAMEBUFFER_COMPLETE);

SpecularIBLGenerator::getInstance()->setTextureUnits(m_cube_map);
SpecularIBLGenerator::getInstance()
->setTextureUnits(m_cube_map, sample_texture);
SpecularIBLGenerator::getInstance()->setUniforms(M[face],
viewportSize);

glDrawArrays(GL_TRIANGLES, 0, 3);
}
glActiveTexture( GL_TEXTURE0
+ SpecularIBLGenerator::getInstance()->m_tu_samples);
glBindBuffer(GL_TEXTURE_BUFFER, 0);
glBindTexture(GL_TEXTURE_BUFFER, 0);

delete[] tmp;
glDeleteTextures(1, &sampleTex);
glDeleteBuffers(1, &sampleBuffer);
glDeleteTextures(1, &sample_texture);
glDeleteBuffers(1, &sample_buffer);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &fbo);
Expand Down
13 changes: 11 additions & 2 deletions src/graphics/texture_shader.cpp
Expand Up @@ -33,7 +33,8 @@ TextureShaderBase::BindFunction TextureShaderBase::m_all_bind_functions[] =
/* ST_VOLUME_LINEAR_FILTERED */ &TextureShaderBase::bindTextureVolume,
/* ST_NEARED_CLAMPED_FILTERED */ &TextureShaderBase::bindTextureNearestClamped,
/* ST_BILINEAR_CLAMPED_FILTERED */ &TextureShaderBase::bindTextureBilinearClamped,
/* ST_SEMI_TRILINEAR */ &TextureShaderBase::bindTextureSemiTrilinear
/* ST_SEMI_TRILINEAR */ &TextureShaderBase::bindTextureSemiTrilinear,
/* ST_TEXTURE_BUFFER */ &TextureShaderBase::bindTextureBuffer
};

GLuint TextureShaderBase::m_all_texture_types[] =
Expand All @@ -46,7 +47,8 @@ GLuint TextureShaderBase::m_all_texture_types[] =
/* ST_VOLUME_LINEAR_FILTERED */ GL_TEXTURE_3D,
/* ST_NEARED_CLAMPED_FILTERED */ GL_TEXTURE_2D,
/* ST_BILINEAR_CLAMPED_FILTERED */ GL_TEXTURE_2D,
/* ST_SEMI_TRILINEAR */ GL_TEXTURE_2D
/* ST_SEMI_TRILINEAR */ GL_TEXTURE_2D,
/* ST_TEXTURE_BUFFER */ GL_TEXTURE_BUFFER
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -116,6 +118,11 @@ void TextureShaderBase::bindTextureNearestClamped(GLuint texture_unit,
} // bindTextureNearestClamped

// ----------------------------------------------------------------------------
void TextureShaderBase::bindTextureBuffer(GLuint texture_unit, GLuint tex_id)
{
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_BUFFER, tex_id);
} // bindTextureBuffer

// ----------------------------------------------------------------------------
void TextureShaderBase::bindTextureBilinear(GLuint texture_unit, GLuint tex)
Expand Down Expand Up @@ -239,6 +246,8 @@ GLuint TextureShaderBase::createSamplers(SamplerTypeNew sampler_type)
return createBilinearClampedSampler();
case ST_SEMI_TRILINEAR:
return createSemiTrilinearSampler();
case ST_TEXTURE_BUFFER:
return 0;
default:
assert(false);
return 0;
Expand Down
7 changes: 5 additions & 2 deletions src/graphics/texture_shader.hpp
Expand Up @@ -43,7 +43,8 @@ enum SamplerTypeNew
ST_NEARED_CLAMPED_FILTERED,
ST_BILINEAR_CLAMPED_FILTERED,
ST_SEMI_TRILINEAR,
ST_MAX = ST_SEMI_TRILINEAR
ST_TEXTURE_BUFFER,
ST_MAX = ST_TEXTURE_BUFFER
}; // SamplerTypeNew

// ============================================================================
Expand All @@ -68,6 +69,7 @@ class TextureShaderBase
static void bindTextureShadow(GLuint tex_unit, GLuint tex_id);
static void bindTrilinearClampedArrayTexture(GLuint tex_unit, GLuint tex_id);
static void bindTextureVolume(GLuint tex_unit, GLuint tex_id);
static void bindTextureBuffer(GLuint tex_unit, GLuint tex_id);

GLuint createSamplers(SamplerTypeNew sampler_type);
private:
Expand Down Expand Up @@ -179,7 +181,8 @@ class TextureShader : public TextureShaderBase
{
glActiveTexture(GL_TEXTURE0 + m_texture_units[N]);
glBindTexture(m_texture_type[N], tex_id);
glBindSampler(m_texture_units[N], m_sampler_ids[N]);
if (m_sampler_ids[N] != 0)
glBindSampler(m_texture_units[N], m_sampler_ids[N]);
}
else
{
Expand Down

0 comments on commit ca0a605

Please sign in to comment.