Orklev2 fxpipeline

From tweakoz/orkid · View interactive version

Orklev2 fxpipeline is an AI agent skill from the GitHub repository tweakoz/orkid. Answer questions about orkid's FxPipeline system, FxPipelinePermutation, FxPipelineCache, shader technique selection, parameter binding, state lambdas, named parameter providers, RenderContextInstData/FrameData, and Python pipeline bindings. Use when the user asks about render pipelines, technique s

How to use this skill

Skill file

Orkid FxPipeline System Reference

When answering questions about FxPipeline, render context, or shader parameter binding in orkid, consult the files below. All under ork.lev2/.

Key Files

| Component | File |

|-----------|------|

| FxPipeline / Cache / Permutation | inc/ork/lev2/gfx/fx_pipeline.h |

| FxPipeline Implementation | src/gfx/fx_pipeline.cpp |

| Shader System | inc/ork/lev2/gfx/shadman.h |

| Render Context (RCID/RCFD) | inc/ork/lev2/gfx/renderer/rendercontext.h |

| GfxMaterial Base | inc/ork/lev2/gfx/gfxmaterial.h |

| PBR Pipeline Creation | src/gfx/material/material_pbr_pipeline.cpp |

| Python Material Bindings | pyext/src/pyext_gfx_material.cpp |

| Python Shader Bindings | pyext/src/pyext_gfx_shader.cpp |

| Type Definitions | inc/ork/lev2/lev2_types.h |

Architecture Overview

GfxMaterial
  └─ pipelineCache() → FxPipelineCache
       └─ findPipeline(permutation) → FxPipeline
            ├─ _technique → FxShaderTechnique
            ├─ _params → { FxShaderParam* : varval_t }
            ├─ _uniformbuffers → { FxUniformBlock* : varval_t }
            ├─ _storages → { FxShaderStorageBlock* : varval_t }
            └─ _statelambdas → [ (RCID) → void ]

Core Data Structures

FxPipelinePermutation (fx_pipeline.h:38–59)

Encodes rendering configuration as a 64-bit hash (genIndex(), fx_pipeline.cpp:21–39):

| Flag | Bit | Description |

|------|-----|-------------|

| _stereo | 1 | Single-pass stereo |

| _instanced | 2 | Instance rendering |

| _skinned | 3 | Skeletal animation |

| _is_picking | 4 | Picking/selection mode |

| _has_vtxcolors | 5 | Per-vertex colors |

| _is_alpha | 6 | Alpha-blend variant |

| _is_vertex_ssbo | 7 | SSBO-sourced vertices → FWD_SSBO_CUSTOM variant |

| _instanced_matrices_only | 8 | Instanced from a matrices-only dynamic block (no per-inst color) |

| _is_impostor | 9 | LOD impostor: billboard VS + atlas-sampling surface → FWD_SSBO_CUSTOM_IMPOSTOR |

| _is_mesh_shader | 10 | Geometry generated by a mesh stage → FWD_SSBO_CUSTOM_MESH variant |

| _is_sun_cookie | 11 | Cloud-shadow fill pass → alpha-only FWD_SUNCOOKIE variant |

| _rendering_model | 16+ | CRC hash (e.g., "FORWARD_PBR"_crcu) |

FxPipeline (fx_pipeline.h:91–143)

Active rendering state for a specific permutation:

Key methods:

FxPipelineCache (fx_pipeline.h:145–153)

Caches pipelines by permutation hash:

FxPipelineCacheImpl<MtlClass> (fx_pipeline.h:155–225)

Template for per-material-class caches. Sets _on_miss to call MtlClass::_createFxPipeline(permu, material).

