void DepthOfField::renderPass(const DRBCameraDrawableList &infos) { if (infos.cameraInfos.data.dof) { SCOPE_profile_gpu_i("Depth of field"); SCOPE_profile_cpu_i("RenderTimer", "Depth of field"); _depth->bind(); _depth->generateMipmaps(); OpenGLState::glDepthMask(GL_FALSE); OpenGLState::glDisable(GL_DEPTH_TEST); OpenGLState::glDisable(GL_STENCIL_TEST); OpenGLState::glDisable(GL_CULL_FACE); _programs[PROGRAM_DEPTH_OF_FIELD]->use(); _programs[PROGRAM_DEPTH_OF_FIELD]->get_resource<Sampler2D>(StringID("cleanMap", 0x90c5a5421e083038)).set(_clean); _programs[PROGRAM_DEPTH_OF_FIELD]->get_resource<Sampler2D>(StringID("depthMap", 0xeb35b90435165cd4)).set(_depth); _programs[PROGRAM_DEPTH_OF_FIELD]->get_resource<Sampler2D>(StringID("blurredMap1", 0x031e7d3c1b84e96a)).set(_blurred1); _programs[PROGRAM_DEPTH_OF_FIELD]->get_resource<Sampler2D>(StringID("blurredMap2", 0x031e7c3c1b84e7b7)).set(_blurred2); _quadPainter->uniqueDrawBegin(_programs[PROGRAM_DEPTH_OF_FIELD]); _quadPainter->uniqueDraw(GL_TRIANGLES, _programs[PROGRAM_DEPTH_OF_FIELD], _quadVertices); _quadPainter->uniqueDrawEnd(); } }
void SdlContext::swapContext() { MicroProfileFlip(); { SCOPE_profile_cpu_i("RenderTimer", "swapContext"); SCOPE_profile_gpu_i("SwapContext"); SDL_GL_SwapWindow(_window); } }
bool RenderThread::init() { registerCallback<Tasks::Render::CreateRenderContext>([this](Tasks::Render::CreateRenderContext &msg) { _context = msg.engine->setInstance<SdlContext, IRenderContext>(); auto configurationManager = msg.engine->getInstance<ConfigurationManager>(); auto w = configurationManager->getConfiguration<int>("windowW")->getValue(); auto h = configurationManager->getConfiguration<int>("windowH")->getValue(); auto f = configurationManager->getConfiguration<bool>("fullScreen")->getValue(); if (!_context->init(w, h, f, "~AGE~ V0.00001 Demo")) { msg.setValue(false); return; } if (_depthMapManager == nullptr) { _depthMapManager = new DepthMapManager; } #ifdef DEBUG _depthMapManager->init(1280, 720, 4); #else _depthMapManager->init(1280, 720, 2); #endif msg.setValue(true); }); registerCallback<Tasks::Render::InitRenderPipelines>([this](Tasks::Render::InitRenderPipelines &msg) { _context = msg.engine->setInstance<SdlContext, IRenderContext>(); pipelines.resize(RenderType::TOTAL); pipelines[DEFERRED] = std::make_unique<DeferredShading>(_context->getScreenSize(), paintingManager); pipelines[DEBUG_DEFERRED] = std::make_unique<DebugDeferredShading>(_context->getScreenSize(), paintingManager); _recompileShaders(); _initPipelines(); _bonesTexture = createRenderPassOutput<TextureBuffer>(8184 * 2, GL_RGBA32F, sizeof(glm::mat4), GL_DYNAMIC_DRAW); msg.setValue(true); }); registerCallback<Commands::ToRender::Flush>([&](Commands::ToRender::Flush& msg) { SCOPE_profile_cpu_i("RenderTimer", "Render frame"); if (msg.isRenderFrame) { #ifdef AGE_ENABLE_IMGUI std::shared_ptr<AGE::RenderImgui> imguiRenderList = nullptr; { std::lock_guard<AGE::SpinLock> lock(_mutex); imguiRenderList = _imguiRenderlist; } if (imguiRenderList != nullptr) { AGE::Imgui::getInstance()->renderThreadRenderFn(imguiRenderList->cmd_lists); } static bool first = true; if (first || imguiRenderList) { TMQ::TaskManager::emplaceMainTask<ImGuiEndOfFrame>(); first = false; } #endif _context->swapContext(); { SCOPE_profile_gpu_i("Clear buffer"); SCOPE_profile_cpu_i("RenderTimer", "Clear buffer"); glClear(GL_COLOR_BUFFER_BIT); } ++_frameCounter; if (_context) { _context->refreshInputs(); } } }); registerCallback<Tasks::Render::ReloadShaders>([&](Tasks::Render::ReloadShaders& msg) { #ifdef AGE_DEBUG _recompileShaders(); #else std::cerr << "Error : You cannot recompile shader at runtime. This feature is enabled only in debug mode\n"; #endif }); registerSharedCallback<AGE::Tasks::Basic::BoolFunction>([&](AGE::Tasks::Basic::BoolFunction& msg) { SCOPE_profile_cpu_i("RenderTimer", "Bool function"); msg.setValue(msg.function()); }); registerCallback<AGE::Tasks::Basic::VoidFunction>([&](AGE::Tasks::Basic::VoidFunction& msg) { SCOPE_profile_cpu_i("RenderTimer", "Void function"); if (msg.function) msg.function(); }); registerCallback<AGE::Tasks::Basic::Exit>([&](AGE::Tasks::Basic::Exit& msg) { AGE::GetEngine()->deleteInstance<IRenderContext>(); this->_insideRun = false; TMQ::TaskManager::emplaceSharedTask<Tasks::Basic::Exit>(); }); registerCallback<AGE::Tasks::Render::ContextGrabMouse>([&](AGE::Tasks::Render::ContextGrabMouse &msg) { _context->grabMouse(msg.grabMouse == 1 ? true : false); }); registerCallback<AGE::DRBCameraDrawableListCommand>([&](AGE::DRBCameraDrawableListCommand &msg) { std::shared_ptr<Painter> line2DPainter = nullptr; std::shared_ptr<Painter> line3DPainter = nullptr; std::shared_ptr<Painter> line3DPainterDepth = nullptr; //if (pipelines[msg.list->cameraInfos.data.pipeline]->isDebug()) //{ DebugDrawManager* debugDrawManager = nullptr; if (GetEngine()->hasInstance<DebugDrawManager>()) { debugDrawManager = GetEngine()->getInstance<DebugDrawManager>(); } if (debugDrawManager) { debugDrawManager->renderBegin(paintingManager); } pipelines[msg.list->cameraInfos.data.pipeline]->render(*msg.list.get()); if (debugDrawManager) { debugDrawManager->renderEnd(); } }); registerCallback<AGE::Tasks::UploadBonesToGPU>([&](AGE::Tasks::UploadBonesToGPU& msg) { SCOPE_profile_cpu_i("!!!HACK!!!", "Upload bones matrix to GPU"); _bonesTexture->set(msg.bones->data(), msg.bones->size()); }); return true; }
void DeferredShadowBuffering::renderPass(const DRBCameraDrawableList &infos) { //@PROUT SCOPE_profile_gpu_i("DeferredShadowBuffering render pass"); SCOPE_profile_cpu_i("RenderTimer", "DeferredShadowBuffering render pass"); OpenGLState::glEnable(GL_CULL_FACE); OpenGLState::glCullFace(GL_FRONT); OpenGLState::glDepthMask(GL_TRUE); OpenGLState::glDepthFunc(GL_LEQUAL); OpenGLState::glDisable(GL_BLEND); OpenGLState::glDisable(GL_STENCIL_TEST); OpenGLState::glEnable(GL_DEPTH_TEST); OpenGLState::glDepthMask(GL_TRUE); OpenGLState::glDepthFunc(GL_LESS); _programs[PROGRAM_BUFFERING]->use(); // handle the number of sample if (_depthBuffers.size() < infos.spotLights.size()) { std::size_t count = infos.spotLights.size() - _depthBuffers.size(); for (int index = 0; index < count; ++index) { _depthBuffers.push_back(createRenderPassOutput<Texture2D>(_frame_buffer.width(), _frame_buffer.height(), GL_DEPTH24_STENCIL8, true)); _depthBuffers.back()->bind(); _depthBuffers.back()->parameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST); _depthBuffers.back()->parameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST); _depthBuffers.back()->parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); _depthBuffers.back()->parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); _depthBuffers.back()->parameter(GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); } } else if (_depthBuffers.size() > infos.spotLights.size()) { std::size_t count = _depthBuffers.size() - infos.spotLights.size(); for (int index = 0; index < count; ++index) { _depthBuffers.pop_back(); } } // start to render to texture for each depth map auto it = _depthBuffers.begin(); std::shared_ptr<Painter> painter = nullptr; std::shared_ptr<Painter> oldPainter = nullptr; for (auto &spotLightPtr : infos.spotLights) { SCOPE_profile_gpu_i("Spotlight pass"); SCOPE_profile_cpu_i("RenderTimer", "Spotlight pass"); DRBSpotLightData *spotlight = (DRBSpotLightData*)(spotLightPtr->spotLight.get()); glViewport(0, 0, _frame_buffer.width(), _frame_buffer.height()); _frame_buffer.attachment(*(*it), GL_DEPTH_STENCIL_ATTACHMENT); glClear(GL_DEPTH_BUFFER_BIT); _programs[PROGRAM_BUFFERING]->registerProperties(spotlight->globalProperties); _programs[PROGRAM_BUFFERING]->updateProperties(spotlight->globalProperties); // draw for the spot light selected for (auto &meshPaint : spotLightPtr->meshs) { auto mesh = (DRBMeshData*)(meshPaint.get()); //temporary //todo, do not spawn entity while mesh is not loaded //currently it's not safe, because the paiter key can be invalid //during the first frames if (mesh->getPainterKey().isValid()) { painter = _painterManager->get_painter(mesh->getPainterKey()); if (painter != oldPainter) { if (oldPainter) { oldPainter->uniqueDrawEnd(); } painter->uniqueDrawBegin(_programs[PROGRAM_BUFFERING]); } oldPainter = painter; painter->uniqueDraw(GL_TRIANGLES, _programs[PROGRAM_BUFFERING], mesh->globalProperties, mesh->getVerticesKey()); } } spotlight->shadowMap = *it; ++it; } if (oldPainter) { oldPainter->uniqueDrawEnd(); } }
void Imgui::renderThreadRenderFn(std::vector<Age_ImDrawList> const &cmd_lists) { SCOPE_profile_cpu_function("RenderTimer"); SCOPE_profile_gpu_i("Render IMGUI"); if (cmd_lists.empty()) return; OpenGLState::glEnable(GL_BLEND); OpenGLState::glBlendEquation(GL_FUNC_ADD); OpenGLState::glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); OpenGLState::glDisable(GL_CULL_FACE); OpenGLState::glDisable(GL_DEPTH_TEST); OpenGLState::glEnable(GL_SCISSOR_TEST); glActiveTexture(GL_TEXTURE0); // Setup orthographic projection matrix const float width = ImGui::GetIO().DisplaySize.x; const float height = ImGui::GetIO().DisplaySize.y; const float ortho_projection[4][4] = { { 2.0f / width, 0.0f, 0.0f, 0.0f }, { 0.0f, 2.0f / -height, 0.0f, 0.0f }, { 0.0f, 0.0f, -1.0f, 0.0f }, { -1.0f, 1.0f, 0.0f, 1.0f }, }; glUseProgram(shader_handle); glUniform1i(texture_location, 0); glUniformMatrix4fv(ortho_location, 1, GL_FALSE, &ortho_projection[0][0]); glBindVertexArray(vao_handle); for (int n = 0; n < cmd_lists.size(); n++) { const Age_ImDrawList& cmd_list = cmd_lists[n]; const ImDrawIdx* idx_buffer = cmd_list.idx_buffer.data(); glBindBuffer(GL_ARRAY_BUFFER, vbo_handle); size_t needed_vtx_size = cmd_list.vtx_buffer.size() * sizeof(ImDrawVert); if (vbo_size < needed_vtx_size) { // Grow our buffer if needed vbo_size = needed_vtx_size + 2000 * sizeof(ImDrawVert); glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)vbo_size, NULL, GL_STREAM_DRAW); } unsigned char* vtx_data = (unsigned char*)glMapBufferRange(GL_ARRAY_BUFFER, 0, needed_vtx_size, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); if (!vtx_data) continue; memcpy(vtx_data, &cmd_list.vtx_buffer[0], cmd_list.vtx_buffer.size() * sizeof(ImDrawVert)); glUnmapBuffer(GL_ARRAY_BUFFER); for (auto pcmd = cmd_list.commands.begin(); pcmd != cmd_list.commands.end(); pcmd++) { glBindTexture(GL_TEXTURE_2D, fontTex); glScissor((int)pcmd->ClipRect.x, (int)(height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y)); glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, GL_UNSIGNED_SHORT, idx_buffer); idx_buffer += pcmd->ElemCount; } } // Restore modified state glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glUseProgram(0); OpenGLState::glDisable(GL_SCISSOR_TEST); glBindTexture(GL_TEXTURE_2D, 0); }