OpenXR - Multiview uniforms working

This commit is contained in:
Lubos 2022-08-17 19:14:36 +02:00
parent d570a194dd
commit 4026d49104
6 changed files with 25 additions and 43 deletions

View File

@ -279,7 +279,7 @@ void GLQueueRunner::RunInitSteps(const std::vector<GLRInitStep> &steps, bool ski
for (size_t j = 0; j < program->queries_.size(); j++) {
auto &query = program->queries_[j];
_dbg_assert_(query.name);
#ifdef OPENXR_MULTIVIEW
#ifdef OPENXR
int location = -1;
int index = GetStereoBufferIndex(query.name);
if (index >= 0) {
@ -288,7 +288,7 @@ void GLQueueRunner::RunInitSteps(const std::vector<GLRInitStep> &steps, bool ski
GLuint buffer = 0;
glGenBuffers(1, &buffer);
glBindBuffer(GL_UNIFORM_BUFFER, location);
glBindBuffer(GL_UNIFORM_BUFFER, buffer);
glBufferData(GL_UNIFORM_BUFFER,2 * 16 * sizeof(float),NULL, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
location = buffer;
@ -1031,28 +1031,24 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step, bool first, bool last
CHECK_GL_ERROR_IF_DEBUG();
break;
}
#ifdef OPENXR
case GLRRenderCommand::UNIFORMSTEREOMATRIX:
{
_dbg_assert_(curProgram);
int loc = c.uniformMatrix4.loc ? *c.uniformMatrix4.loc : -1;
if (c.uniformMatrix4.name) {
loc = curProgram->GetUniformLoc(c.uniformMatrix4.name);
}
if (loc >= 0) {
int layout = GetStereoBufferIndex(c.uniformMatrix4.name);
if (layout >= 0) {
int size = 2 * 16 * sizeof(float);
int layout = GetStereoBufferIndex(c.uniformMatrix4.name);
glBindBufferBase(GL_UNIFORM_BUFFER, layout, loc);
glBindBuffer(GL_UNIFORM_BUFFER, loc);
glBindBufferBase(GL_UNIFORM_BUFFER, layout, *c.uniformMatrix4.loc);
glBindBuffer(GL_UNIFORM_BUFFER, *c.uniformMatrix4.loc);
void *viewMatrices = glMapBufferRange(GL_UNIFORM_BUFFER, 0, size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
if (viewMatrices) {
memcpy(viewMatrices, c.uniformMatrix4.m, size);
}
memcpy(viewMatrices, c.uniformMatrix4.m, size);
glUnmapBuffer(GL_UNIFORM_BUFFER);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
CHECK_GL_ERROR_IF_DEBUG();
break;
}
#endif
case GLRRenderCommand::UNIFORMMATRIX:
{
_dbg_assert_(curProgram);

View File

@ -742,17 +742,20 @@ public:
curRenderStep_->commands.push_back(data);
}
void SetUniformM4x4Stereo(const GLint *loc, const float *left, const float *right) {
#ifdef OPENXR
void SetUniformM4x4Stereo(const char *name, const GLint *loc, const float *left, const float *right) {
_dbg_assert_(curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER);
#ifdef _DEBUG
_dbg_assert_(curProgram_);
#endif
GLRRenderData data{ GLRRenderCommand::UNIFORMSTEREOMATRIX };
data.uniformMatrix4.name = name;
data.uniformMatrix4.loc = loc;
memcpy(&data.uniformMatrix4.m[0], left, sizeof(float) * 16);
memcpy(&data.uniformMatrix4.m[16], right, sizeof(float) * 16);
curRenderStep_->commands.push_back(data);
}
#endif
void SetUniformM4x4(const char *name, const float *udata) {
_dbg_assert_(curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER);

View File

@ -102,7 +102,6 @@ bool ovrFramebuffer_Create(
frameBuffer->FrameBuffers = (GLuint*)malloc(frameBuffer->TextureSwapChainLength * sizeof(GLuint));
for (uint32_t i = 0; i < frameBuffer->TextureSwapChainLength; i++) {
#ifdef OPENXR_MULTIVIEW
// Create the color buffer texture.
const GLuint colorTexture = frameBuffer->ColorSwapChainImage[i].image;
@ -114,6 +113,7 @@ bool ovrFramebuffer_Create(
GL(glTexParameteri(textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL(glBindTexture(textureTarget, 0));
#ifdef OPENXR_MULTIVIEW
// Create depth buffer.
GL(glGenTextures(1, &frameBuffer->DepthBuffers[i]));
GL(glBindTexture(textureTarget, frameBuffer->DepthBuffers[i]));
@ -132,16 +132,6 @@ bool ovrFramebuffer_Create(
return false;
}
#else
// Create the color buffer texture.
const GLuint colorTexture = frameBuffer->ColorSwapChainImage[i].image;
GLenum colorTextureTarget = GL_TEXTURE_2D;
GL(glBindTexture(colorTextureTarget, colorTexture));
GL(glTexParameteri(colorTextureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GL(glTexParameteri(colorTextureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
GL(glTexParameteri(colorTextureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL(glTexParameteri(colorTextureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GL(glBindTexture(colorTextureTarget, 0));
// Create depth buffer.
GL(glGenRenderbuffers(1, &frameBuffer->DepthBuffers[i]));
GL(glBindRenderbuffer(GL_RENDERBUFFER, frameBuffer->DepthBuffers[i]));
@ -153,13 +143,13 @@ bool ovrFramebuffer_Create(
GL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer->FrameBuffers[i]));
GL(glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, frameBuffer->DepthBuffers[i]));
GL(glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0));
#endif
GL(GLenum renderFramebufferStatus = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER));
GL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
if (renderFramebufferStatus != GL_FRAMEBUFFER_COMPLETE) {
ALOGE("Incomplete frame buffer object: %d", renderFramebufferStatus);
return false;
}
#endif
}
return true;

View File

@ -417,7 +417,8 @@ void VR_EndFrame( engine_t* engine ) {
OXR(xrEndFrame(engine->appState.Session, &endFrameInfo));
frameBuffer->TextureSwapChainIndex++;
frameBuffer->TextureSwapChainIndex %= frameBuffer->TextureSwapChainLength;}
frameBuffer->TextureSwapChainIndex %= frameBuffer->TextureSwapChainLength;
}
int VR_GetConfig( VRConfig config ) {
return vrConfig[config];

View File

@ -151,7 +151,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag
gl_exts.push_back("#extension GL_ARB_cull_distance : enable");
}
}
#ifdef OPENXR_MULTIVIEW
#ifdef OPENXR
gl_exts.push_back("#extension GL_OVR_multiview2 : enable\nlayout(num_views=2) in;");
#endif
ShaderWriter p(buffer, compat, ShaderStage::Vertex, gl_exts.data(), gl_exts.size());
@ -472,8 +472,8 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag
WRITE(p, "uniform mat4 u_proj_through;\n");
*uniformMask |= DIRTY_PROJTHROUGHMATRIX;
} else if (useHWTransform) {
#ifdef OPENXR_MULTIVIEW
WRITE(p, "layout(shared) uniform ProjectionMatrix { uniform mat4 u_proj; };\n");
#ifdef OPENXR
WRITE(p, "layout(shared) uniform ProjectionMatrix { uniform mat4 u_proj[2]; };\n");
#else
WRITE(p, "uniform mat4 u_proj;\n");
#endif
@ -484,8 +484,8 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag
// When transforming by hardware, we need a great deal more uniforms...
// TODO: Use 4x3 matrices where possible. Though probably doesn't matter much.
WRITE(p, "uniform mat4 u_world;\n");
#ifdef OPENXR_MULTIVIEW
WRITE(p, "layout(shared) uniform ViewMatrices { uniform mat4 u_view; };\n");
#ifdef OPENXR
WRITE(p, "layout(shared) uniform ViewMatrices { uniform mat4 u_view[2]; };\n");
#else
WRITE(p, "uniform mat4 u_view;\n");
#endif
@ -912,7 +912,7 @@ bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguag
}
std::string matrixPostfix;
#ifdef OPENXR_MULTIVIEW
#ifdef OPENXR
matrixPostfix = "[gl_ViewID_OVR]";
#endif

View File

@ -421,11 +421,7 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid, bool useBu
ScaleProjMatrix(leftEyeMatrix, useBufferedRendering);
ScaleProjMatrix(rightEyeMatrix, useBufferedRendering);
#ifdef OPENXR_MULTIVIEW
render_->SetUniformM4x4Stereo(&u_proj, leftEyeMatrix.m, rightEyeMatrix.m);
#else
render_->SetUniformM4x4(&u_proj, leftEyeMatrix.m);
#endif
render_->SetUniformM4x4Stereo("u_proj", &u_proj, leftEyeMatrix.m, rightEyeMatrix.m);
#else
Matrix4x4 flippedMatrix;
memcpy(&flippedMatrix, gstate.projMatrix, 16 * sizeof(float));
@ -549,11 +545,7 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid, bool useBu
VR_TweakView(leftEyeView, gstate.projMatrix, VR_VIEW_MATRIX_LEFT_EYE);
VR_TweakView(rightEyeView, gstate.projMatrix, VR_VIEW_MATRIX_RIGHT_EYE);
}
#ifdef OPENXR_MULTIVIEW
render_->SetUniformM4x4Stereo(&u_view, leftEyeView, rightEyeView);
#else
render_->SetUniformM4x4(&u_view, leftEyeView);
#endif
render_->SetUniformM4x4Stereo("u_view", &u_view, leftEyeView, rightEyeView);
#else
SetMatrix4x3(render_, &u_view, gstate.viewMatrix);
#endif