Add abstract gpu program and source classes

This commit is contained in:
kaetemi 2013-09-07 18:33:18 +02:00
parent a45299bfe5
commit d54774f978
5 changed files with 567 additions and 1 deletions

View file

@ -0,0 +1,233 @@
/**
* \file gpu_program.h
* \brief CGPUProgram
* \date 2013-09-07 15:00GMT
* \author Jan Boon (Kaetemi)
* IGPUProgram
*/
/*
* Copyright (C) 2013 by authors
*
* This file is part of NL3D.
* NL3D is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* NL3D 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 Affero General
* Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with NL3D. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef NL3D_GPU_PROGRAM_H
#define NL3D_GPU_PROGRAM_H
#include <nel/misc/types_nl.h>
// STL includes
// NeL includes
#include <nel/misc/smart_ptr.h>
// Project includes
namespace NL3D {
/**
* \brief CVertexProgramInfo
* \date 2013-09-07 15:00GMT
* \author Jan Boon (Kaetemi)
* Read-only information structure.
*/
struct CVertexProgramInfo
{
public:
std::string DisplayName;
enum TFeatures
{
// World
// transform
// Lights
Ambient = 0x0001,
Sun = 0x0002,
PointLight0 = 0x0004,
PointLight1 = 0x0008,
PointLight2 = 0x0010,
// Lights, additional parameters for user shaders
/// Adds an enabled/disabled parameter to all of the lights
DynamicLights = 0x0020,
};
/// Bitfield containing features used by this vertex program.
uint Features;
/// Indices of parameters used by features.
/// Lights, NeL supports:
/// - Ambient light
uint AmbientIdx; // (Ambient)
/// - One directional light
uint SunDirectionIdx; // (Sun)
uint SunDiffuseIdx; // (Sun)
/// - Zero to three point lights
uint PointLight0PositionIdx; // (PointLight0)
uint PointLight0DiffuseIdx; // (PointLight0)
uint PointLight1PositionIdx; // (PointLight1)
uint PointLight1DiffuseIdx; // (PointLight1)
uint PointLight2PositionIdx; // (PointLight2)
uint PointLight2DiffuseIdx; // (PointLight2)
/// DynamicLights
uint SunEnabledIdx; // (DynamicLights && Sun)
uint PointLight0EnabledIdx; // (DynamicLights && PointLight0)
uint PointLight1EnabledIdx; // (DynamicLights && PointLight1)
uint PointLight2EnabledIdx; // (DynamicLights && PointLight2)
};
/**
* \brief CPixelProgramInfo
* \date 2013-09-07 15:00GMT
* \author Jan Boon (Kaetemi)
* Read-only information structure.
*/
struct CPixelProgramInfo
{
public:
std::string DisplayName;
enum TFeatures
{
/// Use texture stages from CMaterial as texture parameters
MaterialTextures = 0x0001,
/// Set driver fog parameters on this program
Fog = 0x0002,
/// Adds an enabled/disabled parameter to the fog, for user shaders
DynamicFog = 0x0004,
};
// Bitfield containing features used by this pixel program
uint Features;
// Indices of parameters used by features
uint FogEnabledIdx; // (Fog && DynamicFog) nlFogEnabled, fog enabled
uint FogStartEndIdx; // (Fog) nlFogStartEnd, start and end of fog
uint FogColorIdx; // (Fog) nlFogColor, fog color
};
/**
* \brief CGeometryProgramInfo
* \date 2013-09-07 15:00GMT
* \author Jan Boon (Kaetemi)
* Read-only information structure.
*/
struct CGeometryProgramInfo
{
public:
/*enum TFeatures
{
};*/
/// Bitfield containing features used by this pixel program.
// uint Features;
/// Indices of parameters used by features.
};
/**
* \brief CGPUProgram
* \date 2013-09-07 15:00GMT
* \author Jan Boon (Kaetemi)
* A compiled GPU program
*/
class IGPUProgram : public NLMISC::CRefCount
{
public:
enum TProfile
{
// types
// Vertex Shader = 0x01
// Pixel Shader = 0x02
// Geometry Shader = 0x03
// nel - 0x31,type,bitfield
nelvp = 0x31010001, // VP supported by CVertexProgramParser, similar to arbvp1, can be translated to vs_1_1
// direct3d - 0xD9,type,major,minor
// vertex programs
vs_1_1 = 0xD9010101,
vs_2_0 = 0xD9010200,
// vs_2_sw = 0xD9010201, // not sure...
// vs_2_x = 0xD9010202, // not sure...
// vs_3_0 = 0xD9010300, // not supported
// pixel programs
ps_1_1 = 0xD9020101,
ps_1_2 = 0xD9020102,
ps_1_3 = 0xD9020103,
ps_1_4 = 0xD9020104,
ps_2_0 = 0xD9020200,
// ps_2_x = 0xD9020201, // not sure...
// ps_3_0 = 0xD9020300, // not supported
// opengl - 0x61,type,bitfield
// vertex programs
// vp20 = 0x61010001, // NV_vertex_program1_1, outdated
arbvp1 = 0x61010002, // ARB_vertex_program
vp30 = 0x61010004, // NV_vertex_program2
vp40 = 0x61010008, // NV_vertex_program3 + NV_fragment_program3
gp4vp = 0x61010010, // NV_gpu_program4
gp5vp = 0x61010020, // NV_gpu_program5
// pixel programs
// fp20 = 0x61020001, // very limited and outdated, unnecessary
// fp30 = 0x61020002, // NV_fragment_program, now arbfp1, redundant
arbfp1 = 0x61020004, // ARB_fragment_program
fp40 = 0x61020008, // NV_fragment_program2, arbfp1 with "OPTION NV_fragment_program2;\n"
gp4fp = 0x61020010, // NV_gpu_program4
gp5fp = 0x61020020, // NV_gpu_program5
// geometry programs
gp4gp = 0x61030001, // NV_gpu_program4
gp5gp = 0x61030001, // NV_gpu_program5
// glsl - 0x65,type,version
glsl330v = 0x65010330, // GLSL vertex program version 330
glsl330f = 0x65020330, // GLSL fragment program version 330
glsl330g = 0x65030330, // GLSL geometry program version 330
};
public:
IGPUProgram();
virtual ~IGPUProgram();
/// Get the idx of a parameter (ogl: uniform, d3d: constant, etcetera) by name. Invalid name returns ~0
virtual uint getParamIdx(char *name) const = 0;
void buildVPInfo(const char *displayName, uint features);
void buildPPInfo(const char *displayName, uint features);
void buildGPInfo(const char *displayName, uint features);
inline const CVertexProgramInfo *getVPInfo() const { return Info.VertexProgram; }
inline const CPixelProgramInfo *getPPInfo() const { return Info.PixelProgram; }
inline const CGeometryProgramInfo *getGPInfo() const { return Info.GeometryProgram; }
private:
union
{
CVertexProgramInfo *VertexProgram;
CPixelProgramInfo *PixelProgram;
CGeometryProgramInfo *GeometryProgram;
void *Ptr;
} Info;
}; /* class IGPUProgram */
} /* namespace NL3D */
#endif /* #ifndef NL3D_GPU_PROGRAM_H */
/* end of file */

