Skip to content

Commit

Permalink
Share shader extension and string identification tables
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthayhurst committed Jul 7, 2024
1 parent 1c88358 commit b45a5c3
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/ammonite/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ namespace ammonite {
//Set by updateCacheSupport(), when GLEW loads
bool isBinaryCacheSupported = false;

//Identify shader types by extensions / contained strings
std::map<std::string, GLenum> shaderMatches = {
{".vert", GL_VERTEX_SHADER}, {".vs", GL_VERTEX_SHADER}, {"vert", GL_VERTEX_SHADER},
{".frag", GL_FRAGMENT_SHADER}, {".fs", GL_FRAGMENT_SHADER}, {"frag", GL_FRAGMENT_SHADER},
{".geom", GL_GEOMETRY_SHADER}, {".gs", GL_GEOMETRY_SHADER}, {"geom", GL_GEOMETRY_SHADER},
{".tessc", GL_TESS_CONTROL_SHADER}, {".tsc", GL_TESS_CONTROL_SHADER},
{"tessc", GL_TESS_CONTROL_SHADER}, {"control", GL_TESS_CONTROL_SHADER},
{".tesse", GL_TESS_EVALUATION_SHADER}, {".tes", GL_TESS_EVALUATION_SHADER},
{"tesse", GL_TESS_EVALUATION_SHADER}, {"eval", GL_TESS_EVALUATION_SHADER},
{".comp", GL_COMPUTE_SHADER}, {".cs", GL_COMPUTE_SHADER}, {"compute", GL_COMPUTE_SHADER},
{".glsl", GL_FALSE} //Don't ignore generic shaders
};

//Data required by cache worker
struct CacheWorkerData {
int shaderCount;
Expand Down Expand Up @@ -140,15 +153,6 @@ namespace ammonite {
}

static GLenum attemptIdentifyShaderType(std::string shaderPath) {
std::map<std::string, GLenum> shaderMatches = {
{"vert", GL_VERTEX_SHADER},
{"frag", GL_FRAGMENT_SHADER},
{"geom", GL_GEOMETRY_SHADER},
{"tessc", GL_TESS_CONTROL_SHADER}, {"control", GL_TESS_CONTROL_SHADER},
{"tesse", GL_TESS_EVALUATION_SHADER}, {"eval", GL_TESS_EVALUATION_SHADER},
{"compute", GL_COMPUTE_SHADER}
};

std::string lowerShaderPath;
for (unsigned int i = 0; i < shaderPath.size(); i++) {
lowerShaderPath += std::tolower(shaderPath[i]);
Expand All @@ -157,7 +161,9 @@ namespace ammonite {
//Try and match the filename to a supported shader
for (auto it = shaderMatches.begin(); it != shaderMatches.end(); it++) {
if (lowerShaderPath.contains(it->first)) {
return it->second;
if (it->second != GL_FALSE) {
return it->second;
}
}
}

Expand Down Expand Up @@ -359,26 +365,15 @@ namespace ammonite {
*/
int createProgram(std::string* inputShaderPaths, int inputShaderCount,
bool* externalSuccess) {
//Convert file extensions to shader types
std::map<std::string, GLenum> shaderExtensions = {
{".vert", GL_VERTEX_SHADER}, {".vs", GL_VERTEX_SHADER},
{".frag", GL_FRAGMENT_SHADER}, {".fs", GL_FRAGMENT_SHADER},
{".geom", GL_GEOMETRY_SHADER}, {".gs", GL_GEOMETRY_SHADER},
{".tessc", GL_TESS_CONTROL_SHADER}, {".tsc", GL_TESS_CONTROL_SHADER},
{".tesse", GL_TESS_EVALUATION_SHADER}, {".tes", GL_TESS_EVALUATION_SHADER},
{".comp", GL_COMPUTE_SHADER}, {".cs", GL_COMPUTE_SHADER},
{".glsl", GL_FALSE} //Detect generic shaders, attempt to identify further
};

//Find all shaders
std::vector<std::string> shaderPaths(0);
std::vector<GLenum> shaderTypes(0);
for (int i = 0; i < inputShaderCount; i++) {
std::filesystem::path filePath{inputShaderPaths[i]};
std::string extension = filePath.extension();

if (shaderExtensions.contains(extension)) {
GLenum shaderType = shaderExtensions[extension];
if (shaderMatches.contains(extension)) {
GLenum shaderType = shaderMatches[extension];

//Shader can't be identified through extension, use filename
if (shaderType == GL_FALSE) {
Expand Down

0 comments on commit b45a5c3

Please sign in to comment.