- 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
220 lines
8.8 KiB
HTML
220 lines
8.8 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Entity Query Usage | GhostEngine </title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="title" content="Entity Query Usage | 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="entity-query-usage">Entity Query Usage</h1>
|
|
|
|
<p>This page describes practical <code>EntityQuery</code> usage patterns from the runtime ECS API.
|
|
For full API signatures, see the generated API pages under <code>doc/api/</code>.</p>
|
|
<h2 id="overview">Overview</h2>
|
|
<p><code>EntityQuery</code> is the main data access surface for ECS iteration in <code>Ghost.Entities</code>.</p>
|
|
<ul>
|
|
<li>Build a query once using <code>QueryBuilder</code> and reuse the query handle (<code>Identifier<EntityQuery></code>) across frames.</li>
|
|
<li>Access query data in three common ways:
|
|
<ul>
|
|
<li><code>ForEach</code> for simple lambda-style iteration.</li>
|
|
<li><code>GetEntityComponentIterator<T...>()</code> when you need entity IDs with component refs.</li>
|
|
<li><code>GetChunkIterator()</code> for cache-friendly chunk-level traversal.</li>
|
|
</ul>
|
|
</li>
|
|
<li>Schedule jobs (<code>IJobChunk</code> / <code>IJobEntity<...></code>) for parallel work.</li>
|
|
</ul>
|
|
<h2 id="minimal-setup">Minimal Setup</h2>
|
|
<pre><code class="lang-csharp">using Misaki.HighPerformance.Jobs;
|
|
using Misaki.HighPerformance.LowLevel.Buffer;
|
|
|
|
var scheduler = new JobScheduler(4);
|
|
var world = World.Create(scheduler);
|
|
|
|
using var scope = AllocationManager.CreateStackScope();
|
|
using var set = new ComponentSet(scope.AllocationHandle, ComponentTypeID<Transform>.Value);
|
|
|
|
Span<Entity> entities = stackalloc Entity[1000];
|
|
world.EntityManager.CreateEntities(entities, set);
|
|
|
|
var queryID = new QueryBuilder().WithAllRW<Transform>().Build(world);
|
|
ref var query = ref world.ComponentManager.GetEntityQueryReference(queryID);
|
|
</code></pre>
|
|
<h2 id="pattern-1-foreach-for-simple-iteration">Pattern 1: <code>ForEach</code> for Simple Iteration</h2>
|
|
<p>Use <code>ForEach<T...></code> when you want concise in-place updates or debug output.</p>
|
|
<pre><code class="lang-csharp">query.ForEach<Transform>((entity, ref Transform transform) =>
|
|
{
|
|
transform.position += new float3(1, 0, 0);
|
|
});
|
|
</code></pre>
|
|
<p>Why this pattern:</p>
|
|
<ul>
|
|
<li>Fast to write and easy to read.</li>
|
|
<li>Good for gameplay logic and quick data transforms.</li>
|
|
</ul>
|
|
<h2 id="pattern-2-entity--component-iterator">Pattern 2: Entity + Component Iterator</h2>
|
|
<p>Use <code>GetEntityComponentIterator<T...>()</code> when you need both identity and mutable component data.</p>
|
|
<pre><code class="lang-csharp">foreach (var (entity, transform) in query.GetEntityComponentIterator<Transform>())
|
|
{
|
|
transform.Get().position += new float3(0, 1, 0);
|
|
}
|
|
</code></pre>
|
|
<p>Why this pattern:</p>
|
|
<ul>
|
|
<li>Explicit access to the entity in each iteration.</li>
|
|
<li>Works well when you need side lookups keyed by entity.</li>
|
|
</ul>
|
|
<h2 id="pattern-3-chunk-iterator-for-throughput">Pattern 3: Chunk Iterator for Throughput</h2>
|
|
<p>Use <code>GetChunkIterator()</code> for the most cache-friendly, low-overhead traversal.</p>
|
|
<pre><code class="lang-csharp">foreach (var chunk in query.GetChunkIterator())
|
|
{
|
|
var transforms = chunk.GetComponentDataRW<Transform>();
|
|
var entitiesInChunk = chunk.GetEntities();
|
|
|
|
for (var i = 0; i < chunk.EntityCount; i++)
|
|
{
|
|
transforms[i].position += new float3(0, 0, 1);
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>Why this pattern:</p>
|
|
<ul>
|
|
<li>Minimizes per-entity overhead.</li>
|
|
<li>Best fit for heavy simulation and broad data transforms.</li>
|
|
</ul>
|
|
<h2 id="parallel-job-scheduling">Parallel Job Scheduling</h2>
|
|
<p><code>EntityQuery</code> supports chunk and entity parallel scheduling.</p>
|
|
<p><code>IJobChunk</code> example:</p>
|
|
<pre><code class="lang-csharp">internal struct MoveChunkJob : IJobChunk
|
|
{
|
|
public void Execute(ChunkView view, ref readonly JobExecutionContext ctx)
|
|
{
|
|
var transforms = view.GetComponentDataRW<Transform>();
|
|
for (var i = 0; i < view.EntityCount; i++)
|
|
{
|
|
transforms[i].position += new float3(5, 5, 5);
|
|
}
|
|
}
|
|
}
|
|
|
|
var handle = query.ScheduleChunkParallel(new MoveChunkJob(), batchSize: 1, JobHandle.Invalid);
|
|
scheduler.Wait(handle);
|
|
</code></pre>
|
|
<p>Notes:</p>
|
|
<ul>
|
|
<li>Ensure all dependencies are represented in the input <code>JobHandle</code>.</li>
|
|
<li>Wait or chain handles before reading modified data on the main thread.</li>
|
|
<li>Prefer chunk jobs for contiguous memory access patterns.</li>
|
|
</ul>
|
|
<h2 id="lifecycle-and-cleanup">Lifecycle and Cleanup</h2>
|
|
<pre><code class="lang-csharp">world.EntityManager.DestroyEntities(entities);
|
|
world.Dispose();
|
|
scheduler.Dispose();
|
|
JobScheduler.ReleaseTempAllocator();
|
|
</code></pre>
|
|
<p>If your test or app creates worlds/schedulers manually, always clean them up in deterministic order.</p>
|
|
|
|
</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>
|