Restructure agent setup
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
---
|
||||
name: prism-dag
|
||||
description: DAG / graph patterns for Prism — adjacency list rules, arena allocation, Kahn's algorithm, flat buffer layout
|
||||
license: MIT
|
||||
compatibility: opencode
|
||||
metadata:
|
||||
domain: core
|
||||
---
|
||||
## What I do
|
||||
|
||||
Captures the conventions for Prism's directed acyclic graph (DAG) implementation: how edges are stored, how the graph is validated, and how to avoid common pitfalls.
|
||||
|
||||
## When to use me
|
||||
|
||||
Use this when working on graph/DAG structures (`pr_graph.h`, `pr_graph.c`, or `scratchpad/dag.c`), adding new node types, or modifying the topological sort / cycle detection logic.
|
||||
|
||||
## Conventions
|
||||
|
||||
### Adjacency lists — separate edge nodes
|
||||
|
||||
Adjacency list nodes must be **separately allocated from the vertex array**. Never use the vertex struct itself as a linked-list node in another vertex's adjacency chain — that shares the `next` pointer between two roles and corrupts the graph.
|
||||
|
||||
```c
|
||||
// correct — per-edge copy on the arena
|
||||
static void addEdge(PrGraph *g, const WpAllocator *alloc, u64 from, u64 to) {
|
||||
PrVertex *src = &g->vertices[from];
|
||||
PrVertex *dst = wpMemAllocatorAlloc(alloc, sizeof(PrVertex));
|
||||
if (!dst) { /* handle OOM */ return; }
|
||||
dst->id = g->vertices[to].id;
|
||||
dst->value = g->vertices[to].value;
|
||||
dst->next = src->next;
|
||||
src->next = dst;
|
||||
}
|
||||
|
||||
// wrong — reuses the destination vertex as the list node
|
||||
static void addEdge_bad(PrGraph *g, u64 from, u64 to) {
|
||||
PrVertex *src = &g->vertices[from];
|
||||
PrVertex *dest = &g->vertices[to];
|
||||
dest->next = src->next;
|
||||
src->next = dest; // overwrites dest->next used elsewhere
|
||||
}
|
||||
```
|
||||
|
||||
Arena bump allocation (`wpMemAllocatorAlloc`) is the natural fit for edge nodes. Always NULL-check the result — arena allocators can fail if the backing buffer is exhausted.
|
||||
|
||||
### Flat buffer layout
|
||||
|
||||
For hot-path graph evaluation, prefer SoA layouts and keep the DAG in contiguous arrays rather than individually allocated linked structures.
|
||||
|
||||
### Cycle detection
|
||||
|
||||
Use Kahn's algorithm integrated into `prGraphAddEdge` for cycle detection with rollback. When an edge would create a cycle, the edge is not added and the function returns an error.
|
||||
|
||||
### Memory management
|
||||
|
||||
Stack-allocate where possible; pass `WpAllocator *` explicitly for heap allocations.
|
||||
@@ -0,0 +1,74 @@
|
||||
---
|
||||
name: prism-rhi
|
||||
description: RHI (Rendering Hardware Interface) patterns — compile-time dispatch, by-value desc structs, wapp array aliases, backend file layout
|
||||
license: MIT
|
||||
compatibility: opencode
|
||||
metadata:
|
||||
domain: rendering
|
||||
---
|
||||
## What I do
|
||||
|
||||
Captures the RHI conventions for Prism: how backends are dispatched, how descriptor structs and arrays are handled, and how the files are organized.
|
||||
|
||||
## When to use me
|
||||
|
||||
Use this when working on any file in `scratchpad/rhi/` or `src/prism/rhi/`, or when creating a new backend (Vulkan, D3D12, Metal).
|
||||
|
||||
## Conventions
|
||||
|
||||
### Backend dispatch
|
||||
|
||||
Backend selection is compile-time via `-D PR_RHI_VULKAN` / `-D PR_RHI_D3D12` / `-D PR_RHI_METAL`. The umbrella header `pr_rhi.h` includes the appropriate alias file:
|
||||
|
||||
```c
|
||||
#if defined(PR_RHI_VULKAN)
|
||||
# include "vulkan/pr_rhi_vk_aliases.h"
|
||||
#elif defined(PR_RHI_D3D12)
|
||||
# include "d3d12/pr_rhi_d3d12_aliases.h"
|
||||
#elif defined(PR_RHI_METAL)
|
||||
# include "metal/pr_rhi_metal_aliases.h"
|
||||
#else
|
||||
# error "Define one of: PR_RHI_VULKAN, PR_RHI_D3D12, PR_RHI_METAL"
|
||||
#endif
|
||||
```
|
||||
|
||||
Each `_aliases.h` file maps generic names to backend-specific names:
|
||||
|
||||
```c
|
||||
#define prRhiCreateDevice prRhiCreateDeviceVk
|
||||
#define prRhiCreateSwapchain prRhiCreateSwapchainVk
|
||||
#define prRhiCreateBuffer prRhiCreateBufferVk
|
||||
// …
|
||||
```
|
||||
|
||||
Backend implementations are suffixed with the backend name: `pr_rhi_vk_device.c`, `pr_rhi_vk_swapchain.c`, etc.
|
||||
|
||||
### Desc structs
|
||||
|
||||
All descriptor structs are passed **by value**, not `const *`:
|
||||
|
||||
```c
|
||||
// correct
|
||||
PrRhiDevice *prRhiCreateDevice(PrRhiPhysicalDevice *pdev, PrRhiSurface *surface,
|
||||
PrRhiDeviceDesc desc, WpAllocator *alloc);
|
||||
|
||||
// wrong
|
||||
PrRhiDevice *prRhiCreateDevice(PrRhiPhysicalDevice *pdev, PrRhiSurface *surface,
|
||||
const PrRhiDeviceDesc *desc, WpAllocator *alloc);
|
||||
```
|
||||
|
||||
### File layout
|
||||
|
||||
```
|
||||
scratchpad/rhi/
|
||||
├── pr_rhi.h ← umbrella header (API declarations + backend dispatch)
|
||||
├── pr_rhi_types.h ← shared types (enums, element types, array aliases, desc structs, opaque handles)
|
||||
├── vulkan/
|
||||
│ ├── pr_rhi_vk.h ← Vulkan backend header (opaque struct defs + Vk-suffixed decls)
|
||||
│ └── pr_rhi_vk_aliases.h ← #define alias mapping
|
||||
├── d3d12/
|
||||
│ └── …
|
||||
└── metal/
|
||||
└── …
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user