1
0
forked from 0ad/0ad

Removes low level GL code from GUI sprite rendering and switches to Canvas2D.

Tested By: Langbart
Differential Revision: https://code.wildfiregames.com/D4031
This was SVN commit r25603.
This commit is contained in:
Vladislav Belov 2021-05-30 13:48:58 +00:00
parent c388ef64ad
commit 1f192f1593
8 changed files with 55 additions and 106 deletions

View File

@ -3,11 +3,23 @@
TEMP colorTex;
TEX colorTex, fragment.texcoord[0], texture[0], 2D;
TEMP grayscale;
MOV grayscale.r, 0.3;
MOV grayscale.g, 0.59;
MOV grayscale.b, 0.11;
MOV grayscale.a, 0.0;
PARAM colorAdd = program.local[1];
PARAM colorMul = program.local[2];
PARAM grayscaleFactor = program.local[3];
TEMP colorGray;
DP3 colorGray.rgb, colorTex, grayscale;
MOV colorGray.a, colorTex.a;
TEMP color;
MUL color, colorTex, colorMul;
LRP color, grayscaleFactor, colorGray, colorTex;
MUL color, color, colorMul;
ADD color, color, colorAdd;
MOV result.color, color;

View File

@ -11,6 +11,7 @@
<uniform name="tex" loc="0" type="sampler2D"/>
<uniform name="colorAdd" loc="1" type="vec4"/>
<uniform name="colorMul" loc="2" type="vec4"/>
<uniform name="grayscaleFactor" loc="3" type="float"/>
</fragment>
</program>

View File

@ -1,12 +1,15 @@
#version 110
uniform sampler2D tex;
uniform vec4 colorAdd;
uniform vec4 colorMul;
uniform sampler2D tex;
uniform float grayscaleFactor;
varying vec2 v_uv;
void main()
{
gl_FragColor = clamp(texture2D(tex, v_uv) * colorMul + colorAdd, 0.0, 1.0);
vec4 colorTex = texture2D(tex, v_uv);
vec3 grayColor = vec3(dot(vec3(0.3, 0.59, 0.11), colorTex.rgb));
gl_FragColor = clamp(mix(colorTex, vec4(grayColor, colorTex.a), grayscaleFactor) * colorMul + colorAdd, 0.0, 1.0);
}

View File

