Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve OpenGL rendering performance. #5859

Merged
merged 1 commit into from Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/openrct2-ui/drawing/engines/opengl/DrawCommands.h
Expand Up @@ -35,13 +35,4 @@ struct DrawLineCommand {
sint32 pos[4];
};

struct DrawImageCommand {
uint32 flags;
vec4f colour;
sint32 clip[4];
CachedTextureInfo texMask;
CachedTextureInfo texColour;
CachedTextureInfo texPalette;
sint32 bounds[4];
bool mask;
};
typedef DrawImageInstance DrawImageCommand;
171 changes: 65 additions & 106 deletions src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp
Expand Up @@ -769,18 +769,7 @@ void OpenGLDrawingContext::DrawSprite(uint32 image, sint32 x, sint32 y, uint32 t
right += _clipLeft;
bottom += _clipTop;

DrawImageCommand command = {};

command.flags = 0;
command.mask = false;

command.clip[0] = _clipLeft;
command.clip[1] = _clipTop;
command.clip[2] = _clipRight;
command.clip[3] = _clipBottom;

auto texture = _textureCache->GetOrLoadImageTexture(image);
command.texColour = texture;

bool special = false;
if (!(image & IMAGE_TYPE_REMAP_2_PLUS) && (image & IMAGE_TYPE_REMAP)) {
Expand All @@ -793,23 +782,28 @@ void OpenGLDrawingContext::DrawSprite(uint32 image, sint32 x, sint32 y, uint32 t
tertiaryColour = 0;
}

texture = _textureCache->GetOrLoadPaletteTexture(image, tertiaryColour, special);
command.texPalette = texture;

if (image & IMAGE_TYPE_TRANSPARENT) {
command.flags |= (1 << 3);
}
else if (image & (IMAGE_TYPE_REMAP_2_PLUS | IMAGE_TYPE_REMAP)){
command.flags |= (1 << 1);
}
command.flags |= special << 2;

command.bounds[0] = left;
command.bounds[1] = top;
command.bounds[2] = right;
command.bounds[3] = bottom;

_commandBuffers.images.push_back(command);
auto texture2 = _textureCache->GetOrLoadPaletteTexture(image, tertiaryColour, special);

DrawImageCommand command;

command.clip = { _clipLeft, _clipTop, _clipRight, _clipBottom };
command.texColourAtlas = texture.index;
command.texColourBounds = texture.normalizedBounds;
command.texMaskAtlas = 0;
command.texMaskBounds = { 0.0f, 0.0f, 0.0f };
command.texPaletteAtlas = texture2.index;
command.texPaletteBounds = {
texture2.normalizedBounds.x,
texture2.normalizedBounds.y,
(texture2.normalizedBounds.z - texture2.normalizedBounds.x) / (float)(texture2.bounds.z - texture2.bounds.x),
(texture2.normalizedBounds.w - texture2.normalizedBounds.y) / (float)(texture2.bounds.w - texture2.bounds.y)
};
command.flags = (!!(image & IMAGE_TYPE_TRANSPARENT) << 3) | (!!(image & (IMAGE_TYPE_REMAP_2_PLUS | IMAGE_TYPE_REMAP)) << 1) | (special << 2);
command.colour = { 0.0f, 0.0f, 0.0f };
command.bounds = { left, top, right, bottom };
command.mask = 0;

_commandBuffers.images.emplace_back(std::move(command));
}

void OpenGLDrawingContext::DrawSpriteRawMasked(sint32 x, sint32 y, uint32 maskImage, uint32 colourImage)
Expand Down Expand Up @@ -856,24 +850,21 @@ void OpenGLDrawingContext::DrawSpriteRawMasked(sint32 x, sint32 y, uint32 maskIm
right += _clipLeft;
bottom += _clipTop;

DrawImageCommand command = {};
DrawImageCommand command;

command.mask = true;

command.clip[0] = _clipLeft;
command.clip[1] = _clipTop;
command.clip[2] = _clipRight;
command.clip[3] = _clipBottom;

command.texMask = textureMask;
command.texColour = textureColour;

command.bounds[0] = left;
command.bounds[1] = top;
command.bounds[2] = right;
command.bounds[3] = bottom;
command.clip = { _clipLeft, _clipTop, _clipRight, _clipBottom };
command.texColourAtlas = textureColour.index;
command.texColourBounds = textureColour.normalizedBounds;
command.texMaskAtlas = textureMask.index;
command.texMaskBounds = textureMask.normalizedBounds;
command.texPaletteAtlas = 0;
command.texPaletteBounds = {0.0f, 0.0f, 0.0f};
command.flags = 0;
command.colour = {0.0f, 0.0f, 0.0f};
command.bounds = { left, top, right, bottom };
command.mask = 1;

_commandBuffers.images.push_back(command);
_commandBuffers.images.emplace_back(std::move(command));
}

void OpenGLDrawingContext::DrawSpriteSolid(uint32 image, sint32 x, sint32 y, uint8 colour)
Expand Down Expand Up @@ -909,25 +900,26 @@ void OpenGLDrawingContext::DrawSpriteSolid(uint32 image, sint32 x, sint32 y, uin
right += _offsetX;
bottom += _offsetY;

DrawImageCommand command = {};

DrawImageCommand command;

command.clip = { _clipLeft, _clipTop, _clipRight, _clipBottom };
command.texColourAtlas = texture.index;
command.texColourBounds = texture.normalizedBounds;
command.texMaskAtlas = 0;
command.texMaskBounds = { 0.0f, 0.0f, 0.0f };
command.texPaletteAtlas = texture.index;
command.texPaletteBounds = {
texture.normalizedBounds.x,
texture.normalizedBounds.y,
(texture.normalizedBounds.z - texture.normalizedBounds.x) / (float)(texture.bounds.z - texture.bounds.x),
(texture.normalizedBounds.w - texture.normalizedBounds.y) / (float)(texture.bounds.w - texture.bounds.y)
};
command.flags = 1;
command.mask = false;
command.colour = paletteColour;
command.bounds = { left, top, right, bottom };
command.mask = 0;

command.clip[0] = _clipLeft;
command.clip[1] = _clipTop;
command.clip[2] = _clipRight;
command.clip[3] = _clipBottom;

command.texColour = texture;

command.bounds[0] = left;
command.bounds[1] = top;
command.bounds[2] = right;
command.bounds[3] = bottom;

_commandBuffers.images.push_back(command);
_commandBuffers.images.emplace_back(std::move(command));
}

void OpenGLDrawingContext::DrawGlyph(uint32 image, sint32 x, sint32 y, uint8 * palette)
Expand Down Expand Up @@ -961,31 +953,27 @@ void OpenGLDrawingContext::DrawGlyph(uint32 image, sint32 x, sint32 y, uint8 * p
right += _offsetX;
bottom += _offsetY;

DrawImageCommand command = {};
DrawImageCommand command;

command.clip = { _clipLeft, _clipTop, _clipRight, _clipBottom };
command.texColourAtlas = texture.index;
command.texColourBounds = texture.normalizedBounds;
command.texMaskAtlas = 0;
command.texMaskBounds = { 0.0f, 0.0f, 0.0f };
command.texPaletteAtlas = 0;
command.texPaletteBounds = { 0.0f, 0.0f, 0.0f};
command.flags = 0;
command.mask = false;
command.colour = { 0.0f, 0.0f, 0.0f };
command.bounds = { left, top, right, bottom };
command.mask = 0;

command.clip[0] = _clipLeft;
command.clip[1] = _clipTop;
command.clip[2] = _clipRight;
command.clip[3] = _clipBottom;

command.texColour = texture;

command.bounds[0] = left;
command.bounds[1] = top;
command.bounds[2] = right;
command.bounds[3] = bottom;

_commandBuffers.images.push_back(command);
_commandBuffers.images.emplace_back(std::move(command));
}

void OpenGLDrawingContext::FlushCommandBuffers()
{
FlushRectangles();
FlushLines();

FlushImages();
}

Expand Down Expand Up @@ -1023,37 +1011,8 @@ void OpenGLDrawingContext::FlushImages()

OpenGLAPI::SetTexture(0, GL_TEXTURE_2D_ARRAY, _textureCache->GetAtlasesTexture());

std::vector<DrawImageInstance> instances;
instances.reserve(_commandBuffers.images.size());

for (const auto& command : _commandBuffers.images)
{
DrawImageInstance instance;

instance.clip = {command.clip[0], command.clip[1], command.clip[2], command.clip[3]};
instance.texColourAtlas = command.texColour.index;
instance.texColourBounds = command.texColour.normalizedBounds;
instance.texMaskAtlas = command.texMask.index;
instance.texMaskBounds = command.texMask.normalizedBounds;
instance.flags = command.flags;
instance.colour = command.colour;
instance.bounds = {command.bounds[0], command.bounds[1], command.bounds[2], command.bounds[3]};
instance.mask = command.mask;
instance.texPaletteAtlas = command.texPalette.index;
if (instance.flags != 0) {
instance.texPaletteBounds = {
command.texPalette.normalizedBounds.x,
command.texPalette.normalizedBounds.y,
(command.texPalette.normalizedBounds.z - command.texPalette.normalizedBounds.x) / (float)(command.texPalette.bounds.z - command.texPalette.bounds.x),
(command.texPalette.normalizedBounds.w - command.texPalette.normalizedBounds.y) / (float)(command.texPalette.bounds.w - command.texPalette.bounds.y)
};
}

instances.push_back(instance);
}

_drawImageShader->Use();
_drawImageShader->DrawInstances(instances);
_drawImageShader->DrawInstances(_commandBuffers.images);

_commandBuffers.images.clear();
}
Expand Down