Changeset - r24905:e535e9d6c85d
[Not reviewed]
master
0 5 0
Michael Lutz - 3 years ago 2021-01-16 15:43:38
michi@icosahedron.de
Add: [OpenGL] Accelerated mouse cursor drawing.
5 files changed with 481 insertions and 29 deletions:
0 comments (0 inline, 0 general)
src/table/opengl_shader.h
Show inline comments
 
@@ -6,31 +6,39 @@
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file opengl_shader.h OpenGL shader programs. */
 

	
 
/** Vertex shader that just passes colour and tex coords through. */
 
static const char *_vertex_shader_direct[] = {
 
/** Vertex shader that positions a sprite on screen.. */
 
static const char *_vertex_shader_sprite[] = {
 
	"#version 110\n",
 
	"uniform vec4 sprite;",
 
	"uniform vec2 screen;",
 
	"attribute vec2 position, colour_uv;",
 
	"varying vec2 colour_tex_uv;",
 
	"void main() {",
 
	"  vec2 size = sprite.zw / screen.xy;",
 
	"  vec2 offset = ((2.0 * sprite.xy + sprite.zw) / screen.xy - 1.0) * vec2(1.0, -1.0);",
 
	"  colour_tex_uv = colour_uv;",
 
	"  gl_Position = vec4(position, 0.0, 1.0);",
 
	"  gl_Position = vec4(position * size + offset, 0.0, 1.0);",
 
	"}",
 
};
 

	
 
/** GLSL 1.50 vertex shader that just passes colour and tex coords through. */
 
static const char *_vertex_shader_direct_150[] = {
 
/** GLSL 1.50 vertex shader that positions a sprite on screen. */
 
static const char *_vertex_shader_sprite_150[] = {
 
	"#version 150\n",
 
	"uniform vec4 sprite;",
 
	"uniform vec2 screen;",
 
	"in vec2 position, colour_uv;",
 
	"out vec2 colour_tex_uv;",
 
	"void main() {",
 
	"  vec2 size = sprite.zw / screen.xy;",
 
	"  vec2 offset = ((2.0 * sprite.xy + sprite.zw) / screen.xy - 1.0) * vec2(1.0, -1.0);",
 
	"  colour_tex_uv = colour_uv;",
 
	"  gl_Position = vec4(position, 0.0, 1.0);",
 
	"  gl_Position = vec4(position * size + offset, 0.0, 1.0);",
 
	"}",
 
};
 

	
 
/** Fragment shader that reads the fragment colour from a 32bpp texture. */
 
static const char *_frag_shader_direct[] = {
 
	"#version 110\n",
 
@@ -73,6 +81,68 @@ static const char *_frag_shader_palette_
 
	"out vec4 colour;",
 
	"void main() {",
 
	"  float idx = texture(colour_tex, colour_tex_uv).r;",
 
	"  colour = texture(palette, idx);",
 
	"}",
 
};
 

	
 

	
 
/** Fragment shader function for remap brightness modulation. */
 
static const char *_frag_shader_remap_func = \
 
	"float max3(vec3 v) {"
 
	"  return max(max(v.x, v.y), v.z);"
 
	"}"
 
	""
 
	"vec3 adj_brightness(vec3 colour, float brightness) {"
 
	"  vec3 adj = colour * (brightness > 0.0 ? brightness / 0.5 : 1.0);"
 
	"  vec3 ob_vec = clamp(adj - 1.0, 0.0, 1.0);"
 
	"  float ob = (ob_vec.r + ob_vec.g + ob_vec.b) / 2.0;"
 
	""
 
	"  return clamp(adj + ob * (1.0 - adj), 0.0, 1.0);"
 
	"}";
 

	
 
/** Fragment shader that performs a palette lookup to read the colour from an 8bpp texture. */
 
static const char *_frag_shader_rgb_mask_blend[] = {
 
	"#version 110\n",
 
	"#extension GL_ATI_shader_texture_lod: enable\n",
 
	"#extension GL_ARB_shader_texture_lod: enable\n",
 
	"uniform sampler2D colour_tex;",
 
	"uniform sampler1D palette;",
 
	"uniform sampler2D remap_tex;",
 
	"uniform bool rgb;",
 
	"uniform float zoom;",
 
	"varying vec2 colour_tex_uv;",
 
	"",
 
	_frag_shader_remap_func,
 
	"",
 
	"void main() {",
 
	"  float idx = texture2DLod(remap_tex, colour_tex_uv, zoom).r;",
 
	"  vec4 remap_col = texture1D(palette, idx);",
 
	"  vec4 rgb_col = texture2DLod(colour_tex, colour_tex_uv, zoom);",
 
	"",
 
	"  gl_FragData[0].a = rgb ? rgb_col.a : remap_col.a;",
 
	"  gl_FragData[0].rgb = idx > 0.0 ? adj_brightness(remap_col.rgb, max3(rgb_col.rgb)) : rgb_col.rgb;",
 
	"}",
 
};
 

	
 
/** GLSL 1.50 fragment shader that performs a palette lookup to read the colour from an 8bpp texture. */
 
static const char *_frag_shader_rgb_mask_blend_150[] = {
 
	"#version 150\n",
 
	"uniform sampler2D colour_tex;",
 
	"uniform sampler1D palette;",
 
	"uniform sampler2D remap_tex;",
 
	"uniform float zoom;",
 
	"uniform bool rgb;",
 
	"in vec2 colour_tex_uv;",
 
	"out vec4 colour;",
 
	"",
 
	_frag_shader_remap_func,
 
	"",
 
	"void main() {",
 
	"  float idx = textureLod(remap_tex, colour_tex_uv, zoom).r;",
 
	"  vec4 remap_col = texture(palette, idx);",
 
	"  vec4 rgb_col = textureLod(colour_tex, colour_tex_uv, zoom);",
 
	"",
 
	"  colour.a = rgb ? rgb_col.a : remap_col.a;",
 
	"  colour.rgb = idx > 0.0 ? adj_brightness(remap_col.rgb, max3(rgb_col.rgb)) : rgb_col.rgb;",
 
	"}",
 
};
src/video/opengl.cpp
Show inline comments
 
@@ -23,15 +23,17 @@
 
#include "../3rdparty/opengl/glext.h"
 

	
 
#include "opengl.h"
 
#include "../core/geometry_func.hpp"
 
#include "../core/mem_func.hpp"
 
#include "../core/math_func.hpp"
 
#include "../core/mem_func.hpp"
 
#include "../gfx_func.h"
 
#include "../debug.h"
 
#include "../blitter/factory.hpp"
 
#include "../zoom_func.h"
 

	
 
#include "../table/opengl_shader.h"
 

	
 

	
 
#include "../safeguards.h"
 

	
 
@@ -64,12 +66,15 @@ static PFNGLSHADERSOURCEPROC _glShaderSo
 
static PFNGLCOMPILESHADERPROC _glCompileShader;
 
static PFNGLATTACHSHADERPROC _glAttachShader;
 
static PFNGLGETSHADERIVPROC _glGetShaderiv;
 
static PFNGLGETSHADERINFOLOGPROC _glGetShaderInfoLog;
 
static PFNGLGETUNIFORMLOCATIONPROC _glGetUniformLocation;
 
static PFNGLUNIFORM1IPROC _glUniform1i;
 
static PFNGLUNIFORM1FPROC _glUniform1f;
 
static PFNGLUNIFORM2FPROC _glUniform2f;
 
static PFNGLUNIFORM4FPROC _glUniform4f;
 

	
 
static PFNGLGETATTRIBLOCATIONPROC _glGetAttribLocation;
 
static PFNGLENABLEVERTEXATTRIBARRAYPROC _glEnableVertexAttribArray;
 
static PFNGLDISABLEVERTEXATTRIBARRAYPROC _glDisableVertexAttribArray;
 
static PFNGLVERTEXATTRIBPOINTERARBPROC _glVertexAttribPointer;
 
static PFNGLBINDFRAGDATALOCATIONPROC _glBindFragDataLocation;
 
@@ -77,12 +82,15 @@ static PFNGLBINDFRAGDATALOCATIONPROC _gl
 
/** A simple 2D vertex with just position and texture. */
 
struct Simple2DVertex {
 
	float x, y;
 
	float u, v;
 
};
 

	
 
/** Maximum number of cursor sprites to cache. */
 
static const int MAX_CACHED_CURSORS = 48;
 

	
 
/* static */ OpenGLBackend *OpenGLBackend::instance = nullptr;
 

	
 
GetOGLProcAddressProc GetOGLProcAddress;
 

	
 
/**
 
 * Find a substring in a string made of space delimited elements. The substring
 
@@ -228,12 +236,15 @@ static bool BindShaderExtensions()
 
		_glCompileShader = (PFNGLCOMPILESHADERPROC)GetOGLProcAddress("glCompileShader");
 
		_glAttachShader = (PFNGLATTACHSHADERPROC)GetOGLProcAddress("glAttachShader");
 
		_glGetShaderiv = (PFNGLGETSHADERIVPROC)GetOGLProcAddress("glGetShaderiv");
 
		_glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)GetOGLProcAddress("glGetShaderInfoLog");
 
		_glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)GetOGLProcAddress("glGetUniformLocation");
 
		_glUniform1i = (PFNGLUNIFORM1IPROC)GetOGLProcAddress("glUniform1i");
 
		_glUniform1f = (PFNGLUNIFORM1FPROC)GetOGLProcAddress("glUniform1f");
 
		_glUniform2f = (PFNGLUNIFORM2FPROC)GetOGLProcAddress("glUniform2f");
 
		_glUniform4f = (PFNGLUNIFORM4FPROC)GetOGLProcAddress("glUniform4f");
 

	
 
		_glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)GetOGLProcAddress("glGetAttribLocation");
 
		_glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)GetOGLProcAddress("glEnableVertexAttribArray");
 
		_glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)GetOGLProcAddress("glDisableVertexAttribArray");
 
		_glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERARBPROC)GetOGLProcAddress("glVertexAttribPointer");
 
	} else {
 
@@ -250,12 +261,15 @@ static bool BindShaderExtensions()
 
		_glCompileShader = (PFNGLCOMPILESHADERPROC)GetOGLProcAddress("glCompileShaderARB");
 
		_glAttachShader = (PFNGLATTACHSHADERPROC)GetOGLProcAddress("glAttachObjectARB");
 
		_glGetShaderiv = (PFNGLGETSHADERIVPROC)GetOGLProcAddress("glGetObjectParameterivARB");
 
		_glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)GetOGLProcAddress("glGetInfoLogARB");
 
		_glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)GetOGLProcAddress("glGetUniformLocationARB");
 
		_glUniform1i = (PFNGLUNIFORM1IPROC)GetOGLProcAddress("glUniform1iARB");
 
		_glUniform1f = (PFNGLUNIFORM1FPROC)GetOGLProcAddress("glUniform1fARB");
 
		_glUniform2f = (PFNGLUNIFORM2FPROC)GetOGLProcAddress("glUniform2fARB");
 
		_glUniform4f = (PFNGLUNIFORM4FPROC)GetOGLProcAddress("glUniform4fARB");
 

	
 
		_glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)GetOGLProcAddress("glGetAttribLocationARB");
 
		_glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)GetOGLProcAddress("glEnableVertexAttribArrayARB");
 
		_glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)GetOGLProcAddress("glDisableVertexAttribArrayARB");
 
		_glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERARBPROC)GetOGLProcAddress("glVertexAttribPointerARB");
 
	}
 
@@ -268,14 +282,15 @@ static bool BindShaderExtensions()
 
	} else {
 
		_glBindFragDataLocation = nullptr;
 
	}
 

	
 
	return _glCreateProgram != nullptr && _glDeleteProgram != nullptr && _glLinkProgram != nullptr && _glGetProgramiv != nullptr && _glGetProgramInfoLog != nullptr &&
 
		_glCreateShader != nullptr && _glDeleteShader != nullptr && _glShaderSource != nullptr && _glCompileShader != nullptr && _glAttachShader != nullptr &&
 
		_glGetShaderiv != nullptr && _glGetShaderInfoLog != nullptr && _glGetUniformLocation != nullptr && _glUniform1i != nullptr &&
 
		_glGetAttribLocation != nullptr && _glEnableVertexAttribArray != nullptr && _glDisableVertexAttribArray != nullptr && _glVertexAttribPointer != nullptr;
 
		_glGetShaderiv != nullptr && _glGetShaderInfoLog != nullptr && _glGetUniformLocation != nullptr && _glUniform1i != nullptr && _glUniform1f != nullptr &&
 
		_glUniform2f != nullptr && _glUniform4f != nullptr && _glGetAttribLocation != nullptr && _glEnableVertexAttribArray != nullptr && _glDisableVertexAttribArray != nullptr &&
 
		_glVertexAttribPointer != nullptr;
 
}
 

	
 
/** Callback to receive OpenGL debug messages. */
 
void APIENTRY DebugOutputCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
 
{
 
	/* Make severity human readable. */
 
@@ -353,22 +368,26 @@ void SetupDebugOutput()
 
	OpenGLBackend::instance = nullptr;
 
}
 

	
 
/**
 
 * Construct OpenGL back-end class.
 
 */
 
OpenGLBackend::OpenGLBackend()
 
OpenGLBackend::OpenGLBackend() : cursor_cache(MAX_CACHED_CURSORS)
 
{
 
}
 

	
 
/**
 
 * Free allocated resources.
 
 */
 
OpenGLBackend::~OpenGLBackend()
 
{
 
	ClearCursorCache();
 
	OpenGLSprite::Destroy();
 

	
 
	if (_glDeleteProgram != nullptr) {
 
		_glDeleteProgram(this->remap_program);
 
		_glDeleteProgram(this->vid_program);
 
		_glDeleteProgram(this->pal_program);
 
	}
 
	if (_glDeleteVertexArrays != nullptr) _glDeleteVertexArrays(1, &this->vao_quad);
 
	if (_glDeleteBuffers != nullptr) {
 
		_glDeleteBuffers(1, &this->vbo_quad);
 
@@ -416,13 +435,18 @@ const char *OpenGLBackend::Init()
 
	if (!BindVBAExtension()) return "Failed to bind VBA extension functions";
 
	/* Check for shader objects. */
 
	if (!IsOpenGLVersionAtLeast(2, 0) && (!IsOpenGLExtensionSupported("GL_ARB_shader_objects") || !IsOpenGLExtensionSupported("GL_ARB_fragment_shader") || !IsOpenGLExtensionSupported("GL_ARB_vertex_shader"))) return "No shader support";
 
	if (!BindShaderExtensions()) return "Failed to bind shader extension functions";
 
	if (IsOpenGLVersionAtLeast(3, 2) && _glBindFragDataLocation == nullptr) return "OpenGL claims to support version 3.2 but doesn't have glBindFragDataLocation";
 

	
 
	DEBUG(driver, 2, "OpenGL shading language version: %s", (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
 
	/* Check available texture units. */
 
	GLint max_tex_units = 0;
 
	glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_tex_units);
 
	if (max_tex_units < 3) return "Not enough simultaneous textures supported";
 

	
 
	DEBUG(driver, 2, "OpenGL shading language version: %s, texture units = %d", (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION), (int)max_tex_units);
 

	
 
	if (!this->InitShaders()) return "Failed to initialize shaders";
 

	
 
	/* Setup video buffer texture. */
 
	glGenTextures(1, &this->vid_texture);
 
	glBindTexture(GL_TEXTURE_2D, this->vid_texture);
 
@@ -443,25 +467,47 @@ const char *OpenGLBackend::Init()
 
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
	glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 256, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
 
	glBindTexture(GL_TEXTURE_1D, 0);
 
	if (glGetError() != GL_NO_ERROR) return "Can't generate palette lookup texture";
 

	
 
	/* Bind uniforms in RGB rendering shader program. */
 
	/* Bind uniforms in rendering shader program. */
 
	GLint tex_location = _glGetUniformLocation(this->vid_program, "colour_tex");
 
	GLint palette_location = _glGetUniformLocation(this->vid_program, "palette");
 
	GLint sprite_location = _glGetUniformLocation(this->vid_program, "sprite");
 
	GLint screen_location = _glGetUniformLocation(this->vid_program, "screen");
 
	_glUseProgram(this->vid_program);
 
	_glUniform1i(tex_location, 0);     // Texture unit 0.
 
	_glUniform1i(palette_location, 1); // Texture unit 1.
 
	/* Values that result in no transform. */
 
	_glUniform4f(sprite_location, 0.0f, 0.0f, 1.0f, 1.0f);
 
	_glUniform2f(screen_location, 1.0f, 1.0f);
 

	
 
	/* Bind uniforms in palette rendering shader program. */
 
	tex_location = _glGetUniformLocation(this->pal_program, "colour_tex");
 
	palette_location = _glGetUniformLocation(this->pal_program, "palette");
 
	sprite_location = _glGetUniformLocation(this->pal_program, "sprite");
 
	screen_location = _glGetUniformLocation(this->pal_program, "screen");
 
	_glUseProgram(this->pal_program);
 
	_glUniform1i(tex_location, 0);     // Texture unit 0.
 
	_glUniform1i(palette_location, 1); // Texture unit 1.
 
	_glUniform4f(sprite_location, 0.0f, 0.0f, 1.0f, 1.0f);
 
	_glUniform2f(screen_location, 1.0f, 1.0f);
 

	
 
	/* Bind uniforms in remap shader program. */
 
	tex_location = _glGetUniformLocation(this->remap_program, "colour_tex");
 
	palette_location = _glGetUniformLocation(this->remap_program, "palette");
 
	GLint remap_location = _glGetUniformLocation(this->remap_program, "remap_tex");
 
	this->remap_sprite_loc = _glGetUniformLocation(this->remap_program, "sprite");
 
	this->remap_screen_loc = _glGetUniformLocation(this->remap_program, "screen");
 
	this->remap_zoom_loc = _glGetUniformLocation(this->remap_program, "zoom");
 
	this->remap_rgb_loc = _glGetUniformLocation(this->remap_program, "rgb");
 
	_glUseProgram(this->remap_program);
 
	_glUniform1i(tex_location, 0);     // Texture unit 0.
 
	_glUniform1i(palette_location, 1); // Texture unit 1.
 
	_glUniform1i(remap_location, 2);   // Texture unit 2.
 

	
 
	/* Create pixel buffer object as video buffer storage. */
 
	_glGenBuffers(1, &this->vid_pbo);
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
 
	if (glGetError() != GL_NO_ERROR) return "Can't allocate pixel buffer for video buffer";
 

	
 
@@ -491,14 +537,20 @@ const char *OpenGLBackend::Init()
 
	_glEnableVertexAttribArray(loc_position);
 
	_glEnableVertexAttribArray(colour_position);
 
	_glVertexAttribPointer(loc_position, 2, GL_FLOAT, GL_FALSE, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, x));
 
	_glVertexAttribPointer(colour_position, 2, GL_FLOAT, GL_FALSE, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, u));
 
	_glBindVertexArray(0);
 

	
 
	/* Create resources for sprite rendering. */
 
	if (!OpenGLSprite::Create()) return "Failed to create sprite rendering resources";
 

	
 
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 
	glDisable(GL_DEPTH_TEST);
 
	/* Enable alpha blending using the src alpha factor. */
 
	glEnable(GL_BLEND);
 
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
	(void)glGetError(); // Clear errors.
 

	
 
	return nullptr;
 
}
 

	
 
/**
 
@@ -560,13 +612,13 @@ bool OpenGLBackend::InitShaders()
 
	int glsl_minor = ver[2] - '0';
 

	
 
	bool glsl_150 = (IsOpenGLVersionAtLeast(3, 2) || glsl_major > 1 || (glsl_major == 1 && glsl_minor >= 5)) && _glBindFragDataLocation != nullptr;
 

	
 
	/* Create vertex shader. */
 
	GLuint vert_shader = _glCreateShader(GL_VERTEX_SHADER);
 
	_glShaderSource(vert_shader, glsl_150 ? lengthof(_vertex_shader_direct_150) : lengthof(_vertex_shader_direct), glsl_150 ? _vertex_shader_direct_150 : _vertex_shader_direct, nullptr);
 
	_glShaderSource(vert_shader, glsl_150 ? lengthof(_vertex_shader_sprite_150) : lengthof(_vertex_shader_sprite), glsl_150 ? _vertex_shader_sprite_150 : _vertex_shader_sprite, nullptr);
 
	_glCompileShader(vert_shader);
 
	if (!VerifyShader(vert_shader)) return false;
 

	
 
	/* Create fragment shader for plain RGBA. */
 
	GLuint frag_shader_rgb = _glCreateShader(GL_FRAGMENT_SHADER);
 
	_glShaderSource(frag_shader_rgb, glsl_150 ? lengthof(_frag_shader_direct_150) : lengthof(_frag_shader_direct), glsl_150 ? _frag_shader_direct_150 : _frag_shader_direct, nullptr);
 
@@ -576,36 +628,51 @@ bool OpenGLBackend::InitShaders()
 
	/* Create fragment shader for paletted only. */
 
	GLuint frag_shader_pal = _glCreateShader(GL_FRAGMENT_SHADER);
 
	_glShaderSource(frag_shader_pal, glsl_150 ? lengthof(_frag_shader_palette_150) : lengthof(_frag_shader_palette), glsl_150 ? _frag_shader_palette_150 : _frag_shader_palette, nullptr);
 
	_glCompileShader(frag_shader_pal);
 
	if (!VerifyShader(frag_shader_pal)) return false;
 

	
 
	/* Sprite remap fragment shader. */
 
	GLuint remap_shader = _glCreateShader(GL_FRAGMENT_SHADER);
 
	_glShaderSource(remap_shader, glsl_150 ? lengthof(_frag_shader_rgb_mask_blend_150) : lengthof(_frag_shader_rgb_mask_blend), glsl_150 ? _frag_shader_rgb_mask_blend_150 : _frag_shader_rgb_mask_blend, nullptr);
 
	_glCompileShader(remap_shader);
 
	if (!VerifyShader(remap_shader)) return false;
 

	
 
	/* Link shaders to program. */
 
	this->vid_program = _glCreateProgram();
 
	_glAttachShader(this->vid_program, vert_shader);
 
	_glAttachShader(this->vid_program, frag_shader_rgb);
 

	
 
	this->pal_program = _glCreateProgram();
 
	_glAttachShader(this->pal_program, vert_shader);
 
	_glAttachShader(this->pal_program, frag_shader_pal);
 

	
 
	this->remap_program = _glCreateProgram();
 
	_glAttachShader(this->remap_program, vert_shader);
 
	_glAttachShader(this->remap_program, remap_shader);
 

	
 
	if (glsl_150) {
 
		/* Bind fragment shader outputs. */
 
		_glBindFragDataLocation(this->vid_program, 0, "colour");
 
		_glBindFragDataLocation(this->pal_program, 0, "colour");
 
		_glBindFragDataLocation(this->remap_program, 0, "colour");
 
	}
 

	
 
	_glLinkProgram(this->vid_program);
 
	if (!VerifyProgram(this->vid_program)) return false;
 

	
 
	_glLinkProgram(this->pal_program);
 
	if (!VerifyProgram(this->pal_program)) return false;
 

	
 
	_glLinkProgram(this->remap_program);
 
	if (!VerifyProgram(this->remap_program)) return false;
 

	
 
	_glDeleteShader(vert_shader);
 
	_glDeleteShader(frag_shader_rgb);
 
	_glDeleteShader(frag_shader_pal);
 
	_glDeleteShader(remap_shader);
 

	
 
	return true;
 
}
 

	
 
/**
 
 * Change the size of the drawing window and allocate matching resources.
 
@@ -653,12 +720,16 @@ bool OpenGLBackend::Resize(int w, int h,
 
	/* Set new viewport. */
 
	_screen.height = h;
 
	_screen.width = w;
 
	_screen.pitch = pitch;
 
	_screen.dst_ptr = nullptr;
 

	
 
	/* Update screen size in remap shader program. */
 
	_glUseProgram(this->remap_program);
 
	_glUniform2f(this->remap_screen_loc, (float)_screen.width, (float)_screen.height);
 

	
 
	return true;
 
}
 

	
 
