Skip to content

Commit

Permalink
#5584: Start expanding the RenderSystem interface to provide a separa…
Browse files Browse the repository at this point in the history
…te method to acquire built-in shaders.

This should replace all the shaders addressed by magic strings like "$WIRE_OVERLAY".
  • Loading branch information
codereader committed Jan 24, 2022
1 parent 8bd6ca6 commit 60f78e5
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 33 deletions.
11 changes: 11 additions & 0 deletions include/irender.h
Expand Up @@ -534,6 +534,11 @@ enum class RenderViewType
OrthoView = 1 << 1,
};

enum class BuiltInShaderType
{
FlatshadeOverlay,
};

/**
* \brief
* The main interface for the backend renderer.
Expand Down Expand Up @@ -564,6 +569,12 @@ class RenderSystem
*/
virtual ITextRenderer::Ptr captureTextRenderer(IGLFont::Style style, std::size_t size) = 0;

/**
* Acquire one of DarkRadiant's built-in shaders, used for drawing
* things like connectors, lines, points and overlays.
*/
virtual ShaderPtr capture(BuiltInShaderType type) = 0;

/**
* \brief
* Main render method.
Expand Down
1 change: 1 addition & 0 deletions radiantcore/CMakeLists.txt
Expand Up @@ -215,6 +215,7 @@ add_library(radiantcore MODULE
rendersystem/backend/glprogram/GLSLBumpProgram.cpp
rendersystem/backend/glprogram/GLSLDepthFillProgram.cpp
rendersystem/backend/glprogram/GLSLDepthFillAlphaProgram.cpp
rendersystem/backend/BuiltInShader.cpp
rendersystem/backend/OpenGLShader.cpp
rendersystem/backend/OpenGLShaderPass.cpp
rendersystem/backend/DepthFillPass.cpp
Expand Down
52 changes: 33 additions & 19 deletions radiantcore/rendersystem/OpenGLRenderSystem.cpp
Expand Up @@ -8,6 +8,7 @@
#include "math/Matrix4.h"
#include "module/StaticModule.h"
#include "backend/GLProgramFactory.h"
#include "backend/BuiltInShader.h"
#include "debugging/debugging.h"

#include <functional>
Expand Down Expand Up @@ -102,7 +103,7 @@ ITextRenderer::Ptr OpenGLRenderSystem::captureTextRenderer(IGLFont::Style style,
return existing->second;
}

ShaderPtr OpenGLRenderSystem::capture(const std::string& name)
ShaderPtr OpenGLRenderSystem::capture(const std::string& name, const std::function<OpenGLShaderPtr()>& createShader)
{
// Usual ritual, check cache and return if found, otherwise create/
// insert/return.
Expand All @@ -115,18 +116,37 @@ ShaderPtr OpenGLRenderSystem::capture(const std::string& name)

// Either the shader was not found, or the weak pointer failed to lock
// because the shader had been deleted. Either way, create a new shader
// and insert into the cache.
auto shd = std::make_shared<OpenGLShader>(name, *this);
_shaders[name] = shd;
// using the given factory functor and insert into the cache.
auto shader = createShader();
_shaders[name] = shader;

// Realise the shader if the cache is realised
if (_realised)
{
shd->realise();
shader->realise();
}

// Return the new shader
return shd;
return shader;
}

ShaderPtr OpenGLRenderSystem::capture(const std::string& name)
{
// Forward to the method accepting our factory method
return capture(name, [&]()
{
return std::make_shared<OpenGLShader>(name, *this);
});
}

ShaderPtr OpenGLRenderSystem::capture(BuiltInShaderType type)
{
// Forward to the method accepting our factory method
auto name = BuiltInShader::GetNameForType(type);

return capture(name, [&]()
{
return std::make_shared<BuiltInShader>(type, *this);
});
}

/*
Expand Down Expand Up @@ -262,13 +282,10 @@ void OpenGLRenderSystem::realise()
_glProgramFactory->realise();
}

// Realise the OpenGLShader objects
for (ShaderMap::iterator i = _shaders.begin(); i != _shaders.end(); ++i)
// Realise all shaders
for (auto& [_, shader] : _shaders)
{
OpenGLShaderPtr sp = i->second;
assert(sp);

sp->realise();
shader->realise();
}
}

Expand All @@ -280,13 +297,10 @@ void OpenGLRenderSystem::unrealise()

_realised = false;

// Unrealise the OpenGLShader objects
for (ShaderMap::iterator i = _shaders.begin(); i != _shaders.end(); ++i)
// Unrealise all OpenGLShader objects
for (auto& [_, shader] : _shaders)
{
OpenGLShaderPtr sp = i->second;
assert(sp);

sp->unrealise();
shader->unrealise();
}

if (GlobalOpenGLContext().getSharedContext() &&
Expand Down
7 changes: 5 additions & 2 deletions radiantcore/rendersystem/OpenGLRenderSystem.h
Expand Up @@ -24,8 +24,7 @@ class OpenGLRenderSystem
public OpenGLStateManager
{
// Map of named Shader objects
typedef std::map<std::string, OpenGLShaderPtr> ShaderMap;
ShaderMap _shaders;
std::map<std::string, OpenGLShaderPtr> _shaders;

// whether this module has been realised
bool _realised;
Expand Down Expand Up @@ -68,6 +67,7 @@ class OpenGLRenderSystem
ITextRenderer::Ptr captureTextRenderer(IGLFont::Style style, std::size_t size) override;

ShaderPtr capture(const std::string& name) override;
ShaderPtr capture(BuiltInShaderType type) override;
void render(RenderViewType renderViewType, RenderStateFlags globalstate,
const Matrix4& modelview,
const Matrix4& projection,
Expand Down Expand Up @@ -108,6 +108,9 @@ class OpenGLRenderSystem
virtual const StringSet& getDependencies() const override;
virtual void initialiseModule(const IApplicationContext& ctx) override;
virtual void shutdownModule() override;

private:
ShaderPtr capture(const std::string& name, const std::function<OpenGLShaderPtr()>& createShader);
};
typedef std::shared_ptr<OpenGLRenderSystem> OpenGLRenderSystemPtr;

Expand Down
19 changes: 19 additions & 0 deletions radiantcore/rendersystem/backend/BuiltInShader.cpp
@@ -0,0 +1,19 @@
#include "BuiltInShader.h"

#include "string/convert.h"

namespace render
{

BuiltInShader::BuiltInShader(BuiltInShaderType type, OpenGLRenderSystem& renderSystem) :
OpenGLShader(GetNameForType(type), renderSystem)
{}



std::string BuiltInShader::GetNameForType(BuiltInShaderType type)
{
return "$BUILT_IN_SHADER[" + string::to_string(static_cast<std::size_t>(type)) + "]";
}

}
17 changes: 17 additions & 0 deletions radiantcore/rendersystem/backend/BuiltInShader.h
@@ -0,0 +1,17 @@
#pragma once

#include "OpenGLShader.h"

namespace render
{

class BuiltInShader :
public OpenGLShader
{
public:
BuiltInShader(BuiltInShaderType type, OpenGLRenderSystem& renderSystem);

static std::string GetNameForType(BuiltInShaderType type);
};

}
3 changes: 1 addition & 2 deletions radiantcore/rendersystem/backend/OpenGLShader.cpp
Expand Up @@ -79,7 +79,6 @@ OpenGLRenderSystem& OpenGLShader::getRenderSystem()
void OpenGLShader::destroy()
{
_enabledViewTypes = 0;
_vertexBuffer.reset();
_materialChanged.disconnect();
_material.reset();
_shaderPasses.clear();
Expand Down Expand Up @@ -141,7 +140,7 @@ void OpenGLShader::drawSurfaces(const VolumeTest& view)

bool OpenGLShader::hasSurfaces() const
{
return !_geometryRenderer.empty() || !_surfaceRenderer.empty() || _vertexBuffer && _vertexBuffer->getNumVertices() > 0;
return !_geometryRenderer.empty() || !_surfaceRenderer.empty();
}

IGeometryRenderer::Slot OpenGLShader::addGeometry(GeometryType indexType,
Expand Down
12 changes: 2 additions & 10 deletions radiantcore/rendersystem/backend/OpenGLShader.h
Expand Up @@ -5,7 +5,6 @@
#include "irender.h"
#include "ishaders.h"
#include "string/string.h"
#include "render/IndexedVertexBuffer.h"
#include "render/WindingRenderer.h"
#include "GeometryRenderer.h"
#include "SurfaceRenderer.h"
Expand All @@ -21,7 +20,7 @@ class OpenGLRenderSystem;
/**
* Implementation of the Shader class.
*/
class OpenGLShader final :
class OpenGLShader :
public Shader
{
private:
Expand All @@ -48,8 +47,6 @@ class OpenGLShader final :
typedef std::set<Observer*> Observers;
Observers _observers;

std::unique_ptr<render::IndexedVertexBuffer<ArbitraryMeshVertex>> _vertexBuffer;

std::unique_ptr<IBackendWindingRenderer> _windingRenderer;
GeometryRenderer _geometryRenderer;
SurfaceRenderer _surfaceRenderer;
Expand Down Expand Up @@ -99,7 +96,7 @@ class OpenGLShader final :
/// Construct and initialise
OpenGLShader(const std::string& name, OpenGLRenderSystem& renderSystem);

~OpenGLShader();
virtual ~OpenGLShader();

// Returns the owning render system
OpenGLRenderSystem& getRenderSystem();
Expand All @@ -111,7 +108,6 @@ class OpenGLShader final :
const LightSources* lights,
const IRenderEntity* entity) override;

