Skip to content

Commit

Permalink
OpenGL: use program pipelines or monolithic shader programs depending…
Browse files Browse the repository at this point in the history
… on GL_ARB_separate_shader_programs support.

This is first step on removing dependency on this extension.
Dependency on GLSL 4.10 is still there.
  • Loading branch information
Xottab-DUTY committed Aug 7, 2022
1 parent 5460f48 commit b0e4de8
Show file tree
Hide file tree
Showing 46 changed files with 619 additions and 331 deletions.
4 changes: 2 additions & 2 deletions res/gamedata/shaders/gl/clouds.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function normal (shader, t_base, t_second, t_detail)
: zb (false,false)
: sorting (3, true)
: blend (true, blend.srcalpha,blend.invsrcalpha)
shader:sampler ("s_clouds0") :texture ("$user$clouds0") : wrap() : f_anisotropic()
shader:sampler ("s_clouds1") :texture ("$user$clouds1") : wrap() : f_anisotropic()
shader:sampler ("s_clouds0") :texture ("$null") : wrap() : f_anisotropic()
shader:sampler ("s_clouds1") :texture ("$null") : wrap() : f_anisotropic()
shader:sampler ("s_tonemap") :texture ("$user$tonemap")
end
70 changes: 34 additions & 36 deletions src/Layers/xrRender/Blender_Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,12 @@ void CBlender_Compile::PassBegin()
passMatrices.clear();
passConstants.clear();
ctable.clear();
dwStage = 0;

// Set default pipeline state
PassSET_ZB(true, true);
PassSET_Blend(false, D3DBLEND_ONE, D3DBLEND_ZERO, false, 0);
PassSET_LightFog(false, false);

// Set default shaders
xr_strcpy(pass_ps, "null");
xr_strcpy(pass_vs, "null");
xr_strcpy(pass_gs, "null");
xr_strcpy(pass_hs, "null");
xr_strcpy(pass_ds, "null");
xr_strcpy(pass_cs, "null");
dwStage = 0;
}

void CBlender_Compile::PassEnd()
Expand All @@ -187,28 +179,16 @@ void CBlender_Compile::PassEnd()
RS.SetTSS(Stage(), D3DTSS_ALPHAOP, D3DTOP_DISABLE);

// Create pass
if (!dest.vs)
if (!dest.vs) // XXX: remove
{
dest.vs = RImplementation.Resources->_CreateVS("null");
}

if (!dest.ps)
if (!dest.ps) // XXX: remove
{
dest.ps = RImplementation.Resources->_CreatePS("null");
}

#if defined(USE_DX11) || defined(USE_OGL)
dest.gs = RImplementation.Resources->_CreateGS(pass_gs);
ctable.merge(&dest.gs->constants);
#ifdef USE_DX11
dest.hs = RImplementation.Resources->_CreateHS(pass_hs);
ctable.merge(&dest.hs->constants);
dest.ds = RImplementation.Resources->_CreateDS(pass_ds);
ctable.merge(&dest.ds->constants);
dest.cs = RImplementation.Resources->_CreateCS(pass_cs);
ctable.merge(&dest.cs->constants);
#endif // USE_DX11
#endif // !USE_DX9
SetMapping();
dest.state = RImplementation.Resources->_CreateState(RS.GetContainer());
dest.constants = RImplementation.Resources->_CreateConstantTable(ctable);
Expand All @@ -222,20 +202,38 @@ void CBlender_Compile::PassEnd()
SH->passes.push_back(_pass_);
}

void CBlender_Compile::PassSET_PS(LPCSTR name)
{
xr_strcpy(pass_ps, name);
xr_strlwr(pass_ps);
dest.ps = RImplementation.Resources->_CreatePS(pass_ps);
ctable.merge(&dest.ps->constants);
}

void CBlender_Compile::PassSET_VS(LPCSTR name)
void CBlender_Compile::PassSET_Shaders(pcstr _vs, pcstr _ps, pcstr _gs /*= nullptr*/, pcstr _hs /*= nullptr*/, pcstr _ds /*= nullptr*/)
{
xr_strcpy(pass_vs, name);
xr_strlwr(pass_vs);
dest.vs = RImplementation.Resources->_CreateVS(pass_vs);
ctable.merge(&dest.vs->constants);
#if defined(USE_OGL)
dest.pp = RImplementation.Resources->_CreatePP(_vs, _ps, _gs, _hs, _ds);
if (HW.SeparateShaderObjectsSupported || !dest.pp->pp)
#endif
{
dest.ps = RImplementation.Resources->_CreatePS(_ps);
ctable.merge(&dest.ps->constants);
u32 flags = 0;
#if defined(USE_DX11)
if (dest.ps->constants.dx9compatibility)
flags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
#endif
dest.vs = RImplementation.Resources->_CreateVS(_vs, flags);
ctable.merge(&dest.vs->constants);
#if defined(USE_DX11) || defined(USE_OGL)
dest.gs = RImplementation.Resources->_CreateGS(_gs);
ctable.merge(&dest.gs->constants);
# ifdef USE_DX11
dest.hs = RImplementation.Resources->_CreateHS(_hs);
dest.ds = RImplementation.Resources->_CreateDS(_ds);
ctable.merge(&dest.hs->constants);
ctable.merge(&dest.ds->constants);
dest.cs = RImplementation.Resources->_CreateCS("null");
# endif
#endif // !USE_DX9
}
#if defined(USE_OGL)
RImplementation.Resources->_LinkPP(dest);
ctable.merge(&dest.pp->constants);
#endif
}