/**
 
 * Update the stored palette.
 
 * @param pal Palette array with at least 256 elements.
 
@@ -680,20 +751,60 @@ void OpenGLBackend::UpdatePalette(const 
 
 * Render video buffer to the screen.
 
 */
 
void OpenGLBackend::Paint()
 
{
 
	glClear(GL_COLOR_BUFFER_BIT);
 

	
 
	glDisable(GL_BLEND);
 

	
 
	/* Blit video buffer to screen. */
 
	_glActiveTexture(GL_TEXTURE0);
 
	glBindTexture(GL_TEXTURE_2D, this->vid_texture);
 
	_glActiveTexture(GL_TEXTURE1);
 
	glBindTexture(GL_TEXTURE_1D, this->pal_texture);
 
	_glUseProgram(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 8 ? this->pal_program : this->vid_program);
 
	_glBindVertexArray(this->vao_quad);
 
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 

	
 
	glEnable(GL_BLEND);
 
}
 

	
 
/**
 
 * Draw mouse cursor on screen.
 
 */
 
void OpenGLBackend::DrawMouseCursor()
 
{
 
	/* Draw cursor on screen */
 
	_cur_dpi = &_screen;
 
	for (uint i = 0; i < _cursor.sprite_count; ++i) {
 
		SpriteID sprite = _cursor.sprite_seq[i].sprite;
 

	
 
		if (!this->cursor_cache.Contains(sprite)) {
 
			Sprite *old = this->cursor_cache.Insert(sprite, (Sprite *)GetRawSprite(sprite, ST_NORMAL, &SimpleSpriteAlloc, this));
 
			if (old != nullptr) {
 
				OpenGLSprite *sprite = (OpenGLSprite *)old->data;
 
				sprite->~OpenGLSprite();
 
				free(old);
 
			}
 
		}
 

	
 
		this->RenderOglSprite((OpenGLSprite *)this->cursor_cache.Get(sprite)->data, _cursor.pos.x + _cursor.sprite_pos[i].x, _cursor.pos.y + _cursor.sprite_pos[i].y, ZOOM_LVL_GUI);
 
	}
 
}
 

	
 
