Skip to content
Permalink
Browse files

Fix private header inclusion of opcolor.h (#1053)

* Extend ShadingSystem::getattribute of type pointer's to also return the size of the pointer.
Add "colorsystem:colorspace" to ShadingSystem::getattribute.
Remove private include of opcolor.h.

* Be a little less hackey about the whole thing

* Have gettatrr return size and number of strings for a more general pattern.
  • Loading branch information...
marsupial authored and lgritz committed Sep 6, 2019
1 parent 9c8dd33 commit f0a0039ca967d70e85853b84c94ef0f46e45e172
Showing with 74 additions and 46 deletions.
  1. +1 −1 src/liboslexec/opcolor.h
  2. +14 −0 src/liboslexec/shadingsys.cpp
  3. +30 −23 src/testrender/optixraytracer.cpp
  4. +29 −22 src/testshade/optixgridrender.cpp
@@ -110,7 +110,7 @@ class ColorSystem {
template <typename Color> OSL_HOSTDEVICE Color
ocio_transform (StringParam fromspace, StringParam tospace, const Color& C, Context);

OSL_HOSTDEVICE StringParam colorspace() const { return m_colorspace; }
OSL_HOSTDEVICE const StringParam& colorspace() const { return m_colorspace; }

OSL_HOSTDEVICE void error(StringParam src, StringParam dst, Context);

@@ -1402,6 +1402,20 @@ ShadingSystemImpl::getattribute (string_view name, TypeDesc type,
*(void**)val = &colorsystem();
return true;
}
if (name == "colorsystem:sizes" && type.basetype == TypeDesc::LONGLONG) {
if (type.arraylen != 2) {
error ("Must request two colorsystem:sizes, [sizeof(pvt::ColorSystem), num-strings]");
return false;
}
long long* lptr = (long long*) val;
lptr[0] = sizeof(pvt::ColorSystem);
lptr[1] = 1; // 1 string (pvt::ColorSystem::m_colorspace)

// Make sure everything adds up!
ASSERT((((char*)&colorsystem() + lptr[0]) - sizeof(ustring)*lptr[1]) ==
(char*)&colorsystem().colorspace());
return true;
}

return false;
#undef ATTR_DECODE
@@ -33,7 +33,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <OSL/oslconfig.h>

#include "optixraytracer.h"
#include "../liboslexec/opcolor.h"

// The pre-compiled renderer support library LLVM bitcode is embedded
// into the executable and made available through these variables.
@@ -162,45 +161,53 @@ OptixRaytracer::synch_attributes ()

{
const char* name = OSL_NAMESPACE_STRING "::pvt::s_color_system";

pvt::ColorSystem* colorSys = nullptr;
shadingsys->getattribute("colorsystem", TypeDesc::PTR, (void*)&colorSys);
if (colorSys == nullptr) {

char* colorSys = nullptr;
long long cpuDataSizes[2] = {0,0};
if (!shadingsys->getattribute("colorsystem", TypeDesc::PTR, (void*)&colorSys) ||
!shadingsys->getattribute("colorsystem:sizes", TypeDesc(TypeDesc::LONGLONG,2), (void*)&cpuDataSizes) ||
!colorSys || !cpuDataSizes[0]) {
errhandler().error ("No colorsystem available.");
return false;
}

auto cpuDataSize = cpuDataSizes[0];
auto numStrings = cpuDataSizes[1];

// Get the size data-size, minus the ustring size
const size_t dataSize = sizeof(pvt::ColorSystem) - sizeof(StringParam);

const size_t podDataSize = cpuDataSize - sizeof(StringParam)*numStrings;
optix::Buffer buffer = m_optix_ctx->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_USER);
if (!buffer) {
errhandler().error ("Could not create buffer for '%s'.", name);
return false;
}

// set the elemet size to char
buffer->setElementSize(sizeof(char));

// and number of elements to the actual size needed.
buffer->setSize(dataSize + sizeof(DeviceString));

buffer->setSize(podDataSize + sizeof(DeviceString)*numStrings);
// copy the base data
char* dstData = (char*) buffer->map();
if (!dstData) {
char* gpuData = (char*) buffer->map();
if (!gpuData) {
errhandler().error ("Could not map buffer for '%s' (size: %lu).",
name, dataSize);
name, podDataSize + sizeof(DeviceString)*numStrings);
return false;
}
::memcpy(dstData, colorSys, dataSize);

// convert the ustring to a device string
uint64_t devStr = register_string (colorSys->colorspace().string(), "");

::memcpy(gpuData, colorSys, podDataSize);

// then copy the device string to the end, first strings starting at dataPtr - (numStrings)
// FIXME -- Should probably handle alignment better.
// then copy the device string to the end
::memcpy(dstData+dataSize, &devStr, sizeof(devStr));

const ustring* cpuString = (const ustring*)(colorSys + (cpuDataSize - sizeof(StringParam)*numStrings));
char* gpuStrings = gpuData + podDataSize;
for (const ustring* end = cpuString + numStrings; cpuString < end; ++cpuString) {
// convert the ustring to a device string
uint64_t devStr = register_string (cpuString->string(), "");
::memcpy(gpuStrings, &devStr, sizeof(devStr));
gpuStrings += sizeof(DeviceString);
}

buffer->unmap();
m_optix_ctx[name]->setBuffer(buffer);
}
@@ -33,7 +33,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <OSL/oslconfig.h>

#include "optixgridrender.h"
#include "../liboslexec/opcolor.h"


// The pre-compiled renderer support library LLVM bitcode is embedded
@@ -148,45 +147,53 @@ OptixGridRenderer::synch_attributes ()

{
const char* name = OSL_NAMESPACE_STRING "::pvt::s_color_system";

pvt::ColorSystem* colorSys = nullptr;
shadingsys->getattribute("colorsystem", TypeDesc::PTR, (void*)&colorSys);
if (colorSys == nullptr) {

char* colorSys = nullptr;
long long cpuDataSizes[2] = {0,0};
if (!shadingsys->getattribute("colorsystem", TypeDesc::PTR, (void*)&colorSys) ||
!shadingsys->getattribute("colorsystem:sizes", TypeDesc(TypeDesc::LONGLONG,2), (void*)&cpuDataSizes) ||
!colorSys || !cpuDataSizes[0]) {
errhandler().error ("No colorsystem available.");
return false;
}
auto cpuDataSize = cpuDataSizes[0];
auto numStrings = cpuDataSizes[1];

// Get the size data-size, minus the ustring size
const size_t dataSize = sizeof(pvt::ColorSystem) - sizeof(StringParam);

const size_t podDataSize = cpuDataSize - sizeof(StringParam)*numStrings;
optix::Buffer buffer = m_optix_ctx->createBuffer(RT_BUFFER_INPUT, RT_FORMAT_USER);
if (!buffer) {
errhandler().error ("Could not create buffer for '%s'.", name);
return false;
}

// set the elemet size to char
buffer->setElementSize(sizeof(char));

// and number of elements to the actual size needed.
buffer->setSize(dataSize + sizeof(DeviceString));

buffer->setSize(podDataSize + sizeof(DeviceString)*numStrings);
// copy the base data
char* dstData = (char*) buffer->map();
if (!dstData) {
char* gpuData = (char*) buffer->map();
if (!gpuData) {
errhandler().error ("Could not map buffer for '%s' (size: %lu).",
name, dataSize);
name, podDataSize + sizeof(DeviceString)*numStrings);
return false;
}
::memcpy(dstData, colorSys, dataSize);

// convert the ustring to a device string
uint64_t devStr = register_string (colorSys->colorspace().string(), "");

::memcpy(gpuData, colorSys, podDataSize);

// then copy the device string to the end, first strings starting at dataPtr - (numStrings)
// FIXME -- Should probably handle alignment better.
// then copy the device string to the end
::memcpy(dstData+dataSize, &devStr, sizeof(devStr));

const ustring* cpuString = (const ustring*)(colorSys + (cpuDataSize - sizeof(StringParam)*numStrings));
char* gpuStrings = gpuData + podDataSize;
for (const ustring* end = cpuString + numStrings; cpuString < end; ++cpuString) {
// convert the ustring to a device string
uint64_t devStr = register_string (cpuString->string(), "");
::memcpy(gpuStrings, &devStr, sizeof(devStr));
gpuStrings += sizeof(DeviceString);
}

buffer->unmap();
m_optix_ctx[name]->setBuffer(buffer);
}

0 comments on commit f0a0039

Please sign in to comment.
You can’t perform that action at this time.