Skip to content

Commit

Permalink
Make GLSL use #version 120 consistently.
Browse files Browse the repository at this point in the history
This removes gl_VertexID, since it didn't seem to work with #version 120.

See ticket:4536, maybe fixes.
  • Loading branch information
Cyp committed Apr 5, 2017
1 parent 9c04d4a commit 4134f82
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 55 deletions.
2 changes: 1 addition & 1 deletion data/base/shaders/button.frag
@@ -1,5 +1,5 @@
#version 120
#pragma debug(on)
//#pragma debug(on)

uniform sampler2D Texture;
uniform sampler2D TextureTcmask;
Expand Down
2 changes: 1 addition & 1 deletion data/base/shaders/button.vert
@@ -1,5 +1,5 @@
#version 120
#pragma debug(on)
//#pragma debug(on)

uniform mat4 ModelViewProjectionMatrix;

Expand Down
7 changes: 3 additions & 4 deletions data/base/shaders/gfx.frag
@@ -1,10 +1,9 @@
#version 130
#version 120

in vec4 color;
varying vec4 color;

out vec4 FragColor;

void main()
{
FragColor = color;
gl_FragColor = color;
}
12 changes: 6 additions & 6 deletions data/base/shaders/gfx.vert
@@ -1,13 +1,13 @@
#version 130
#version 120

uniform mat4 posMatrix;

in vec4 vertex;
in vec2 vertexTexCoord;
in vec4 vertexColor;
attribute vec4 vertex;
attribute vec2 vertexTexCoord;
attribute vec4 vertexColor;

out vec2 uv;
out vec4 color;
varying vec2 uv;
varying vec4 color;

void main()
{
Expand Down
12 changes: 5 additions & 7 deletions data/base/shaders/line.vert
@@ -1,16 +1,14 @@
#version 130
#version 120

uniform vec2 from;
uniform vec2 to;
uniform mat4 ModelViewProjectionMatrix;

vec4 getPos(int id)
{
if (id == 0) return vec4(from, 0., 1.);
if (id == 1) return vec4(to, 0., 1.);
}
attribute vec4 rect;


void main()
{
gl_Position = ModelViewProjectionMatrix * getPos(gl_VertexID);
vec4 pos = vec4(from + (to - from)*rect.y, 0.0, 1.0);
gl_Position = ModelViewProjectionMatrix * pos;
}
2 changes: 1 addition & 1 deletion data/base/shaders/nolight.frag
@@ -1,5 +1,5 @@
#version 120
#pragma debug(on)
//#pragma debug(on)

uniform sampler2D Texture;
uniform vec4 colour;
Expand Down
2 changes: 1 addition & 1 deletion data/base/shaders/nolight.vert
@@ -1,5 +1,5 @@
#version 120
#pragma debug(on)
//#pragma debug(on)

uniform mat4 ModelViewProjectionMatrix;

Expand Down
5 changes: 2 additions & 3 deletions data/base/shaders/rect.frag
@@ -1,10 +1,9 @@
#version 130
#version 120

uniform vec4 color = vec4(1.);

out vec4 FragColor;

void main()
{
FragColor = color;
gl_FragColor = color;
}
26 changes: 8 additions & 18 deletions data/base/shaders/rect.vert
@@ -1,5 +1,8 @@
#version 130
#version 120

// gl_VertexID seems to not be supported on 120, despite documentation to the contrary.

// Old comment:
// This shader uses vertex id to generate
// vertex position. This allows to save
// a vertex buffer binding and thus
Expand All @@ -9,26 +12,13 @@ uniform mat4 transformationMatrix;
uniform vec2 tuv_offset;
uniform vec2 tuv_scale;

out vec2 uv;
attribute vec4 rect;

vec4 getPos(int id)
{
if (id == 0) return vec4(0.0, 1.0, 0.0, 1.0);
if (id == 1) return vec4(0.0, 0.0, 0.0, 1.0);
if (id == 2) return vec4(1.0, 1.0, 0.0, 1.0);
if (id == 3) return vec4(1.0, 0.0, 0.0, 1.0);
}
varying vec2 uv;

vec2 getTC(int id)
{
if (id == 0) return vec2(0.0, 1.0);
if (id == 1) return vec2(0.0, 0.0);
if (id == 2) return vec2(1.0, 1.0);
if (id == 3) return vec2(1.0, 0.0);
}

void main()
{
gl_Position = transformationMatrix * getPos(gl_VertexID);
uv = tuv_scale * getTC(gl_VertexID) + tuv_offset;
gl_Position = transformationMatrix * rect;
uv = tuv_scale * rect.xy + tuv_offset;
}
2 changes: 1 addition & 1 deletion data/base/shaders/tcmask.frag
@@ -1,5 +1,5 @@
#version 120
#pragma debug(on)
//#pragma debug(on)

uniform sampler2D Texture; // diffuse
uniform sampler2D TextureTcmask; // tcmask
Expand Down
2 changes: 1 addition & 1 deletion data/base/shaders/tcmask.vert
@@ -1,5 +1,5 @@
#version 120
#pragma debug(on)
//#pragma debug(on)

uniform float stretch;
uniform mat4 ModelViewMatrix;
Expand Down
10 changes: 4 additions & 6 deletions data/base/shaders/text.frag
@@ -1,15 +1,13 @@
#version 130
#version 120

uniform vec4 color = vec4(1.);
uniform sampler2D texture;

in vec2 uv;
out vec4 FragColor;
out vec4 FragColor1;
varying vec2 uv;

void main()
{
vec4 texColour = texture2D(texture, uv) * color.a;
FragColor = texColour * color;
FragColor1 = texColour;
gl_FragData[0] = texColour * color;
gl_FragData[1] = texColour;
}
7 changes: 3 additions & 4 deletions data/base/shaders/texturedrect.frag
@@ -1,12 +1,11 @@
#version 130
#version 120

uniform vec4 color = vec4(1.);
uniform sampler2D texture;

in vec2 uv;
out vec4 FragColor;
varying vec2 uv;

void main()
{
FragColor = texture2D(texture, uv) * color;
gl_FragColor = texture2D(texture, uv) * color;
}
23 changes: 23 additions & 0 deletions lib/ivis_opengl/pieblitfunc.cpp
Expand Up @@ -196,6 +196,19 @@ GFX::~GFX()
}
}

