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

OSL Image Sampler Prototype Update #5963

Draft
wants to merge 10 commits into
base: 1.4_maintenance
Choose a base branch
from
26 changes: 21 additions & 5 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,24 @@ cyclesDefines = [
( "WITH_OPTIX" ),
]

cyclesLibraries = [
"cycles_session", "cycles_scene", "cycles_graph", "cycles_bvh", "cycles_device", "cycles_kernel", "cycles_kernel_osl",
"cycles_integrator", "cycles_util", "cycles_subd", "extern_sky", "extern_cuew"
]

# It's very weird to link the Cycles static libraries twice, to both
# lib/libGafferCycles.so and python/GafferCycles/_GafferCycles.so, resulting in 2 copies of everything.
# This has caused issues on Ubuntu where the init stuff cycles_session runs twice, terminating Gaffer with an
# exception when the node buffer_pass is registered twice. It seems to work fine on Linux if we don't double
# link it - the symbols from the static library can be included just in libGafferCycles, and the dynamic linker
# will find them OK.
# But on Windows, it seems the symbols from the static libraries aren't made available in libGafferCycles, so
# it wouldn't work to just link them once. It is currently unknown why Windows doesn't have problems with
# running the initialization in cycles_session twice.
# Hopefully this hackery works for now - we're hoping in the long run, Cycles might ship as a dynamic lib, which
# would simplify all this.
includeCyclesLibrariesInPythonModule = env["PLATFORM"] == "win32"

