Skip to content

Commit 9adbd78

Browse files
committed
Tiny bit more work on shader manager implementation.
1 parent 14a2d41 commit 9adbd78

File tree

6 files changed

+171
-67
lines changed

6 files changed

+171
-67
lines changed

engine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ add_executable(hosae-engine WIN32
9797
# -- generic
9898
client/renderer/light.cpp
9999
client/renderer/material.cpp
100+
client/renderer/shader_manager.cpp
100101

101102
# Server
102103
server/sv_ccmds.cpp
Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,79 @@
1-
/*
2-
Copyright (C) 1997-2001 Id Software, Inc.
3-
Copyright (C) 2020-2021 Mark E Sowden <hogsy@oldtimes-software.com>
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com>
43

5-
This program is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU General Public License
7-
as published by the Free Software Foundation; either version 2
8-
of the License, or (at your option) any later version.
4+
#include "gl_local.h"
5+
#include "gl_shader_manager.h"
96

10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7+
nox::renderer::gl::ShaderProgram::ShaderProgram()
8+
{
9+
Q_ZERO( glStages, sizeof( uint32_t ) * Stage::MAX_STAGES );
10+
}
1311

14-
See the GNU General Public License for more details.
12+
nox::renderer::gl::ShaderProgram::~ShaderProgram()
13+
{
14+
}
1515

16-
You should have received a copy of the GNU General Public License
17-
along with this program; if not, write to the Free Software
18-
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16+
bool nox::renderer::gl::ShaderProgram::LoadShaderStage( const std::string &path, Stage stage )
17+
{
18+
assert( glStages[ stage ] == 0 );
19+
if ( glStages[ stage ] != 0 )
20+
{
21+
Com_Printf( "A stage of this type has already been cached!\n" );
22+
return false;
23+
}
1924

20-
*/
25+
GLenum glStage;
26+
assert( stage < Stage::MAX_STAGES );
27+
if ( stage == Stage::STAGE_PIXEL )
28+
{
29+
glStage = GL_VERTEX_SHADER;
30+
}
31+
else if ( stage == Stage::STAGE_VERTEX )
32+
{
33+
glStage = GL_FRAGMENT_SHADER;
34+
}
35+
else
36+
{
37+
Com_Printf( "Unsupported shader stage: %u\n", stage );
38+
return false;
39+
}
2140

22-
#include "gl_local.h"
23-
#include "gl_shader_manager.h"
41+
XGL_CALL( glStages[ stage ] = glCreateShader( glStage ) );
42+
if ( glStages[ stage ] == 0 )
43+
{
44+
Com_Printf( "An error occurred on stage creation!\n" );
45+
return false;
46+
}
47+
48+
char *buffer;
49+
int length = FS_LoadFile( path.c_str(), ( void **) &buffer );
50+
if ( length == -1 )
51+
{
52+
Com_Printf( "Failed to open shader: %s\n", path.c_str() );
53+
XGL_CALL( glDeleteShader( glStages[ stage ] ) );
54+
glStages[ stage ] = 0;
55+
return false;
56+
}
57+
58+
return false;
59+
}
60+
61+
void nox::renderer::gl::ShaderProgram::Enable()
62+
{
63+
assert( glProgram != 0 );
64+
if ( glProgram == 0 )
65+
{
66+
//TODO: use fallback
67+
return;
68+
}
69+
XGL_CALL( glUseProgram( glProgram ) );
70+
}
71+
72+
void nox::renderer::gl::ShaderProgram::Disable()
73+
{
74+
XGL_CALL( glUseProgram( 0 ) );
75+
}
76+
77+
void nox::renderer::gl::ShaderProgram::Reload()
78+
{
79+
}
Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
1-
/*
2-
Copyright (C) 1997-2001 Id Software, Inc.
3-
Copyright (C) 2020-2021 Mark E Sowden <hogsy@oldtimes-software.com>
4-
5-
This program is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU General Public License
7-
as published by the Free Software Foundation; either version 2
8-
of the License, or (at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13-
14-
See the GNU General Public License for more details.
15-
16-
You should have received a copy of the GNU General Public License
17-
along with this program; if not, write to the Free Software
18-
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19-
20-
*/
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com>
213

