Cogs.Core
GuiRenderTask.cpp
1#include "GuiRenderTask.h"
2
3#include "Rendering/IContext.h"
4#include "Rendering/ITextures.h"
5#include "Renderer/InspectorGui/InspectorGuiHelper.h"
6#include "Renderer/InspectorGui/InspectorGuiRenderer.h"
7
8#include "Context.h"
9
10#include "Renderer/Renderer.h"
11#include "Renderer/RenderTexture.h"
12#include "Renderer/RenderTarget.h"
13
14#include "GuiSystem.h"
15#include "GuiRenderComponent.h"
16
17#include <glm/gtc/type_ptr.hpp>
18
19void Cogs::Core::GuiRenderTask::apply(RenderTaskContext * renderContext)
20{
21 RenderInstrumentationScope(renderContext->device->getImmediateContext(), SCOPE_RENDERING, "GuiRenderTask::apply");
22
23 auto renderComponent = guiRenderComponent.resolveComponent<GuiRenderComponent>();
24
25 if (!renderComponent) {
26 return;
27 }
28
29 auto guiComponent = renderComponent->guiComponent.resolveComponent<GuiComponent>();
30
31 if (!guiComponent || !guiComponent->document) {
32 return;
33 }
34
35 auto * context = renderContext->context;
36 auto * guiSystem = static_cast<GuiSystem*>(context->getExtensionSystem(GuiSystem::getTypeId()));
37 assert(guiSystem);
38 auto & guiData = guiSystem->getData(guiComponent);
39
40 if (!renderComponent->isVisible()) return;
41 if (renderComponent->target && !renderTexture) return;
42 if (!guiData.htmlDocument) return;
43 if (!guiData.invalidated) return;
44
45 glm::vec2 guiSize = guiData.size;
46
47 if (renderTexture) {
48 renderContext->renderer->getInspectorGuiRenderer()->beginRender(renderContext->context, renderContext->renderer);
49 }
50
51 ImGui::GetIO().DisplaySize = ImVec2(guiSize.x, guiSize.y);
52
53 const ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar |
54 ImGuiWindowFlags_NoResize |
55 ImGuiWindowFlags_NoScrollbar |
56 ImGuiWindowFlags_NoMove |
57 ImGuiWindowFlags_NoSavedSettings |
58 ImGuiWindowFlags_NoInputs |
59 ImGuiWindowFlags_NoFocusOnAppearing;
60
61 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0, 0 });
62 ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
63 ImGui::SetNextWindowSize(ImVec2(guiSize.x, guiSize.y));
64 ImGui::SetNextWindowBgAlpha(0.0f);
65 ImGui::Begin("Gui", nullptr, flags);
66
67 guiData.htmlDocument->render((int)guiSize.x, litehtml::render_all);
68
69 litehtml::position clip(0, 0, (int)guiSize.x, (int)guiSize.y);
70
71 guiData.htmlDocument->draw((litehtml::uint_ptr)0x1234, 0, 0, &clip);
72
73 ImGui::End();
74 ImGui::PopStyleVar();
75
76 if (renderTexture) {
77 auto deviceContext = renderContext->device->getImmediateContext();
78
79 deviceContext->setRenderTarget(renderTexture->renderTarget->renderTargetHandle, renderTexture->renderTarget->depthTargetHandle);
80 deviceContext->clearRenderTarget(glm::value_ptr(renderComponent->clearColor));
81
82 renderContext->renderer->getInspectorGuiRenderer()->endRender(renderContext->context, renderContext->renderer);
83
84 deviceContext->setRenderTarget(renderContext->defaultRenderTarget->renderTargetHandle, renderContext->defaultRenderTarget->depthTargetHandle);
85
86 if ((renderTexture->description.flags & TextureFlags::GenerateMipMaps) != 0) {
87 renderContext->device->getTextures()->generateMipmaps(renderTexture->textureHandle);
88 }
89 }
90
91 guiData.invalidated = false;
92}
static Reflection::TypeId getTypeId()
Get the type id of the component type used by the system.
ComponentType * resolveComponent() const
Definition: Component.h:90
@ GenerateMipMaps
The texture supports automatic mipmap generation performed by the graphics device.
Definition: Flags.h:124