//void addSurface(const std::vector<ArbitraryMeshVertex>& vertices, const std::vector<unsigned int>& indices) override;
bool hasSurfaces() const;
void drawSurfaces(const VolumeTest& view);

Expand Down Expand Up @@ -143,11 +139,7 @@ class OpenGLShader final :

bool isRealised() override;

/**
* Realise this shader
*/
void realise();

void unrealise();

// Return the Material used by this shader
Expand Down
2 changes: 2 additions & 0 deletions tools/msvc/DarkRadiantCore.vcxproj
Expand Up @@ -622,6 +622,7 @@
<ClCompile Include="..\..\radiantcore\log\StringLogDevice.cpp" />
<ClCompile Include="..\..\radiantcore\modulesystem\ModuleLoader.cpp" />
<ClCompile Include="..\..\radiantcore\modulesystem\ModuleRegistry.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\BuiltInShader.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\DepthFillPass.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\GLProgramFactory.cpp" />
<ClCompile Include="..\..\radiantcore\rendersystem\backend\glprogram\GenericVFPProgram.cpp" />
Expand Down Expand Up @@ -979,6 +980,7 @@
<ClInclude Include="..\..\radiantcore\messagebus\MessageBus.h" />
<ClInclude Include="..\..\radiantcore\modulesystem\ModuleLoader.h" />
<ClInclude Include="..\..\radiantcore\modulesystem\ModuleRegistry.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\BuiltInShader.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\DepthFillPass.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\GeometryRenderer.h" />
<ClInclude Include="..\..\radiantcore\rendersystem\backend\GLProgramFactory.h" />
Expand Down
6 changes: 6 additions & 0 deletions tools/msvc/DarkRadiantCore.vcxproj.filters
Expand Up @@ -1114,6 +1114,9 @@
<ClCompile Include="..\..\radiantcore\entity\RenderableEntityName.cpp">
<Filter>src\entity</Filter>
</ClCompile>
<ClCompile Include="..\..\radiantcore\rendersystem\backend\BuiltInShader.cpp">
<Filter>src\rendersystem\backend</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\radiantcore\modulesystem\ModuleLoader.h">
Expand Down Expand Up @@ -2280,5 +2283,8 @@
<ClInclude Include="..\..\radiantcore\entity\RenderableEntityName.h">
<Filter>src\entity</Filter>
</ClInclude>
<ClInclude Include="..\..\radiantcore\rendersystem\backend\BuiltInShader.h">
<Filter>src\rendersystem\backend</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 60f78e5

Please sign in to comment.