Skip to content

Commit

Permalink
Batchgroup from material templates (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duttenheim authored Aug 16, 2024
1 parent 577ce74 commit c8de306
Show file tree
Hide file tree
Showing 125 changed files with 5,097 additions and 2,423 deletions.
42 changes: 17 additions & 25 deletions code/addons/dynui/im3d/im3dcontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "frame/framecode.h"
#include "imgui.h"

#include "frame/default.h"

using namespace Math;
using namespace CoreGraphics;
using namespace Graphics;
Expand Down Expand Up @@ -106,8 +108,6 @@ struct Im3dState
Ptr<Im3dInputHandler> inputHandler;
Im3d::Id depthLayerId;
byte* vertexPtr;

Memory::ArenaAllocator<sizeof(Frame::FrameCode)> frameOpAllocator;
};
static Im3dState imState;

Expand Down Expand Up @@ -167,35 +167,34 @@ Im3dContext::Create()
// map buffer
imState.vertexPtr = (byte*)CoreGraphics::BufferMap(imState.vbo);

Frame::FrameCode* op = imState.frameOpAllocator.Alloc<Frame::FrameCode>();
op->domain = CoreGraphics::BarrierDomain::Pass;
op->func = [](const CoreGraphics::CmdBufferId cmdBuf, const IndexT frame, const IndexT bufferIndex)
FrameScript_default::RegisterSubgraph_Im3D_Pass([](const CoreGraphics::CmdBufferId cmdBuf, const Math::rectangle<int>& viewport, const IndexT frame, const IndexT bufferIndex)
{
Render(cmdBuf, frame);
};
op->buildFunc = [](const CoreGraphics::PassId pass, const uint subpass)
});
FrameScript_default::RegisterSubgraphPipelines_Im3D_Pass([](const CoreGraphics::PassId pass, const uint subpass)
{
if (imState.linesPipeline != CoreGraphics::InvalidPipelineId)
CoreGraphics::DestroyGraphicsPipeline(imState.linesPipeline);
imState.linesPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.lines, pass, subpass, CoreGraphics::InputAssemblyKey{CoreGraphics::PrimitiveTopology::LineList, false} });
imState.linesPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.lines, pass, subpass, CoreGraphics::InputAssemblyKey{ CoreGraphics::PrimitiveTopology::LineList, false } });

if (imState.depthLinesPipeline != CoreGraphics::InvalidPipelineId)
CoreGraphics::DestroyGraphicsPipeline(imState.depthLinesPipeline);
imState.depthLinesPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.depthLines, pass, subpass, CoreGraphics::InputAssemblyKey{CoreGraphics::PrimitiveTopology::LineList, false} });
imState.depthLinesPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.depthLines, pass, subpass, CoreGraphics::InputAssemblyKey{ CoreGraphics::PrimitiveTopology::LineList, false } });

if (imState.trianglesPipeline != CoreGraphics::InvalidPipelineId)
CoreGraphics::DestroyGraphicsPipeline(imState.trianglesPipeline);
imState.trianglesPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.triangles, pass, subpass, CoreGraphics::InputAssemblyKey{CoreGraphics::PrimitiveTopology::TriangleList, false} });
imState.trianglesPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.triangles, pass, subpass, CoreGraphics::InputAssemblyKey{ CoreGraphics::PrimitiveTopology::TriangleList, false } });

if (imState.depthTrianglesPipeline != CoreGraphics::InvalidPipelineId)
CoreGraphics::DestroyGraphicsPipeline(imState.depthTrianglesPipeline);
imState.depthTrianglesPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.depthTriangles, pass, subpass, CoreGraphics::InputAssemblyKey{CoreGraphics::PrimitiveTopology::TriangleList, false} });
imState.depthTrianglesPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.depthTriangles, pass, subpass, CoreGraphics::InputAssemblyKey{ CoreGraphics::PrimitiveTopology::TriangleList, false } });

