-
Notifications
You must be signed in to change notification settings - Fork 116
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
glClearBuffer & glReadAttachmentPixel #935
glClearBuffer & glReadAttachmentPixel #935
Conversation
+glClearBuffer +glReadAttachmentPixel
size = 4; | ||
} break; | ||
default: { | ||
size = 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.
Add assert(false) for the default case.
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 is a utility function. Assert must or must not be placed in a function that uses it, not here.
} | ||
|
||
int LuaOpenGL::ClearBuffer(lua_State* L) | ||
{ |
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 dislike 0, -1 and nils in this API, as much as possible the API should be self-explainatory, which is not the case here. Use semantics of https://springrts.com/wiki/Lua_FBO_and_RBO ("depth", "stencil", "color0", etc) or literal GL constants.
Also there's no need to for a user to explicitly select what kind of format the attached texture has, the implementation should either store it somewhere or retrieve it from the GL server - https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetFramebufferAttachmentParameter.xhtml
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 dislike 0, -1 and nils in this API, as much as possible the API should be self-explainatory, which is not the case here. Use semantics of https://springrts.com/wiki/Lua_FBO_and_RBO ("depth", "stencil", "color0", etc) or literal GL constants.
Also there's no need to for a user to explicitly select what kind of format the attached texture has, the implementation should either store it somewhere or retrieve it from the GL server - https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetFramebufferAttachmentParameter.xhtml
Why don't we keep such info as part of FBO class ourselves?
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.
Because it was rarely (if it all) needed before
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.
Going to update this later.
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 is good possibility this GetFramebuffer... thing is busted.
I may be needing to update FBO class to retain info of its attachments.
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.
What do you mean "busted"?
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.
What do you mean "busted"?
Not busted, I got it working in the end.
But this function is limited. The only useful is GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE, but that is not the only thing I need.
I am planning to make Lua FBOs store attachment formats tomorrow, so they can be retrieved. What do you 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.
Yeah no reason not to store additional information, if you need it.
} | ||
} | ||
|
||
int LuaOpenGL::ReadAttachmentPixel(lua_State* L) |
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.
Why do you need such function? GPU-->CPU read is almost always bad and will cause significant performance implications. If anything consider using PBOs and respective asynchronous API https://riptutorial.com/opengl/example/28872/using-pbos
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.
Why do you need such function? GPU-->CPU read is almost always bad and will cause significant performance implications. If anything consider using PBOs and respective asynchronous API https://riptutorial.com/opengl/example/28872/using-pbos
Do we already have anything like this implemented? Any reference to Recoil code.
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.
Why do you need such function? GPU-->CPU read is almost always bad and will cause significant performance implications. If anything consider using PBOs and respective asynchronous API https://riptutorial.com/opengl/example/28872/using-pbos
Performance is not priority. Think of this function as a function for a game editor or debug process, not the game itself. glReadPixels as it is works incorrectly for integer textures and doesn't allow reading individual attachments from within big FBO.
I don't have experience with PBO. Can you clarify, if I am to read a pixel data from the game render, does it allow to read the result of this exact frame which is happening right now, or it will be one of the previous frames?
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.
Do we already have anything like this implemented? Any reference to Recoil code.
Nope but have a look at this old thread https://community.khronos.org/t/pbo-glreadpixels-not-so-fast/55783/13
does it allow to read the result of this exact frame which is happening right now, or it will be one of the previous frames?
Nope, but usually getting the pixel data a few frames late is fine.
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.
Do we already have anything like this implemented? Any reference to Recoil code.
Nope but have a look at this old thread https://community.khronos.org/t/pbo-glreadpixels-not-so-fast/55783/13
does it allow to read the result of this exact frame which is happening right now, or it will be one of the previous frames?
Nope, but usually getting the pixel data a few frames late is fine.
I am aware of glReadPixels performance. If I hit a bottleneck with this, I am going to investigate it further.
The purpose of this function is not fast performant texture read, it is to momentarily read the data from texture, same as gl.ReadPixels, limited to a single pixel, but applied to individual attachment and able to work with integer textures.
This function is a part of an update that enables work with integer textures and individual attachments (glClearBuffer). (Proper setup of integer textures was fixed few PRs ago)
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.
Would be good if someone actually tests this later, to find out how slow it really is.
Can do this test with gl.ReadPixels as well.
But this is not the point of this PR.
Keep track of active Lua FBO Store and reuse attachment format info
API is better but not ideal yet, I think it's best to stick to string literals for color attachments in that case too. You might want to go switch/case(s) route which is more readable and utilize |
I am not a fan of "colori" stuff. What purpose does it serve? It is also 0-based, not what Lua should be. |
There is another thing that must be said, ClearBuffer doesn't operate on attachment, but a draw buffer. On the other hand, the current FBO is setup using this construction table: Hopefully it explains why I don't like it. |
Although its reasonable for modifying fbo like |
So @Krogoth100 do you plan to incorporate the changes I requested? |
Working on another stuff rn |
Yes, this feature is for your game only, but there's nothing wrong with that. My only concern here is that it complies to the API standard I feel are right for the job. If this ask is too much - feel free to close. |
I will post updated version in beginning of September. |
@lhog Done. |
LGTM, can you resolve the merge conflict please? |
Done |
Is there any change other than adding |
There is only cosmetic code change besides that, should be no effect |
Sorry I was (and still is) terribly busy IRL and I missed the merge window. Will merge as soon as we release a stable version. |
Updated this PR at #1807 (merged current master) |
Superseded by #1807 |
Add gl.ClearBuffer(drawBufferIndex, clearingFunc, r,g,b,a)
Clears an individual draw buffer of an FBO and properly clears integer textures (the latter is more important)
drawBufferIndex special values: 0 - depth, -1 - stencil
gl.ClearBuffer(0, nil, depth) - clear depth buffer
gl.ClearBuffer(-1, nil, value) - clear stencil buffer
gl.ClearBuffer(1, nil, r,g,b,a) - clear float color buffer 1
gl.ClearBuffer(2, 2, r,g,b,a) - clear unsigned integer color buffer 2
Add gl.ReadAttachmentPixel(attachmentIndex, x, y, internalFormat)
A version of ReadPixels that reads a single pixel, but does it from specified FBO attachment and works with many formats (e.g. integer textures).
(See implementation for details)
Don't give same treatment to gl.ReadPixels, that function is complex as it is
attachmentIndex special values: 0 - depth
local depth = gl.ReadAttachmentPixel(0, x,y, GL_DEPTH_COMPONENT32F) - read FBO depth
local r,g,b,a = gl.ReadAttachmentPixel(2, x,y, GL_RGBA16F) - read color attachment 2