1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef UNITDRAWER_STATE_H
#define UNITDRAWER_STATE_H
#include <array>
#include "Map/MapDrawPassTypes.h"
#include "System/type2.h"
#include "System/Matrix44f.h"
struct float4;
class CUnitDrawer;
class CCamera;
struct ISkyLight;
class LuaMatShader;
namespace Shader {
struct IProgramObject;
}
enum {
DRAWER_STATE_NOP = 0, // no-op path
DRAWER_STATE_SSP = 1, // standard-shader path
DRAWER_STATE_LUA = 2, // custom-shader path
DRAWER_STATE_SEL = 3, // selected path
DRAWER_STATE_CNT = 4,
};
struct IUnitDrawerState {
public:
static IUnitDrawerState* GetInstance(bool nopState, bool luaState);
static void FreeInstance(IUnitDrawerState* state) { delete state; }
static const CMatrix44f& GetDummyPieceMatrixRef(size_t idx = 0);
static const CMatrix44f* GetDummyPieceMatrixPtr(size_t idx = 0);
static float4 GetTeamColor(int team, float alpha);
IUnitDrawerState() { modelShaders.fill(nullptr); }
virtual ~IUnitDrawerState() {}
virtual bool Init(const CUnitDrawer*) { return false; }
virtual void Kill() {}
virtual bool CanEnable(const CUnitDrawer*) const { return false; }
virtual bool CanDrawAlpha() const { return false; }
virtual bool CanDrawDeferred() const { return false; }
virtual void Enable(const CUnitDrawer*, bool, bool) = 0;
virtual void Disable(const CUnitDrawer*, bool) = 0;
virtual void EnableTextures() const = 0;
virtual void DisableTextures() const = 0;
virtual void EnableShaders(const CUnitDrawer*) = 0;
virtual void DisableShaders(const CUnitDrawer*) = 0;
virtual void SetSkyLight(const ISkyLight*) const = 0;
virtual void SetAlphaTest(const float4& params) const = 0;
virtual void SetTeamColor(int team, const float2 alpha) const = 0;
virtual void SetNanoColor(const float4& color) const = 0;
virtual void SetMatrices(const CMatrix44f& modelMat, const std::vector<CMatrix44f>& pieceMats) const = 0;
virtual void SetMatrices(const CMatrix44f& modelMat, const CMatrix44f* pieceMats, size_t numPieceMats) const = 0;
virtual void SetWaterClipPlane(const DrawPass::e& drawPass) const = 0; // water
virtual void SetBuildClipPlanes(const float4&, const float4&) const = 0; // nano-frames
void SetActiveShader(unsigned int shadowed, unsigned int deferred) {
// shadowed=1 --> shader 1 (deferred=0) or 3 (deferred=1)
// shadowed=0 --> shader 0 (deferred=0) or 2 (deferred=1)
modelShaders[MODEL_SHADER_ACTIVE] = modelShaders[shadowed + deferred * 2];
}
const Shader::IProgramObject* GetActiveShader() const { return modelShaders[MODEL_SHADER_ACTIVE]; }
Shader::IProgramObject* GetActiveShader() { return modelShaders[MODEL_SHADER_ACTIVE]; }
enum ModelShaderProgram {
MODEL_SHADER_NOSHADOW_STANDARD = 0, ///< model shader (V+F) without self-shadowing
MODEL_SHADER_SHADOWED_STANDARD = 1, ///< model shader (V+F) with self-shadowing
MODEL_SHADER_NOSHADOW_DEFERRED = 2, ///< deferred version of MODEL_SHADER_NOSHADOW (GLSL-only)
MODEL_SHADER_SHADOWED_DEFERRED = 3, ///< deferred version of MODEL_SHADER_SHADOW (GLSL-only)
MODEL_SHADER_ACTIVE = 4, ///< currently active model shader
MODEL_SHADER_COUNT = 5,
};
protected:
// shared ARB and GLSL state managers
void EnableCommon(const CUnitDrawer*, bool);
void DisableCommon(const CUnitDrawer*, bool);
void EnableTexturesCommon() const;
void DisableTexturesCommon() const;
protected:
std::array<Shader::IProgramObject*, MODEL_SHADER_COUNT> modelShaders;
};
struct UnitDrawerStateNOP: public IUnitDrawerState {
public:
bool Init(const CUnitDrawer*) override { return true; }
void Kill() override {}
bool CanEnable(const CUnitDrawer*) const override { return true; }
bool CanDrawAlpha() const override { return false; }
bool CanDrawDeferred() const override { return false; }
void Enable(const CUnitDrawer*, bool, bool) override {}
void Disable(const CUnitDrawer*, bool) override {}
void EnableTextures() const override {}
void DisableTextures() const override {}
void EnableShaders(const CUnitDrawer*) override {}
void DisableShaders(const CUnitDrawer*) override {}
void SetSkyLight(const ISkyLight*) const override {}
void SetAlphaTest(const float4& params) const override {}
void SetTeamColor(int team, const float2 alpha) const override {}
void SetNanoColor(const float4& color) const override {}
void SetMatrices(const CMatrix44f& modelMat, const std::vector<CMatrix44f>& pieceMats) const override {}
void SetMatrices(const CMatrix44f& modelMat, const CMatrix44f* pieceMats, size_t numPieceMats) const override {}
void SetWaterClipPlane(const DrawPass::e& drawPass) const override {}
void SetBuildClipPlanes(const float4&, const float4&) const override {}
};
// does nothing by itself; LuaObjectDrawer handles custom uniforms
// this exists to ensure UnitDrawerStateGLSL::SetMatrices will not
// be invoked (via *Drawer::Draw*Trans) during a custom bin-pass
struct UnitDrawerStateLUA: public IUnitDrawerState {
public:
void Enable(const CUnitDrawer* ud, bool, bool) override { EnableShaders(ud); }
void Disable(const CUnitDrawer* ud, bool) override { DisableShaders(ud); }
void EnableTextures() const override {}
void DisableTextures() const override {}
void EnableShaders(const CUnitDrawer*) override {}
void DisableShaders(const CUnitDrawer*) override {}
void SetSkyLight(const ISkyLight*) const override {}
void SetAlphaTest(const float4& params) const override {}
void SetTeamColor(int team, const float2 alpha) const override {} // handled via LuaObjectDrawer::SetObjectTeamColor
void SetNanoColor(const float4& color) const override {}
void SetMatrices(const CMatrix44f& modelMat, const std::vector<CMatrix44f>& pieceMats) const override { SetMatrices(modelMat, pieceMats.data(), pieceMats.size()); }
void SetMatrices(const CMatrix44f& modelMat, const CMatrix44f* pieceMats, size_t numPieceMats) const override {} // handled via LuaObjectDrawer::SetObjectMatrices
void SetWaterClipPlane(const DrawPass::e& drawPass) const override {}
void SetBuildClipPlanes(const float4&, const float4&) const override {}
};
struct UnitDrawerStateGLSL: public IUnitDrawerState {
public:
bool Init(const CUnitDrawer*) override;
void Kill() override;
bool CanEnable(const CUnitDrawer*) const override { return true; }
bool CanDrawAlpha() const override { return true; }
bool CanDrawDeferred() const override { return true; }
void Enable(const CUnitDrawer*, bool, bool) override;
void Disable(const CUnitDrawer*, bool) override;
void EnableTextures() const override;
void DisableTextures() const override;
void EnableShaders(const CUnitDrawer*) override;
void DisableShaders(const CUnitDrawer*) override;
void SetSkyLight(const ISkyLight*) const override;
void SetAlphaTest(const float4& params) const override;
void SetTeamColor(int team, const float2 alpha) const override;
void SetNanoColor(const float4& color) const override;
void SetMatrices(const CMatrix44f& modelMat, const std::vector<CMatrix44f>& pieceMats) const override { SetMatrices(modelMat, pieceMats.data(), pieceMats.size()); }
void SetMatrices(const CMatrix44f& modelMat, const CMatrix44f* pieceMats, size_t numPieceMats) const override;
void SetWaterClipPlane(const DrawPass::e& drawPass) const override;
void SetBuildClipPlanes(const float4&, const float4&) const override;
};
#endif
|