Skip to content

Commit

Permalink
Support .glsl shaders, identify type via filename
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthayhurst committed Jul 31, 2023
1 parent 8c4bf7f commit 2e920ce
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/ammonite/graphics/shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ namespace ammonite {
return false;
}

static GLenum attemptIdentifyShaderType(const char* shaderPath) {
std::string shaderPathString = 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 < shaderPathString.size(); i++) {
lowerShaderPath += std::tolower(shaderPathString[i]);
}

//Try and match the filename to a supported shader
for (auto it = shaderMatches.begin(); it != shaderMatches.end(); it++) {
if (lowerShaderPath.find(it->first) != std::string::npos) {
return it->second;
}
}

return GL_FALSE;
}

//Set by updateGLCacheSupport(), when GLEW loads
bool isBinaryCacheSupported = false;
}
Expand Down Expand Up @@ -303,7 +330,8 @@ namespace ammonite {
{".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}
{".comp", GL_COMPUTE_SHADER}, {".cs", GL_COMPUTE_SHADER},
{".glsl", GL_FALSE} //Detect generic shaders, attempt to identify
};

//Find all shaders
Expand All @@ -316,6 +344,19 @@ namespace ammonite {
if (shaderExtensions.contains(extension)) {
GLenum shaderType = shaderExtensions[extension];

//Shader can't be identified through extension, use filename
if (shaderType == GL_FALSE) {
GLenum newType = attemptIdentifyShaderType(inputShaderPaths[i]);
if (newType == GL_FALSE) {
std::cerr << ammonite::utils::warning << "Couldn't identify type of shader '"
<< inputShaderPaths[i] << "'" << std::endl;
continue;
}

//Found a type, so use it
shaderType = newType;
}

//Check for compute shader support if needed
if (shaderType == GL_COMPUTE_SHADER) {
if (!ammonite::utils::checkExtension("GL_ARB_compute_shader", "GL_VERSION_4_3")) {
Expand Down

0 comments on commit 2e920ce

Please sign in to comment.