-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial Implementation of Shader Programs #73
Conversation
Also see #74 |
Okay I've fixed a weird segfault. This should be good to go. glShaderSource is a bizarre function... EDIT: It isn't, I just realised my file IO on about line 93 of glprogram.cpp is REALLY bad. I'll fix it ASAP. |
5a376ca
to
4518d34
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the glDelete*
stuff should just be in the destructor of the class (even in the error case). This way resources will always be released when the sh3_glprogram
is destroyed, be it because it couldn't be loaded or when it simply isn't needed anymore.
source/SH3/system/glprogram.cpp
Outdated
std::string fname; // Name of the file we want to open | ||
std::vector<GLchar> errorLog; // Error log | ||
|
||
if(type == GL_VERTEX_SHADER) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's plenty of code duplication in these branches.
For each shader type, only fname seems to be the difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'm getting on fixing this ASAP.
source/SH3/system/glprogram.cpp
Outdated
die(&errorLog[0]); | ||
} | ||
|
||
source.close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for that, RAII will do that.
source/SH3/system/glprogram.cpp
Outdated
|
||
errorLog.resize(logLength); | ||
glGetProgramInfoLog(programID, logLength, &logLength, &errorLog[0]); | ||
glDeleteProgram(vertShader); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be glDeleteShader
I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops haha
source/SH3/system/glprogram.cpp
Outdated
@@ -51,21 +53,24 @@ GLint sh3_glprogram::Load(const std::string& name) | |||
|
|||
void sh3_glprogram::Bind() | |||
{ | |||
|
|||
if(programID != 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(programID == 0)
{
//warn
return;
}
?
source/SH3/system/glprogram.cpp
Outdated
} | ||
|
||
void sh3_glprogram::Unbind() | ||
{ | ||
|
||
glUseProgram(0); // The 'correct' way to unbind a shader, but apparently undefined?!?! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a huge issue with this, but as a fix we could use a dummy shader (and have a static class variable refer to it) that is set on Unbind()
.
The dummy shader would either just render a solid color or an error message if we want to be fancy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that sounds like a good idea. Maybe even a checkered square like the Source engine haha.
source/SH3/system/glprogram.cpp
Outdated
@@ -75,11 +80,17 @@ GLuint sh3_glprogram::Compile(std::uint32_t type) | |||
|
|||
source.open(fname); | |||
|
|||
if(!source.is_open()) | |||
die("sh3_glprogram::Compile( ): Unable to open a handle to %s!", fname.c_str()); | |||
|
|||
while(!source.eof()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could require the shader sources to have UNIX-style line endings ('\n'), in which case we can open the file in binary mode and use a std::ostringstream
and read the whole file via stringstream << source.rdbuf();
.
Reading the file like this is safer though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh I see. I'm using Windows, so I think the problem is stemming from the stupid \r\n
at the end of each line.
Why is this method safer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Safer as in, doesn't lead to errors when using different line endings.
You should be able to switch to different line endings in your editor.
Regarding |
89e1dcf
to
08779e4
Compare
source/SH3/system/glprogram.cpp
Outdated
@@ -39,7 +41,7 @@ GLint sh3_glprogram::Load(const std::string& name) | |||
|
|||
errorLog.resize(logLength); | |||
glGetProgramInfoLog(programID, logLength, &logLength, &errorLog[0]); | |||
glDeleteProgram(vertShader); | |||
glDeleteShader(vertShader); | |||
glDeleteShader(fragShader); | |||
|
|||
die(&errorLog[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the string contains something looking like a format specifier, things will go wrong. Use die("%s", ...)
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely missed that, it's fixed now.
The |
If anything, they should be destroyed right after the link is performed, as they are no longer needed and are just a waste of memory (as I have read, but it seems everyone has their own opinion on this stuff haha), though with the amount most people have in their system, this is probably a non-issue (though if we preload stuff it may be).
Will the constructor still be run if a call to |
b49b9bd
to
256d5c9
Compare
I though that |
True, we should probably just keep them to aid with debugging shaders at this point.
Wouldn't that imply the file is corrupt or damaged in some way though? Also, do you want me to move the |
That or driver issues probably. |
Yeah, that seems good. Although now |
Good point, I'll get rid of all of the calls to
Bloody hell, more of my 1AM programming... I think I should make myself a coffee before I attempt to program (that or crack open a beer). I've fixed it now.
I had no clue this was the case. Everything I read 'recommended' a detach before delete. Very interesting. I've removed all calls to detach. |
d96d423
to
ddf1425
Compare
source/SH3/system/glprogram.cpp
Outdated
@@ -93,7 +95,7 @@ GLuint sh3_glprogram::Compile(GLenum type) | |||
source.open(fname); | |||
|
|||
if(!source.is_open()) | |||
die("sh3_glprogram::Compile( ): Unable to open a handle to %s!", fname.c_str()); | |||
Log(LogLevel::ERROR, "sh3_glprogram::Compile( ): Unable to open a handle to %s!", fname.c_str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can just return
at this point.
//TODO: default shader fallback.
You can also squash the commits. I don't see a good reason to keep them separate. |
5486af6
to
c9dbaac
Compare
{ | ||
id = glCreateShader(GL_FRAGMENT_SHADER); // Generate a shader ID | ||
fname = "data/shaders/" + programName + ".frag"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else
die()
source/SH3/system/glprogram.cpp
Outdated
#include <fstream> | ||
#include <vector> | ||
|
||
#define BAD_SHADER 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a static const GLuint
IMO.
Can load, compile and link shaders from files. Currently untested (as we don't have a GLContext working as of yet!)
f55481f
to
a5257b8
Compare
Can load, compile and link shaders from files. Currently untested (as we
don't have a GLContext working as of yet!)
@z33ky I'm going to add more to this over the next few days. We should probably start doing some GL stuff to test drawing/some other systems (like textures). Dis you want to try out fast-forwaring instead of merging this time?