Skip to content

Commit

Permalink
GL::State & Texture bind cosmetic (#927)
Browse files Browse the repository at this point in the history
* Texture binding cosmetic

Eliminate "duplicate" of FormatToQuery (Format2Query), move FormatToQuery -> TextureFormat.h and make it extern (only 1 instance among all units).
Gate unaccepted texture targets for Lua via switch.


* GL::State

State is implemented as a namespace.
Use it like this:
using namespace GL::State;

auto stateChange = StateChange(DepthMask(GL_TRUE), Viewport(0,0,1,1));
...
stateChange.pop()

or
{
    auto stateChange = StateChange(...);
    ...
    // stateChange destructed
}

Unknown status is implemented via union. It should be safe, no practical value should collide with it.

* Update GL::State

Enabled "capability" ("binary") attributes

Corrected previous union usage, it now uses a pair (not optional, because it works with values when false)

Todo: BindTexture, ClipDistance

* Update, should be ready

StateChange > PushState

PushState should be ready, but is not test yet. Shall be testing.

+ClipDistance attribute. Note, it doesn't check how many clip distances are available, it just runs gl function as it should. Check clip distances, if you need, prior to ClipDistance code.

Replace glPushAttrib with PushState in some places where its easy

* >SubState + operator <<

PushState > SubState

Add operator << to modify state
  • Loading branch information
Krogoth100 authored Aug 1, 2023
1 parent 81653a4 commit b60cb19
Show file tree
Hide file tree
Showing 18 changed files with 638 additions and 207 deletions.
17 changes: 10 additions & 7 deletions rts/Game/UI/MiniMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/glExtra.h"
#include "Rendering/GL/RenderBuffers.h"
#include "Rendering/GL/SubState.h"
#include "Rendering/Textures/Bitmap.h"
#include "Sim/Units/CommandAI/CommandAI.h"
#include "Sim/Units/Unit.h"
Expand All @@ -50,6 +51,9 @@
#include "System/FileSystem/SimpleParser.h"
#include "System/Sound/ISoundChannels.h"

using namespace GL::State;


CONFIG(std::string, MiniMapGeometry).defaultValue("2 2 200 200");
CONFIG(bool, MiniMapFullProxy).defaultValue(true);
CONFIG(int, MiniMapButtonSize).defaultValue(16);
Expand Down Expand Up @@ -1128,17 +1132,18 @@ void CMiniMap::Draw()
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushAttrib(GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_FALSE);

auto state = GL::SubState(
DepthTest(GL_FALSE),
DepthFunc(GL_LEQUAL),
DepthMask(GL_FALSE));

glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);

if (minimized) {
DrawMinimizedButtonQuad();
DrawMinimizedButtonLoop();
glPopAttrib();
glEnable(GL_TEXTURE_2D);
return;
}
Expand All @@ -1148,8 +1153,6 @@ void CMiniMap::Draw()
DrawFrame();
DrawButtons();
}

glPopAttrib();
}

// draw minimap itself
Expand Down
16 changes: 8 additions & 8 deletions rts/Game/UI/MouseHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Rendering/Fonts/glFont.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/RenderBuffers.h"
#include "Rendering/GL/SubState.h"
#include "Rendering/Textures/Bitmap.h"
#include "Sim/Features/Feature.h"
#include "Sim/Units/Unit.h"
Expand All @@ -46,6 +47,8 @@
#include <SDL_events.h>
#include <SDL_keycode.h>

using namespace GL::State;


CONFIG(bool, HardwareCursor).defaultValue(false).description("Sets hardware mouse cursor rendering. If you have a low framerate, your mouse cursor will seem \"laggy\". Setting hardware cursor will render the mouse cursor separately from spring and the mouse will behave normally. Note, not all GPU drivers support it in fullscreen mode!");
CONFIG(bool, InvertMouse).defaultValue(false);
Expand Down Expand Up @@ -572,20 +575,17 @@ void CMouseHandler::DrawSelectionBox() const
{tpLeft , cmdColors.mouseBox},
});

glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc((GLenum)cmdColors.MouseBoxBlendSrc(), (GLenum)cmdColors.MouseBoxBlendDst());
auto state = GL::SubState(
DepthTest(GL_FALSE),
Blending(GL_TRUE),
BlendFunc((GLenum)cmdColors.MouseBoxBlendSrc(), (GLenum)cmdColors.MouseBoxBlendDst()),
LineWidth(cmdColors.MouseBoxLineWidth()));

glLineWidth(cmdColors.MouseBoxLineWidth());
sh.Enable();

rb.DrawArrays(GL_LINE_LOOP);

sh.Disable();
glLineWidth(1.0f);

glPopAttrib();
}

int2 CMouseHandler::GetViewMouseCenter() const
Expand Down
7 changes: 2 additions & 5 deletions rts/Lua/LuaOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "Rendering/Env/WaterRendering.h"
#include "Rendering/Env/MapRendering.h"
#include "Rendering/GL/glExtra.h"
#include "Rendering/GL/TexBind.h"
#include "Rendering/Models/3DModel.h"
#include "Rendering/Shaders/Shader.h"
#include "Rendering/Textures/Bitmap.h"
Expand Down Expand Up @@ -3627,12 +3628,8 @@ int LuaOpenGL::GenerateMipmap(lua_State* L)
if (tex == nullptr)
return 0;