/**
 
 * Clear all cached cursor sprites.
 
 */
 
void OpenGLBackend::ClearCursorCache()
 
{
 
	Sprite *sp;
 
	while ((sp = this->cursor_cache.Pop()) != nullptr) {
 
		OpenGLSprite *sprite = (OpenGLSprite *)sp->data;
 
		sprite->~OpenGLSprite();
 
		free(sp);
 
	}
 
}
 

	
 
/**
 
 * Get a pointer to the memory for the video driver to draw to.
 
 * @return Pointer to draw on.
 
 */
 
@@ -728,6 +839,223 @@ void OpenGLBackend::ReleaseVideoBuffer(c
 
				glTexSubImage2D(GL_TEXTURE_2D, 0, update_rect.left, update_rect.top, update_rect.right - update_rect.left, update_rect.bottom - update_rect.top, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, (GLvoid *)(size_t)(update_rect.top * _screen.pitch * 4 + update_rect.left * 4));
 
				break;
 
		}
 
	}
 
}
 

	
 
/* virtual */ Sprite *OpenGLBackend::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
 
{
 
	/* Allocate and construct sprite data. */
 
	Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(OpenGLSprite));
 

	
 
	OpenGLSprite *gl_sprite = (OpenGLSprite *)dest_sprite->data;
 
	new (gl_sprite) OpenGLSprite(sprite->width, sprite->height, sprite->type == ST_FONT ? 1 : ZOOM_LVL_COUNT, sprite->colours);
 

	
 
	/* Upload texture data. */
 
	for (int i = 0; i < (sprite->type == ST_FONT ? 1 : ZOOM_LVL_COUNT); i++) {
 
		gl_sprite->Update(sprite[i].width, sprite[i].height, i, sprite[i].data);
 
	}
 

	
 
	dest_sprite->height = sprite->height;
 
	dest_sprite->width  = sprite->width;
 
	dest_sprite->x_offs = sprite->x_offs;
 
	dest_sprite->y_offs = sprite->y_offs;
 

	
 
	return dest_sprite;
 
}
 

	
 
