1
0
forked from 0ad/0ad

Removes binding native GLuint textures from public ShaderProgram API.

Differential Revision: https://code.wildfiregames.com/D4407
This was SVN commit r26143.
This commit is contained in:
Vladislav Belov 2021-12-30 16:37:51 +00:00
parent e4455a8e8f
commit abc3190c03
17 changed files with 81 additions and 63 deletions

View File

@ -107,12 +107,12 @@ void CLOSTexture::MakeDirty()
m_Dirty = true;
}
GLuint CLOSTexture::GetTextureSmooth()
Renderer::Backend::GL::CTexture* CLOSTexture::GetTextureSmooth()
{
if (CRenderer::IsInitialised() && !g_RenderingOptions.GetSmoothLOS())
return GetTexture();
else
return (whichTex ? m_TextureSmooth1 : m_TextureSmooth2)->GetHandle();
return (whichTex ? m_TextureSmooth1 : m_TextureSmooth2).get();
}
void CLOSTexture::InterpolateLOS()
@ -155,8 +155,8 @@ void CLOSTexture::InterpolateLOS()
shader->Bind();
shader->BindTexture(str_losTex1, m_Texture->GetHandle());
shader->BindTexture(str_losTex2, (whichTex ? m_TextureSmooth1 : m_TextureSmooth2)->GetHandle());
shader->BindTexture(str_losTex1, m_Texture.get());
shader->BindTexture(str_losTex2, (whichTex ? m_TextureSmooth1 : m_TextureSmooth2).get());
shader->Uniform(str_delta, (float)g_Renderer.GetTimeManager().GetFrameDelta() * 4.0f, 0.0f, 0.0f, 0.0f);
@ -200,7 +200,7 @@ void CLOSTexture::InterpolateLOS()
}
GLuint CLOSTexture::GetTexture()
Renderer::Backend::GL::CTexture* CLOSTexture::GetTexture()
{
if (m_Dirty)
{
@ -208,7 +208,7 @@ GLuint CLOSTexture::GetTexture()
m_Dirty = false;
}
return m_Texture->GetHandle();
return m_Texture.get();
}
const CMatrix3D& CLOSTexture::GetTextureMatrix()

View File

