Skip to content
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

Cycles : Improve device selection and handling #5678

Merged
merged 4 commits into from
Feb 21, 2024
Merged
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
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Improvements
- GraphEditor : Improved logic used to connect a newly created node to the selected nodes.
- ScenePlug, ImagePlug : Child plugs are now serialisable. Among other things, this enables them to be driven by expressions (#3986).
- Premultiply : Added `useDeepVisibility` plug, which weights samples according to their visibility based on the opacity of samples in front.
- CyclesOptions : Improved device selection UI.

Fixes
-----
Expand Down
2 changes: 0 additions & 2 deletions include/GafferCycles/IECoreCyclesPreview/IECoreCycles.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,5 @@ IECORECYCLES_API int minorVersion();
IECORECYCLES_API int patchVersion();
/// Returns a string of the form "major.minor.patch"
IECORECYCLES_API const std::string &versionString();
/// Returns a vector of ccl::devices
IECORECYCLES_API const std::vector<ccl::DeviceInfo> &devices();

}
89 changes: 49 additions & 40 deletions python/GafferCyclesUI/CyclesOptionsUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ def __sessionSummary( plug ) :
info = []

if plug["device"]["enabled"].getValue() :
info.append( "Device(s) {}".format( plug["device"]["value"].getValue() ) )
# We don't have enough space to display the full device string, but the
# `:00` device indices are kindof confusing. Just strip off the
# indices so we're showing a list of device types.
devices = set(
d.partition( ":" )[0]
for d in plug["device"]["value"].getValue().split()
)
info.append( " + ".join( devices ) )

if plug["shadingSystem"]["enabled"].getValue() :
info.append( "Shading System {}".format( plug["shadingSystem"]["value"].getValue() ) )
Expand Down Expand Up @@ -359,57 +366,51 @@ def __logSummary( plug ) :

return ", ".join( info )

def __devicesPreset() :
def __registerDevicePresets() :

Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU", "CPU" )

cudaIndex = 0
hipIndex = 0
optixIndex = 0
metalIndex = 0

typeIndices = {}
for device in GafferCycles.devices :

index = 0
if device["type"] == "MULTI" or device["type"] == "CPU" :
if device["type"] == "CPU" :
continue
elif device["type"] == "CUDA" :
index = cudaIndex
cudaIndex += 1
elif device["type"] == "HIP" :
index = hipIndex
hipIndex += 1
elif device["type"] == "OPTIX" :
index = optixIndex
optixIndex += 1
elif device["type"] == "METAL" :
index = metalIndex
metalIndex += 1

typeIndex = typeIndices.setdefault( device["type"], 0 )
typeIndices[device["type"]] += 1

Gaffer.Metadata.registerValue(
GafferCycles.CyclesOptions,
"options.device.value",
"preset:%s:%02i - %s" % ( device["type"], index, device["description"] ),
"%s:%02i" % ( device["type"], index )
)
"preset:{}/{}".format( device["type"], device["description"] ),
"{}:{:02}".format( device["type"], typeIndex )
)

Gaffer.Metadata.registerValue(
GafferCycles.CyclesOptions,
"options.device.value",
"preset:CPU and %s:%02i - %s" % ( device["type"], index, device["description"] ),
"CPU %s:%02i" % ( device["type"], index )
)
"preset:{}/{} + CPU".format( device["type"], device["description"] ),
"CPU {}:{:02}".format( device["type"], typeIndex )
)

Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:All CUDA", "CUDA:*" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:All OptiX", "OPTIX:*" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:All HIP", "HIP:*" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:All Metal", "METAL:*" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU and all CUDA", "CPU CUDA:*" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU and all OptiX", "CPU OPTIX:*" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU and all HIP", "CPU HIP:*" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU and all Metal", "CPU METAL:*" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU and first CUDA found", "CPU CUDA:00" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU and first OptiX found", "CPU OPTIX:00" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU and first HIP found", "CPU HIP:00" )
Gaffer.Metadata.registerValue( GafferCycles.CyclesOptions, "options.device.value", "preset:CPU and first Metal found", "CPU METAL:00" )
for deviceType, count in typeIndices.items() :

if count <= 1 :
continue

Gaffer.Metadata.registerValue(
GafferCycles.CyclesOptions,
"options.device.value",
"preset:{}/All".format( deviceType ),
"{}:*".format( deviceType )
)

Gaffer.Metadata.registerValue(
GafferCycles.CyclesOptions,
"options.device.value",
"preset:{}/All + CPU".format( deviceType ),
"CPU {}:*".format( deviceType )
)

Gaffer.Metadata.registerNode(

Expand Down Expand Up @@ -475,6 +476,14 @@ def __devicesPreset() :

],

"options.device.value" : [


"plugValueWidget:type", "GafferUI.PresetsPlugValueWidget",
"presetsPlugValueWidget:allowCustom", True

],

"options.shadingSystem" : [

"description",
Expand Down Expand Up @@ -1684,7 +1693,7 @@ def __devicesPreset() :
}
)

__devicesPreset()
__registerDevicePresets()

if not GafferCycles.withTextureCache :

Expand Down
7 changes: 4 additions & 3 deletions python/GafferCyclesUI/CyclesShaderBallUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#
##########################################################################

import IECore
import Gaffer
import GafferCycles

Expand All @@ -46,7 +47,7 @@
Generates scenes suitable for rendering shader balls with Cycles.
""",

"layout:activator:deviceIsCpuOrMulti", lambda node : node["device"]["value"].getValue() in ["CPU", "MULTI"],
"layout:activator:deviceIncludesCPU", lambda node : IECore.StringAlgo.matchMultiple( "CPU", node["device"]["value"].getValue(), ),

plugs = {

Expand Down Expand Up @@ -84,10 +85,10 @@
the rest of the UI for instance.
""",

"layout:visibilityActivator", "deviceIsCpuOrMulti",
"layout:activator", "deviceIncludesCPU",

],

}

)
)
15 changes: 0 additions & 15 deletions src/GafferCycles/IECoreCyclesPreview/IECoreCycles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ namespace

// Version
std::string cyclesVersion = CYCLES_VERSION_STRING;
// Store devices once
std::vector<ccl::DeviceInfo> cyclesDevices;

}

Expand Down Expand Up @@ -82,14 +80,6 @@ bool init()
ccl::util_logging_start();
ccl::util_logging_verbosity_set( 0 );

// Get devices
ccl::vector<ccl::DeviceInfo> devices = ccl::Device::available_devices( ccl::DEVICE_MASK_CPU | ccl::DEVICE_MASK_HIP | ccl::DEVICE_MASK_CUDA | ccl::DEVICE_MASK_METAL | ccl::DEVICE_MASK_OPTIX );
devices.push_back( ccl::Device::get_multi_device( devices, 0, true ) );

for( auto device : devices )
{
cyclesDevices.push_back( device );
}

return true;
}
Expand All @@ -114,9 +104,4 @@ const std::string &versionString()
return cyclesVersion;
}

const std::vector<ccl::DeviceInfo> &devices()
{
return cyclesDevices;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long ago from what I remember the available_devices() call wasn't reentrant and gave different results after the first call, giving me many headaches until I realised that the Blender code only calls it once and stores the results, but maybe they've fixed that now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info on the history here Alex. In my own testing available_devices() seems to always return exactly the same results each time, and a glance at the Cycles source suggests that is expected (it uses a lock and flags to avoid multiple initialisations). Unless we see problems in practice, my preference is for the more minimal approach.

}

}
Loading
Loading