/**
 
 * Render a sprite to the back buffer.
 
 * @param gl_sprite Sprite to render.
 
 * @param x X position of the sprite.
 
 * @param y Y position of the sprite.
 
 * @param zoom Zoom level to use.
 
 */
 
void OpenGLBackend::RenderOglSprite(OpenGLSprite *gl_sprite, uint x, uint y, ZoomLevel zoom)
 
{
 
	/* Set textures. */
 
	bool rgb = gl_sprite->BindTextures();
 
	_glActiveTexture(GL_TEXTURE0 + 1);
 
	glBindTexture(GL_TEXTURE_1D, this->pal_texture);
 

	
 
	/* Set up shader program. */
 
	Dimension dim = gl_sprite->GetSize(zoom);
 
	_glUseProgram(this->remap_program);
 
	_glUniform4f(this->remap_sprite_loc, (float)x, (float)y, (float)dim.width, (float)dim.height);
 
	_glUniform1f(this->remap_zoom_loc, (float)(zoom - ZOOM_LVL_BEGIN));
 
	_glUniform2f(this->remap_screen_loc, (float)_screen.width, (float)_screen.height);
 
	_glUniform1i(this->remap_rgb_loc,  rgb ? 1 : 0);
 

	
 
	_glBindVertexArray(this->vao_quad);
 
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
}
 

	
 

	
 
