- Refactor D3D12 backend and RenderGraph module - Update graphics RHI and core rendering components - Add Random.hlsl shader include - Regenerate API documentation and update user guides
182 lines
7.9 KiB
HTML
182 lines
7.9 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>ECS Workflows | GhostEngine </title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="title" content="ECS Workflows | GhostEngine ">
|
|
|
|
|
|
<link rel="icon" href="../favicon.ico">
|
|
<link rel="stylesheet" href="../public/docfx.min.css">
|
|
<link rel="stylesheet" href="../public/main.css">
|
|
<meta name="docfx:navrel" content="../toc.html">
|
|
<meta name="docfx:tocrel" content="toc.html">
|
|
|
|
<meta name="docfx:rel" content="../">
|
|
|
|
|
|
|
|
<meta name="loc:inThisArticle" content="In this article">
|
|
<meta name="loc:searchResultsCount" content="{count} results for "{query}"">
|
|
<meta name="loc:searchNoResults" content="No results for "{query}"">
|
|
<meta name="loc:tocFilter" content="Filter by title">
|
|
<meta name="loc:nextArticle" content="Next">
|
|
<meta name="loc:prevArticle" content="Previous">
|
|
<meta name="loc:themeLight" content="Light">
|
|
<meta name="loc:themeDark" content="Dark">
|
|
<meta name="loc:themeAuto" content="Auto">
|
|
<meta name="loc:changeTheme" content="Change theme">
|
|
<meta name="loc:copy" content="Copy">
|
|
<meta name="loc:downloadPdf" content="Download PDF">
|
|
|
|
<script type="module" src="./../public/docfx.min.js"></script>
|
|
|
|
<script>
|
|
const theme = localStorage.getItem('theme') || 'auto'
|
|
document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body class="tex2jax_ignore" data-layout="" data-yaml-mime="">
|
|
<header class="bg-body border-bottom">
|
|
<nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
|
|
<div class="container-xxl flex-nowrap">
|
|
<a class="navbar-brand" href="../index.html">
|
|
<img id="logo" class="svg" src="../logo.svg" alt="GhostEngine">
|
|
GhostEngine
|
|
</a>
|
|
<button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
|
|
<i class="bi bi-three-dots"></i>
|
|
</button>
|
|
<div class="collapse navbar-collapse" id="navpanel">
|
|
<div id="navbar">
|
|
<form class="search" role="search" id="search">
|
|
<i class="bi bi-search"></i>
|
|
<input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search">
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
</header>
|
|
|
|
<main class="container-xxl">
|
|
<div class="toc-offcanvas">
|
|
<div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
|
|
<div class="offcanvas-header">
|
|
<h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
|
|
</div>
|
|
<div class="offcanvas-body">
|
|
<nav class="toc" id="toc"></nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="content">
|
|
<div class="actionbar">
|
|
<button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
|
|
<i class="bi bi-list"></i>
|
|
</button>
|
|
|
|
<nav id="breadcrumb"></nav>
|
|
</div>
|
|
|
|
<article data-uid="">
|
|
<h1 id="ecs-workflows">ECS Workflows</h1>
|
|
|
|
<p>This document outlines standard daily workflows for working within GhostEngine's ECS.</p>
|
|
<h2 id="creating-entity-queries">Creating Entity Queries</h2>
|
|
<p>To read or modify data, you first build an <code>EntityQuery</code>. You use the <code>QueryBuilder</code> struct to define inclusion and exclusion constraints.</p>
|
|
<pre><code class="lang-csharp">Identifier<EntityQuery> queryId = QueryBuilder.Create()
|
|
.WithAll<Position>() // Must have Position
|
|
.WithAny<Player, Enemy>() // Must have Player OR Enemy
|
|
.WithNone<Dead>() // Must NOT have Dead structurally
|
|
.WithDisabled<Renderable>() // Must have Renderable structurally, but it must be disabled
|
|
.Build(world);
|
|
</code></pre>
|
|
<p>Queries are hashed and cached internally by the <code>ComponentManager</code>. Building the same query mask twice will yield the same <code>Identifier<EntityQuery></code>.</p>
|
|
<h2 id="iterating-chunk-views">Iterating Chunk Views</h2>
|
|
<p>Once you have a query, use <code>GetChunkIterator()</code> to traverse the unmanaged memory directly. This returns a <code>ChunkView</code> struct.</p>
|
|
<p>Because the data is unmanaged, you can extract <code>Span<T></code> and modify it with extreme speed.</p>
|
|
<pre><code class="lang-csharp">ref var query = ref world.ComponentManager.GetEntityQueryReference(queryId);
|
|
|
|
foreach (var chunkView in query.GetChunkIterator())
|
|
{
|
|
// Get Read-Only Span
|
|
ReadOnlySpan<Velocity> vels = chunkView.GetComponentData<Velocity>();
|
|
|
|
// Get Read-Write Span
|
|
Span<Position> pos = chunkView.GetComponentDataRW<Position>();
|
|
|
|
// For loop for cache-aligned processing
|
|
for (int i = 0; i < chunkView.EntityCount; i++)
|
|
{
|
|
pos[i].Value += vels[i].Value * dt;
|
|
}
|
|
}
|
|
</code></pre>
|
|
<blockquote>
|
|
<p><strong>Note:</strong> Accessing data using <code>GetComponentDataRW<T>()</code> will automatically bump the internal version numbers for that component type in that specific chunk, allowing other systems to detect changes efficiently.</p>
|
|
</blockquote>
|
|
<h2 id="entity-command-buffers-structural-changes">Entity Command Buffers (Structural Changes)</h2>
|
|
<p>In ECS, structural changes (adding/removing components, destroying entities) reorganize chunk memory. Doing this while iterating over chunks invalidates the memory layout.</p>
|
|
<p>To safely defer structural changes, use an <code>EntityCommandBuffer</code>.</p>
|
|
<pre><code class="lang-csharp">// Get the world's main ECB
|
|
var ecb = world.EntityCommandBuffer;
|
|
|
|
foreach (var chunk in query.GetChunkIterator())
|
|
{
|
|
var entities = chunk.GetEntities();
|
|
var healths = chunk.GetComponentData<Health>();
|
|
|
|
for (int i = 0; i < chunk.EntityCount; i++)
|
|
{
|
|
if (healths[i].Value <= 0)
|
|
{
|
|
// Defer the destruction until Playback() is called
|
|
ecb.DestroyEntity(entities[i]);
|
|
}
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>At the end of the frame (or the end of the system group update), you must call:</p>
|
|
<pre><code class="lang-csharp">world.PlaybackEntityCommandBuffers();
|
|
</code></pre>
|
|
<h3 id="multi-threading-ecbs">Multi-threading ECBs</h3>
|
|
<p>When using the <code>JobScheduler</code>, you can record deferred commands across multiple worker threads concurrently by requesting thread-local ECBs:</p>
|
|
<pre><code class="lang-csharp">int workerIndex = JobScheduler.CurrentThreadIndex;
|
|
var threadEcb = world.GetThreadLocalEntityCommandBuffer(workerIndex);
|
|
|
|
// Safely record structural changes from parallel jobs
|
|
threadEcb.AddComponent<Dead>(entity);
|
|
</code></pre>
|
|
|
|
</article>
|
|
|
|
<div class="contribution d-print-none">
|
|
</div>
|
|
|
|
<div class="next-article d-print-none border-top" id="nextArticle"></div>
|
|
|
|
</div>
|
|
|
|
<div class="affix">
|
|
<nav id="affix"></nav>
|
|
</div>
|
|
</main>
|
|
|
|
<div class="container-xxl search-results" id="search-results"></div>
|
|
|
|
<footer class="border-top text-secondary">
|
|
<div class="container-xxl">
|
|
<div class="flex-fill">
|
|
<span>Made with <a href="https://dotnet.github.io/docfx">docfx</a></span>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
</body>
|
|
</html>
|