1#include "GuiExtension.h"
4#include "ExtensionRegistry.h"
5#include "Serialization/EntityReader.h"
6#include "Resources/ResourceStore.h"
7#include "Renderer/IRenderer.h"
8#include "Renderer/Tasks/RenderTaskFactory.h"
9#include "Renderer/RenderStateUpdater.h"
10#include "Renderer/InspectorGui/InspectorGuiRenderer.h"
11#include "Renderer/RenderTarget.h"
13#include "GuiComponent.h"
14#include "GuiRenderComponent.h"
15#include "GuiManager.h"
18#include "GuiRenderSystem.h"
19#include "GuiRenderTask.h"
21#include "Rendering/IGraphicsDevice.h"
23#include "Foundation/HashSequence.h"
24#include "Foundation/Logging/Logger.h"
27 #define GUI_EXT_API __declspec(dllexport)
55 void setupPipelines(
const DrawContext * drawContext)
57 Context * context = drawContext->context;
58 Renderer * renderer = drawContext->renderer;
64 for (
auto & gui : guiRenderSystem->
pool) {
66 auto it = pipelineManager->instanceByKey(context,
Cogs::hashSequence(
"Gui",
reinterpret_cast<intptr_t
>(&gui), gui.getGeneration()));
67 auto * instance = it.first;
70 instance->name =
"GuiExtension";
71 instance->priority = 300;
72 instance->sanityCheck = &gui;
73 instance->options.push_back({
"Pass",
"Offscreen" });
74 LOG_DEBUG(logger,
"GuiRenderer added camera pipeline");
77 assert(instance->sanityCheck == &gui);
80 RenderTexture * renderTexture = resources.getRenderTexture(gui.target);
84 if (!renderTexture->renderTarget) {
85 RenderTexture * depthTexture = resources.createRenderTexture();
87 depthTexture->description.format = Cogs::TextureFormat::R32_TYPELESS;
88 depthTexture->sizeSource = renderTexture;
89 renderTexture->depthTexture = depthTexture;
91 RenderTarget * renderTarget = resources.createRenderTarget();
92 renderTarget->textures.push_back(renderTexture);
93 renderTarget->depth = depthTexture;
94 renderTarget->width = renderTexture->description.width;
95 renderTarget->height = renderTexture->description.height;
96 renderTexture->renderTarget = renderTarget;
100 reinterpret_cast<intptr_t
>(renderTexture->renderTarget),
101 reinterpret_cast<intptr_t
>(renderTexture->depthTexture));
104 if (it.second || instance->dataHash != dataHash) {
105 instance->dataHash = dataHash;
108 def.type =
"GuiRender";
110 PipelineOptions options;
111 std::list<std::unique_ptr<SubContext>> subs;
112 auto guiTask = (
GuiRenderTask *)createRenderTask(taskContext, def, subs, pipelineManager->getExpressionContextRoot(), options);
113 instance->pipeline.tasks.push_back(guiTask);
116 instance->pipeline.updateResources.add(renderTexture->depthTexture);
117 instance->pipeline.updateResources.add(renderTexture->renderTarget);
121 guiTask->renderTexture = renderTexture;
122 instance->dataHash = dataHash;
128 void renderGuiInstances(
const DrawContext * drawContext)
130 Context * context = drawContext->context;
132 Renderer * renderer = drawContext->renderer;
135 for (
auto & gui : guiRenderSystem->
pool) {
138 inspectorGuiRenderer->beginRender(context, renderer);
141 guiRenderTask.apply(drawContext->taskContext);
143 inspectorGuiRenderer->endRender(context, renderer);
157 setupPipelines(drawContext);
160 renderGuiInstances(drawContext);
180 static constexpr const char* cExtensionKey =
"Gui";
183 reinterpret_cast<LoadHTMLFn
>(&GuiExtension::loadHTML),
184 reinterpret_cast<SetComponentHTMLFn
>(&GuiExtension::setComponentHTML),
185 reinterpret_cast<SetInnerHTMLFn
>(&GuiExtension::setInnerHTML),
195 GuiComponent::registerType();
196 GuiRenderComponent::registerType();
202 context->
engine->addResourceManager(std::make_unique<GuiManager>(context));
204 addTaskType(
"GuiRender", createGuiRenderTask, destroyGuiRenderTask);
218 static ResourceId loadHTML(
Context* context,
const char* path) {
219 GuiManager* manager =
static_cast<GuiManager*
>(context->
engine->getResourceManagerByValueType(
static_cast<int>(DefaultValueType::Gui)));
226 if (document->
getId() == NoResourceId) {
229 return document->
getId();
235 GuiManager* manager =
static_cast<GuiManager*
>(context->
engine->getResourceManagerByValueType(
static_cast<int>(DefaultValueType::Gui)));
238 component->document = manager->
getHandle(
id);
242 static void setInnerHTML(
Context* context,
GuiComponent* component,
const char* elementId,
const char* html) {
244 GuiData& data = guiSystem->getData(component);
246 if (data.htmlDocument) {
247 std::shared_ptr<litehtml::element> element = findElement(data.htmlDocument->root(), elementId);
250 element->set_inner_html(html);
251 data.invalidated =
true;
256 } guiExtensionInstance;
261 return &Cogs::Core::guiExtensionInstance;
static Reflection::TypeId getTypeId()
Get the type id of the component type used by the system.
ComponentPool< ComponentType > pool
Pool of components managed by the system.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class ComponentSystemBase * getExtensionSystem(const uint16_t id)
Retrieve the system with the given id.
class IRenderer * renderer
Renderer.
std::unique_ptr< class Engine > engine
Engine instance.
static void add(Extension *extension, StringView version)
Adds the given extension to the registry, ensuring the initialization methods are called at appropria...
Defines an extension to the renderer, capable of doing custom rendering.
virtual void handleEvent(uint32_t eventId, const DrawContext *renderingContext)=0
Called when rendering events occur.
virtual void initialize(class Context *context, IGraphicsDevice *device)=0
Initialize the extension using the given context and device.
virtual void registerExtension(IRendererExtension *extension)=0
Register an extension with the renderer.
Contains render resources used by the renderer.
void setResourceId(ResourceBase *resource, ResourceId id) override
Assign the given id to a previously created resource.
ResourceId getNextResourceId() override
Get the next unique resource id.
void processLoading() final
Process loading resources.
ResourceHandle getHandle(const ResourceId id) const
Get a resource handle to the resource with the given id.
Represents a graphics device used to manage graphics resources and issue drawing commands.
Log implementation class.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
constexpr Log getLogger(const char(&name)[LEN]) noexcept
constexpr size_t hashSequence(const T &t, const U &u)
Hash the last two items in a sequence of objects.
Defines an extension to Cogs.Core and provides methods to override in order to initialize extension c...
virtual const void * getPublicAPI() const override
Retrieve a pointer to a struct containing all publicly available function pointers.
virtual const char * getExtensionKey() const override
Get the extensions unique key, used to check for extension presence and retrieve extension specific d...
virtual bool initialize(Context *context) override
Initialize extension for the given context.
virtual bool initializeStatic() override
Initialize extension statically.
@ RenderGui
Rendering GUI elements.
@ PreRender
Pre rendering happening for a given rendering context.
ResourceId getId() const
Get the resource id of this instance.
ResourceType * resolve() const
Resolve the handle, returning a pointer to the actual resource.
@ PreTransform
Run before transformations are updated.
@ DepthBuffer
The texture can be used as a depth target and have depth buffer values written into.