Changeset - r24908:4dfe7bc46264
[Not reviewed]
master
0 6 0
Michael Lutz - 3 years ago 2021-01-16 15:43:42
michi@icosahedron.de
Add: [OpenGL] Support for a separate animation buffer that stores the palette values of the screen in addition to the colour buffer.
6 files changed with 122 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/blitter/base.hpp
Show inline comments
 
@@ -183,12 +183,20 @@ public:
 
	 * Check if the blitter uses palette animation at all.
 
	 * @return True if it uses palette animation.
 
	 */
 
	virtual Blitter::PaletteAnimation UsePaletteAnimation() = 0;
 

	
 
	/**
 
	 * Does this blitter require a separate animation buffer from the video backend?
 
	 */
 
	virtual bool NeedsAnimationBuffer()
 
	{
 
		return false;
 
	}
 

	
 
	/**
 
	 * Get the name of the blitter, the same as the Factory-instance returns.
 
	 */
 
	virtual const char *GetName() = 0;
 

	
 
	/**
 
	 * Get how many bytes are needed to store a pixel.
src/video/opengl.cpp
Show inline comments
 
@@ -389,14 +389,16 @@ OpenGLBackend::~OpenGLBackend()
 
		_glDeleteProgram(this->pal_program);
 
	}
 
	if (_glDeleteVertexArrays != nullptr) _glDeleteVertexArrays(1, &this->vao_quad);
 
	if (_glDeleteBuffers != nullptr) {
 
		_glDeleteBuffers(1, &this->vbo_quad);
 
		_glDeleteBuffers(1, &this->vid_pbo);
 
		_glDeleteBuffers(1, &this->anim_pbo);
 
	}
 
	glDeleteTextures(1, &this->vid_texture);
 
	glDeleteTextures(1, &this->anim_texture);
 
	glDeleteTextures(1, &this->pal_texture);
 
}
 

	
 
/**
 
 * Check for the needed OpenGL functionality and allocate all resources.
 
 * @return Error string or nullptr if successful.
 
@@ -455,12 +457,23 @@ const char *OpenGLBackend::Init()
 
	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);
 
	glBindTexture(GL_TEXTURE_2D, 0);
 
	if (glGetError() != GL_NO_ERROR) return "Can't generate video buffer texture";
 

	
 
	/* Setup video buffer texture. */
 
	glGenTextures(1, &this->anim_texture);
 
	glBindTexture(GL_TEXTURE_2D, this->anim_texture);
 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_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);
 
	glBindTexture(GL_TEXTURE_2D, 0);
 
	if (glGetError() != GL_NO_ERROR) return "Can't generate animation buffer texture";
 

	
 
	/* Setup palette texture. */
 
	glGenTextures(1, &this->pal_texture);
 
	glBindTexture(GL_TEXTURE_1D, this->pal_texture);
 
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
 
@@ -502,16 +515,19 @@ const char *OpenGLBackend::Init()
 
	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.
 
	(void)glGetError(); // Clear errors.
 

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

	
 
	/* Prime vertex buffer with a full-screen quad and store
 
	 * the corresponding state in a vertex array object. */
 
	static const Simple2DVertex vert_array[] = {
 
		//  x     y    u    v
 
@@ -683,13 +699,13 @@ bool OpenGLBackend::InitShaders()
 
 */
 
bool OpenGLBackend::Resize(int w, int h, bool force)
 
{
 
	if (!force && _screen.width == w && _screen.height == h) return false;
 

	
 
	int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
 
	int pitch = bpp != 32 ? Align(w, 4) : w;
 
	int pitch = Align(w, 4);
 

	
 
	glViewport(0, 0, w, h);
 

	
 
	/* Re-allocate video buffer texture and backing store. */
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
 
	_glBufferData(GL_PIXEL_UNPACK_BUFFER, pitch * h * bpp / 8, nullptr, GL_DYNAMIC_READ); // Buffer content has to persist from frame to frame and is read back by the blitter, which means a READ usage hint.
 
@@ -712,12 +728,29 @@ bool OpenGLBackend::Resize(int w, int h,
 
			break;
 

	
 
		default:
 
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
 
			break;
 
	}
 

	
 
	/* Does this blitter need a separate animation buffer? */
 
	if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
 
		_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
 
		_glBufferData(GL_PIXEL_UNPACK_BUFFER, pitch * h, NULL, GL_DYNAMIC_READ); // Buffer content has to persist from frame to frame and is read back by the blitter, which means a READ usage hint.
 
		_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 

	
 
		glBindTexture(GL_TEXTURE_2D, this->anim_texture);
 
		glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
 
	} else {
 
		/* Allocate dummy texture that always reads as 0 == no remap. */
 
		uint dummy = 0;
 
		glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
 
		glBindTexture(GL_TEXTURE_2D, this->anim_texture);
 
		glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &dummy);
 
	}
 

	
 
	glBindTexture(GL_TEXTURE_2D, 0);
 

	
 
	/* Set new viewport. */
 
	_screen.height = h;
 
	_screen.width = w;
 
	_screen.pitch = pitch;
 