View file

@ -0,0 +1,91 @@
/**
* \file gpu_program_source.h
* \brief CGPUProgramSource
* \date 2013-09-07 14:54GMT
* \author Jan Boon (Kaetemi)
* CGPUProgramSource
*/
/*
* Copyright (C) 2013 by authors
*
* This file is part of NL3D.
* NL3D is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* NL3D 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 Affero General
* Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with NL3D. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef NL3D_GPU_PROGRAM_SOURCE_H
#define NL3D_GPU_PROGRAM_SOURCE_H
#include <nel/misc/types_nl.h>
// STL includes
// NeL includes
#include <nel/misc/string_mapper.h>
// Project includes
#include <nel/3d/gpu_program.h>
namespace NL3D {
/**
* \brief CGPUProgramSource
* \date 2013-09-07 14:54GMT
* \author Jan Boon (Kaetemi)
* A single GPU program with a specific profile.
*/
struct CGPUProgramSource
{
public:
std::string DisplayName;
/// Minimal required profile for this GPU program
IGPUProgram::TProfile Profile;
char *SourcePtr;
/// Copy the source string
inline void setSource(char *source) { SourceCopy = source; SourcePtr = &source[0]; }
/// Set pointer to source string without copying the string
inline void setSourcePtr(char *sourcePtr) { SourceCopy.clear(); SourcePtr = sourcePtr; }
/// CVertexProgramInfo/CPixelProgramInfo/... NeL features
uint Features;
/// Map with known parameter indices, used for assembly programs
std::map<std::string, uint> ParamIndices;
private:
std::string SourceCopy;
}; /* class CGPUProgramSource */
/**
* \brief CGPUProgramSourceCont
* \date 2013-09-07 14:54GMT
* \author Jan Boon (Kaetemi)
* Container for the source code of a single GPU program, allowing
* variations in different language profiles.
*/
struct CGPUProgramSourceCont
{
public:
std::vector<CGPUProgramSource> Sources;
}; /* class CGPUProgramSourceCont */
} /* namespace NL3D */
#endif /* #ifndef NL3D_GPU_PROGRAM_SOURCE_H */
/* end of file */