Eviction is MANDATORY: the map is keyed by raw material pointer and the allocator reuses addresses — every material class that instantiates this template must call removeCache(material) from its destructor, or a successor material can inherit a cache whose pipelines hold raw backrefs to the DEAD material (use-after-free surfacing as a GPU page fault at the next submit). The evicted cache is RETURNED rather than dropped so the caller controls release timing (its pipelines can hold the last shared_ptr to GPU-owning binds — route through the owning Context's delayed-destroy queue). PBRMaterial does this via _evictPbrPipelineCache (material_pbr.inl:42).

Parameter Binding Types

varval_t (= varmap::VarMap::value_type) supports:

| Type | Examples |

|------|---------|

| Scalar | float, int, bool, uint32_t |

| Vector | fvec2, fvec3, fvec4 |

| Matrix | fmtx3, fmtx4 |

| Texture | texture_ptr_t, TextureArray* |

| Buffer | FxShaderStorageBuffer*, storagebufferptr_t |

| CrcString | Named parameter provider lookup |

| Generator | varval_generator_t — deferred evaluation lambda |

Named Parameter Providers (`FxPipelineNamedParamProviders` ctor, fx_pipeline.cpp:212–680)

Key design pattern: When a CrcString is bound as a parameter value (from C++ or Python), the runtime resolves it at render time via a singleton map of name → C++ provider function. This is a declarative binding — the wiring is set up once, then the C++ runtime evaluates the provider every frame with zero scripting overhead. Engine-internal data (camera matrices, PBR environment maps, time, etc.) flows to shader parameters without the caller needing to know how that data is computed. This pattern is used throughout the engine in both C++ material setup (e.g., PBRMaterial state lambdas) and Python scene composition.

Registered providers:

| Provider Name | Returns |

|---------------|---------|

| "RCID_PickID" | Pick variant encoding |

| "RCFD_M" | Model matrix |

| "RCFD_Camera_MVP_Mono" | Model-View-Projection |

| "RCFD_Camera_MV_Mono" | Model-View |

| "RCFD_Camera_V_Mono" | View matrix |

| "RCFD_Camera_P_Mono" | Projection matrix |

| "RCFD_TIME" | Current time |

| "CPD_Rtg_Dim" | Render target dimensions |

| "RCFD_MODCOLOR" | Modifier color |

| "RCFD_MONOCAM_NEAR_FAR" | Near/far planes |

| "RCFD_EYE_POSITION" | Camera eye position |

| "RCFD_PBR_BRDF_INTEGRATION_GGX" | BRDF LUT texture (from activeRadianceMaps(), pairing it with the env textures the sibling providers bind) |

| "RCFD_PBR_ENV_SH" / "RCFD_PBR_ENV_SH_VALID" | Diffuse ambient (nine L2 coefficients via CommonStuff::envSHCoeffs + its gate) |

| "RCFD_PBR_SPECULAR_ENV" | Specular environment array (envSpecularTexture()) |

| "RCFD_PBR_SPECULAR_ENV_PREV" | The OUTGOING IBL set during a procedural refilter crossfade (envSpecularTexturePrev(); aliases the bind above whenever no fade is running) |

| "RCFD_PBR_ENV_BLEND_WEIGHT" | The crossfade blend weight (envCrossfadeWeight(), 1.0 outside a fade). The ambient has no prev twin — its crossfade is resolved on the CPU inside envSHCoeffs |

(Plus more: FBI_RTG_DIM/INVDIM, RCFD_DEPTH_MAP, RCFD_PBR_BLACK/WHITE_* fallback textures, LMGR_ACTIVE_* light counts/cookies, inverse-matrix variants — grep _providers[ in fx_pipeline.cpp for the full set.)

Stereo variants append "_Left" / "_Right" suffixes.

Render Context Data Structures

RenderContextInstData (RCID) — per-drawable (rendercontext.h:34–89)

RenderContextFrameData (RCFD) — per-frame (rendercontext.h:96–168)

Pipeline Execution Flow

beginBlock() (fx_pipeline.cpp:107–178)

  1. Get FxInterface from context
  2. FXI->BeginBlock(technique, RCID) → pass count
  3. Execute all state lambdas with RCID
  4. _syncMaterialParams() — stamp-gated overlay of material-level rebinds
  5. Bind all parameters via type dispatch (_set_typed_param)
  6. Bind all storage buffers (with per-block sub-range offsets)
  7. FXI->applyRasterState(*_rasterstate) if the pipeline carries one (the shader-stateblock interplay resolves at draw time — see below)
  8. Return pass count

Cache Lookup (`FxPipelineCache::findPipeline`, fx_pipeline.cpp:789–831)

  1. Extract permutation flags from RCID/RCFD
  2. genIndex() → 64-bit hash
  3. Check _lut[hash]
  4. On miss: call _on_miss(permutation) → create pipeline
  5. Cache and return

Raster State Priority Override

Raster state is resolved via a priority_stack<rasterstate_ptr_t> (ork.core/inc/ork/kernel/priority_stack.inl). When a rasterstate is pushed onto FXI, it competes with the shader's stateblock rasterstate based on the _priority field — higher priority wins.

Resolution logic — VkFxInterface::_effectiveRasterState() (vulkan_fxi_pipelines.cpp:34–44):

rasterstate_ptr_t VkFxInterface::_effectiveRasterState() {
  rasterstate_ptr_t effective_rasterstate = _rasterstate_stack.resolve(); // highest-priority pushed state
  int iraspri = effective_rasterstate ? effective_rasterstate->_priority : 0;
  if (_currentVKPASS && _currentVKPASS->_stateblock_rasterstate) {
    auto try_rs = _currentVKPASS->_stateblock_rasterstate;
    if (try_rs->_priority >= iraspri) {
      effective_rasterstate = _currentVKPASS->_stateblock_rasterstate; // shader stateblock wins ties
    }
  }
  return effective_rasterstate;
}

Called from the pipeline-state fetch/composition sites (vulkan_fxi_pipelines.cpp:88, 248, 346) and the bind path (vulkan_fxi_pipelines_bind.cpp:127), so every draw resolves the same interplay.

Usage pattern — push a higher-priority rasterstate to override the shader's stateblock:

// Override shader's blend/depth/cull for this draw call
auto RSTATE = std::make_shared<RasterState>();
RSTATE->setDepthTest(EDepthTest::OFF);
RSTATE->setCullTest(ECullTest::OFF);
RSTATE->setBlendingMacro(BlendingMacro::ALPHA);
RSTATE->_priority = 1 << 10;  // higher than shader stateblock (default 0)

context->FXI()->pushRasterState(RSTATE);
// ... draw calls use this rasterstate instead of shader's ...
context->FXI()->popRasterState();

Examples in codebase:

Python API

from orkengine import lev2

# Permutation
permu = lev2.FxPipelinePermutation(rendermodel="FORWARD_PBR")
permu.stereo = False
permu.instanced = True
permu.skinned = False
permu.is_picking = False

# Cache lookup
cache = material.fxcache
pipeline = cache.findPipeline(permu)

# Pipeline usage
pipeline.technique = tek
pipeline.bindParam(param, 0.5)          # float — stored, applied in C++ at render time
pipeline.bindParam(param, fvec4(...))   # vector — same, no Python on render path
pipeline.bindParam(param, texture)      # texture — same
pipeline.bindParam(param, crcstring)    # named provider — C++ resolves engine data each frame
pipeline.bindParam(param, lambda: 0.5)  # Python lambda — only case where Python runs per-frame
pipeline.bindStorage(storage, ssbo)     # SSBO
pipeline.wrappedDrawCall(rcid, lambda: draw())

# NOTE: All bindings except Python lambdas execute entirely in C++ at render time.
# Python sets up the wiring once; the C++ runtime evaluates it every frame.
# This means Python scene composition has zero per-frame scripting overhead
# for parameter binding — only Python lambdas invoke the interpreter.

How to Answer

  1. For permutation flags: check FxPipelinePermutation in fx_pipeline.h and genIndex() in fx_pipeline.cpp:21–39
  2. For parameter binding: check _set_typed_param() in fx_pipeline.cpp
  3. For named providers: check the FxPipelineNamedParamProviders ctor's _providers[...] registrations in fx_pipeline.cpp
  4. For PBR-specific pipeline creation: check material_pbr_pipeline.cpp
  5. For render context: check rendercontext.h
  6. For raster-state resolution: check VkFxInterface::_effectiveRasterState() in vulkan_fxi_pipelines.cpp
  7. For Python: check the FxPipelinePermutation/FxPipelineCache/FxPipeline classes in pyext_gfx_material.cpp (from line 141)
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.