@@ -758,13 +791,24 @@ void OpenGLBackend::Paint()
 

	
 
	/* 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);
 
	/* Is the blitter relying on a separate animation buffer? */
 
	if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
 
		_glActiveTexture(GL_TEXTURE2);
 
		glBindTexture(GL_TEXTURE_2D, this->anim_texture);
 
		_glUseProgram(this->remap_program);
 
		_glUniform4f(this->remap_sprite_loc, 0.0f, 0.0f, 1.0f, 1.0f);
 
		_glUniform2f(this->remap_screen_loc, 1.0f, 1.0f);
 
		_glUniform1f(this->remap_zoom_loc, 0);
 
		_glUniform1i(this->remap_rgb_loc, 1);
 
	} else {
 
		_glUseProgram(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 8 ? this->pal_program : this->vid_program);
 
	}
 
	_glBindVertexArray(this->vao_quad);
 
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 

	
 
	glEnable(GL_BLEND);
 
}
 

	
 
@@ -812,12 +856,24 @@ void *OpenGLBackend::GetVideoBuffer()
 
{
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
 
	return _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
 
}
 

	
 
/**
 
 * Get a pointer to the memory for the separate animation buffer.
 
 * @return Pointer to draw on.
 
 */
 
uint8 *OpenGLBackend::GetAnimBuffer()
 
{
 
	if (this->anim_pbo == 0) return nullptr;
 

	
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
 
	return (uint8 *)_glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
 
}
 

	
 
/**
 
 * Update video buffer texture after the video buffer was filled.
 
 * @param update_rect Rectangle encompassing the dirty region of the video buffer.
 
 */
 
void OpenGLBackend::ReleaseVideoBuffer(const Rect &update_rect)
 
{
 
	assert(this->vid_pbo != 0);
 
@@ -839,12 +895,32 @@ 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;
 
		}
 
	}
 
}
 

	
 
/**
 
 * Update animation buffer texture after the animation buffer was filled.
 
 * @param update_rect Rectangle encompassing the dirty region of the animation buffer.
 
 */
 
void OpenGLBackend::ReleaseAnimBuffer(const Rect &update_rect)
 
{
 
	if (this->anim_pbo == 0) return;
 

	
 
	_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
 
	_glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
 

	
 
	/* Update changed rect of the video buffer texture. */
 
	if (update_rect.left != update_rect.right) {
 
		_glActiveTexture(GL_TEXTURE0);
 
		glBindTexture(GL_TEXTURE_2D, this->anim_texture);
 
		glPixelStorei(GL_UNPACK_ROW_LENGTH, _screen.pitch);
 
		glTexSubImage2D(GL_TEXTURE_2D, 0, update_rect.left, update_rect.top, update_rect.right - update_rect.left, update_rect.bottom - update_rect.top, GL_RED, GL_UNSIGNED_BYTE, (GLvoid *)(size_t)(update_rect.top * _screen.pitch + update_rect.left));
 
	}
 
}
 

	
 
