示例#1
0
        public void SetData <T>(T[] data)
            where T : struct
        {
            if ((mType & BufferType.Input) != BufferType.Input)
            {
                throw new OptixException("OptixBuffer Error: Attempting to set data on a non Input buffer!");
            }

            if (data == null || data.Length == 0)
            {
                return;
            }

            uint mySize = 0;

            CheckError(Api.rtBufferGetElementSize(InternalPtr, ref mySize));

            mySize *= (uint)mWidth;
            if (mHeight > 0)
            {
                mySize *= (uint)mHeight;
            }
            if (mDepth > 0)
            {
                mySize *= (uint)mDepth;
            }

            uint size = (uint)(data.Length * Marshal.SizeOf <T>());

            if (size > mySize)
            {
                throw new OptixException("OptixBuffer Error: Attempting to set data that is larger than the buffer size");
            }

            var l         = GCHandle.Alloc(data, GCHandleType.Pinned);
            var bufferMap = IntPtr.Zero;

            CheckError(Api.rtBufferMap(InternalPtr, ref bufferMap));
            if (bufferMap == IntPtr.Zero)
            {
                throw new OptixException("Buffer Error: Internal buffer cannot be mapped");
            }
            MemoryHelper.MemCopy(bufferMap, l.AddrOfPinnedObject(), size);
            //MemoryHelper.CopyMemory(bufferMap, l.AddrOfPinnedObject(), size);
            CheckError(Api.rtBufferUnmap(InternalPtr));
            l.Free();
        }
示例#2
0
        public void Write <T>(T r)
            where T : struct
        {
            if (!mCanWrite)
            {
                throw new NotSupportedException("Optix Error: BufferStream: Cannot write to non Input Optix buffers");
            }

            var elemSize    = Marshal.SizeOf <T>();
            var sizeInBytes = elemSize;

            if (mPosition + sizeInBytes > mSize)
            {
                throw new EndOfStreamException();
            }
            var l = GCHandle.Alloc(r, GCHandleType.Pinned);

            MemoryHelper.MemCopy(IntPtr.Add(Buffer, (int)mPosition), l.AddrOfPinnedObject(), (uint)sizeInBytes);
            mPosition += sizeInBytes;
            l.Free();
        }