static void enableRect()
{
glEnableVertexAttribArray(VERTEX_POS_ATTRIB_INDEX);
glBindBuffer(GL_ARRAY_BUFFER, pie_internal::rectBuffer);
glVertexAttribPointer(VERTEX_POS_ATTRIB_INDEX, 4, GL_BYTE, false, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

static void disableRect()
{
glDisableVertexAttribArray(VERTEX_POS_ATTRIB_INDEX);
}

void iV_Line(int x0, int y0, int x1, int y1, PIELIGHT colour)
{
pie_SetTexturePage(TEXPAGE_NONE);
Expand All @@ -207,7 +220,9 @@ void iV_Line(int x0, int y0, int x1, int y1, PIELIGHT colour)
);
const auto &mat = glm::ortho(0.f, static_cast<float>(pie_GetVideoBufferWidth()), static_cast<float>(pie_GetVideoBufferHeight()), 0.f);
pie_ActivateShader(SHADER_LINE, glm::vec2(x0, y0), glm::vec2(x1, y1), color, mat);
enableRect();
glDrawArrays(GL_LINES, 0, 2);
disableRect();
pie_DeactivateShader();
}

Expand All @@ -221,12 +236,14 @@ void iV_Lines(const std::vector<glm::ivec4> &lines, PIELIGHT colour)
colour.vector[3] / 255.f
);
const auto &mat = glm::ortho(0.f, static_cast<float>(pie_GetVideoBufferWidth()), static_cast<float>(pie_GetVideoBufferHeight()), 0.f);
enableRect();
for (const auto &line : lines)
{
pie_ActivateShader(SHADER_LINE, glm::vec2(line.x, line.y), glm::vec2(line.z, line.w), color, mat);
glDrawArrays(GL_LINES, 0, 2);
}
pie_DeactivateShader();
disableRect();
}

