-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tiny bit more work on shader manager implementation.
- Loading branch information
Showing
6 changed files
with
171 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,79 @@ | ||
/* | ||
Copyright (C) 1997-2001 Id Software, Inc. | ||
Copyright (C) 2020-2021 Mark E Sowden <hogsy@oldtimes-software.com> | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com> | ||
|
||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
as published by the Free Software Foundation; either version 2 | ||
of the License, or (at your option) any later version. | ||
#include "gl_local.h" | ||
#include "gl_shader_manager.h" | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
nox::renderer::gl::ShaderProgram::ShaderProgram() | ||
{ | ||
Q_ZERO( glStages, sizeof( uint32_t ) * Stage::MAX_STAGES ); | ||
} | ||
|
||
See the GNU General Public License for more details. | ||
nox::renderer::gl::ShaderProgram::~ShaderProgram() | ||
{ | ||
} | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
bool nox::renderer::gl::ShaderProgram::LoadShaderStage( const std::string &path, Stage stage ) | ||
{ | ||
assert( glStages[ stage ] == 0 ); | ||
if ( glStages[ stage ] != 0 ) | ||
{ | ||
Com_Printf( "A stage of this type has already been cached!\n" ); | ||
return false; | ||
} | ||
|
||
*/ | ||
GLenum glStage; | ||
assert( stage < Stage::MAX_STAGES ); | ||
if ( stage == Stage::STAGE_PIXEL ) | ||
{ | ||
glStage = GL_VERTEX_SHADER; | ||
} | ||
else if ( stage == Stage::STAGE_VERTEX ) | ||
{ | ||
glStage = GL_FRAGMENT_SHADER; | ||
} | ||
else | ||
{ | ||
Com_Printf( "Unsupported shader stage: %u\n", stage ); | ||
return false; | ||
} | ||
|
||
#include "gl_local.h" | ||
#include "gl_shader_manager.h" | ||
XGL_CALL( glStages[ stage ] = glCreateShader( glStage ) ); | ||
if ( glStages[ stage ] == 0 ) | ||
{ | ||
Com_Printf( "An error occurred on stage creation!\n" ); | ||
return false; | ||
} | ||
|
||
char *buffer; | ||
int length = FS_LoadFile( path.c_str(), ( void **) &buffer ); | ||
if ( length == -1 ) | ||
{ | ||
Com_Printf( "Failed to open shader: %s\n", path.c_str() ); | ||
XGL_CALL( glDeleteShader( glStages[ stage ] ) ); | ||
glStages[ stage ] = 0; | ||
return false; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void nox::renderer::gl::ShaderProgram::Enable() | ||
{ | ||
assert( glProgram != 0 ); | ||
if ( glProgram == 0 ) | ||
{ | ||
//TODO: use fallback | ||
return; | ||
} | ||
XGL_CALL( glUseProgram( glProgram ) ); | ||
} | ||
|
||
void nox::renderer::gl::ShaderProgram::Disable() | ||
{ | ||
XGL_CALL( glUseProgram( 0 ) ); | ||
} | ||
|
||
void nox::renderer::gl::ShaderProgram::Reload() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,43 @@ | ||
/* | ||
Copyright (C) 1997-2001 Id Software, Inc. | ||
Copyright (C) 2020-2021 Mark E Sowden <hogsy@oldtimes-software.com> | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
as published by the Free Software Foundation; either version 2 | ||
of the License, or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
See the GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
*/ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com> | ||
|
||
#pragma once | ||
|
||
namespace nox | ||
#include "../shader_manager.h" | ||
|
||
namespace nox::renderer::gl | ||
{ | ||
class GLShaderProgram | ||
class ShaderProgram : public IShaderProgram | ||
{ | ||
public: | ||
GLShaderProgram(); | ||
ShaderProgram(); | ||
~ShaderProgram(); | ||
|
||
bool LoadShaderStage( const std::string &path ); | ||
bool LoadShaderStage( const std::string &path, Stage stage ); | ||
|
||
void Enable() override; | ||
void Disable() override; | ||
|
||
void Reload() override; | ||
|
||
protected: | ||
private: | ||
std::string path_; | ||
uint32_t glProgram{ 0 }; | ||
uint32_t glStages[ ( uint32_t ) Stage::MAX_STAGES ]; | ||
|
||
std::string fragmentPath; | ||
std::string vertexPath; | ||
}; | ||
|
||
class GLShaderManager | ||
class ShaderManager : public IShaderManager | ||
{ | ||
public: | ||
ShaderManager() {} | ||
~ShaderManager() {} | ||
|
||
|
||
|
||
protected: | ||
private: | ||
std::map< std::string, GLShaderProgram > programs_; | ||
}; | ||
}// namespace nox | ||
}// namespace nox::renderer::gl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com> | ||
|
||
#include "../client.h" | ||
#include "shader_manager.h" | ||
|
||
nox::renderer::IShaderProgram *nox::renderer::IShaderManager::FetchProgram( const std::string &name ) | ||
{ | ||
auto i = programs.find( name ); | ||
if ( i == programs.end() ) | ||
{ | ||
Com_Printf( "Failed to find program, \"%s\"!\n", name.c_str() ); | ||
return nullptr; | ||
} | ||
|
||
return i->second; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com> | ||
|
||
#pragma once | ||
|
||
namespace nox::renderer | ||
{ | ||
class IShaderProgram | ||
{ | ||
public: | ||
enum Stage | ||
{ | ||
STAGE_VERTEX, | ||
STAGE_PIXEL, | ||
|
||
MAX_STAGES | ||
}; | ||
virtual bool LoadShaderStage( const std::string &path, Stage stage ) = 0; | ||
|
||
virtual void Enable() = 0; | ||
virtual void Disable() = 0; | ||
|
||
virtual void Reload() = 0; | ||
}; | ||
|
||
class IShaderManager | ||
{ | ||
public: | ||
void SetupDefaultPrograms(); | ||
|
||
IShaderProgram *FetchProgram( const std::string &name ); | ||
virtual IShaderProgram *LoadProgram( const std::string &name, const std::string &vertexPath, const std::string &pixelPath ) = 0; | ||
|
||
private: | ||
std::map< std::string, IShaderProgram * > programs; | ||
}; | ||
|
||
nox::renderer::IShaderProgram *InitializeShaderManager(); | ||
}// namespace nox::renderer |