GLint currentBinding;
assert(LuaTextures::Format2Query.find(tex->target) != LuaTextures::Format2Query.end());
glGetIntegerv(LuaTextures::Format2Query.find(tex->target)->second, &currentBinding);
glBindTexture(tex->target, tex->id);
auto texBind = GL::TexBind(tex->target, tex->id);
glGenerateMipmapEXT(tex->target);
glBindTexture(tex->target, currentBinding);

return 0;
}
Expand Down
49 changes: 28 additions & 21 deletions rts/Lua/LuaTextures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Rendering/Textures/TextureFormat.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/GL/FBO.h"
#include "Rendering/GL/TexBind.h"
#include "System/SpringMath.h"
#include "System/StringUtil.h"
#include "System/Log/ILog.h"
Expand All @@ -13,33 +14,42 @@
#include "fmt/format.h"


const spring::unordered_map<GLenum, GLenum> LuaTextures::Format2Query =
{
{ GL_TEXTURE_1D , GL_TEXTURE_BINDING_1D },
{ GL_TEXTURE_2D , GL_TEXTURE_BINDING_2D },
{ GL_TEXTURE_3D , GL_TEXTURE_BINDING_3D },
// { GL_TEXTURE_1D_ARRAY , GL_TEXTURE_BINDING_1D_ARRAY },
{ GL_TEXTURE_2D_ARRAY , GL_TEXTURE_BINDING_2D_ARRAY },
// { GL_TEXTURE_RECTANGLE , GL_TEXTURE_BINDING_RECTANGLE },
{ GL_TEXTURE_CUBE_MAP , GL_TEXTURE_BINDING_CUBE_MAP },
// { GL_TEXTURE_BUFFER , GL_TEXTURE_BINDING_BUFFER },
{ GL_TEXTURE_2D_MULTISAMPLE , GL_TEXTURE_BINDING_2D_MULTISAMPLE },
// { GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY },
};
namespace Impl {
static inline bool IsValidLuaTextureTarget(GLenum target) {
switch(target) {
case GL_TEXTURE_1D:
case GL_TEXTURE_2D:
case GL_TEXTURE_3D:
//case GL_TEXTURE_1D_ARRAY:
case GL_TEXTURE_2D_ARRAY:
//case GL_TEXTURE_RECTANGLE:
case GL_TEXTURE_CUBE_MAP:
//case GL_TEXTURE_BUFFER:
case GL_TEXTURE_2D_MULTISAMPLE:
//case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
return true;
default: break;
}
return false;
}
}

/******************************************************************************/
/******************************************************************************/

std::string LuaTextures::Create(const Texture& tex)
{
GLint currentBinding = 0;

if (Format2Query.find(tex.target) == Format2Query.end()) {
GLenum query = 0;
if (Impl::IsValidLuaTextureTarget(tex.target)) {
query = GL::GetBindingQueryFromTarget(tex.target);
}
if (!query) {
LOG_L(L_ERROR, "[LuaTextures::%s] texture-target %d is not supported", __func__, tex.target);
return "";
}

glGetIntegerv(Format2Query.find(tex.target)->second, &currentBinding);
GLint currentBinding;
glGetIntegerv(query, &currentBinding);

GLuint texID;
glGenTextures(1, &texID);
Expand Down Expand Up @@ -254,11 +264,8 @@ void LuaTextures::ApplyParams(const Texture& tex) const

void LuaTextures::ChangeParams(const Texture& tex) const
{
GLint currentBinding = 0;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &currentBinding);
glBindTexture(tex.target, tex.id);
auto texBind = GL::TexBind(tex.target, tex.id);
ApplyParams(tex);
glBindTexture(GL_TEXTURE_2D, currentBinding); // revert the current binding
}


Expand Down
3 changes: 0 additions & 3 deletions rts/Lua/LuaTextures.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <vector>

#include "Rendering/GL/myGL.h"
#include "System/UnorderedMap.hpp"


class LuaTextures {
Expand Down Expand Up @@ -73,8 +72,6 @@ class LuaTextures {

const Texture* GetInfo(const std::string& name) const { return (GetInfo(GetIdx(name))); }
Texture* GetInfo(const std::string& name) { return (GetInfo(GetIdx(name))); }
public:
static const spring::unordered_map<GLenum, GLenum> Format2Query;
private:
int lastCode;

Expand Down
2 changes: 2 additions & 0 deletions rts/Rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ set(sources_engine_Rendering
"${CMAKE_CURRENT_SOURCE_DIR}/GL/VBO.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/GL/VAO.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/GL/glExtra.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/GL/State.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/GL/myGL.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/VK/VkInit.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/VK/VkInfo.cpp"
Expand Down Expand Up @@ -115,6 +116,7 @@ set(sources_engine_Rendering
"${CMAKE_CURRENT_SOURCE_DIR}/Textures/NamedTextures.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Textures/S3OTextureHandler.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Textures/TAPalette.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Textures/TextureFormat.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Textures/TextureAtlas.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Textures/TexturesSet.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Textures/nv_dds.cpp"
Expand Down
3 changes: 3 additions & 0 deletions rts/Rendering/GL/State.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "State.h"

decltype(GL::State::Attributes) GL::State::Attributes;
Loading

2 comments on commit b60cb19

@lhog
Copy link
Collaborator

@lhog lhog commented on b60cb19 Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Krogoth100 the code does not compile on MSVC.

@lhog
Copy link
Collaborator

@lhog lhog commented on b60cb19 Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.