/* static */ GLuint OpenGLSprite::dummy_tex[] = { 0, 0 };
 

	
 
/**
 
 * Create all common resources for sprite rendering.
 
 * @return True if no error occurred.
 
 */
 
/* static */ bool OpenGLSprite::Create()
 
{
 
	glGenTextures(NUM_TEX, OpenGLSprite::dummy_tex);
 

	
 
	for (int t = TEX_RGBA; t < NUM_TEX; t++) {
 
		glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[t]);
 

	
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
	}
 

	
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 
	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 

	
 
	/* Load dummy RGBA texture. */
 
	const Colour rgb_pixel(0, 0, 0);
 
	glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[TEX_RGBA]);
 
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &rgb_pixel);
 

	
 
	/* Load dummy remap texture. */
 
	const uint pal = 0;
 
	glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[TEX_REMAP]);
 
	glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &pal);
 

	
 
	return glGetError() == GL_NO_ERROR;
 
}
 

	
 
/** Free all common resources for sprite rendering. */
 
/* static */ void OpenGLSprite::Destroy()
 
{
 
	glDeleteTextures(NUM_TEX, OpenGLSprite::dummy_tex);
 
}
 

	
 
/**
 
 * Create an OpenGL sprite with a palette remap part.
 
 * @param width Width of the top-level texture.
 
 * @param height Height of the top-level texture.
 
 * @param levels Number of mip-map levels.
 
 * @param components Indicates which sprite components are used.
 
 */
 
