-
Hey there :) I am currently working on optimizing some stuff and I am trying to extract things like the projection matrix for my application from each shader setting a uniform into a uniform buffer. How do I properly put the You can take below code as an example (tho i've wrapped everything nicely in classes). GL gl = ...;
uint matrixSize = (uint) Marshal.SizeOf<Matrix4x4>();
uint bytes = matrixSize * 2;// * 2 since I want two matrices actually. a projection matrix and a view matrix
uint bindingPoint = GetNextFreeBindingPoint();// for the sake of this example you can assume this to be 0
uint bufferHandle = gl.GenBuffer();// create the buffer
gl.BindBuffer(BufferTargetARB.UniformBuffer, bufferHandle);// bind it
gl.BufferData(BufferTargetARB.UniformBuffer, bytes, null, BufferUsageARB.StaticDraw);// allocate video memory for the buffer
gl.BindBuffer(BufferTargetARB.UniformBuffer, 0);// unbind it
gl.BindBufferBase(BufferTargetARB.UniformBuffer, bindingPoint , bufferHandle);// bind the buffer to a binding point
uint shaderHandle = CreateShader();
uint uniformBlockIndex = gl.GetUniformBlockIndex(shaderHandle, "camera");
if(uniformBlockIndex == unchecked((uint) -1)) throw new InvalidOperationException(); // will never happen; Do we have a constant for this somewhere?
gl.UniformBlockBinding(shaderHandle, uniformBlockIndex, bindingPoint);// bind the shaders uniform block to the binding point
Matrix4x4 viewMatrix = GetViewMatrix();
Matrix4x4 projectionMatrix = GetProjectionMatrix();
// TODO - put these matrices into the buffer
// THIS DOES NOT WORK AS INTENDED
gl.BindBuffer(BufferTargetARB.UniformBuffer, bufferHandle);// bind the uniform buffer
unsafe {
gl.BufferSubData(BufferTargetARB.UniformBuffer, 0, matrixSize, &viewMatrix);
gl.BufferSubData(BufferTargetARB.UniformBuffer, matrixSize, matrixSize, &projectionMatrix);
}
gl.BindBuffer(BufferTargetARB.UniformBuffer, 0);// unbind it Following could be an example vertex shader. For the sake of this example, there is no model matrix, texture or lighting involved.
Thank you very much for your time and thoughts in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The problem was not the code, but rather that I mixed up the order of projection and view matrix. In my shader I had the view-matrix before the projection matrix, while in the C# code I set the projection matrix with an offset and the viewmatrix without one. Order matters! |
Beta Was this translation helpful? Give feedback.
The problem was not the code, but rather that I mixed up the order of projection and view matrix.
In my shader I had the view-matrix before the projection matrix, while in the C# code I set the projection matrix with an offset and the viewmatrix without one. Order matters!