@@ -7,13 +7,14 @@ pub mod export;
77use document_container:: AnyContainer ;
88use document_container:: backends:: memory:: MemoryBackend ;
99use document_format:: { GddV1 , GddV1Layout } ;
10+ use graph_craft:: application_io:: resource:: LoadResource ;
1011use graph_craft:: application_io:: { PlatformApplicationIo , PlatformEditorApi } ;
1112use graph_craft:: document:: { NetworkLocation , NodeId , NodeNetwork , OwnedNetworkLocation } ;
1213use graph_craft:: graphene_compiler:: Compiler ;
1314use graph_craft:: proto:: ProtoNetwork ;
1415use graph_craft:: util:: load_network;
1516use graphene_std:: application_io:: ApplicationIo ;
16- use graphene_std:: runtime:: { AsyncRuntime , Spawn , Task } ;
17+ use graphene_std:: runtime:: { AsyncRuntime , RuntimeHandle , Spawn , Task } ;
1718use interpreted_executor:: dynamic_executor:: DynamicExecutor ;
1819use interpreted_executor:: util:: wrap_network_in_scope;
1920use std:: collections:: HashMap ;
@@ -93,12 +94,12 @@ pub struct CompiledDocument {
9394 spawner : Arc < TokioSpawner > ,
9495}
9596
96- /// Compile `document` against a fresh GPU context , resolving its resources from its `.gdd` .
97- pub async fn compile_document ( document : OpenedDocument ) -> Result < CompiledDocument , Box < dyn Error > > {
97+ /// A GPU context and the editor API handed to compiled graphs , resolving resources through `resource_proxy` when given .
98+ pub async fn editor_api ( resource_proxy : Option < Box < dyn LoadResource > > ) -> ( Arc < PlatformApplicationIo > , Arc < PlatformEditorApi > ) {
9899 log:: info!( "Creating GPU context" ) ;
99100 let mut application_io = PlatformApplicationIo :: new ( ) . await ;
100- if let Some ( gdd ) = & document . gdd {
101- application_io. inject_resource_proxy ( Box :: new ( gdd . resource_proxy ( ) ) ) ;
101+ if let Some ( resource_proxy ) = resource_proxy {
102+ application_io. inject_resource_proxy ( resource_proxy) ;
102103 }
103104 let application_io = Arc :: new ( application_io) ;
104105 if let Some ( executor) = application_io. gpu_executor ( ) {
@@ -109,14 +110,15 @@ pub async fn compile_document(document: OpenedDocument) -> Result<CompiledDocume
109110 let editor_api = Arc :: new ( PlatformEditorApi {
110111 application_io : Some ( application_io. clone ( ) ) ,
111112 } ) ;
112- let spawner = Arc :: new ( TokioSpawner :: default ( ) ) ;
113- let runtime = AsyncRuntime :: new ( spawner . clone ( ) , Arc :: new ( std :: time :: Instant :: now ( ) ) ) ;
113+ ( application_io , editor_api )
114+ }
114115
115- let mut network = wrap_network_in_scope ( document. network , editor_api, runtime. handle ( ) ) ;
116+ /// Compile `network`. A `.gdd` resolves resource hashes from its registry, while a legacy `.graphite` has no resource store.
117+ pub fn compile_graph ( network : NodeNetwork , editor_api : Arc < PlatformEditorApi > , gdd : Option < & GddV1 > , runtime : RuntimeHandle ) -> Result < ProtoNetwork , Box < dyn Error > > {
118+ let mut network = wrap_network_in_scope ( network, editor_api, runtime) ;
116119
117- // A `.gdd` resolves resource hashes from its registry, while a legacy `.graphite` has no resource store
118120 let preprocessor = preprocessor:: Preprocessor :: new ( ) ;
119- let preprocessed = match & document . gdd {
121+ let preprocessed = match gdd {
120122 Some ( gdd) => preprocessor. preprocess ( & mut network, & |resource_id| gdd. registry ( ) . resources . get ( & resource_id) . and_then ( |resource| resource. hash ) ) ,
121123 None => preprocessor. preprocess ( & mut network, & |_| None ) ,
122124 } ;
@@ -126,9 +128,26 @@ pub async fn compile_document(document: OpenedDocument) -> Result<CompiledDocume
126128 for warning in & compiled_network. warnings {
127129 eprintln ! ( "{warning:?}" ) ;
128130 }
131+ Ok ( compiled_network. network )
132+ }
133+
134+ /// An executor for `proto_network`, with its errors joined into one.
135+ pub fn create_executor ( proto_network : ProtoNetwork ) -> Result < DynamicExecutor , Box < dyn Error > > {
136+ DynamicExecutor :: new ( proto_network) . map_err ( |errors| {
137+ let messages: Vec < String > = errors. iter ( ) . map ( |error| format ! ( "{error:?}" ) ) . collect ( ) ;
138+ messages. join ( "\n " ) . into ( )
139+ } )
140+ }
141+
142+ /// Compile `document` against a fresh GPU context, resolving its resources from its `.gdd`.
143+ pub async fn compile_document ( document : OpenedDocument ) -> Result < CompiledDocument , Box < dyn Error > > {
144+ let ( application_io, editor_api) = editor_api ( document. gdd . as_ref ( ) . map ( |gdd| Box :: new ( gdd. resource_proxy ( ) ) as Box < dyn LoadResource > ) ) . await ;
145+ let spawner = Arc :: new ( TokioSpawner :: default ( ) ) ;
146+ let runtime = AsyncRuntime :: new ( spawner. clone ( ) , Arc :: new ( std:: time:: Instant :: now ( ) ) ) ;
147+ let proto_network = compile_graph ( document. network , editor_api, document. gdd . as_ref ( ) , runtime. handle ( ) ) ?;
129148
130149 Ok ( CompiledDocument {
131- proto_network : compiled_network . network ,
150+ proto_network,
132151 application_io,
133152 runtime,
134153 spawner,
@@ -137,10 +156,7 @@ pub async fn compile_document(document: OpenedDocument) -> Result<CompiledDocume
137156
138157impl CompiledDocument {
139158 pub fn into_renderer ( self ) -> Result < Renderer , Box < dyn Error > > {
140- let executor = DynamicExecutor :: new ( self . proto_network ) . map_err ( |errors| {
141- let messages: Vec < String > = errors. iter ( ) . map ( |error| format ! ( "{error:?}" ) ) . collect ( ) ;
142- messages. join ( "\n " )
143- } ) ?;
159+ let executor = create_executor ( self . proto_network ) ?;
144160 self . runtime . retain_sources ( executor. sources ( ) ) ;
145161
146162 let device = self . application_io . gpu_executor ( ) . ok_or ( "No GPU executor is available" ) ?. context ( ) . device . clone ( ) ;
0 commit comments