OpenGLSprite::OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components)
 
{
 
	assert(levels > 0);
 
	(void)glGetError();
 

	
 
	this->dim.width = width;
 
	this->dim.height = height;
 

	
 
	MemSetT(this->tex, 0, NUM_TEX);
 
	_glActiveTexture(GL_TEXTURE0);
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 

	
 
	for (int t = TEX_RGBA; t < NUM_TEX; t++) {
 
		/* Sprite component present? */
 
		if (t == TEX_RGBA && components == SCC_PAL) continue;
 
		if (t == TEX_REMAP && (components & SCC_PAL) != SCC_PAL) continue;
 

	
 
		/* Allocate texture. */
 
		glGenTextures(1, &this->tex[t]);
 
		glBindTexture(GL_TEXTURE_2D, this->tex[t]);
 

	
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, levels - 1);
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 

	
 
		/* Set size. */
 
		for (uint i = 0, w = width, h = height; i < levels; i++, w /= 2, h /= 2) {
 
			assert(w * h != 0);
 
			if (t == TEX_REMAP) {
 
				glTexImage2D(GL_TEXTURE_2D, i, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
 
			} else {
 
				glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
 
			}
 
		}
 
	}
 

	
 
	assert(glGetError() == GL_NO_ERROR);
 
}
 

	
 
