Skip to content

Share Shading Context when optimizing/jitting a shader… #1952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions src/liboslexec/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ ShadingContext::~ShadingContext()
free_dict_resources();
}

ShadingContext::RestoreState
ShadingContext::repurposeForJit()
{
process_errors();
// Match previous behavior of nullptr value for texture thread info
// as if we created a new ShadingContext
RestoreState restore_state { texture_thread_info() };
texture_thread_info(nullptr);
return restore_state;
}

// Process any errors from JIT and restore the texture_thread_info
void
ShadingContext::restoreFromJit(const RestoreState& restore_state)
{
// Restore "this" ShadingContext for execution
process_errors();
texture_thread_info(restore_state.m_pre_jit_texture_thread_info);
}


bool
Expand All @@ -86,14 +105,14 @@ ShadingContext::execute_init(ShaderGroup& sgroup, int threadindex,
if (sgroup.nlayers()) {
sgroup.start_running();
if (!sgroup.jitted()) {
auto ctx = shadingsys().get_context(thread_info());
shadingsys().optimize_group(sgroup, ctx, true /*do_jit*/);
auto restore_state = repurposeForJit();
shadingsys().optimize_group(sgroup, this, true /*do_jit*/);
if (shadingsys().m_greedyjit
&& shadingsys().m_groups_to_compile_count) {
// If we are greedily JITing, optimize/JIT everything now
shadingsys().optimize_all_groups();
}
shadingsys().release_context(ctx);
restoreFromJit(restore_state);
}
if (sgroup.does_nothing())
return false;
Expand Down Expand Up @@ -260,19 +279,16 @@ ShadingContext::Batched<WidthT>::execute_init(
if (sgroup.nlayers()) {
sgroup.start_running();
if (!sgroup.batch_jitted()) {
// Matching ShadingContext::execute_init behavior
// of grabbing another context.
// TODO: Is this necessary, why can't we just use the
// the existing context()?
//shadingsys().template batched<WidthT>().jit_group(sgroup, &context());
auto ctx = shadingsys().get_context(context().thread_info());
shadingsys().template batched<WidthT>().jit_group(sgroup, ctx);
auto restore_state = context().repurposeForJit();
shadingsys().template batched<WidthT>().jit_group(sgroup,
&context());

if (shadingsys().m_greedyjit
&& shadingsys().m_groups_to_compile_count) {
// If we are greedily JITing, optimize/JIT everything now
shadingsys().template batched<WidthT>().jit_all_groups();
}
shadingsys().release_context(ctx);
context().restoreFromJit(restore_state);
}
// To handle layers that were not used but still possibly had
// render outputs, we always generate a run function even for
Expand Down
12 changes: 12 additions & 0 deletions src/liboslexec/oslexec_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,18 @@ class OSLEXECPUBLIC ShadingContext {
// wide data offsets should be used
int batch_size_executed;
bool execution_is_batched() const { return batch_size_executed != 0; }


struct RestoreState {
TextureSystem::Perthread* m_pre_jit_texture_thread_info;
};

// Rather than allocate an additional ShadingContext for JIT
// reuse this one by processing any existing errors
// and saving off any necessary state to be restored afterwards
RestoreState repurposeForJit();
// Process any errors from JIT and restore the state
void restoreFromJit(const RestoreState&);
};


Expand Down
Loading