@ -38,7 +38,7 @@ using PlaneArray2D = std::array<float, 8>;
inline void DrawTextureImpl(CTexturePtr texture,
const PlaneArray2D& vertices, PlaneArray2D uvs,
const CColor& multiply, const CColor& add)
const CColor& multiply, const CColor& add, const float grayscaleFactor)
{
CShaderDefines defines;
CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(
@ -58,6 +58,7 @@ inline void DrawTextureImpl(CTexturePtr texture,
shader->Uniform(str_transform, GetDefaultGuiMatrix());
shader->Uniform(str_colorAdd, add);
shader->Uniform(str_colorMul, multiply);
shader->Uniform(str_grayscaleFactor, grayscaleFactor);
shader->VertexPointer(2, GL_FLOAT, 0, vertices.data());
shader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, 0, uvs.data());
shader->AssertPointersBound();
@ -121,19 +122,19 @@ void CCanvas2D::DrawRect(const CRect& rect, const CColor& color)
DrawTextureImpl(
g_Renderer.GetTextureManager().GetTransparentTexture(),
vertices, uvs, CColor(0.0f, 0.0f, 0.0f, 0.0f), color);
vertices, uvs, CColor(0.0f, 0.0f, 0.0f, 0.0f), color, 0.0f);
}
void CCanvas2D::DrawTexture(CTexturePtr texture, const CRect& destination)
{
DrawTexture(texture,
destination, CRect(0, 0, texture->GetWidth(), texture->GetHeight()),
CColor(1.0f, 1.0f, 1.0f, 1.0f), CColor(0.0f, 0.0f, 0.0f, 0.0f));
CColor(1.0f, 1.0f, 1.0f, 1.0f), CColor(0.0f, 0.0f, 0.0f, 0.0f), 0.0f);
}
void CCanvas2D::DrawTexture(
CTexturePtr texture, const CRect& destination, const CRect& source,
const CColor& multiply, const CColor& add)
const CColor& multiply, const CColor& add, const float grayscaleFactor)
{
const PlaneArray2D uvs = {
source.left, source.bottom,
@ -148,5 +149,5 @@ void CCanvas2D::DrawTexture(
destination.left, destination.top
};
DrawTextureImpl(texture, vertices, uvs, multiply, add);
DrawTextureImpl(texture, vertices, uvs, multiply, add, grayscaleFactor);
}

View File

@ -48,10 +48,12 @@ public:
* Draws a piece of the texture from the source rect into the
* destination rect. The result color is set by the following formula:
* TEXTURE_COLOR * COLOR_MULTIPLY + COLOR_ADD
* The texture color is blended with its own grayscale version according to
* the grayscale factor.
*/
void DrawTexture(CTexturePtr texture,
const CRect& destination, const CRect& source,
const CColor& multiply, const CColor& add);
const CColor& multiply, const CColor& add, const float grayscaleFactor);
/**
* A simpler version of the previous one, draws the texture into the

View File

@ -20,7 +20,6 @@
#include "GUIRenderer.h"
#include "graphics/Canvas2D.h"
#include "graphics/ShaderManager.h"
#include "graphics/TextureManager.h"
#include "gui/CGUI.h"
#include "gui/CGUISprite.h"
@ -206,25 +205,22 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const
Call.m_Vertices.bottom = (int)(Call.m_Vertices.bottom + 0.5f);
}
bool hasTexture = false;
if (!(*cit)->m_TextureName.empty())
{
CTextureProperties textureProps(g_L10n.LocalizePath((*cit)->m_TextureName));
textureProps.SetWrap((*cit)->m_WrapMode);
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
texture->Prefetch();
Call.m_HasTexture = true;
hasTexture = true;
Call.m_Texture = texture;
Call.m_ObjectSize = ObjectSize;
}
else
{
Call.m_HasTexture = false;
}
Call.m_BackColor = &(*cit)->m_BackColor;
if (!Call.m_HasTexture)
Call.m_GrayscaleFactor = 0.0f;
if (!hasTexture)
{
Call.m_Material = str_gui_solid;
Call.m_ColorAdd = *Call.m_BackColor;
Call.m_ColorMultiply = CColor(0.0f, 0.0f, 0.0f, 0.0f);
Call.m_Texture = g_Renderer.GetTextureManager().GetTransparentTexture();
@ -233,33 +229,30 @@ void GUIRenderer::UpdateDrawCallCache(const CGUI& pGUI, DrawCalls& Calls, const
{
if ((*cit)->m_Effects->m_AddColor != CGUIColor())
{
Call.m_Material = str_gui_add;
const CColor color = (*cit)->m_Effects->m_AddColor;
Call.m_ColorAdd = CColor(color.r, color.g, color.b, 0.0f);
Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f);
}
else if ((*cit)->m_Effects->m_Greyscale)
{
Call.m_Shader = g_Renderer.GetShaderManager().LoadEffect(str_gui_grayscale);
Call.m_Material = str_gui_grayscale;
Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f);
Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f);
Call.m_GrayscaleFactor = 1.0f;
}
else if ((*cit)->m_Effects->m_SolidColor != CGUIColor())
{
Call.m_Material = str_gui_solid_mask;
const CColor color = (*cit)->m_Effects->m_SolidColor;
Call.m_ColorAdd = CColor(color.r, color.g, color.b, 0.0f);
Call.m_ColorMultiply = CColor(0.0f, 0.0f, 0.0f, color.a);
}
else /* Slight confusion - why no effects? */
{
Call.m_Material = str_gui_basic;
Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f);
Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f);
}
}
else
{
Call.m_Material = str_gui_basic;
Call.m_ColorAdd = CColor(0.0f, 0.0f, 0.0f, 0.0f);
Call.m_ColorMultiply = CColor(1.0f, 1.0f, 1.0f, 1.0f);
}
@ -334,15 +327,11 @@ void GUIRenderer::Draw(DrawCalls& Calls, CCanvas2D& canvas)
// TODO: batching by shader/texture/etc would be nice
CMatrix3D matrix = GetDefaultGuiMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Iterate through each DrawCall, and execute whatever drawing code is being called
for (DrawCalls::const_iterator cit = Calls.begin(); cit != Calls.end(); ++cit)
{
if (cit->m_HasTexture && (cit->m_Material == str_gui_basic || cit->m_Material == str_gui_solid_mask || cit->m_Material == str_gui_add))
{
// A hack to preload the handle to get a correct texture size.
GLuint h;
@ -365,60 +354,7 @@ void GUIRenderer::Draw(DrawCalls& Calls, CCanvas2D& canvas)
}
canvas.DrawTexture(cit->m_Texture,
rect, texCoords, cit->m_ColorMultiply, cit->m_ColorAdd);
}
else if (cit->m_HasTexture)
{
cit->m_Shader->BeginPass();
CShaderProgramPtr shader = cit->m_Shader->GetShader();
shader->Uniform(str_transform, matrix);
shader->Uniform(str_color, cit->m_ShaderColorParameter);
shader->BindTexture(str_tex, cit->m_Texture);
CRect TexCoords = cit->ComputeTexCoords();
// Ensure the quad has the correct winding order, and update texcoords to match
CRect Verts = cit->m_Vertices;
if (Verts.right < Verts.left)
{
std::swap(Verts.right, Verts.left);
std::swap(TexCoords.right, TexCoords.left);
}
if (Verts.bottom < Verts.top)
{
std::swap(Verts.bottom, Verts.top);
std::swap(TexCoords.bottom, TexCoords.top);
}
std::vector<float> data;
#define ADD(u, v, x, y, z) STMT(data.push_back(u); data.push_back(v); data.push_back(x); data.push_back(y); data.push_back(z))
ADD(TexCoords.left, TexCoords.bottom, Verts.left, Verts.bottom, 0.0f);
ADD(TexCoords.right, TexCoords.bottom, Verts.right, Verts.bottom, 0.0f);
ADD(TexCoords.right, TexCoords.top, Verts.right, Verts.top, 0.0f);
ADD(TexCoords.right, TexCoords.top, Verts.right, Verts.top, 0.0f);
ADD(TexCoords.left, TexCoords.top, Verts.left, Verts.top, 0.0f);
ADD(TexCoords.left, TexCoords.bottom, Verts.left, Verts.bottom, 0.0f);
#undef ADD
shader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, 5*sizeof(float), &data[0]);
shader->VertexPointer(3, GL_FLOAT, 5*sizeof(float), &data[2]);
glDrawArrays(GL_TRIANGLES, 0, 6);
cit->m_Shader->EndPass();
}
else
{
// Ensure the quad has the correct winding order
CRect rect = cit->m_Vertices;
if (rect.right < rect.left)
std::swap(rect.right, rect.left);
if (rect.bottom < rect.top)
std::swap(rect.bottom, rect.top);
canvas.DrawTexture(cit->m_Texture,
rect, CRect(0, 0, cit->m_Texture->GetWidth(), cit->m_Texture->GetHeight()),
cit->m_ColorMultiply, cit->m_ColorAdd);
}
rect, texCoords, cit->m_ColorMultiply, cit->m_ColorAdd, cit->m_GrayscaleFactor);
}
glDisable(GL_BLEND);

View File

@ -44,23 +44,16 @@ namespace GUIRenderer
const SGUIImage* m_Image;
bool m_HasTexture;
CTexturePtr m_Texture;
CRect m_ObjectSize;
CShaderTechniquePtr m_Shader;
CColor m_ShaderColorParameter;
CRect m_Vertices;
CGUIColor* m_BackColor;
// Temporary type to make a soft transition to canvas rendering.
CStrIntern m_Material;
CColor m_ColorAdd;
CColor m_ColorMultiply;
float m_GrayscaleFactor;
};
class DrawCalls : public std::vector<SDrawCall>

View File

@ -102,6 +102,7 @@ X(foamTex)
X(fogColor)
X(fogParams)
X(foreground_overlay)
X(grayscaleFactor)
X(gui_add)
X(gui_basic)
X(gui_grayscale)