diff --git a/ext/native/thin3d/GLQueueRunner.cpp b/ext/native/thin3d/GLQueueRunner.cpp index f5275fdc72..0468cdb014 100644 --- a/ext/native/thin3d/GLQueueRunner.cpp +++ b/ext/native/thin3d/GLQueueRunner.cpp @@ -19,7 +19,12 @@ void GLQueueRunner::RunInitSteps(const std::vector &steps) { const GLRInitStep &step = steps[i]; switch (step.stepType) { case GLRInitStepType::CREATE_TEXTURE: + { + GLRTexture *tex = step.create_texture.texture; + glGenTextures(1, &tex->texture); + glBindTexture(tex->target, tex->texture); break; + } case GLRInitStepType::CREATE_BUFFER: { GLRBuffer *buffer = step.create_buffer.buffer; @@ -122,8 +127,14 @@ void GLQueueRunner::RunInitSteps(const std::vector &steps) { glTexImage2D(tex->target, step.texture_image.level, step.texture_image.internalFormat, step.texture_image.width, step.texture_image.height, 0, step.texture_image.format, step.texture_image.type, step.texture_image.data); delete[] step.texture_image.data; CHECK_GL_ERROR_IF_DEBUG(); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); break; } + default: + Crash(); } } } @@ -147,6 +158,9 @@ void GLQueueRunner::RunSteps(const std::vector &steps) { case GLRStepType::READBACK_IMAGE: PerformReadbackImage(step); break; + default: + Crash(); + break; } delete steps[i]; } @@ -172,9 +186,6 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step) { // This is supposed to bind a vulkan render pass to the command buffer. PerformBindFramebufferAsRenderTarget(step); - int curWidth = step.render.framebuffer ? step.render.framebuffer->width : 0; // vulkan_->GetBackbufferWidth(); - int curHeight = step.render.framebuffer ? step.render.framebuffer->height : 0; // vulkan_->GetBackbufferHeight(); - GLRFramebuffer *fb = step.render.framebuffer; GLRProgram *curProgram = nullptr; GLint activeTexture = GL_TEXTURE0; @@ -293,7 +304,8 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step) { glEnableVertexAttribArray(i); } } - for (auto &entry: layout->entries) { + for (int i = 0; i < layout->entries.size(); i++) { + auto &entry = layout->entries[i]; glVertexAttribPointer(entry.location, entry.count, entry.type, entry.normalized, entry.stride, (const void *)(c.inputLayout.offset + entry.offset)); } break; @@ -325,12 +337,26 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, c.textureSampler.magFilter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, c.textureSampler.minFilter); break; + case GLRRenderCommand::RASTER: + if (c.raster.cullEnable) { + glEnable(GL_CULL_FACE); + glFrontFace(c.raster.frontFace); + glCullFace(c.raster.cullFace); + } else { + glDisable(GL_CULL_FACE); + } + break; + default: + Crash(); + break; } } + if (activeTexture != GL_TEXTURE0) glActiveTexture(GL_TEXTURE0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glDisable(GL_SCISSOR_TEST); } void GLQueueRunner::PerformCopy(const GLRStep &step) { diff --git a/ext/native/thin3d/GLRenderManager.cpp b/ext/native/thin3d/GLRenderManager.cpp index ea0f7708dd..ddbaca0a19 100644 --- a/ext/native/thin3d/GLRenderManager.cpp +++ b/ext/native/thin3d/GLRenderManager.cpp @@ -224,12 +224,14 @@ void GLRenderManager::Finish() { FrameData &frameData = frameData_[curFrame]; if (!useThread_) { frameData.steps = std::move(steps_); + frameData.initSteps = std::move(initSteps_); frameData.type = GLRRunType::END; Run(curFrame); } else { std::unique_lock lock(frameData.pull_mutex); VLOG("PUSH: Frame[%d].readyForRun = true", curFrame); frameData.steps = std::move(steps_); + frameData.initSteps = std::move(initSteps_); frameData.readyForRun = true; frameData.type = GLRRunType::END; frameData.pull_condVar.notify_all(); @@ -239,6 +241,8 @@ void GLRenderManager::Finish() { frameData_[curFrame_].deleter.Take(deleter_); curFrame_++; + if (curFrame_ >= MAX_INFLIGHT_FRAMES) + curFrame_ = 0; insideFrame_ = false; } diff --git a/ext/native/thin3d/GLRenderManager.h b/ext/native/thin3d/GLRenderManager.h index ebec0abddc..fe18490bd8 100644 --- a/ext/native/thin3d/GLRenderManager.h +++ b/ext/native/thin3d/GLRenderManager.h @@ -217,16 +217,16 @@ public: for (int i = 0; i < shaders.size(); i++) { step.create_program.shaders[i] = shaders[i]; } + step.create_program.num_shaders = (int)shaders.size(); initSteps_.push_back(step); return step.create_program.program; } GLRInputLayout *CreateInputLayout(std::vector &entries) { - GLRInitStep step{ GLRInitStepType::CREATE_PROGRAM }; - assert(shaders.size() <= ARRAY_SIZE(step.create_program.shaders)); + GLRInitStep step{ GLRInitStepType::CREATE_INPUT_LAYOUT }; step.create_input_layout.inputLayout = new GLRInputLayout(); - step.create_input_layout.inputLayout->entries = std::move(entries); - for (auto &iter : entries) { + step.create_input_layout.inputLayout->entries = entries; + for (auto &iter : step.create_input_layout.inputLayout->entries) { step.create_input_layout.inputLayout->semanticsMask_ |= 1 << iter.location; } initSteps_.push_back(step); @@ -271,6 +271,7 @@ public: void TextureImage(GLRTexture *texture, int level, int width, int height, GLenum internalFormat, GLenum format, GLenum type, uint8_t *data) { GLRInitStep step{ GLRInitStepType::TEXTURE_IMAGE }; + step.texture_image.texture = texture; step.texture_image.data = data; step.texture_image.internalFormat = internalFormat; step.texture_image.format = format; @@ -368,7 +369,7 @@ public: void SetUniformM4x4(const char *name, const float *udata) { _dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); - GLRRenderData data{ GLRRenderCommand::UNIFORM4F }; + GLRRenderData data{ GLRRenderCommand::UNIFORMMATRIX }; data.uniformMatrix4.name = name; memcpy(data.uniformMatrix4.m, udata, sizeof(float) * 16); curRenderStep_->commands.push_back(data); diff --git a/ext/native/thin3d/thin3d_gl.cpp b/ext/native/thin3d/thin3d_gl.cpp index a7bcd9a7d0..8aaff8a5c7 100644 --- a/ext/native/thin3d/thin3d_gl.cpp +++ b/ext/native/thin3d/thin3d_gl.cpp @@ -618,8 +618,6 @@ OpenGLTexture::OpenGLTexture(GLRenderManager *render, const TextureDesc &desc) : GLenum target = TypeToTarget(desc.type); tex_ = render->CreateTexture(target, width_, height_); - render->BindTexture(0, tex_); - canWrap_ = isPowerOf2(width_) && isPowerOf2(height_); mipLevels_ = desc.mipLevels; if (!desc.initData.size()) @@ -634,7 +632,6 @@ OpenGLTexture::OpenGLTexture(GLRenderManager *render, const TextureDesc &desc) : } mipLevels_ = desc.generateMips ? desc.mipLevels : level; - render->SetTextureSampler(GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T, GL_LINEAR, mipLevels_ > 1 ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); /* #ifdef USING_GLES2 if (gl_extensions.GLES3) { @@ -650,8 +647,6 @@ OpenGLTexture::OpenGLTexture(GLRenderManager *render, const TextureDesc &desc) : AutoGenMipmaps(); } - // Unbind. - render->BindTexture(0, nullptr); } OpenGLTexture::~OpenGLTexture() { @@ -664,7 +659,7 @@ OpenGLTexture::~OpenGLTexture() { void OpenGLTexture::AutoGenMipmaps() { if (!generatedMips_) { - render_->BindTexture(0, tex_); + // Assumes the texture is bound for editing render_->GenerateMipmap(); generatedMips_ = true; } @@ -1133,8 +1128,8 @@ void OpenGLContext::DrawIndexed(int vertexCount, int offset) { // Note: ibuf binding is stored in the VAO, so call this after binding the fmt. curIBuffer_->Bind(curIBufferOffset_); - glDrawElements(curPipeline_->prim, vertexCount, GL_UNSIGNED_INT, (const void *)(size_t)offset); - + renderManager_.DrawIndexed(curPipeline_->prim, vertexCount, GL_UNSIGNED_INT, (void *)(intptr_t)offset); + curPipeline_->inputLayout->Unapply(); } @@ -1142,7 +1137,7 @@ void OpenGLContext::DrawUP(const void *vdata, int vertexCount) { curPipeline_->inputLayout->Apply(vdata); ApplySamplers(); - glDrawArrays(curPipeline_->prim, 0, vertexCount); + renderManager_.Draw(curPipeline_->prim, 0, vertexCount); curPipeline_->inputLayout->Unapply(); } @@ -1483,6 +1478,14 @@ void OpenGLContext::fbo_unbind() { } void OpenGLContext::BindFramebufferAsRenderTarget(Framebuffer *fbo, const RenderPassInfo &rp) { + OpenGLFramebuffer *fb = (OpenGLFramebuffer *)fbo; + GLRRenderPassAction color = (GLRRenderPassAction)rp.color; + GLRRenderPassAction depth = (GLRRenderPassAction)rp.depth; + + renderManager_.BindFramebufferAsRenderTarget(fb ? fb->framebuffer : nullptr, color, depth, rp.clearColor, rp.clearDepth, rp.clearStencil); + +#if 0 + CHECK_GL_ERROR_IF_DEBUG(); curFB_ = (OpenGLFramebuffer *)fbo; if (fbo) { @@ -1536,6 +1539,7 @@ void OpenGLContext::BindFramebufferAsRenderTarget(Framebuffer *fbo, const Render glstate.stencilMask.restore(); } CHECK_GL_ERROR_IF_DEBUG(); +#endif } void OpenGLContext::CopyFramebufferImage(Framebuffer *fbsrc, int srcLevel, int srcX, int srcY, int srcZ, Framebuffer *fbdst, int dstLevel, int dstX, int dstY, int dstZ, int width, int height, int depth, int channelBits) {