if (imState.pointsPipeline != CoreGraphics::InvalidPipelineId)
CoreGraphics::DestroyGraphicsPipeline(imState.pointsPipeline);
imState.pointsPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.points, pass, subpass, CoreGraphics::InputAssemblyKey{CoreGraphics::PrimitiveTopology::PointList, false} });
};
Frame::AddSubgraph("Im3D", { op });
imState.pointsPipeline = CoreGraphics::CreateGraphicsPipeline({ imState.points, pass, subpass, CoreGraphics::InputAssemblyKey{ CoreGraphics::PrimitiveTopology::PointList, false } });
});
Im3d::NewFrame();

}

//------------------------------------------------------------------------------
Expand All @@ -204,7 +203,6 @@ Im3dContext::Create()
void
Im3dContext::Discard()
{
imState.frameOpAllocator.Release();
Input::InputServer::Instance()->RemoveInputHandler(imState.inputHandler.upcast<InputHandler>());
imState.inputHandler = nullptr;
CoreGraphics::BufferUnmap(imState.vbo);
Expand Down Expand Up @@ -368,14 +366,6 @@ Im3dContext::DrawSphere(const Math::point& pos, float radius, const Math::vec4&
Im3d::PopDrawState();
}

//------------------------------------------------------------------------------
/**
*/
void
Im3dContext::NewFrame(const Graphics::FrameContext& ctx)
{
Im3d::NewFrame();
}

//------------------------------------------------------------------------------
/**
Expand All @@ -385,10 +375,11 @@ Im3dContext::OnPrepareView(const Ptr<Graphics::View>& view, const Graphics::Fram
{
AppData& ad = GetAppData();

const Math::rectangle<int>& viewport = view->GetViewport();

ad.m_deltaTime = ctx.frameTime;
SetGizmoSize(128, 4);
auto const & mode = CoreGraphics::WindowGetDisplayMode(CurrentWindow);
ad.m_viewportSize = Vec2((float)mode.GetWidth(), (float)mode.GetHeight());
ad.m_viewportSize = Vec2((float)viewport.width(), (float)viewport.height());

Graphics::GraphicsEntityId cam = view->GetCamera();
Math::mat4 transform = inverse(CameraContext::GetView(cam));
Expand Down Expand Up @@ -535,6 +526,7 @@ Im3dContext::Render(const CoreGraphics::CmdBufferId cmdBuf, const IndexT frameIn
[](Im3d::DrawList const& l) { return l.m_primType == Im3d::DrawPrimitive_Triangles && l.m_layerId == imState.depthLayerId; });

CoreGraphics::BufferFlush(imState.vbo);
Im3d::NewFrame();
}

//------------------------------------------------------------------------------
Expand Down
3 changes: 0 additions & 3 deletions code/addons/dynui/im3d/im3dcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ class Im3dContext : public Graphics::GraphicsContext
/// draw a cone
static void DrawCone(const Math::mat4& modelTransform, const Math::vec4& color, uint32_t renderFlags = CheckDepth | Wireframe);

/// Start a new Im3d frame
static void NewFrame(const Graphics::FrameContext& ctx);

/// called before frame
static void OnPrepareView(const Ptr<Graphics::View>& view, const Graphics::FrameContext& ctx);
/// called when rendering a frame batch
Expand Down
6 changes: 3 additions & 3 deletions code/addons/dynui/im3d/shaders/im3d.fx
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ gsMain(
vec2 pos1 = gl_in[1].gl_Position.xy / gl_in[1].gl_Position.w;

vec2 dir = pos0 - pos1;
dir = normalize(vec2(dir.x, dir.y * RenderTargetDimensions[0].y * RenderTargetDimensions[0].z)); // correct for aspect ratio
dir = normalize(vec2(dir.x, dir.y * RenderTargetParameter[0].Dimensions.y * RenderTargetParameter[0].Dimensions.z)); // correct for aspect ratio
vec2 tng0 = vec2(-dir.y, dir.x);
vec2 tng1 = tng0 * size[1] * RenderTargetDimensions[0].zw;
tng0 = tng0 * size[0] * RenderTargetDimensions[0].zw;
vec2 tng1 = tng0 * size[1] * RenderTargetParameter[0].Dimensions.zw;
tng0 = tng0 * size[0] * RenderTargetParameter[0].Dimensions.zw;

// line start
gl_Position = vec4((pos0 - tng0) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw);
Expand Down
Loading

0 comments on commit c8de306

Please sign in to comment.