/* 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;
src/video/opengl.h
Show inline comments
 
@@ -36,12 +36,15 @@ private:
 
	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 anim_pbo;     ///< Pixel buffer object storing the memory used for the animation buffer.
 
	GLuint anim_texture; ///< Texture handle for the animation buffer 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;
 

	
 
@@ -69,13 +72,15 @@ public:
 
	void Paint();
 

	
 
	void DrawMouseCursor();
 
	void ClearCursorCache();
 

	
 
	void *GetVideoBuffer();
 
	uint8 *GetAnimBuffer();
 
	void ReleaseVideoBuffer(const Rect &update_rect);
 
	void ReleaseAnimBuffer(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;
src/video/video_driver.hpp
Show inline comments
 
@@ -119,12 +119,30 @@ public:
 
	virtual bool HasEfficient8Bpp() const
 
	{
 
		return false;
 
	}
 

	
 
	/**
 
	 * Does this video driver support a separate animation buffer in addition to the colour buffer?
 
	 * @return True if a separate animation buffer is supported.
 
	 */
 
	virtual bool HasAnimBuffer()
 
	{
 
		return false;
 
	}
 

	
 
	/**
 
	 * Get a pointer to the animation buffer of the video back-end.
 
	 * @return Pointer to the buffer or nullptr if no animation buffer is supported.
 
	 */
 
	virtual uint8 *GetAnimBuffer()
 
	{
 
		return nullptr;
 
	}
 

	
 
	/**
 
	 * An edit box lost the input focus. Abort character compositing if necessary.
 
	 */
 
	virtual void EditBoxLostFocus() {}
 

	
 
	/**
 
	 * An edit box gained the input focus
src/video/win32_v.cpp
Show inline comments
 
@@ -1542,20 +1542,25 @@ bool VideoDriver_Win32OpenGL::AllocateBa
 

	
 
	return res;
 
}
 

	
 
void *VideoDriver_Win32OpenGL::GetVideoPointer()
 
{
 
	if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
 
		this->anim_buffer = OpenGLBackend::Get()->GetAnimBuffer();
 
	}
 
	return OpenGLBackend::Get()->GetVideoBuffer();
 
}
 

	
 
void VideoDriver_Win32OpenGL::ReleaseVideoPointer()
 
{
 
	if (this->anim_buffer != nullptr) OpenGLBackend::Get()->ReleaseAnimBuffer(this->dirty_rect);
 
	OpenGLBackend::Get()->ReleaseVideoBuffer(this->dirty_rect);
 
	this->dirty_rect = {};
 
	_screen.dst_ptr = nullptr;
 
	this->anim_buffer = nullptr;
 
}
 

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

	
src/video/win32_v.h
Show inline comments
 
@@ -125,13 +125,13 @@ public:
 

	
 
#ifdef WITH_OPENGL
 

	
 
/** The OpenGL video driver for windows. */
 
class VideoDriver_Win32OpenGL : public VideoDriver_Win32Base {
 
public:
 
	VideoDriver_Win32OpenGL() : dc(nullptr), gl_rc(nullptr) {}
 
	VideoDriver_Win32OpenGL() : dc(nullptr), gl_rc(nullptr), anim_buffer(nullptr) {}
 

	
 
	const char *Start(const StringList &param) override;
 

	
 
	void Stop() override;
 

	
 
	bool ToggleFullscreen(bool fullscreen) override;
 
@@ -141,18 +141,22 @@ public:
 
	bool HasEfficient8Bpp() const override { return true; }
 

	
 
	bool UseSystemCursor() override { return true; }
 

	
 
	void ClearSystemSprites() override;
 

	
 
	bool HasAnimBuffer() override { return true; }
 
	uint8 *GetAnimBuffer() override { return this->anim_buffer; }
 

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

	
 
protected:
 
	HDC   dc;          ///< Window device context.
 
	HGLRC gl_rc;       ///< OpenGL context.
 
	bool  vsync;       ///< Enable VSync?
 
	HDC    dc;          ///< Window device context.
 
	HGLRC  gl_rc;       ///< OpenGL context.
 
	bool   vsync;       ///< Enable VSync?
 
	uint8 *anim_buffer; ///< Animation buffer from OpenGL back-end.
 

	
 
	uint8 GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp.
 

	
 
	void Paint() override;
 
	void PaintThread() override {}
 

	
0 comments (0 inline, 0 general)