@ -51,10 +51,10 @@ public:
* Also potentially switches the current active texture unit, and enables texturing on it.
* The texture is in 8-bit ALPHA format.
*/
GLuint GetTexture();
Renderer::Backend::GL::CTexture* GetTexture();
void InterpolateLOS();
GLuint GetTextureSmooth();
Renderer::Backend::GL::CTexture* GetTextureSmooth();
/**
* Returns a matrix to map (x,y,z) world coordinates onto (u,v) LOS texture

View File

@ -359,7 +359,7 @@ void CMiniMapTexture::RenderFinalTexture()
shader = tech->GetShader();
if (m_TerrainTexture)
shader->BindTexture(str_baseTex, m_TerrainTexture->GetHandle());
shader->BindTexture(str_baseTex, m_TerrainTexture.get());
CMatrix3D baseTransform;
baseTransform.SetIdentity();

View File

@ -42,7 +42,7 @@ public:
*/
void Render();
GLuint GetTexture() const { return m_FinalTexture->GetHandle(); }
Renderer::Backend::GL::CTexture* GetTexture() const { return m_FinalTexture.get(); }
/**
* @return The maximum height for unit passage in water.

View File

@ -733,6 +733,16 @@ void CShaderProgram::BindTexture(Binding id, const CTexturePtr& tex)
BindTexture(id, h);
}
void CShaderProgram::BindTexture(texture_id_t id, const Renderer::Backend::GL::CTexture* tex)
{
BindTexture(id, tex->GetHandle());
}
void CShaderProgram::BindTexture(Binding id, const Renderer::Backend::GL::CTexture* tex)
{
BindTexture(id, tex->GetHandle());
}
void CShaderProgram::Uniform(Binding id, int v)
{
Uniform(id, (float)v, (float)v, (float)v, (float)v);

View File

@ -22,6 +22,7 @@
#include "graphics/Texture.h"
#include "lib/ogl.h"
#include "lib/file/vfs/vfs_path.h"
#include "renderer/backend/gl/Texture.h"
#include <map>
#include <vector>
@ -144,9 +145,8 @@ public:
// Variants of texture binding:
void BindTexture(texture_id_t id, const CTexturePtr& tex);
void BindTexture(Binding id, const CTexturePtr& tex);
virtual void BindTexture(texture_id_t id, GLuint tex) = 0;
virtual void BindTexture(Binding id, GLuint tex) = 0;
void BindTexture(texture_id_t id, const Renderer::Backend::GL::CTexture* tex);
void BindTexture(Binding id, const Renderer::Backend::GL::CTexture* tex);
virtual Binding GetUniformBinding(uniform_id_t id) = 0;
@ -194,6 +194,9 @@ public:
protected:
CShaderProgram(int streamflags);
virtual void BindTexture(texture_id_t id, GLuint tex) = 0;
virtual void BindTexture(Binding id, GLuint tex) = 0;
bool m_IsValid;
int m_StreamFlags;

View File

@ -35,7 +35,7 @@
// TODO: There's a lot of duplication with CLOSTexture - might be nice to refactor a bit
CTerritoryTexture::CTerritoryTexture(CSimulation2& simulation) :
m_Simulation(simulation), m_DirtyID(0), m_MapSize(0), m_TextureSize(0)
m_Simulation(simulation), m_DirtyID(0), m_MapSize(0)
{
}
@ -55,12 +55,12 @@ bool CTerritoryTexture::UpdateDirty()
return cmpTerritoryManager && cmpTerritoryManager->NeedUpdateTexture(&m_DirtyID);
}
GLuint CTerritoryTexture::GetTexture()
Renderer::Backend::GL::CTexture* CTerritoryTexture::GetTexture()
{
if (UpdateDirty())
RecomputeTexture();
return m_Texture->GetHandle();
return m_Texture.get();
}
const float* CTerritoryTexture::GetTextureMatrix()
@ -84,20 +84,20 @@ void CTerritoryTexture::ConstructTexture()
// Convert size from terrain tiles to territory tiles
m_MapSize = cmpTerrain->GetMapSize() * Pathfinding::NAVCELL_SIZE_INT / ICmpTerritoryManager::NAVCELLS_PER_TERRITORY_TILE;
m_TextureSize = (GLsizei)round_up_to_pow2((size_t)m_MapSize);
const uint32_t textureSize = round_up_to_pow2(static_cast<uint32_t>(m_MapSize));
m_Texture = Renderer::Backend::GL::CTexture::Create2D(
Renderer::Backend::Format::R8G8B8A8, m_TextureSize, m_TextureSize,
Renderer::Backend::Format::R8G8B8A8, textureSize, textureSize,
Renderer::Backend::Sampler::MakeDefaultSampler(
Renderer::Backend::Sampler::Filter::LINEAR,
Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE));
// Initialise texture with transparency, for the areas we don't
// overwrite with glTexSubImage2D later
u8* texData = new u8[m_TextureSize * m_TextureSize * 4];
memset(texData, 0x00, m_TextureSize * m_TextureSize * 4);
u8* texData = new u8[textureSize * textureSize * 4];
memset(texData, 0x00, textureSize * textureSize * 4);
g_Renderer.BindTexture(0, m_Texture->GetHandle());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_TextureSize, m_TextureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureSize, textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);
delete[] texData;
{
@ -107,7 +107,7 @@ void CTerritoryTexture::ConstructTexture()
// world pos (mapsize*cellsize, y, mapsize*cellsize) (i.e. top-right of last tile)
// onto texcoord (mapsize / texsize, mapsize / texsize) (i.e. top-right of last texel)
float s = 1.f / (float)(m_TextureSize * TERRAIN_TILE_SIZE);
float s = 1.f / static_cast<float>(textureSize * TERRAIN_TILE_SIZE);
float t = 0.f;
m_TextureMatrix.SetZero();
m_TextureMatrix._11 = s;
@ -120,7 +120,7 @@ void CTerritoryTexture::ConstructTexture()
{
// Minimap matrix: We want to map UV (0,0)-(1,1) onto (0,0)-(mapsize/texsize, mapsize/texsize)
float s = m_MapSize / (float)m_TextureSize;
float s = m_MapSize / static_cast<float>(textureSize);
m_MinimapTextureMatrix.SetZero();
m_MinimapTextureMatrix._11 = s;
m_MinimapTextureMatrix._22 = s;

View File

@ -15,7 +15,6 @@
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lib/ogl.h"
#include "maths/Matrix3D.h"
#include "renderer/backend/gl/Texture.h"
@ -40,7 +39,7 @@ public:
* Also potentially switches the current active texture unit, and enables texturing on it.
* The texture is in 32-bit BGRA format.
*/
GLuint GetTexture();
Renderer::Backend::GL::CTexture* GetTexture();
/**
* Returns a matrix to map (x,y,z) world coordinates onto (u,v) texture
@ -75,7 +74,6 @@ private:
std::unique_ptr<Renderer::Backend::GL::CTexture> m_Texture;
ssize_t m_MapSize; // tiles per side
GLsizei m_TextureSize; // texels per side
CMatrix3D m_TextureMatrix;
CMatrix3D m_MinimapTextureMatrix;

View File

@ -973,7 +973,7 @@ void CPatchRData::RenderBlends(
Renderer::Backend::GL::CTexture* currentBlendTex = itt->m_Texture->m_TerrainAlpha->second.m_CompositeAlphaMap.get();
if (currentBlendTex != lastBlendTex)
{
shader->BindTexture(str_blendTex, currentBlendTex->GetHandle());
shader->BindTexture(str_blendTex, currentBlendTex);
lastBlendTex = currentBlendTex;
}

View File

@ -213,11 +213,12 @@ void CPostprocManager::RecreateBuffers()
}
void CPostprocManager::ApplyBlurDownscale2x(GLuint inTex, GLuint outTex, int inWidth, int inHeight)
void CPostprocManager::ApplyBlurDownscale2x(
Renderer::Backend::GL::CTexture* inTex, Renderer::Backend::GL::CTexture* outTex, int inWidth, int inHeight)
{
// Bind inTex to framebuffer for rendering.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_BloomFbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, outTex, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, outTex->GetHandle(), 0);
// Get bloom shader with instructions to simply copy texels.
CShaderDefines defines;
@ -227,23 +228,22 @@ void CPostprocManager::ApplyBlurDownscale2x(GLuint inTex, GLuint outTex, int inW
tech->BeginPass();
CShaderProgramPtr shader = tech->GetShader();
GLuint renderedTex = inTex;
// Cheat by creating high quality mipmaps for inTex, so the copying operation actually
// produces good scaling due to hardware filtering.
glBindTexture(GL_TEXTURE_2D, renderedTex);
glBindTexture(GL_TEXTURE_2D, inTex->GetHandle());
glGenerateMipmapEXT(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
shader->BindTexture(str_renderedTex, renderedTex);
shader->BindTexture(str_renderedTex, inTex);
const SViewPort oldVp = g_Renderer.GetViewport();
const SViewPort vp = { 0, 0, inWidth / 2, inHeight / 2 };
g_Renderer.SetViewport(vp);
float quadVerts[] = {
float quadVerts[] =
{
1.0f, 1.0f,
-1.0f, 1.0f,
-1.0f, -1.0f,
@ -252,7 +252,8 @@ void CPostprocManager::ApplyBlurDownscale2x(GLuint inTex, GLuint outTex, int inW
1.0f, -1.0f,
1.0f, 1.0f
};
float quadTex[] = {
float quadTex[] =
{
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
@ -271,11 +272,12 @@ void CPostprocManager::ApplyBlurDownscale2x(GLuint inTex, GLuint outTex, int inW
tech->EndPass();
}
void CPostprocManager::ApplyBlurGauss(GLuint inOutTex, GLuint tempTex, int inWidth, int inHeight)
void CPostprocManager::ApplyBlurGauss(
Renderer::Backend::GL::CTexture* inOutTex, Renderer::Backend::GL::CTexture* tempTex, int inWidth, int inHeight)
{
// Set tempTex as our rendering target.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_BloomFbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tempTex, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tempTex->GetHandle(), 0);
// Get bloom shader, for a horizontal Gaussian blur pass.
CShaderDefines defines2;
@ -291,7 +293,8 @@ void CPostprocManager::ApplyBlurGauss(GLuint inOutTex, GLuint tempTex, int inWid
const SViewPort vp = { 0, 0, inWidth, inHeight };
g_Renderer.SetViewport(vp);
float quadVerts[] = {
float quadVerts[] =
{
1.0f, 1.0f,
-1.0f, 1.0f,
-1.0f, -1.0f,
@ -300,7 +303,8 @@ void CPostprocManager::ApplyBlurGauss(GLuint inOutTex, GLuint tempTex, int inWid
1.0f, -1.0f,
1.0f, 1.0f
};
float quadTex[] = {
float quadTex[] =
{
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f,
@ -320,7 +324,7 @@ void CPostprocManager::ApplyBlurGauss(GLuint inOutTex, GLuint tempTex, int inWid
// Set result texture as our render target.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_BloomFbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, inOutTex, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, inOutTex->GetHandle(), 0);
// Get bloom shader, for a vertical Gaussian blur pass.
CShaderDefines defines3;
@ -353,10 +357,10 @@ void CPostprocManager::ApplyBlur()
int width = m_Width, height = m_Height;
#define SCALE_AND_BLUR(tex1, tex2, temptex) \
ApplyBlurDownscale2x((tex1)->GetHandle(), (tex2)->GetHandle(), width, height); \
ApplyBlurDownscale2x((tex1).get(), (tex2).get(), width, height); \
width /= 2; \
height /= 2; \
ApplyBlurGauss((tex2)->GetHandle(), (temptex)->GetHandle(), width, height);
ApplyBlurGauss((tex2).get(), (temptex).get(), width, height);
// We do the same thing for each scale, incrementally adding more and more blur.
SCALE_AND_BLUR(m_WhichBuffer ? m_ColorTex1 : m_ColorTex2, m_BlurTex2a, m_BlurTex2b);
@ -431,15 +435,15 @@ void CPostprocManager::ApplyEffect(CShaderTechniquePtr &shaderTech1, int pass)
// We also bind a bunch of other textures and parameters, but since
// this only happens once per frame the overhead is negligible.
if (m_WhichBuffer)
shader->BindTexture(str_renderedTex, m_ColorTex1->GetHandle());
shader->BindTexture(str_renderedTex, m_ColorTex1.get());
else
shader->BindTexture(str_renderedTex, m_ColorTex2->GetHandle());
shader->BindTexture(str_renderedTex, m_ColorTex2.get());
shader->BindTexture(str_depthTex, m_DepthTex->GetHandle());
shader->BindTexture(str_depthTex, m_DepthTex.get());
shader->BindTexture(str_blurTex2, m_BlurTex2a->GetHandle());
shader->BindTexture(str_blurTex4, m_BlurTex4a->GetHandle());
shader->BindTexture(str_blurTex8, m_BlurTex8a->GetHandle());
shader->BindTexture(str_blurTex2, m_BlurTex2a.get());
shader->BindTexture(str_blurTex4, m_BlurTex4a.get());
shader->BindTexture(str_blurTex8, m_BlurTex8a.get());
shader->Uniform(str_width, m_Width);
shader->Uniform(str_height, m_Height);

View File

@ -129,12 +129,12 @@ private:
// High quality GPU image scaling to half size. outTex must have exactly half the size
// of inTex. inWidth and inHeight are the dimensions of inTex in texels.
void ApplyBlurDownscale2x(GLuint inTex, GLuint outTex, int inWidth, int inHeight);
void ApplyBlurDownscale2x(Renderer::Backend::GL::CTexture* inTex, Renderer::Backend::GL::CTexture* outTex, int inWidth, int inHeight);
// GPU-based Gaussian blur in two passes. inOutTex contains the input image and will be filled
// with the blurred image. tempTex must have the same size as inOutTex.
// inWidth and inHeight are the dimensions of the images in texels.
void ApplyBlurGauss(GLuint inOutTex, GLuint tempTex, int inWidth, int inHeight);
void ApplyBlurGauss(Renderer::Backend::GL::CTexture* inOutTex, Renderer::Backend::GL::CTexture* tempTex, int inWidth, int inHeight);
// Applies a pass of a given effect to the entire current framebuffer. The shader is
// provided with a number of general-purpose variables, including the rendered screen so far,

View File

@ -693,7 +693,7 @@ void ShadowMap::BindTo(const CShaderProgramPtr& shader) const
if (!shader->GetTextureBinding(str_shadowTex).Active() || !m->Texture)
return;
shader->BindTexture(str_shadowTex, m->Texture->GetHandle());
shader->BindTexture(str_shadowTex, m->Texture.get());
shader->Uniform(str_shadowScale, m->Width, m->Height, 1.0f / m->Width, 1.0f / m->Height);
const CVector3D cameraForward = g_Renderer.GetCullCamera().GetOrientation().GetIn();
shader->Uniform(str_cameraForward, cameraForward.X, cameraForward.Y, cameraForward.Z,
@ -795,7 +795,7 @@ void ShadowMap::RenderDebugTexture()
CShaderProgramPtr texShader = texTech->GetShader();
texShader->Uniform(str_transform, GetDefaultGuiMatrix());
texShader->BindTexture(str_tex, m->Texture->GetHandle());
texShader->BindTexture(str_tex, m->Texture.get());
texShader->Uniform(str_colorAdd, CColor(0.0f, 0.0f, 0.0f, 1.0f));
texShader->Uniform(str_colorMul, CColor(1.0f, 1.0f, 1.0f, 0.0f));
texShader->Uniform(str_grayscaleFactor, 0.0f);

View File

@ -232,7 +232,7 @@ void SkyManager::RenderSky()
g_Renderer.GetShaderManager().LoadEffect(str_sky_simple);
skytech->BeginPass();
CShaderProgramPtr shader = skytech->GetShader();
shader->BindTexture(str_baseTex, m_SkyCubeMap->GetHandle());
shader->BindTexture(str_baseTex, m_SkyCubeMap.get());
// Translate so the sky center is at the camera space origin.
CMatrix3D translate;

View File

@ -48,9 +48,9 @@ public:
return m_SkySet;
}
GLuint GetSkyCube()
Renderer::Backend::GL::CTexture* GetSkyCube()
{
return m_SkyCubeMap->GetHandle();
return m_SkyCubeMap.get();
}
/**

View File

@ -348,12 +348,13 @@ void TerrainTextureOverlay::RenderAfterWater(int cullGroup)
matrix._23 = m_TexelsPerTile / (m_Texture->GetHeight() * TERRAIN_TILE_SIZE);
matrix._44 = 1;
g_Renderer.GetTerrainRenderer().RenderTerrainOverlayTexture(cullGroup, matrix, m_Texture->GetHandle());
g_Renderer.GetTerrainRenderer().RenderTerrainOverlayTexture(cullGroup, matrix, m_Texture.get());
}
SColor4ub TerrainTextureOverlay::GetColor(size_t idx, u8 alpha) const
{
static u8 colors[][3] = {
static u8 colors[][3] =
{
{ 255, 0, 0 },
{ 0, 255, 0 },
{ 0, 0, 255 },

View File

@ -156,7 +156,8 @@ void TerrainRenderer::EndFrame()
m->phase = Phase_Submit;
}
void TerrainRenderer::RenderTerrainOverlayTexture(int cullGroup, CMatrix3D& textureMatrix, GLuint texture)
void TerrainRenderer::RenderTerrainOverlayTexture(int cullGroup, CMatrix3D& textureMatrix,
Renderer::Backend::GL::CTexture* texture)
{
#if CONFIG2_GLES
#warning TODO: implement TerrainRenderer::RenderTerrainOverlayTexture for GLES
@ -441,20 +442,20 @@ bool TerrainRenderer::RenderFancyWater(const CShaderDefines& context, int cullGr
if (waterManager->m_WaterFancyEffects)
{
fancyWaterShader->BindTexture(str_waterEffectsTex, waterManager->m_FancyTexture->GetHandle());
fancyWaterShader->BindTexture(str_waterEffectsTex, waterManager->m_FancyTexture.get());
}
if (waterManager->m_WaterRefraction && waterManager->m_WaterRealDepth)
{
fancyWaterShader->BindTexture(str_depthTex, waterManager->m_RefrFboDepthTexture->GetHandle());
fancyWaterShader->BindTexture(str_depthTex, waterManager->m_RefrFboDepthTexture.get());
fancyWaterShader->Uniform(str_projInvTransform, waterManager->m_RefractionProjInvMatrix);
fancyWaterShader->Uniform(str_viewInvTransform, waterManager->m_RefractionViewInvMatrix);
}
if (waterManager->m_WaterRefraction)
fancyWaterShader->BindTexture(str_refractionMap, waterManager->m_RefractionTexture->GetHandle());
fancyWaterShader->BindTexture(str_refractionMap, waterManager->m_RefractionTexture.get());
if (waterManager->m_WaterReflection)
fancyWaterShader->BindTexture(str_reflectionMap, waterManager->m_ReflectionTexture->GetHandle());
fancyWaterShader->BindTexture(str_reflectionMap, waterManager->m_ReflectionTexture.get());
fancyWaterShader->BindTexture(str_losTex, losTexture.GetTextureSmooth());
const CLightEnv& lightEnv = g_Renderer.GetLightEnv();

View File

@ -25,6 +25,7 @@
#include "graphics/Color.h"
#include "maths/BoundingBoxAligned.h"
#include "renderer/backend/gl/Texture.h"
class CCamera;
class CMatrix3D;
@ -144,7 +145,7 @@ public:
* by the given texture matrix.
* Intended for use by TerrainTextureOverlay.
*/
void RenderTerrainOverlayTexture(int cullGroup, CMatrix3D& textureMatrix, GLuint texture);
void RenderTerrainOverlayTexture(int cullGroup, CMatrix3D& textureMatrix, Renderer::Backend::GL::CTexture* texture);
private:
TerrainRendererInternals* m;