1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
// Copyright 2009-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "default.h"
#include "device.h"
namespace embree
{
/*! Implements an API data buffer object. This class may or may not own the data. */
class Buffer : public RefCount
{
public:
/*! Buffer construction */
Buffer()
: device(nullptr), ptr(nullptr), numBytes(0), shared(false) {}
/*! Buffer construction */
Buffer(Device* device, size_t numBytes_in, void* ptr_in = nullptr)
: device(device), numBytes(numBytes_in)
{
device->refInc();
if (ptr_in)
{
shared = true;
ptr = (char*)ptr_in;
}
else
{
shared = false;
alloc();
}
}
/*! Buffer destruction */
~Buffer() {
free();
device->refDec();
}
/*! this class is not copyable */
private:
Buffer(const Buffer& other) DELETED; // do not implement
Buffer& operator =(const Buffer& other) DELETED; // do not implement
public:
/* inits and allocates the buffer */
void create(Device* device_in, size_t numBytes_in)
{
init(device_in, numBytes_in);
alloc();
}
/* inits the buffer */
void init(Device* device_in, size_t numBytes_in)
{
free();
device = device_in;
ptr = nullptr;
numBytes = numBytes_in;
shared = false;
}
/*! sets shared buffer */
void set(Device* device_in, void* ptr_in, size_t numBytes_in)
{
free();
device = device_in;
ptr = (char*)ptr_in;
if (numBytes_in != (size_t)-1)
numBytes = numBytes_in;
shared = true;
}
/*! allocated buffer */
void alloc()
{
if (device)
device->memoryMonitor(this->bytes(), false);
size_t b = (this->bytes()+15) & ssize_t(-16);
ptr = (char*)alignedMalloc(b,16);
}
/*! frees the buffer */
void free()
{
if (shared) return;
alignedFree(ptr);
if (device)
device->memoryMonitor(-ssize_t(this->bytes()), true);
ptr = nullptr;
}
/*! gets buffer pointer */
void* data()
{
/* report error if buffer is not existing */
if (!device)
throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "invalid buffer specified");
/* return buffer */
return ptr;
}
/*! returns pointer to first element */
__forceinline char* getPtr() const {
return ptr;
}
/*! returns the number of bytes of the buffer */
__forceinline size_t bytes() const {
return numBytes;
}
/*! returns true of the buffer is not empty */
__forceinline operator bool() const {
return ptr;
}
public:
Device* device; //!< device to report memory usage to
char* ptr; //!< pointer to buffer data
size_t numBytes; //!< number of bytes in the buffer
bool shared; //!< set if memory is shared with application
};
/*! An untyped contiguous range of a buffer. This class does not own the buffer content. */
class RawBufferView
{
public:
/*! Buffer construction */
RawBufferView()
: ptr_ofs(nullptr), stride(0), num(0), format(RTC_FORMAT_UNDEFINED), modCounter(1), modified(true), userData(0) {}
public:
/*! sets the buffer view */
void set(const Ref<Buffer>& buffer_in, size_t offset_in, size_t stride_in, size_t num_in, RTCFormat format_in)
{
if ((offset_in + stride_in * num_in) > (stride_in * buffer_in->numBytes))
throw_RTCError(RTC_ERROR_INVALID_ARGUMENT, "buffer range out of bounds");
ptr_ofs = buffer_in->ptr + offset_in;
stride = stride_in;
num = num_in;
format = format_in;
modCounter++;
modified = true;
buffer = buffer_in;
}
/*! returns pointer to the first element */
__forceinline char* getPtr() const {
return ptr_ofs;
}
/*! returns pointer to the i'th element */
__forceinline char* getPtr(size_t i) const
{
assert(i<num);
return ptr_ofs + i*stride;
}
/*! returns the number of elements of the buffer */
__forceinline size_t size() const {
return num;
}
/*! returns the number of bytes of the buffer */
__forceinline size_t bytes() const {
return num*stride;
}
/*! returns the buffer stride */
__forceinline unsigned getStride() const
{
assert(stride <= unsigned(inf));
return unsigned(stride);
}
/*! return the buffer format */
__forceinline RTCFormat getFormat() const {
return format;
}
/*! mark buffer as modified or unmodified */
__forceinline void setModified() {
modCounter++;
modified = true;
}
/*! mark buffer as modified or unmodified */
__forceinline bool isModified(unsigned int otherModCounter) const {
return modCounter > otherModCounter;
}
/*! mark buffer as modified or unmodified */
__forceinline bool isLocalModified() const {
return modified;
}
/*! clear local modified flag */
__forceinline void clearLocalModified() {
modified = false;
}
/*! returns true of the buffer is not empty */
__forceinline operator bool() const {
return ptr_ofs;
}
/*! checks padding to 16 byte check, fails hard */
__forceinline void checkPadding16() const
{
if (ptr_ofs && num)
volatile int MAYBE_UNUSED w = *((int*)getPtr(size()-1)+3); // FIXME: is failing hard avoidable?
}
public:
char* ptr_ofs; //!< base pointer plus offset
size_t stride; //!< stride of the buffer in bytes
size_t num; //!< number of elements in the buffer
RTCFormat format; //!< format of the buffer
unsigned int modCounter; //!< version ID of this buffer
bool modified; //!< local modified data
int userData; //!< special data
Ref<Buffer> buffer; //!< reference to the parent buffer
};
/*! A typed contiguous range of a buffer. This class does not own the buffer content. */
template<typename T>
class BufferView : public RawBufferView
{
public:
typedef T value_type;
/*! access to the ith element of the buffer */
__forceinline T& operator [](size_t i) { assert(i<num); return *(T*)(ptr_ofs + i*stride); }
__forceinline const T& operator [](size_t i) const { assert(i<num); return *(T*)(ptr_ofs + i*stride); }
};
template<>
class BufferView<Vec3fa> : public RawBufferView
{
public:
typedef Vec3fa value_type;
/*! access to the ith element of the buffer */
__forceinline const Vec3fa operator [](size_t i) const
{
assert(i<num);
return Vec3fa(vfloat4::loadu((float*)(ptr_ofs + i*stride)));
}
/*! writes the i'th element */
__forceinline void store(size_t i, const Vec3fa& v)
{
assert(i<num);
vfloat4::storeu((float*)(ptr_ofs + i*stride), (vfloat4)v);
}
};
}
|