224
#pragma once
235

24-
namespace nox
6+
#include "../shader_manager.h"
7+
8+
namespace nox::renderer::gl
259
{
26-
class GLShaderProgram
10+
class ShaderProgram : public IShaderProgram
2711
{
2812
public:
29-
GLShaderProgram();
13+
ShaderProgram();
14+
~ShaderProgram();
3015

31-
bool LoadShaderStage( const std::string &path );
16+
bool LoadShaderStage( const std::string &path, Stage stage );
17+
18+
void Enable() override;
19+
void Disable() override;
20+
21+
void Reload() override;
3222

3323
protected:
3424
private:
35-
std::string path_;
25+
uint32_t glProgram{ 0 };
26+
uint32_t glStages[ ( uint32_t ) Stage::MAX_STAGES ];
27+
28+
std::string fragmentPath;
29+
std::string vertexPath;
3630
};
3731

38-
class GLShaderManager
32+
class ShaderManager : public IShaderManager
3933
{
4034
public:
35+
ShaderManager() {}
36+
~ShaderManager() {}
37+
38+
39+
4140
protected:
4241
private:
43-
std::map< std::string, GLShaderProgram > programs_;
4442
};
45-
}// namespace nox
43+
}// namespace nox::renderer::gl
Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
1-
/*
2-
Copyright (C) 1997-2001 Id Software, Inc.
3-
4-
This program is free software; you can redistribute it and/or
5-
modify it under the terms of the GNU General Public License
6-
as published by the Free Software Foundation; either version 2
7-
of the License, or (at your option) any later version.
8-
9-
This program is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12-
13-
See the GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program; if not, write to the Free Software
17-
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18-
19-
*/
20-
/*
21-
** QGL.H
22-
*/
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
// Copyright (C) 1997-2001 Id Software, Inc.
3+
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com>
234

245
#pragma once
256

@@ -31,4 +12,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3112

3213
#include <GL/glew.h>
3314

15+
#if !defined( NDEBUG )
16+
# define XGL_CALL( X ) \
17+
{ \
18+
glGetError(); \
19+
X; \
20+
unsigned int _err = glGetError(); \
21+
assert( _err == GL_NO_ERROR ); \
22+
}
23+
#else
24+
# define XGL_CALL( X ) X
25+
#endif
26+
3427
bool QGL_Init( void );
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com>
3+
4+
#include "../client.h"
5+
#include "shader_manager.h"
6+
7+
nox::renderer::IShaderProgram *nox::renderer::IShaderManager::FetchProgram( const std::string &name )
8+
{
9+
auto i = programs.find( name );
10+
if ( i == programs.end() )
11+
{
12+
Com_Printf( "Failed to find program, \"%s\"!\n", name.c_str() );
13+
return nullptr;
14+
}
15+
16+
return i->second;
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com>
3+
4+
#pragma once
5+
6+
namespace nox::renderer
7+
{
8+
class IShaderProgram
9+
{
10+
public:
11+
enum Stage
12+
{
13+
STAGE_VERTEX,
14+
STAGE_PIXEL,
15+
16+
MAX_STAGES
17+
};
18+
virtual bool LoadShaderStage( const std::string &path, Stage stage ) = 0;
19+
20+
virtual void Enable() = 0;
21+
virtual void Disable() = 0;
22+
23+
virtual void Reload() = 0;
24+
};
25+
26+
class IShaderManager
27+
{
28+
public:
29+
void SetupDefaultPrograms();
30+
31+
IShaderProgram *FetchProgram( const std::string &name );
32+
virtual IShaderProgram *LoadProgram( const std::string &name, const std::string &vertexPath, const std::string &pixelPath ) = 0;
33+
34+
private:
35+
std::map< std::string, IShaderProgram * > programs;
36+
};
37+
38+
nox::renderer::IShaderProgram *InitializeShaderManager();
39+
}// namespace nox::renderer

0 commit comments

Comments
 (0)