/// <summary> /// Uploads the data pointer to buffer. /// </summary> /// <param name="context">The context.</param> /// <param name="data">The data.</param> /// <param name="byteCount">The count by bytes.</param> /// <param name="byteOffset">The offset by bytes.</param> /// <param name="minBufferSizeByBytes">The minimum buffer count by bytes.</param> public unsafe void UploadDataToBuffer(DeviceContextProxy context, System.IntPtr data, int byteCount, int byteOffset, int minBufferSizeByBytes = default(int)) { ElementCount = byteCount / StructureSize; int newSizeInBytes = byteCount; if (byteCount == 0) { return; } EnsureBufferCapacity(context, ElementCount, minBufferSizeByBytes / StructureSize); if (CapacityUsed + newSizeInBytes <= Capacity && !context.IsDeferred && CanOverwrite) { Offset = CapacityUsed; context.MapSubresource(this.buffer, MapMode.WriteNoOverwrite, MapFlags.None, out DataStream stream); using (stream) { stream.Position = Offset; stream.Write(data, byteOffset, byteCount); } context.UnmapSubresource(this.buffer, 0); CapacityUsed += newSizeInBytes; } else { context.MapSubresource(this.buffer, MapMode.WriteDiscard, MapFlags.None, out DataStream stream); using (stream) { stream.Write(data, byteOffset, byteCount); } context.UnmapSubresource(this.buffer, 0); Offset = CapacityUsed = 0; } }
/// <summary> /// <see cref="IElementsBufferProxy.UploadDataToBuffer{T}(DeviceContextProxy, IList{T}, int, int, int)"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="data"></param> /// <param name="count">Data Count</param> /// <param name="offset"></param> /// <param name="minBufferCount">Used to create a dynamic buffer with size of Max(count, minBufferCount).</param> public void UploadDataToBuffer <T>(DeviceContextProxy context, IList <T> data, int count, int offset, int minBufferCount = default(int)) where T : struct { ElementCount = count; int newSizeInBytes = StructureSize * count; if (count == 0) { return; } EnsureBufferCapacity(context, ElementCount, minBufferCount); if (CapacityUsed + newSizeInBytes <= Capacity && !context.IsDeferred && CanOverwrite) { Offset = CapacityUsed; context.MapSubresource(this.buffer, MapMode.WriteNoOverwrite, MapFlags.None, out DataStream stream); using (stream) { stream.Position = Offset; stream.WriteRange(data.GetArrayByType(), offset, count); } context.UnmapSubresource(this.buffer, 0); CapacityUsed += newSizeInBytes; } else { context.MapSubresource(this.buffer, MapMode.WriteDiscard, MapFlags.None, out DataStream stream); using (stream) { stream.WriteRange(data.GetArrayByType(), offset, count); } context.UnmapSubresource(this.buffer, 0); Offset = CapacityUsed = 0; } }
/// <summary> /// <see cref="IElementsBufferProxy.UploadDataToBuffer{T}(DeviceContextProxy, IList{T}, int, int, int)"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="data"></param> /// <param name="count"></param> /// <param name="offset"></param> /// <param name="minBufferCount">Used to create a dynamic buffer with size of Max(count, minBufferCount).</param> public void UploadDataToBuffer <T>(DeviceContextProxy context, IList <T> data, int count, int offset, int minBufferCount = default(int)) where T : struct { ElementCount = count; int newSizeInBytes = StructureSize * count; if (count == 0) { return; } else if (buffer == null || Capacity < newSizeInBytes) { RemoveAndDispose(ref buffer); var buffdesc = new BufferDescription() { BindFlags = this.BindFlags, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = this.OptionFlags, SizeInBytes = StructureSize * System.Math.Max(count, minBufferCount), StructureByteStride = StructureSize, Usage = ResourceUsage.Dynamic }; Capacity = buffdesc.SizeInBytes; CapacityUsed = 0; buffer = Collect(new Buffer(context, buffdesc)); } if (CapacityUsed + newSizeInBytes <= Capacity && !context.IsDeferred) { Offset = CapacityUsed; context.MapSubresource(this.buffer, MapMode.WriteNoOverwrite, MapFlags.None, out DataStream stream); using (stream) { stream.Position = Offset; stream.WriteRange(data.GetArrayByType(), offset, count); } context.UnmapSubresource(this.buffer, 0); CapacityUsed += newSizeInBytes; } else { context.MapSubresource(this.buffer, MapMode.WriteDiscard, MapFlags.None, out DataStream stream); using (stream) { stream.WriteRange(data.GetArrayByType(), offset, count); } context.UnmapSubresource(this.buffer, 0); Offset = CapacityUsed = 0; } }
/// <summary> /// <see cref="IElementsBufferProxy.UploadDataToBuffer{T}(DeviceContextProxy, IList{T}, int, int)"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="data"></param> /// <param name="count"></param> /// <param name="offset"></param> public void UploadDataToBuffer <T>(DeviceContextProxy context, IList <T> data, int count, int offset) where T : struct { ElementCount = count; if (buffer == null || buffer.Description.SizeInBytes < StructureSize * count) { RemoveAndDispose(ref buffer); var buffdesc = new BufferDescription() { BindFlags = this.BindFlags, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = this.OptionFlags, SizeInBytes = StructureSize * count, StructureByteStride = StructureSize, Usage = ResourceUsage.Dynamic }; buffer = Collect(SDX11::Buffer.Create(context, data.GetArrayByType(), buffdesc)); } else { DataStream stream; context.MapSubresource(this.buffer, MapMode.WriteDiscard, MapFlags.None, out stream); using (stream) { stream.WriteRange(data.GetArrayByType(), offset, count); context.UnmapSubresource(this.buffer, 0); } } }
/// <summary> /// <see cref="IElementsBufferProxy.UploadDataToBuffer{T}(DeviceContextProxy, IList{T}, int, int, int)"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="data"></param> /// <param name="count">Data Count</param> /// <param name="offset"></param> /// <param name="minBufferCount">Used to create a dynamic buffer with size of Max(count, minBufferCount).</param> public void UploadDataToBuffer <T>(DeviceContextProxy context, IList <T> data, int count, int offset, int minBufferCount = default(int)) where T : unmanaged { ElementCount = count; var newSizeInBytes = StructureSize * count; if (count == 0) { return; } EnsureBufferCapacity(context, ElementCount, minBufferCount); var mapMode = MapMode.WriteNoOverwrite; if (CapacityUsed + newSizeInBytes <= Capacity && !context.IsDeferred && CanOverwrite) { Offset = CapacityUsed; CapacityUsed += newSizeInBytes; } else { mapMode = MapMode.WriteDiscard; Offset = CapacityUsed = 0; } var dataArray = data.GetArrayByType(); var dataBox = context.MapSubresource(this.buffer, 0, mapMode, MapFlags.None); UnsafeHelper.Write(dataBox.DataPointer + Offset, dataArray, offset, count); context.UnmapSubresource(this.buffer, 0); }
/// <summary> /// Uploads the data pointer to buffer. /// </summary> /// <param name="context">The context.</param> /// <param name="data">The data.</param> /// <param name="byteCount">The count by bytes.</param> /// <param name="byteOffset">The offset by bytes.</param> /// <param name="minBufferSizeByBytes">The minimum buffer count by bytes.</param> public unsafe void UploadDataToBuffer(DeviceContextProxy context, System.IntPtr data, int byteCount, int byteOffset, int minBufferSizeByBytes = default(int)) { ElementCount = byteCount / StructureSize; var newSizeInBytes = byteCount; if (byteCount == 0) { return; } EnsureBufferCapacity(context, ElementCount, minBufferSizeByBytes / StructureSize); var mapMode = MapMode.WriteNoOverwrite; if (CapacityUsed + newSizeInBytes <= Capacity && !context.IsDeferred && CanOverwrite) { Offset = CapacityUsed; CapacityUsed += newSizeInBytes; } else { mapMode = MapMode.WriteDiscard; Offset = CapacityUsed = 0; } var dataBox = context.MapSubresource(this.buffer, 0, mapMode, MapFlags.None); UnsafeHelper.Write(dataBox.DataPointer + Offset, data, byteOffset, byteCount); context.UnmapSubresource(this.buffer, 0); }
/// <summary> /// Maps the buffer. Make sure to call <see cref="EnsureBufferCapacity(DeviceContextProxy, int, int)"/> to make sure buffer has enough space /// </summary> /// <param name="context">The context.</param> /// <param name="action">The action.</param> public void MapBuffer(DeviceContextProxy context, System.Action <DataBox> action) { var dataBox = context.MapSubresource(this.buffer, 0, MapMode.WriteDiscard, MapFlags.None); action(dataBox); context.UnmapSubresource(this.buffer, 0); Offset = CapacityUsed = 0; }
/// <summary> /// Maps the buffer. Make sure to call <see cref="EnsureBufferCapacity(DeviceContextProxy, int, int)"/> to make sure buffer has enough space /// </summary> /// <param name="context">The context.</param> /// <param name="action">The action.</param> public void MapBuffer(DeviceContextProxy context, System.Action <DataStream> action) { context.MapSubresource(this.buffer, MapMode.WriteDiscard, MapFlags.None, out DataStream stream); using (stream) { action(stream); } context.UnmapSubresource(this.buffer, 0); Offset = CapacityUsed = 0; }
private int ReadCount(string src, DeviceContextProxy context, UnorderedAccessView uav) { context.CopyStructureCount(particleCountStaging, 0, uav); DataStream ds; var db = context.MapSubresource(particleCountStaging, MapMode.Read, MapFlags.None, out ds); int CurrentParticleCount = ds.ReadInt(); #if OUTPUTDEBUGGING Debug.WriteLine("{0}: {1}", src, CurrentParticleCount); #endif context.UnmapSubresource(particleCountStaging, 0); return(CurrentParticleCount); }
private int ReadCount(string src, DeviceContextProxy context, UnorderedAccessView uav) { context.CopyStructureCount(particleCountStaging, 0, uav); var db = context.MapSubresource(particleCountStaging, MapMode.Read, MapFlags.None); var CurrentParticleCount = 0; CurrentParticleCount = UnsafeHelper.Read <int>(db.DataPointer); #if OUTPUTDEBUGGING Debug.WriteLine("{0}: {1}", src, CurrentParticleCount); #endif context.UnmapSubresource(particleCountStaging, 0); return(CurrentParticleCount); }
private int ReadCount(string src, DeviceContextProxy context, UnorderedAccessView uav) { context.CopyStructureCount(particleCountStaging, 0, uav); var db = context.MapSubresource(particleCountStaging, MapMode.Read, MapFlags.None); var currentParticleCount = UnsafeHelper.Read <int>(db.DataPointer); #if OUTPUTDEBUGGING if (logger.IsEnabled(LogLevel.Debug)) { logger.LogDebug("{}: {}", src, currentParticleCount); } #endif context.UnmapSubresource(particleCountStaging, 0); return(currentParticleCount); }
public void UploadDataToBuffer <T>(DeviceContextProxy context, T[] data, int count, int offset) where T : struct { if (bufferDesc.Usage == ResourceUsage.Dynamic) { context.MapSubresource(buffer, 0, MapMode.WriteDiscard, MapFlags.None, out DataStream stream); using (stream) { stream.WriteRange(data, offset, count); } context.UnmapSubresource(buffer, 0); } else { context.UpdateSubresource(data, buffer); } }
public void UploadDataToBuffer <T>(DeviceContextProxy context, T[] data, int count, int offset) where T : unmanaged { lock (lockObj) { if (bufferDesc.Usage == ResourceUsage.Dynamic) { Debug.Assert(count * UnsafeHelper.SizeOf <T>() <= buffer.Description.SizeInBytes); var dataBox = context.MapSubresource(buffer, 0, MapMode.WriteDiscard, MapFlags.None); UnsafeHelper.Write(dataBox.DataPointer, data, offset, count); context.UnmapSubresource(buffer, 0); } else { context.UpdateSubresource(data, buffer); } } }
public void UploadDataToBuffer(DeviceContextProxy context, Action <DataBox> writeFuc) { lock (lockObj) { if (bufferDesc.Usage == ResourceUsage.Dynamic) { var dataBox = context.MapSubresource(buffer, 0, MapMode.WriteDiscard, MapFlags.None); writeFuc?.Invoke(dataBox); context.UnmapSubresource(buffer, 0); } else { #if DEBUG throw new Exception("Constant buffer must be dynamic to use this function."); #endif } } }
public void UploadDataToBuffer(DeviceContextProxy context, Action <DataStream> writeFuc) { if (bufferDesc.Usage == ResourceUsage.Dynamic) { context.MapSubresource(buffer, 0, MapMode.WriteDiscard, MapFlags.None, out DataStream stream); using (stream) { writeFuc?.Invoke(stream); } context.UnmapSubresource(buffer, 0); } else { #if DEBUG throw new Exception("Constant buffer must be dynamic to use this function."); #endif } }
public void UploadToBuffer(IBufferProxy buffer, DeviceContextProxy context) { if (buffer.StructureSize == SizeInBytes) { context.MapSubresource(buffer.Buffer, 0, MapMode.WriteDiscard, MapFlags.None, out DataStream stream); using (stream) { stream.WriteRange(Lights, 0, Lights.Length); stream.Write(AmbientLight); stream.Write(LightCount); } context.UnmapSubresource(buffer.Buffer, 0); } else { #if DEBUG throw new ArgumentException("Buffer type or size do not match the model requirement"); #endif } }
public void UploadToBuffer(IBufferProxy buffer, DeviceContextProxy context) { if (buffer.StructureSize == SizeInBytes) { var dataBox = context.MapSubresource(buffer.Buffer, 0, MapMode.WriteDiscard, MapFlags.None); if (dataBox.IsEmpty) { return; } var ptr = UnsafeHelper.Write(dataBox.DataPointer, Lights, 0, Lights.Length); ptr = UnsafeHelper.Write(ptr, AmbientLight); ptr = UnsafeHelper.Write(ptr, LightCount); ptr = UnsafeHelper.Write(ptr, HasEnvironmentMap ? 1 : 0); ptr = UnsafeHelper.Write(ptr, EnvironmentMapMipLevels); context.UnmapSubresource(buffer.Buffer, 0); } else { #if DEBUG throw new ArgumentException("Buffer type or size do not match the model requirement"); #endif } }
private void ProcessMonoMask(DeviceContextProxy context, bool isMono, ref PointerInfo info, out int width, out int height, out int left, out int top) { var deskWidth = sharedDescription.Width; var deskHeight = sharedDescription.Height; var givenLeft = info.Position.X; var givenTop = info.Position.Y; if (givenLeft < 0) { width = givenLeft + info.ShapeInfo.Width; } else if (givenLeft + info.ShapeInfo.Width > deskWidth) { width = deskWidth - givenLeft; } else { width = info.ShapeInfo.Width; } if (isMono) { info.ShapeInfo.Height /= 2; } if (givenTop < 0) { height = givenTop + info.ShapeInfo.Height; } else if (givenTop + info.ShapeInfo.Height > deskHeight) { height = deskHeight - givenTop; } else { height = info.ShapeInfo.Height; } if (isMono) { info.ShapeInfo.Height *= 2; } left = givenLeft < 0 ? 0 : givenLeft; top = givenTop < 0 ? 0 : givenTop; stageTextureDesc.Width = width; stageTextureDesc.Height = height; if (initBuffer.Length != width * height * BPP) { initBuffer = new byte[width * height * BPP]; } if (copyBuffer == null || stageTextureDesc.Width != width || stageTextureDesc.Height != height) { RemoveAndDispose(ref copyBuffer); copyBuffer = new Texture2D(context, stageTextureDesc); } context.CopySubresourceRegion(SharedTexture, 0, new global::SharpDX.Direct3D11.ResourceRegion(left, top, 0, left + width, top + height, 1), copyBuffer, 0); var dataBox = context.MapSubresource(copyBuffer, 0, global::SharpDX.Direct3D11.MapMode.Read, global::SharpDX.Direct3D11.MapFlags.None); #region process unsafe // Call unmanaged code { fixed(byte *initBufferPtr = initBuffer) { var initBuffer32 = (uint *)initBufferPtr; var desktop32 = (uint *)dataBox.DataPointer; var desktopPitchInPixels = dataBox.RowPitch / sizeof(int); var skipX = (givenLeft < 0) ? (uint)(-1 * givenLeft) : 0; var skipY = (givenTop < 0) ? (uint)(-1 * givenTop) : 0; if (isMono) { for (var row = 0; row < stageTextureDesc.Height; ++row) { // Set mask byte Mask = 0x80; Mask = (byte)(Mask >> (byte)(skipX % 8)); for (var col = 0; col < stageTextureDesc.Width; ++col) { // Get masks using appropriate offsets var AndMask = (byte)(info.PtrShapeBuffer[((col + skipX) / 8) + ((row + skipY) * (info.ShapeInfo.Pitch))] & Mask); var XorMask = (byte)(info.PtrShapeBuffer[((col + skipX) / 8) + ((row + skipY + (info.ShapeInfo.Height / 2)) * (info.ShapeInfo.Pitch))] & Mask); var AndMask32 = (AndMask > 0) ? 0xFFFFFFFF : 0xFF000000; var XorMask32 = (XorMask > 0) ? (uint)0x00FFFFFF : 0x00000000; // Set new pixel initBuffer32[(row * stageTextureDesc.Width) + col] = (desktop32[(row * desktopPitchInPixels) + col] & AndMask32) ^ XorMask32; // Adjust mask if (Mask == 0x01) { Mask = 0x80; } else { Mask = (byte)(Mask >> 1); } } } } else { fixed(byte *shapeBufferPtr = info.PtrShapeBuffer) { var Buffer32 = (uint *)shapeBufferPtr; // Iterate through pixels for (var row = 0; row < stageTextureDesc.Height; ++row) { for (var col = 0; col < stageTextureDesc.Width; ++col) { // Set up mask var MaskVal = 0xFF000000 & Buffer32[(col + skipX) + ((row + skipY) * (info.ShapeInfo.Pitch / sizeof(uint)))]; if (MaskVal > 0) { // Mask was 0xFF initBuffer32[(row * stageTextureDesc.Width) + col] = (desktop32[(row * desktopPitchInPixels) + col] ^ Buffer32[(col + skipX) + ((row + skipY) * (info.ShapeInfo.Pitch / sizeof(uint)))]) | 0xFF000000; } else { // Mask was 0x00 initBuffer32[(row * stageTextureDesc.Width) + col] = Buffer32[(col + skipX) + ((row + skipY) * (info.ShapeInfo.Pitch / sizeof(uint)))] | 0xFF000000; } } } } } } } #endregion context.UnmapSubresource(copyBuffer, 0); }
public DataBox Map(DeviceContextProxy context) { Monitor.Enter(lockObj); return(context.MapSubresource(buffer, 0, MapMode.WriteDiscard, MapFlags.None)); }
public DataStream MapToStream(DeviceContextProxy context) { Monitor.Enter(lockObj); context.MapSubresource(buffer, 0, MapMode.WriteDiscard, MapFlags.None, out var stream); return(stream); }
public DataStream Map(DeviceContextProxy context) { context.MapSubresource(buffer, 0, MapMode.WriteDiscard, MapFlags.None, out DataStream stream); return(stream); }
public bool ProcessCursor(ref PointerInfo pointer, DeviceContextProxy context, out Vector4 rect) { var width = 0; var height = 0; var left = pointer.Position.X; var top = pointer.Position.Y; switch (pointer.ShapeInfo.Type) { case (int)OutputDuplicatePointerShapeType.Color: width = pointer.ShapeInfo.Width; height = pointer.ShapeInfo.Height; break; case (int)OutputDuplicatePointerShapeType.Monochrome: ProcessMonoMask(context, true, ref pointer, out width, out height, out left, out top); break; case (int)OutputDuplicatePointerShapeType.MaskedColor: ProcessMonoMask(context, false, ref pointer, out width, out height, out left, out top); break; default: rect = Vector4.Zero; //Invalid cursor type return(false); } rect = new Vector4(pointer.Position.X, pointer.Position.Y, width, height); var rowPitch = pointer.ShapeInfo.Type == (int)OutputDuplicatePointerShapeType.Color ? pointer.ShapeInfo.Pitch : width * BPP; var slicePitch = 0; if (pointerResource == null || currentType != pointer.ShapeInfo.Type || pointerTexDesc.Width != width || pointerTexDesc.Height != height) { RemoveAndDispose(ref pointerResource); pointerTexDesc.Width = width; pointerTexDesc.Height = height; currentType = pointer.ShapeInfo.Type; global::SharpDX.Utilities.Pin(pointer.ShapeInfo.Type == (int)OutputDuplicatePointerShapeType.Color ? pointer.PtrShapeBuffer : initBuffer, ptr => { pointerResource = new ShaderResourceViewProxy(context, new Texture2D(context, pointerTexDesc, new[] { new DataBox(ptr, rowPitch, slicePitch) })); }); pointerResource.CreateView(pointerSRVDesc); #if OUTPUTDETAIL Console.WriteLine("Create new cursor texture. Type = " + pointer.ShapeInfo.Type); #endif } else { var dataBox = context.MapSubresource(pointerResource.Resource, 0, global::SharpDX.Direct3D11.MapMode.WriteDiscard, global::SharpDX.Direct3D11.MapFlags.None); if (pointer.ShapeInfo.Type == (int)OutputDuplicatePointerShapeType.Color) { #if OUTPUTDETAIL Console.WriteLine("Reuse existing cursor texture for Color."); #endif unsafe { var row = pointer.ShapeInfo.Height; var sourceCounter = 0; var target32 = (byte *)dataBox.DataPointer; for (var i = 0; i < row; ++i) { var targetCounter = i * dataBox.RowPitch; for (var j = 0; j < pointer.ShapeInfo.Pitch; ++j) { target32[targetCounter++] = pointer.PtrShapeBuffer[sourceCounter++]; } } } } else { #if OUTPUTDETAIL Console.WriteLine("Reuse existing cursor texture for Mono and Mask."); #endif unsafe // Call unmanaged code { var target32 = (byte *)dataBox.DataPointer; for (var i = 0; i < initBuffer.Length; ++i) { target32[i] = initBuffer[i]; } } } context.UnmapSubresource(pointerResource.Resource, 0); } return(true); }