OpenGLSprite::~OpenGLSprite()
 
{
 
	glDeleteTextures(NUM_TEX, this->tex);
 
}
 

	
 
/**
 
 * Update a single mip-map level with new pixel data.
 
 * @param width Width of the level.
 
 * @param height Height of the level.
 
 * @param level Mip-map level.
 
 * @param data New pixel data.
 
 */
 
void OpenGLSprite::Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel * data)
 
{
 
	static ReusableBuffer<Colour> buf_rgba;
 
	static ReusableBuffer<uint8> buf_pal;
 

	
 
	_glActiveTexture(GL_TEXTURE0);
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 
	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 

	
 
	if (this->tex[TEX_RGBA] != 0) {
 
		/* Unpack pixel data */
 
		Colour *rgba = buf_rgba.Allocate(width * height);
 
		for (size_t i = 0; i < width * height; i++) {
 
			rgba[i].r = data[i].r;
 
			rgba[i].g = data[i].g;
 
			rgba[i].b = data[i].b;
 
			rgba[i].a = data[i].a;
 
		}
 

	
 
		glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA]);
 
		glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, rgba);
 
	}
 

	
 
	if (this->tex[TEX_REMAP] != 0) {
 
		/* Unpack and align pixel data. */
 
		int pitch = Align(width, 4);
 

	
 
		uint8 *pal = buf_pal.Allocate(pitch * height);
 
		const SpriteLoader::CommonPixel *row = data;
 
		for (uint y = 0; y < height; y++, pal += pitch, row += width) {
 
			for (uint x = 0; x < width; x++) {
 
				pal[x] = row[x].m;
 
			}
 
		}
 

	
 
		glBindTexture(GL_TEXTURE_2D, this->tex[TEX_REMAP]);
 
		glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, buf_pal.GetBuffer());
 
	}
 

	
 
	assert(glGetError() == GL_NO_ERROR);
 
}
 

	
 
/**
 
 * Query the sprite size at a certain zoom level.
 
 * @param level The zoom level to query.
 
 * @return Sprite size at the given zoom level.
 
 */
 
inline Dimension OpenGLSprite::GetSize(ZoomLevel level) const
 
{
 
	Dimension sd = { (uint)UnScaleByZoomLower(this->dim.width, level), (uint)UnScaleByZoomLower(this->dim.height, level) };
 
	return sd;
 
}
 

	
 
/**
 
 * Bind textures for rendering this sprite.
 
 * @return True if the sprite has RGBA data.
 
 */
 
bool OpenGLSprite::BindTextures()
 
{
 
	_glActiveTexture(GL_TEXTURE0);
 
	glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA] != 0 ? this->tex[TEX_RGBA] : OpenGLSprite::dummy_tex[TEX_RGBA]);
 
	_glActiveTexture(GL_TEXTURE0 + 2);
 
	glBindTexture(GL_TEXTURE_2D, this->tex[TEX_REMAP] != 0 ? this->tex[TEX_REMAP] : OpenGLSprite::dummy_tex[TEX_REMAP]);
 

	
 
	return this->tex[TEX_RGBA] != 0;
 
}
 

	
src/video/opengl.h
Show inline comments
 
@@ -12,38 +12,52 @@
 
#ifndef VIDEO_OPENGL_H
 
#define VIDEO_OPENGL_H
 

	
 
#include "../core/alloc_type.hpp"
 
#include "../core/geometry_type.hpp"
 
#include "../gfx_type.h"
 
#include "../spriteloader/spriteloader.hpp"
 
#include "../misc/lrucache.hpp"
 

	
 
typedef void (*OGLProc)();
 
typedef OGLProc (*GetOGLProcAddressProc)(const char *proc);
 

	
 
bool IsOpenGLVersionAtLeast(byte major, byte minor);
 
const char *FindStringInExtensionList(const char *string, const char *substring);
 

	
 
class OpenGLSprite;
 

	
 
/** Platform-independent back-end class for OpenGL video drivers. */
 
class OpenGLBackend : public ZeroedMemoryAllocator {
 
class OpenGLBackend : public ZeroedMemoryAllocator, SpriteEncoder {
 
private:
 
	static OpenGLBackend *instance; ///< Singleton instance pointer.
 

	
 
	GLuint vid_pbo;     ///< Pixel buffer object storing the memory used for the video driver to draw to.
 
	GLuint vid_texture; ///< Texture handle for the video buffer texture.
 
	GLuint vid_program; ///< Shader program for rendering a RGBA video buffer.
 
	GLuint pal_program; ///< Shader program for rendering a paletted video buffer.
 
	GLuint vao_quad;    ///< Vertex array object storing the rendering state for the fullscreen quad.
 
	GLuint vbo_quad;    ///< Vertex buffer with a fullscreen quad.
 
	GLuint pal_texture; ///< Palette lookup texture.
 

	
 
	GLuint remap_program;    ///< Shader program for blending and rendering a RGBA + remap texture.
 
	GLint  remap_sprite_loc; ///< Uniform location for sprite parameters.
 
	GLint  remap_screen_loc; ///< Uniform location for screen size;
 
	GLint  remap_zoom_loc;   ///< Uniform location for sprite zoom;
 
	GLint  remap_rgb_loc;    ///< Uniform location for RGB mode flag;
 

	
 