View file

@ -166,7 +166,11 @@ SOURCE_GROUP(Driver FILES
vertex_program_parse.cpp
../../include/nel/3d/vertex_program_parse.h
pixel_program.cpp
../../include/nel/3d/pixel_program.h)
../../include/nel/3d/pixel_program.h
gpu_program.cpp
../../include/nel/3d/gpu_program.h
gpu_program_source.cpp
../../include/nel/3d/gpu_program_source.h)
SOURCE_GROUP(Font FILES
computed_string.cpp

View file

@ -0,0 +1,191 @@
/**
* \file gpu_program.cpp
* \brief CGPUProgram
* \date 2013-09-07 15:00GMT
* \author Jan Boon (Kaetemi)
* CGPUProgram
*/
/*
* Copyright (C) 2013 by authors
*
* This file is part of NL3D.
* NL3D is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* NL3D 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 Affero General
* Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with NL3D. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <nel/misc/types_nl.h>
#include <nel/3d/gpu_program.h>
// STL includes
// NeL includes
// #include <nel/misc/debug.h>
#include <nel/misc/string_mapper.h>
// Project includes
using namespace std;
// using namespace NLMISC;
namespace NL3D {
IGPUProgram::IGPUProgram()
{
Info.Ptr = NULL;
}
IGPUProgram::~IGPUProgram()
{
delete Info.Ptr;
Info.Ptr = NULL;
}
void IGPUProgram::buildVPInfo(const char *displayName, uint features)
{
nlassert(Info.VertexProgram == NULL);
Info.VertexProgram = new CVertexProgramInfo();
CVertexProgramInfo *info = Info.VertexProgram;
info->DisplayName = displayName;
info->Features = features;
if (features & CVertexProgramInfo::Ambient)
{
info->AmbientIdx = getParamIdx("nlAmbient");
if (info->AmbientIdx == ~0)
{
nlwarning("Missing 'nlAmbient' in gpu program '%s', Ambient disabled", displayName);
info->Features &= ~CVertexProgramInfo::Ambient;
}
}
if (features & CVertexProgramInfo::Sun)
{
if (features & CVertexProgramInfo::DynamicLights)
{
info->SunEnabledIdx = getParamIdx("nlSunEnabled");
if (info->SunEnabledIdx == ~0)
{
nlwarning("Missing 'nlSunEnabled' in gpu program '%s', DynamicLights disabled", displayName);
info->Features &= ~CVertexProgramInfo::DynamicLights;
}
}
info->SunDirectionIdx = getParamIdx("nlSunDirection");
info->SunDiffuseIdx = getParamIdx("nlSunDiffuse");
if (info->SunDirectionIdx == ~0
|| info->SunDiffuseIdx == ~0)
{
nlwarning("Missing 'nlSunDirection/nlSunDiffuse' in gpu program '%s', Sun disabled", displayName);
info->Features &= ~CVertexProgramInfo::Sun;
}
}
if (features & CVertexProgramInfo::PointLight0)
{
if (features & CVertexProgramInfo::DynamicLights)
{
info->PointLight0EnabledIdx = getParamIdx("nlPointLight0Enabled");
if (info->PointLight0EnabledIdx == ~0)
{
nlwarning("Missing 'nlPointLight0Enabled' in gpu program '%s', DynamicLights disabled", displayName);
info->Features &= ~CVertexProgramInfo::DynamicLights;
}
}
info->PointLight0PositionIdx = getParamIdx("nlPointLight0Position");
info->PointLight0DiffuseIdx = getParamIdx("nlPointLight0Diffuse");
if (info->PointLight0PositionIdx == ~0
|| info->PointLight0DiffuseIdx == ~0)
{
nlwarning("Missing 'nlPointLight0Position/nlPointLight0Diffuse' in gpu program '%s', PointLight0 disabled", displayName);
info->Features &= ~CVertexProgramInfo::PointLight0;
}
}
if (features & CVertexProgramInfo::PointLight1)
{
if (features & CVertexProgramInfo::DynamicLights)
{
info->PointLight1EnabledIdx = getParamIdx("nlPointLight1Enabled");
if (info->PointLight1EnabledIdx == ~0)
{
nlwarning("Missing 'nlPointLight1Enabled' in gpu program '%s', DynamicLights disabled", displayName);
info->Features &= ~CVertexProgramInfo::DynamicLights;
}
}
info->PointLight1PositionIdx = getParamIdx("nlPointLight1Position");
info->PointLight1DiffuseIdx = getParamIdx("nlPointLight1Diffuse");
if (info->PointLight1PositionIdx == ~0
|| info->PointLight1DiffuseIdx == ~0)
{
nlwarning("Missing 'nlPointLight1Position/nlPointLight1Diffuse' in gpu program '%s', PointLight1 disabled", displayName);
info->Features &= ~CVertexProgramInfo::PointLight1;
}
}
if (features & CVertexProgramInfo::PointLight2)
{
if (features & CVertexProgramInfo::DynamicLights)
{
info->PointLight2EnabledIdx = getParamIdx("nlPointLight2Enabled");
if (info->PointLight2EnabledIdx == ~0)
{
nlwarning("Missing 'nlPointLight2Enabled' in gpu program '%s', DynamicLights disabled", displayName);
info->Features &= ~CVertexProgramInfo::DynamicLights;
}
}
info->PointLight2PositionIdx = getParamIdx("nlPointLight2Position");
info->PointLight2DiffuseIdx = getParamIdx("nlPointLight2Diffuse");
if (info->PointLight2PositionIdx == ~0
|| info->PointLight2DiffuseIdx == ~0)
{
nlwarning("Missing 'nlPointLight2Position/nlPointLight2Diffuse' in gpu program '%s', PointLight2 disabled", displayName);
info->Features &= ~CVertexProgramInfo::PointLight2;
}
}
}
void IGPUProgram::buildPPInfo(const char *displayName, uint features)
{
nlassert(Info.PixelProgram == NULL);
Info.PixelProgram = new CPixelProgramInfo();
CPixelProgramInfo *info = Info.PixelProgram;
info->DisplayName = displayName;
info->Features = features;
if (features & CPixelProgramInfo::Fog)
{
if (features & CPixelProgramInfo::DynamicFog)
{
info->FogEnabledIdx = getParamIdx("nlFogEnabled");
if (info->FogEnabledIdx == ~0)
{
nlwarning("Missing 'nlFogEnabled' in gpu program '%s', DynamicFog disabled", displayName);
info->Features &= ~CPixelProgramInfo::DynamicFog;
}
}
info->FogStartEndIdx = getParamIdx("nlFogStartEnd");
info->FogColorIdx = getParamIdx("nlFogColor");
if (info->FogStartEndIdx == ~0
|| info->FogStartEndIdx == ~0)
{
nlwarning("Missing 'nlFogStartEnd/nlFogColor' in gpu program '%s', Fog disabled", displayName);
info->Features &= ~CPixelProgramInfo::Fog;
}
}
}
void IGPUProgram::buildGPInfo(const char *displayName, uint features)
{
nlassert(Info.GeometryProgram == NULL);
Info.GeometryProgram = new CGeometryProgramInfo();
}
} /* namespace NL3D */
/* end of file */

View file

@ -0,0 +1,47 @@
/**
* \file gpu_program_source.cpp
* \brief CGPUProgramSource
* \date 2013-09-07 14:54GMT
* \author Jan Boon (Kaetemi)
* CGPUProgramSource
*/
/*
* Copyright (C) 2013 by authors
*
* This file is part of NL3D.
* NL3D is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* NL3D 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 Affero General
* Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with NL3D. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <nel/misc/types_nl.h>
#include <nel/3d/gpu_program_source.h>
// STL includes
// NeL includes
// #include <nel/misc/debug.h>
// Project includes
using namespace std;
// using namespace NLMISC;
namespace NL3D {
void gpu_program_source_cpp_dummy() { }
} /* namespace NL3D */
/* end of file */