void CBlender_Compile::PassSET_ZB(BOOL bZTest, BOOL bZWrite, BOOL bInvertZTest)
Expand Down
10 changes: 1 addition & 9 deletions src/Layers/xrRender/Blender_Recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ class CBlender_Compile
SConstantList passConstants;
u32 dwStage;

string128 pass_vs;
string128 pass_ps;
string128 pass_gs;
string128 pass_hs;
string128 pass_ds;
string128 pass_cs;

private:
inline u32 BC(BOOL v) const { return v ? 1 : 0; }
void SetupSampler(u32 stage, pcstr sampler);
Expand Down Expand Up @@ -99,8 +92,7 @@ class CBlender_Compile
PassSET_Blend(TRUE, D3DBLEND_DESTCOLOR, D3DBLEND_SRCCOLOR, bAref, ref);
}
void PassSET_LightFog(BOOL bLight, BOOL bFog);
void PassSET_PS(LPCSTR name);
void PassSET_VS(LPCSTR name);
void PassSET_Shaders(pcstr _vs, pcstr _ps, pcstr _gs = "null", pcstr _hs = "null", pcstr _ds = "null");
void PassEnd();

void StageBegin();
Expand Down
35 changes: 21 additions & 14 deletions src/Layers/xrRender/Blender_Recorder_R2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,33 @@ void CBlender_Compile::r_Pass(LPCSTR _vs, LPCSTR _ps, bool bFog, BOOL bZtest, BO
PassSET_LightFog(FALSE, bFog);

// Create shaders
SPS* ps = RImplementation.Resources->_CreatePS(_ps);
u32 flags = 0;
#if defined(USE_OGL)
dest.pp = RImplementation.Resources->_CreatePP(_vs, _ps, "null", "null", "null");
if (HW.SeparateShaderObjectsSupported || !dest.pp->pp)
#endif
{
dest.ps = RImplementation.Resources->_CreatePS(_ps);
ctable.merge(&dest.ps->constants);
u32 flags = 0;
#if defined(USE_DX11)
if (ps->constants.dx9compatibility)
flags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
if (dest.ps->constants.dx9compatibility)
flags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
#endif
SVS* vs = RImplementation.Resources->_CreateVS(_vs, flags);
dest.ps = ps;
dest.vs = vs;
dest.vs = RImplementation.Resources->_CreateVS(_vs, flags);
ctable.merge(&dest.vs->constants);
#if defined(USE_DX11) || defined(USE_OGL)
SGS* gs = RImplementation.Resources->_CreateGS("null");
dest.gs = gs;
dest.gs = RImplementation.Resources->_CreateGS("null");
# ifdef USE_DX11
dest.hs = RImplementation.Resources->_CreateHS("null");
dest.ds = RImplementation.Resources->_CreateDS("null");
dest.cs = RImplementation.Resources->_CreateCS("null");
dest.hs = RImplementation.Resources->_CreateHS("null");
dest.ds = RImplementation.Resources->_CreateDS("null");
dest.cs = RImplementation.Resources->_CreateCS("null");
# endif
#endif // !USE_DX9
ctable.merge(&ps->constants);
ctable.merge(&vs->constants);
}
#if defined(USE_OGL)
RImplementation.Resources->_LinkPP(dest);
ctable.merge(&dest.pp->constants);
#endif

// Last Stage - disable
if (0 == xr_stricmp(_ps, "null"))
Expand Down
72 changes: 46 additions & 26 deletions src/Layers/xrRender/R_Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class ECORE_API CBackend
GLuint ps;
GLuint vs;
GLuint gs;
GLuint pp;
#else
# error No graphics API selected or enabled!
#endif
Expand All @@ -149,6 +150,9 @@ class ECORE_API CBackend
LPCSTR cs_name;
#endif // USE_DX11
#endif // !USE_DX9
# ifdef USE_OGL
pcstr pp_name;
# endif
#endif // DEBUG

u32 stencil_enable;
Expand Down Expand Up @@ -202,6 +206,7 @@ class ECORE_API CBackend
u32 calls;
u32 vs;
u32 ps;
u32 pp;
#ifdef DEBUG
u32 decl;
u32 vb;
Expand Down Expand Up @@ -344,6 +349,7 @@ class ECORE_API CBackend

ICF void set_Format(SDeclaration* _decl);

private:
#if defined(USE_DX9) || defined(USE_DX11)
ICF void set_PS(ID3DPixelShader* _ps, LPCSTR _n = nullptr);
#elif defined(USE_OGL)
Expand All @@ -365,29 +371,18 @@ class ECORE_API CBackend

ICF void set_DS(ID3D11DomainShader* _ds, LPCSTR _n = nullptr);
ICF void set_DS(ref_ds& _ds) { set_DS(_ds->sh, _ds->cName.c_str()); }

ICF void set_CS(ID3D11ComputeShader* _cs, LPCSTR _n = nullptr);
ICF void set_CS(ref_cs& _cs) { set_CS(_cs->sh, _cs->cName.c_str()); }
# elif defined(USE_OGL)
ICF void set_GS(GLuint _gs, LPCSTR _n = 0);

ICF void set_PP(GLuint _pp, pcstr _n = nullptr);
ICF void set_PP(ref_pp& _pp) { set_PP(_pp->pp, _pp->cName.c_str()); }
# endif
#endif // USE_DX9


#if defined(USE_DX9) || defined(USE_OGL)
ICF bool is_TessEnabled() { return false; }
#elif defined(USE_DX11)
ICF bool is_TessEnabled();
#else
# error No graphics API selected or enabled!
#endif

ICF void set_VS(ref_vs& _vs);

#if defined(USE_DX11)
ICF void set_VS(SVS* _vs);

protected: // In DX11+ we need input shader signature which is stored in ref_vs
#endif

#if defined(USE_DX9) || defined(USE_DX11)
Expand All @@ -398,8 +393,19 @@ class ECORE_API CBackend
# error No graphics API selected or enabled!
#endif

public:
#if defined(USE_DX11)
ICF void set_CS(ID3D11ComputeShader* _cs, LPCSTR _n = nullptr);
ICF void set_CS(ref_cs& _cs) { set_CS(_cs->sh, _cs->cName.c_str()); }
#endif

public:
#if defined(USE_DX9) || defined(USE_OGL)
ICF bool is_TessEnabled() { return false; }
#elif defined(USE_DX11)
ICF bool is_TessEnabled();
#else
# error No graphics API selected or enabled!
#endif

ICF void set_Vertices(VertexBufferHandle _vb, u32 _vb_stride);
Expand Down Expand Up @@ -442,45 +448,59 @@ class ECORE_API CBackend
template<typename... Args>
ICF void set_c(R_constant* C, Args&&... args)
{
if (C)
constants.set(C, std::forward<Args>(args)...);
if (!C)
return;
#ifdef USE_OGL
if (!HW.SeparateShaderObjectsSupported)
VERIFY(C->pp.program == pp);
#endif
constants.set(C, std::forward<Args>(args)...);
}

template<typename... Args>
ICF void set_ca(R_constant* C, Args&&... args)
{
if (C)
constants.seta(C, std::forward<Args>(args)...);
if (!C)
return;
#ifdef USE_OGL
if (!HW.SeparateShaderObjectsSupported)
VERIFY(C->pp.program == pp);
#endif
constants.seta(C, std::forward<Args>(args)...);
}

// constants - raw string (slow)
template<typename... Args>
ICF void set_c(cpcstr name, Args&&... args)
{
if (ctable)
set_c(&*ctable->get(name), std::forward<Args>(args)...);
if (!ctable)
return;
set_c(&*ctable->get(name), std::forward<Args>(args)...);
}

template<typename... Args>
ICF void set_ca(cpcstr name, Args&&... args)
{
if (ctable)
set_ca(&*ctable->get(name), std::forward<Args>(args)...);
if (!ctable)
return;
set_ca(&*ctable->get(name), std::forward<Args>(args)...);
}

// constants - shared_str (average)
template<typename... Args>
ICF void set_c(const shared_str& name, Args&& ... args)
{
if (ctable)
set_c(&*ctable->get(name), std::forward<Args>(args)...);
if (!ctable)
return;
set_c(&*ctable->get(name), std::forward<Args>(args)...);
}

template<typename... Args>
ICF void set_ca(const shared_str& name, Args&& ... args)
{
if (ctable)
set_ca(&*ctable->get(name), std::forward<Args>(args)...);
if (!ctable)
return;
set_ca(&*ctable->get(name), std::forward<Args>(args)...);
}

// Rendering
Expand Down
19 changes: 13 additions & 6 deletions src/Layers/xrRender/R_Backend_Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,21 @@ IC void CBackend::set_Matrices(SMatrixList* _M)
IC void CBackend::set_Pass(SPass* P)
{
set_States(P->state);
set_PS(P->ps);
set_VS(P->vs);
#ifdef USE_OGL
if (P->pp)
set_PP(P->pp);
else
#endif
{
set_PS(P->ps);
set_VS(P->vs);
#ifdef USE_DX11
set_GS(P->gs);
set_HS(P->hs);
set_DS(P->ds);
set_CS(P->cs);
set_GS(P->gs);
set_HS(P->hs);
set_DS(P->ds);
set_CS(P->cs);
#endif
}
set_Constants(P->constants);
set_Textures(P->T);
#ifdef _EDITOR
Expand Down
Loading

0 comments on commit b0e4de8

Please sign in to comment.