	LRUCache<SpriteID, Sprite> cursor_cache; ///< Cache of encoded cursor sprites.
 

	
 
	OpenGLBackend();
 
	~OpenGLBackend();
 

	
 
	const char *Init();
 
	bool InitShaders();
 

	
 
	void RenderOglSprite(OpenGLSprite *gl_sprite, uint x, uint y, ZoomLevel zoom);
 

	
 
public:
 
	/** Get singleton instance of this class. */
 
	static inline OpenGLBackend *Get()
 
	{
 
		return OpenGLBackend::instance;
 
	}
 
@@ -51,11 +65,51 @@ public:
 
	static void Destroy();
 

	
 
	void UpdatePalette(const Colour *pal, uint first, uint length);
 
	bool Resize(int w, int h, bool force = false);
 
	void Paint();
 

	
 
	void DrawMouseCursor();
 
	void ClearCursorCache();
 

	
 
	void *GetVideoBuffer();
 
	void ReleaseVideoBuffer(const Rect &update_rect);
 

	
 
	/* SpriteEncoder */
 

	
 
	bool Is32BppSupported() override { return true; }
 
	uint GetSpriteAlignment() override { return 1u << (ZOOM_LVL_COUNT - 1); }
 
	Sprite *Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override;
 
};
 

	
 

	
 
/** Class that encapsulates a RGBA texture together with a paletted remap texture. */
 
class OpenGLSprite {
 
private:
 
	/** Enum of all used OpenGL texture objects. */
 
	enum Texture {
 
		TEX_RGBA,    ///< RGBA texture part.
 
		TEX_REMAP,   ///< Remap texture part.
 
		NUM_TEX
 
	};
 

	
 
	Dimension dim;
 
	GLuint tex[NUM_TEX]; ///< The texture objects.
 

	
 
	static GLuint dummy_tex[NUM_TEX]; ///< 1x1 dummy textures to substitute for unused sprite components.
 

	
 
	static bool Create();
 
	static void Destroy();
 

	
 
	bool BindTextures();
 

	
 
public:
 
	OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components);
 
	~OpenGLSprite();
 

	
 
	void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data);
 
	Dimension GetSize(ZoomLevel level) const;
 

	
 
	friend class OpenGLBackend;
 
};
 

	
 
#endif /* VIDEO_OPENGL_H */
src/video/win32_v.cpp
Show inline comments
 
@@ -1517,12 +1517,17 @@ bool VideoDriver_Win32OpenGL::AfterBlitt
 
{
 
	assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
 
	this->ClientSizeChanged(this->width, this->height, true);
 
	return true;
 
}
 

	
 
void VideoDriver_Win32OpenGL::ClearSystemSprites()
 
{
 
	OpenGLBackend::Get()->ClearCursorCache();
 
}
 

	
 
bool VideoDriver_Win32OpenGL::AllocateBackingStore(int w, int h, bool force)
 
{
 
	if (!force && w == _screen.width && h == _screen.height) return false;
 

	
 
	this->width = w = std::max(w, 64);
 
	this->height = h = std::max(h, 64);
 
@@ -1551,34 +1556,25 @@ void VideoDriver_Win32OpenGL::ReleaseVid
 
}
 

	
 
void VideoDriver_Win32OpenGL::Paint()
 
{
 
	PerformanceMeasurer framerate(PFE_VIDEO);
 

	
 
	if (IsEmptyRect(this->dirty_rect)) return;
 

	
 
	if (_cur_palette.count_dirty != 0) {
 
		Blitter *blitter = BlitterFactory::GetCurrentBlitter();
 

	
 
		switch (blitter->UsePaletteAnimation()) {
 
			case Blitter::PALETTE_ANIMATION_BLITTER:
 
				blitter->PaletteAnimate(_local_palette);
 
				break;
 
		/* Always push a changed palette to OpenGL. */
 
		OpenGLBackend::Get()->UpdatePalette(_local_palette.palette, _local_palette.first_dirty, _local_palette.count_dirty);
 
		if (blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_BLITTER) {
 
			blitter->PaletteAnimate(_local_palette);
 
		}
 

	
 
			case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
 
				OpenGLBackend::Get()->UpdatePalette(_local_palette.palette, _local_palette.first_dirty, _local_palette.count_dirty);
 
				break;
 

	
 
			case Blitter::PALETTE_ANIMATION_NONE:
 
				break;
 

	
 
			default:
 
				NOT_REACHED();
 
		}
 
		_cur_palette.count_dirty = 0;
 
	}
 

	
 
	OpenGLBackend::Get()->Paint();
 
	if (_cursor.in_window) OpenGLBackend::Get()->DrawMouseCursor();
 

	
 
	SwapBuffers(this->dc);
 
}
 

	
 
#endif /* WITH_OPENGL */
src/video/win32_v.h
Show inline comments
 
@@ -137,12 +137,16 @@ public:
 
	bool ToggleFullscreen(bool fullscreen) override;
 

	
 
	bool AfterBlitterChange() override;
 

	
 
	bool HasEfficient8Bpp() const override { return true; }
 

	
 
	bool UseSystemCursor() override { return true; }
 

	
 
	void ClearSystemSprites() override;
 

	
 
	const char *GetName() const override { return "win32-opengl"; }
 

	
 
protected:
 
	HDC   dc;          ///< Window device context.
 
	HGLRC gl_rc;       ///< OpenGL context.
 
	bool  vsync;       ///< Enable VSync?
0 comments (0 inline, 0 general)