Skip to content

Commit

Permalink
Tiny bit more work on shader manager implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
hogsy committed Nov 20, 2022
1 parent 14a2d41 commit 9adbd78
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 67 deletions.
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ add_executable(hosae-engine WIN32
# -- generic
client/renderer/light.cpp
client/renderer/material.cpp
client/renderer/shader_manager.cpp

# Server
server/sv_ccmds.cpp
Expand Down
90 changes: 73 additions & 17 deletions engine/client/renderer/ref_gl/gl_shader_manager.cpp
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()
{
}
54 changes: 26 additions & 28 deletions engine/client/renderer/ref_gl/gl_shader_manager.h
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
37 changes: 15 additions & 22 deletions engine/client/renderer/ref_gl/qgl.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
/*
Copyright (C) 1997-2001 Id Software, Inc.
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.
*/
/*
** QGL.H
*/
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 1997-2001 Id Software, Inc.
// Copyright (C) 2020-2022 Mark E Sowden <hogsy@oldtimes-software.com>

#pragma once

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

#include <GL/glew.h>

#if !defined( NDEBUG )
# define XGL_CALL( X ) \
{ \
glGetError(); \
X; \
unsigned int _err = glGetError(); \
assert( _err == GL_NO_ERROR ); \
}
#else
# define XGL_CALL( X ) X
#endif

bool QGL_Init( void );
17 changes: 17 additions & 0 deletions engine/client/renderer/shader_manager.cpp
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;
}
39 changes: 39 additions & 0 deletions engine/client/renderer/shader_manager.h
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

0 comments on commit 9adbd78

Please sign in to comment.