libraries = {

"Gaffer" : {
Expand Down Expand Up @@ -1350,9 +1368,8 @@ libraries = {
"LIBPATH" : [ "$CYCLES_ROOT/lib" ],
"LIBS" : [
"IECoreScene$CORTEX_LIB_SUFFIX", "IECoreImage$CORTEX_LIB_SUFFIX", "IECoreVDB$CORTEX_LIB_SUFFIX",
"Gaffer", "GafferScene", "GafferDispatch", "GafferOSL",
"cycles_session", "cycles_scene", "cycles_graph", "cycles_bvh", "cycles_device", "cycles_kernel", "cycles_kernel_osl",
"cycles_integrator", "cycles_util", "cycles_subd", "extern_sky", "extern_cuew",
"Gaffer", "GafferScene", "GafferDispatch", "GafferOSL"
] + cyclesLibraries + [
"OpenImageIO$OIIO_LIB_SUFFIX", "OpenImageIO_Util$OIIO_LIB_SUFFIX", "oslexec$OSL_LIB_SUFFIX", "oslquery$OSL_LIB_SUFFIX",
"openvdb$VDB_LIB_SUFFIX", "Alembic", "osdCPU", "OpenColorIO$OCIO_LIB_SUFFIX", "embree4", "Iex", "openpgl",
],
Expand All @@ -1364,8 +1381,7 @@ libraries = {
"LIBPATH" : [ "$CYCLES_ROOT/lib" ],
"LIBS" : [
"Gaffer", "GafferScene", "GafferDispatch", "GafferBindings", "GafferCycles", "IECoreScene",
"cycles_session", "cycles_scene", "cycles_graph", "cycles_bvh", "cycles_device", "cycles_kernel", "cycles_kernel_osl",
"cycles_integrator", "cycles_util", "cycles_subd", "extern_sky", "extern_cuew",
] + ( cyclesLibraries if includeCyclesLibrariesInPythonModule else [] ) + [
"OpenImageIO$OIIO_LIB_SUFFIX", "OpenImageIO_Util$OIIO_LIB_SUFFIX", "oslexec$OSL_LIB_SUFFIX", "openvdb$VDB_LIB_SUFFIX",
"oslquery$OSL_LIB_SUFFIX", "Alembic", "osdCPU", "OpenColorIO$OCIO_LIB_SUFFIX", "embree4", "Iex", "openpgl",
],
Expand Down
3 changes: 2 additions & 1 deletion include/GafferImage/Sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class GAFFERIMAGE_API Sampler
/// @param channelName The channel to sample.
/// @param sampleWindow The area from which samples may be requested. It is an error to request samples outside this area.
/// @param boundingMode The method of handling samples that fall outside the data window.
Sampler( const GafferImage::ImagePlug *plug, const std::string &channelName, const Imath::Box2i &sampleWindow, BoundingMode boundingMode = Black );
Sampler( const GafferImage::ImagePlug *plug, const std::string &channelName, const Imath::Box2i &sampleWindow, BoundingMode boundingMode = Black, bool rememberContext = false );

/// Uses `parallelProcessTiles()` to fill the internal tile cache
/// with all tiles in the sample window. Allows `sample()` and
Expand Down Expand Up @@ -135,6 +135,7 @@ class GAFFERIMAGE_API Sampler
const std::string m_channelName;
Imath::Box2i m_sampleWindow;
Imath::Box2i m_dataWindow;
Gaffer::ContextPtr m_overrideContext;

std::vector< IECore::ConstFloatVectorDataPtr > m_dataCache;
std::vector< const float * > m_dataCacheRaw;
Expand Down
10 changes: 9 additions & 1 deletion include/GafferImage/Sampler.inl
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,15 @@ inline void Sampler::cachedData( Imath::V2i p, const float *& tileData, int &til
Imath::V2i tileOrigin( p.x & ~( ImagePlug::tileSize() - 1 ), p.y & ~( ImagePlug::tileSize() - 1 ) );

IECore::ConstFloatVectorDataPtr &cacheTilePtr = m_dataCache[ cacheIndex ];
cacheTilePtr = m_plug->channelData( m_channelName, tileOrigin );
if( m_overrideContext )
{
Gaffer::Context::Scope s( m_overrideContext.get() );
cacheTilePtr = m_plug->channelData( m_channelName, tileOrigin );
}
else
{
cacheTilePtr = m_plug->channelData( m_channelName, tileOrigin );
}
cacheTileRawPtr = &cacheTilePtr->readable()[0];
}

Expand Down
6 changes: 6 additions & 0 deletions include/GafferOSL/OSLImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class GAFFEROSL_API OSLImage : public GafferImage::ImageProcessor
GafferImage::Format computeFormat( const Gaffer::Context *context, const GafferImage::ImagePlug *parent ) const override;
Imath::Box2i computeDataWindow( const Gaffer::Context *context, const GafferImage::ImagePlug *parent ) const override;

Gaffer::ValuePlug::CachePolicy hashCachePolicy( const Gaffer::ValuePlug *output ) const override;


private :

GafferScene::ShaderPlug *shaderPlug();
Expand All @@ -105,6 +108,9 @@ class GAFFEROSL_API OSLImage : public GafferImage::ImageProcessor
Gaffer::StringVectorDataPlug *affectedChannelsPlug();
const Gaffer::StringVectorDataPlug *affectedChannelsPlug() const;

Gaffer::IntPlug *allImageDataNeededPlug();
const Gaffer::IntPlug *allImageDataNeededPlug() const;

void hashShading( const Gaffer::Context *context, IECore::MurmurHash &h ) const;
IECore::ConstCompoundDataPtr computeShading( const Gaffer::Context *context ) const;

Expand Down
15 changes: 14 additions & 1 deletion include/GafferOSL/ShadingEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@
#include "GafferOSL/Export.h"
#include "GafferOSL/TypeIds.h"

#include "GafferImage/ImagePlug.h"

#include "IECoreScene/ShaderNetwork.h"

#include "IECore/CompoundData.h"

#include "OpenImageIO/ustring.h"

#include "boost/container/flat_set.hpp"
#include "boost/container/flat_map.hpp"

namespace GafferOSL
{
Expand Down Expand Up @@ -83,13 +88,17 @@ class GAFFEROSL_API ShadingEngine : public IECore::RefCounted
};

using Transforms = std::map<IECore::InternedString, Transform>;
using ImagePlugs = std::map<IECore::InternedString, const GafferImage::ImagePlug *>;

/// Append a unique hash representing this shading engine to `h`.
void hash( IECore::MurmurHash &h ) const;
IECore::CompoundDataPtr shade( const IECore::CompoundData *points, const Transforms &transforms = Transforms() ) const;
IECore::CompoundDataPtr shade( const IECore::CompoundData *points, const Transforms &transforms = Transforms(), const ImagePlugs &imagePlugs = ImagePlugs() ) const;

bool needsAttribute( const std::string &name ) const;
bool hasDeformation() const;
bool needsImageSamples() const;

IECore::MurmurHash hashPossibleImageSamples( const ImagePlugs &imagePlugs ) const;

private :

Expand All @@ -108,6 +117,10 @@ class GAFFEROSL_API ShadingEngine : public IECore::RefCounted

bool m_hasDeformation;

std::vector< OIIO::ustring > m_gafferTexturesRequested;

boost::container::flat_map< OIIO::ustring, int> m_gafferTextureIndices;

void *m_shaderGroupRef;

};
Expand Down
Loading
Loading