/**
Expand All @@ -247,7 +264,9 @@ static void pie_DrawRect(float x0, float y0, float x1, float y1, PIELIGHT colour

pie_ActivateShader(SHADER_RECT, mvp,
glm::vec4(colour.vector[0] / 255.f, colour.vector[1] / 255.f, colour.vector[2] / 255.f, colour.vector[3] / 255.f));
enableRect();
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
disableRect();
pie_DeactivateShader();
}

Expand All @@ -271,6 +290,7 @@ void iV_Box2(int x0, int y0, int x1, int y1, PIELIGHT first, PIELIGHT second)
first.vector[2] / 255.f,
first.vector[3] / 255.f
);
enableRect();
pie_ActivateShader(SHADER_LINE, glm::vec2(x0, y1), glm::vec2(x0, y0), firstColor, mat);
glDrawArrays(GL_LINES, 0, 2);
pie_ActivateShader(SHADER_LINE, glm::vec2(x0, y0), glm::vec2(x1, y0), firstColor, mat);
Expand All @@ -287,6 +307,7 @@ void iV_Box2(int x0, int y0, int x1, int y1, PIELIGHT first, PIELIGHT second)
pie_ActivateShader(SHADER_LINE, glm::vec2(x0, y1), glm::vec2(x1, y1), secondColor, mat);
glDrawArrays(GL_LINES, 0, 2);
pie_DeactivateShader();
disableRect();
}

/***************************************************************************/
Expand Down Expand Up @@ -332,7 +353,9 @@ static void iv_DrawImageImpl(Vector2i offset, Vector2i size, Vector2f TextureUV,
Vector2f(TextureUV.x, TextureUV.y),
Vector2f(TextureSize.x, TextureSize.y),
glm::vec4(colour.vector[0] / 255.f, colour.vector[1] / 255.f, colour.vector[2] / 255.f, colour.vector[3] / 255.f), 0);
enableRect();
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
disableRect();
pie_DeactivateShader();
}

Expand Down
15 changes: 14 additions & 1 deletion lib/ivis_opengl/piestate.cpp
Expand Up @@ -49,6 +49,7 @@
std::vector<pie_internal::SHADER_PROGRAM> pie_internal::shaderProgram;
static GLfloat shaderStretch = 0;
SHADER_MODE pie_internal::currentShaderMode = SHADER_NONE;
GLuint pie_internal::rectBuffer = 0;
unsigned int pieStateCount = 0; // Used in pie_GetResetCounts
static RENDER_STATE rendStates;
static GLint ecmState = 0;
Expand Down Expand Up @@ -396,7 +397,7 @@ bool pie_LoadShaders()

// Textured rectangular shader
debug(LOG_3D, "Loading shader: SHADER_TEXRECT");
result = pie_LoadShader("Rect program", "shaders/rect.vert", "shaders/texturedrect.frag",
result = pie_LoadShader("Textured rect program", "shaders/rect.vert", "shaders/texturedrect.frag",
{ "transformationMatrix", "tuv_offset", "tuv_scale", "color", "texture" });
ASSERT_OR_RETURN(false, result && ++shaderEnum == SHADER_TEXRECT, "Failed to load textured rect shader");

Expand Down Expand Up @@ -425,6 +426,18 @@ bool pie_LoadShaders()
ASSERT_OR_RETURN(false, result && ++shaderEnum == SHADER_TEXT, "Failed to load text shader");

pie_internal::currentShaderMode = SHADER_NONE;

GLbyte rect[] {
0, 1, 0, 1,
0, 0, 0, 1,
1, 1, 0, 1,
1, 0, 0, 1
};
glGenBuffers(1, &pie_internal::rectBuffer);
glBindBuffer(GL_ARRAY_BUFFER, pie_internal::rectBuffer);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLbyte), rect, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

return true;
}

Expand Down
1 change: 1 addition & 0 deletions lib/ivis_opengl/piestate.h
Expand Up @@ -110,6 +110,7 @@ namespace pie_internal

extern std::vector<SHADER_PROGRAM> shaderProgram;
extern SHADER_MODE currentShaderMode;
extern GLuint rectBuffer;

/**
* setUniforms is an overloaded wrapper around glUniform* functions
Expand Down

0 comments on commit 4134f82

Please sign in to comment.