Beispiel #1
0
 public PrimitiveBuffer(BufferFrequencies frequency, BufferUsages usage, out uint bufferId,
                        out uint indexId, uint bufferCapacity = 0, uint indexCapacity = 0) :
     this(frequency, usage, bufferCapacity, indexCapacity)
 {
     bufferId = this.bufferId;
     indexId  = this.indexId;
 }
Beispiel #2
0
        public static IVertexBuffer New(VideoTypes videoType, IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, VertexBufferTopologys topology, float[] vertices, int[] indices)
        {
            IVertexBuffer api = null;

            #if WIN32
            if (videoType == VideoTypes.D3D9) api = new D3D9.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            #endif

            #if WIN32 || WINRT || WP8
            if (videoType == VideoTypes.D3D11) api = new D3D11.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            #endif

            #if WIN32 || OSX || LINUX || iOS || ANDROID || NaCl
            if (videoType == VideoTypes.OpenGL) api = new OpenGL.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            #endif

            #if XNA
            if (videoType == VideoTypes.XNA) api = new XNA.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            #endif

            #if VITA
            if (videoType == VideoTypes.Vita) api = new Vita.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            #endif

            if (api == null) Debug.ThrowError("VertexBufferAPI", "Unsuported InputType: " + videoType);
            return api;
        }
Beispiel #3
0
        // Note that buffer capacity is given in bytes while index capacity is given in unsigned shorts.
        public unsafe PrimitiveBuffer(BufferFrequencies frequency, BufferUsages usage, uint bufferCapacity = 0,
                                      uint indexCapacity = 0)
        {
            this.usage = (uint)frequency + (uint)usage;

            GLUtilities.GenerateBuffers(out bufferId, out indexId);

            buffer      = new byte[bufferCapacity];
            indexBuffer = new ushort[indexCapacity];
            maxIndex    = -1;

            if (bufferCapacity > 0)
            {
                allocatedBufferCapacity = bufferCapacity;

                glBindBuffer(GL_ARRAY_BUFFER, bufferId);
                glBufferData(GL_ARRAY_BUFFER, bufferCapacity, (void *)0, this.usage);
            }

            if (indexCapacity > 0)
            {
                allocatedIndexCapacity = sizeof(ushort) * indexCapacity;

                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId);
                glBufferData(GL_ELEMENT_ARRAY_BUFFER, allocatedIndexCapacity, (void *)0, this.usage);
            }
        }
Beispiel #4
0
        internal HostBuffer(
            Device logicalDevice,
            Pool memoryPool,
            BufferUsages usages,
            long size)
        {
            if (logicalDevice == null)
            {
                throw new ArgumentNullException(nameof(logicalDevice));
            }
            if (memoryPool == null)
            {
                throw new ArgumentNullException(nameof(memoryPool));
            }
            this.size = size;

            //Create the buffer
            buffer = logicalDevice.CreateBuffer(new BufferCreateInfo(
                                                    size: size,
                                                    usages: usages,
                                                    flags: BufferCreateFlags.None,
                                                    sharingMode: SharingMode.Exclusive
                                                    ));
            //Allocate memory for this buffer
            memory = memoryPool.AllocateAndBind(buffer, Chunk.Location.Host);
        }
Beispiel #5
0
        public VKBuffer(
            string name, Graphics graphics, long count, BufferUsages usages, MemoryProperties memoryProperties,
            BufferCreateFlags flags  = BufferCreateFlags.None, SharingMode sharingMode = SharingMode.Exclusive,
            int[] queueFamilyIndices = null
            )
        {
            Name     = name;
            Graphics = graphics;
            Count    = count;
            Size     = count * Interop.SizeOf <T>();
            Usages   = usages;
            Buffer   = Graphics.Device.CreateBuffer(new BufferCreateInfo(
                                                        size: Size,
                                                        usages: usages,
                                                        flags: flags,
                                                        sharingMode: sharingMode,
                                                        queueFamilyIndices: queueFamilyIndices
                                                        ));
            var reqs = Buffer.GetMemoryRequirements();

            DeviceMemory = Graphics.Device.AllocateMemory(new MemoryAllocateInfo(
                                                              reqs.Size,
                                                              graphics.GetMemoryTypeIndex(reqs.MemoryTypeBits, memoryProperties)
                                                              ));
            Buffer.BindMemory(DeviceMemory);
        }
 internal static DeviceBuffer UploadData <T>(
     ReadOnlyMemory <T> data,
     RenderScene scene,
     BufferUsages usages) where T : struct
 {
     return(UploadData <T>(data.Span, scene, usages));
 }
        internal DeviceBuffer(
            Device logicalDevice,
            Pool memoryPool,
            BufferUsages usages,
            long size)
        {
            if (logicalDevice == null)
            {
                throw new ArgumentNullException(nameof(logicalDevice));
            }
            if (memoryPool == null)
            {
                throw new ArgumentNullException(nameof(memoryPool));
            }

            this.size = size;

            //Create the buffer
            buffer = logicalDevice.CreateBuffer(new BufferCreateInfo(
                                                    size: size,
                                                    //Adding 'TransferDst' otherwise we can never copy data to it
                                                    usages: usages | BufferUsages.TransferDst,
                                                    flags: BufferCreateFlags.None,
                                                    sharingMode: SharingMode.Exclusive
                                                    ));
            //Bind memory from our pool to this buffer
            memory = memoryPool.AllocateAndBind(buffer, Chunk.Location.Device);
        }
Beispiel #8
0
 public Texture2D(DisposableI parent, string fileName, bool generateMipmaps, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent)
 {
                 #if SILVERLIGHT
     Image.New(fileName, false,
               delegate(object sender, bool succeeded)
     {
         if (succeeded)
         {
             var image = (Image)sender;
             init(parent, null, image, image.Size.Width, image.Size.Height, generateMipmaps, MultiSampleTypes.None, SurfaceFormats.Unknown, RenderTargetUsage.PlatformDefault, usage, false, loadedCallback);
         }
         else
         {
             FailedToLoad = true;
             Dispose();
             if (loadedCallback != null)
             {
                 loadedCallback(this, false);
             }
         }
     });
                 #else
     init(parent, fileName, null, 0, 0, generateMipmaps, MultiSampleTypes.None, SurfaceFormats.Unknown, RenderTargetUsage.PlatformDefault, usage, false, loadedCallback);
                 #endif
 }
Beispiel #9
0
        public static ITexture2D New(VideoTypes videoType, IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
        {
            ITexture2D api = null;

            #if WIN32
            if (videoType == VideoTypes.D3D9) api = new D3D9.Texture2D(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, loadedCallback);
            #endif

            #if WIN32 || WINRT || WP8
            if (videoType == VideoTypes.D3D11) api = new D3D11.Texture2D(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, loadedCallback);
            #endif

            #if WIN32 || OSX || LINUX || iOS || ANDROID || NaCl
            if (videoType == VideoTypes.OpenGL) api = new OpenGL.Texture2D(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, loadedCallback);
            #endif

            #if XNA
            if (videoType == VideoTypes.XNA) api = new XNA.Texture2D(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, loadedCallback);
            #endif

            #if VITA
            if (videoType == VideoTypes.Vita) api = new Vita.Texture2D(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, loadedCallback);
            #endif

            if (api == null) Debug.ThrowError("Texture2DAPI", "Unsuported InputType: " + videoType);
            return api;
        }
Beispiel #10
0
 protected IVertexBuffer(IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, float[] vertices)
     : base(parent)
 {
     this.usage = usage;
     vertexByteSize = bufferLayoutDesc.ByteSize;
     vertexFloatArraySize = bufferLayoutDesc.FloatCount;
     vertexCount = vertices.Length / vertexFloatArraySize;
 }
Beispiel #11
0
 protected IVertexBuffer(IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, float[] vertices)
     : base(parent)
 {
     this.usage           = usage;
     vertexByteSize       = bufferLayoutDesc.ByteSize;
     vertexFloatArraySize = bufferLayoutDesc.FloatCount;
     vertexCount          = vertices.Length / vertexFloatArraySize;
 }
Beispiel #12
0
 protected IIndexBuffer(IDisposableResource parent, BufferUsages usage, int[] indices)
     : base(parent)
 {
     this.usage    = usage;
     indexByteSize = sizeof(short);
     indexCount    = indices.Length;
     _32BitIndices = indexCount > short.MaxValue;
     indexByteSize = _32BitIndices ? sizeof(int) : sizeof(short);
 }
Beispiel #13
0
 protected IIndexBuffer(IDisposableResource parent, BufferUsages usage, int[] indices)
     : base(parent)
 {
     this.usage = usage;
     indexByteSize = sizeof(short);
     indexCount = indices.Length;
     _32BitIndices = indexCount > short.MaxValue;
     indexByteSize = _32BitIndices ? sizeof(int) : sizeof(short);
 }
Beispiel #14
0
        public void CreateBuffer(int sizeInBytes, string name,
                                 BufferUsages usage, SharingMode sm, int [] famIndexes = null)
        {
            BufferCreateInfo bci = new BufferCreateInfo(sizeInBytes,
                                                        usage, BufferCreateFlags.None,
                                                        sm, famIndexes);

            Buffer buf = mLogical.CreateBuffer(bci);

            mBuffers.Add(name, buf);
        }
 internal static DeviceBuffer UploadData <T>(
     ReadOnlySpan <T> data,
     RenderScene scene,
     BufferUsages usages) where T : struct
 {
     return(UploadData <T>(
                data,
                scene.LogicalDevice,
                scene.MemoryPool,
                usages,
                scene.StagingBuffer,
                scene.Executor));
 }
Beispiel #16
0
        public unsafe IndexBuffer(IDisposableResource parent, BufferUsages bufferUsage, int[] indices)
            : base(parent, bufferUsage, indices)
        {
            try
            {
                video = parent.FindParentOrSelfWithException <Video>();

                uint vPtr = 0;
                GL.GenBuffers(1, &vPtr);
                indexBuffer = vPtr;
                if (indexBuffer == 0)
                {
                    Debug.ThrowError("IndexBuffer", "Failed to create IndexBuffer");
                }

                GL.BindBuffer(GL.ELEMENT_ARRAY_BUFFER, indexBuffer);
                if (_32BitIndices)
                {
                    fixed(int *indicesPtr = indices)
                    {
                        var bufferSize = new IntPtr(indexByteSize * indexCount);

                        GL.BufferData(GL.ELEMENT_ARRAY_BUFFER, bufferSize, indicesPtr, GL.STATIC_DRAW);
                    }
                }
                else
                {
                    var indices16Bit = new short[indices.Length];
                    for (int i = 0; i != indices.Length; ++i)
                    {
                        indices16Bit[i] = (short)indices[i];
                    }

                    fixed(short *indicesPtr = indices16Bit)
                    {
                        var bufferSize = new IntPtr(indexByteSize * indexCount);

                        GL.BufferData(GL.ELEMENT_ARRAY_BUFFER, bufferSize, indicesPtr, GL.STATIC_DRAW);
                    }
                }

                Video.checkForError();
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Beispiel #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BufferCreateInfo"/> structure.
 /// </summary>
 /// <param name="size">The size in bytes of the buffer to be created.</param>
 /// <param name="usages">The bitmask describing the allowed usages of the buffer.</param>
 /// <param name="flags">Additional parameters of the buffer.</param>
 /// <param name="sharingMode">
 /// The sharing mode of the buffer when it will be accessed by multiple queue families.
 /// </param>
 /// <param name="queueFamilyIndices">
 /// A list of queue families that will access this buffer (ignored if <see
 /// cref="SharingMode"/> is not <see cref="VulkanCore.SharingMode.Concurrent"/>).
 /// </param>
 /// <param name="next">
 /// Is <see cref="IntPtr.Zero"/> or a pointer to an extension-specific structure.
 /// </param>
 public BufferCreateInfo(
     long size,
     BufferUsages usages,
     BufferCreateFlags flags  = 0,
     SharingMode sharingMode  = SharingMode.Exclusive,
     int[] queueFamilyIndices = null,
     IntPtr next = default(IntPtr))
 {
     Next               = next;
     Size               = size;
     Usages             = usages;
     Flags              = flags;
     SharingMode        = sharingMode;
     QueueFamilyIndices = queueFamilyIndices;
 }
Beispiel #18
0
        private void init(DisposableI parent, BufferLayoutDescI bufferLayoutDesc, BufferUsages bufferUsage, VertexBufferTopologys vertexBufferTopology, float[] vertices, int[] indices)
        {
            try
            {
                video    = parent.FindParentOrSelfWithException <Video>();
                Topology = vertexBufferTopology;

                var format = new G.VertexFormat[bufferLayoutDesc.Elements.Count];
                for (int i = 0; i != bufferLayoutDesc.Elements.Count; ++i)
                {
                    switch (bufferLayoutDesc.Elements[i].FloatCount)
                    {
                    case 1: format[i] = G.VertexFormat.Float; break;

                    case 2: format[i] = G.VertexFormat.Float2; break;

                    case 3: format[i] = G.VertexFormat.Float3; break;

                    case 4: format[i] = G.VertexFormat.Float4; break;
                    }
                }

                if (indices != null && indices.Length != 0)
                {
                    vertexBuffer = new G.VertexBuffer(vertexCount, indices.Length, format);

                    indexCount = indices.Length;
                    var indicesShort = new ushort[indexCount];
                    for (int i = 0; i != indexCount; ++i)
                    {
                        indicesShort[i] = (ushort)indices[i];
                    }
                    vertexBuffer.SetIndices(indicesShort);
                }
                else
                {
                    vertexBuffer = new G.VertexBuffer(vertexCount, format);
                }
                vertexBuffer.SetVertices(vertices);
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Beispiel #19
0
 public IndexBuffer(IDisposableResource parent, BufferUsages usage, int[] indices)
     : base(parent, usage, indices)
 {
     try
     {
         var video = parent.FindParentOrSelfWithException<Video>();
         com = new IndexBufferCom(video.com);
         var bufferUsage = (usage == BufferUsages.Write) ? REIGN_D3D11_USAGE.DYNAMIC : REIGN_D3D11_USAGE.DEFAULT;
         var cpuUsage = (usage == BufferUsages.Write) ? REIGN_D3D11_CPU_ACCESS_FLAG.WRITE : (REIGN_D3D11_CPU_ACCESS_FLAG)0;
         var error = com.Init(indices, indexCount, indexByteSize, bufferUsage, cpuUsage, _32BitIndices);
         if (error == IndexBufferErrors.Buffer) Debug.ThrowError("IndexBuffer", "Failed to create Buffer");
     }
     catch (Exception e)
     {
         Dispose();
         throw e;
     }
 }
Beispiel #20
0
        public unsafe IndexBuffer(IDisposableResource parent, BufferUsages bufferUsage, int[] indices)
            : base(parent, bufferUsage, indices)
        {
            try
            {
                video = parent.FindParentOrSelfWithException<Video>();

                uint vPtr = 0;
                GL.GenBuffers(1, &vPtr);
                indexBuffer = vPtr;
                if (indexBuffer == 0) Debug.ThrowError("IndexBuffer", "Failed to create IndexBuffer");

                GL.BindBuffer(GL.ELEMENT_ARRAY_BUFFER, indexBuffer);
                if (_32BitIndices)
                {
                    fixed(int* indicesPtr = indices)
                    {
                        var bufferSize = new IntPtr(indexByteSize * indexCount);
                        GL.BufferData(GL.ELEMENT_ARRAY_BUFFER, bufferSize, indicesPtr, GL.STATIC_DRAW);
                    }
                }
                else
                {
                    var indices16Bit = new short[indices.Length];
                    for (int i = 0; i != indices.Length; ++i)
                    {
                        indices16Bit[i] = (short)indices[i];
                    }

                    fixed(short* indicesPtr = indices16Bit)
                    {
                        var bufferSize = new IntPtr(indexByteSize * indexCount);
                        GL.BufferData(GL.ELEMENT_ARRAY_BUFFER, bufferSize, indicesPtr, GL.STATIC_DRAW);
                    }
                }

                Video.checkForError();
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Beispiel #21
0
 public Texture2D(IDisposableResource parent, string filename, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent)
 {
     Image.New(filename, false,
     delegate (object sender, bool succeeded)
     {
         if (succeeded)
         {
             var image = (Image)sender;
             init(parent, image, image.Size.Width, image.Size.Height, generateMipmaps, MultiSampleTypes.None, image.SurfaceFormat, RenderTargetUsage.PlatformDefault, usage, false, loadedCallback);
         }
         else
         {
             FailedToLoad = true;
             Dispose();
             if (loadedCallback != null) loadedCallback(this, false);
         }
     });
 }
Beispiel #22
0
 public IndexBuffer(IDisposableResource parent, BufferUsages usage, int[] indices)
     : base(parent, usage, indices)
 {
     try
     {
         var video = parent.FindParentOrSelfWithException <Video>();
         com = new IndexBufferCom(video.com);
         var error = com.Init(indices, indexCount, indexByteSize, REIGN_D3DUSAGE.WRITEONLY, _32BitIndices);
         if (error == IndexBufferErrors.IndexBuffer)
         {
             Debug.ThrowError("IndexBuffer", "Failed to create IndexBuffer");
         }
     }
     catch (Exception e)
     {
         Dispose();
         throw e;
     }
 }
Beispiel #23
0
        public IndexBuffer(DisposableI parent, BufferUsages bufferUsage, int[] indices)
            : base(parent, bufferUsage, indices)
        {
            try
            {
                video = parent.FindParentOrSelfWithException<Video>();

                #if SILVERLIGHT
                X.IndexElementSize elementSize = X.IndexElementSize.SixteenBits;
                #else
                X.IndexElementSize elementSize = _32BitIndices ? X.IndexElementSize.ThirtyTwoBits : X.IndexElementSize.SixteenBits;
                #endif
                indexBuffer = new X.IndexBuffer(video.Device, elementSize, indices.Length, X.BufferUsage.WriteOnly);
                Update(indices, indexCount);
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Beispiel #24
0
        public IndexBuffer(DisposableI parent, BufferUsages bufferUsage, int[] indices)
            : base(parent, bufferUsage, indices)
        {
            try
            {
                video = parent.FindParentOrSelfWithException <Video>();

                                #if SILVERLIGHT
                X.IndexElementSize elementSize = X.IndexElementSize.SixteenBits;
                                #else
                X.IndexElementSize elementSize = _32BitIndices ? X.IndexElementSize.ThirtyTwoBits : X.IndexElementSize.SixteenBits;
                                #endif
                indexBuffer = new X.IndexBuffer(video.Device, elementSize, indices.Length, X.BufferUsage.WriteOnly);
                Update(indices, indexCount);
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Beispiel #25
0
 public IndexBuffer(IDisposableResource parent, BufferUsages usage, int[] indices)
     : base(parent, usage, indices)
 {
     try
     {
         var video = parent.FindParentOrSelfWithException <Video>();
         com = new IndexBufferCom(video.com);
         var bufferUsage = (usage == BufferUsages.Write) ? REIGN_D3D11_USAGE.DYNAMIC : REIGN_D3D11_USAGE.DEFAULT;
         var cpuUsage    = (usage == BufferUsages.Write) ? REIGN_D3D11_CPU_ACCESS_FLAG.WRITE : (REIGN_D3D11_CPU_ACCESS_FLAG)0;
         var error       = com.Init(indices, indexCount, indexByteSize, bufferUsage, cpuUsage, _32BitIndices);
         if (error == IndexBufferErrors.Buffer)
         {
             Debug.ThrowError("IndexBuffer", "Failed to create Buffer");
         }
     }
     catch (Exception e)
     {
         Dispose();
         throw e;
     }
 }
Beispiel #26
0
        private void init(DisposableI parent, BufferLayoutDescI bufferLayoutDesc, BufferUsages bufferUsage, VertexBufferTopologys vertexBufferTopology, float[] vertices, int[] indices)
        {
            try
            {
                video        = parent.FindParentOrSelfWithException <Video>();
                Topology     = vertexBufferTopology;
                bufferLayout = new BufferLayout(this, null, bufferLayoutDesc, true);

                vertexBuffer = new X.VertexBuffer(video.Device, bufferLayout.layout, vertexCount, X.BufferUsage.WriteOnly);
                Update(vertices, vertexCount);

                if (indices != null && indices.Length != 0)
                {
                    indexBuffer = new IndexBuffer(this, usage, indices);
                }
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Beispiel #27
0
        protected override bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, Loader.LoadedCallbackMethod loadedCallback)
        {
            initSuccess = base.init(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, true, loadedCallback);
            if (!initSuccess) return false;

            try
            {
                if (usage == BufferUsages.Write) Debug.ThrowError("RenderTarget", "Only Textures may be writable");

                if (image != null)
                {
                    width = image.Size.Width;
                    height = image.Size.Height;
                    surfaceFormat = image.SurfaceFormat;
                }

                renderTargetCom = new RenderTargetCom();
                var error = renderTargetCom.Init(video.com, width, height, com, 0, video.surfaceFormat(surfaceFormat), usage == BufferUsages.Read);

                switch (error)
                {
                    case RenderTargetError.RenderTargetView: Debug.ThrowError("RenderTarget", "Failed to create RenderTargetView"); break;
                    case RenderTargetError.StagingTexture: Debug.ThrowError("RenderTarget", "Failed to create Staging Texture"); break;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null) loadedCallback(this, false);
                return false;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
            return true;
        }
Beispiel #28
0
        public static IIndexBuffer New(VideoTypes videoType, IDisposableResource parent, BufferUsages usage, int[] indices)
        {
            IIndexBuffer api = null;

            #if WIN32
            if (videoType == VideoTypes.D3D9) api = new D3D9.IndexBuffer(parent, usage, indices);
            #endif

            #if WIN32 || WINRT || WP8
            if (videoType == VideoTypes.D3D11) api = new D3D11.IndexBuffer(parent, usage, indices);
            #endif

            #if WIN32 || OSX || LINUX || iOS || ANDROID || NaCl
            if (videoType == VideoTypes.OpenGL) api = new OpenGL.IndexBuffer(parent, usage, indices);
            #endif

            #if XNA
            if (videoType == VideoTypes.XNA) api = new XNA.IndexBuffer(parent, usage, indices);
            #endif

            if (api == null) Debug.ThrowError("IndexBufferAPI", "Unsuported InputType: " + videoType);
            return api;
        }
Beispiel #29
0
 public Texture2D(DisposableI parent, string fileName, bool generateMipmaps, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent)
 {
     #if SILVERLIGHT
     Image.New(fileName, false,
     delegate(object sender, bool succeeded)
     {
         if (succeeded)
         {
             var image = (Image)sender;
             init(parent, null, image, image.Size.Width, image.Size.Height, generateMipmaps, MultiSampleTypes.None, SurfaceFormats.Unknown, RenderTargetUsage.PlatformDefault, usage, false, loadedCallback);
         }
         else
         {
             FailedToLoad = true;
             Dispose();
             if (loadedCallback != null) loadedCallback(this, false);
         }
     });
     #else
     init(parent, fileName, null, 0, 0, generateMipmaps, MultiSampleTypes.None, SurfaceFormats.Unknown, RenderTargetUsage.PlatformDefault, usage, false, loadedCallback);
     #endif
 }
        internal static DeviceBuffer UploadData <T>(
            ReadOnlySpan <T> data,
            Device logicalDevice,
            Pool memoryPool,
            BufferUsages usages,
            HostBuffer stagingBuffer,
            TransientExecutor executor) where T : struct
        {
            //First write the data to the staging buffer
            int size = stagingBuffer.Write <T>(data, offset: 0);

            //Then create a device buffer with that size
            DeviceBuffer targetBuffer = new DeviceBuffer(logicalDevice, memoryPool, usages, size);

            //Then copy the data from the staging buffer to the devicebuffer
            executor.ExecuteBlocking(commandBuffer =>
            {
                commandBuffer.CmdCopyBuffer(
                    srcBuffer: stagingBuffer.VulkanBuffer,
                    dstBuffer: targetBuffer.VulkanBuffer,
                    new BufferCopy(size: size, srcOffset: 0, dstOffset: 0));
            });
            return(targetBuffer);
        }
Beispiel #31
0
        public static IIndexBuffer New(VideoTypes videoType, IDisposableResource parent, BufferUsages usage, int[] indices)
        {
            IIndexBuffer api = null;

                        #if WIN32
            if (videoType == VideoTypes.D3D9)
            {
                api = new D3D9.IndexBuffer(parent, usage, indices);
            }
                        #endif

                        #if WIN32 || WINRT || WP8
            if (videoType == VideoTypes.D3D11)
            {
                api = new D3D11.IndexBuffer(parent, usage, indices);
            }
                        #endif

                        #if WIN32 || OSX || LINUX || iOS || ANDROID || NaCl
            if (videoType == VideoTypes.OpenGL)
            {
                api = new OpenGL.IndexBuffer(parent, usage, indices);
            }
                        #endif

                        #if XNA
            if (videoType == VideoTypes.XNA)
            {
                api = new XNA.IndexBuffer(parent, usage, indices);
            }
                        #endif

            if (api == null)
            {
                Debug.ThrowError("IndexBufferAPI", "Unsuported InputType: " + videoType);
            }
            return(api);
        }
Beispiel #32
0
 public static Texture2D New(DisposableI parent, string fileName, bool generateMipmaps, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
 {
     return new Texture2D(parent, fileName, generateMipmaps, usage, loadedCallback);
 }
Beispiel #33
0
 public static IRenderTarget New(IDisposableResource parent, int width, int height, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, DepthStencilFormats depthStencilFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
 {
     return New(VideoAPI.DefaultAPI, parent, (Image)null, width, height, false, multiSampleType, surfaceFormat, depthStencilFormat, renderTargetUsage, usage, loadedCallback);
 }
Beispiel #34
0
 public RenderTarget(DisposableI parent, string fileName, MultiSampleTypes multiSampleType, BufferUsages usage, RenderTargetUsage renderTargetUsage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent, fileName, false, usage, loadedCallback)
 {
 }
Beispiel #35
0
        public static IVertexBuffer New(VideoTypes videoType, IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, VertexBufferTopologys topology, float[] vertices, int[] indices)
        {
            IVertexBuffer api = null;

                        #if WIN32
            if (videoType == VideoTypes.D3D9)
            {
                api = new D3D9.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            }
                        #endif

                        #if WIN32 || WINRT || WP8
            if (videoType == VideoTypes.D3D11)
            {
                api = new D3D11.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            }
                        #endif

                        #if WIN32 || OSX || LINUX || iOS || ANDROID || NaCl
            if (videoType == VideoTypes.OpenGL)
            {
                api = new OpenGL.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            }
                        #endif

                        #if XNA
            if (videoType == VideoTypes.XNA)
            {
                api = new XNA.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            }
                        #endif

                        #if VITA
            if (videoType == VideoTypes.Vita)
            {
                api = new Vita.VertexBuffer(parent, bufferLayoutDesc, usage, topology, vertices, indices);
            }
                        #endif

            if (api == null)
            {
                Debug.ThrowError("VertexBufferAPI", "Unsuported InputType: " + videoType);
            }
            return(api);
        }
Beispiel #36
0
        protected unsafe virtual bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                if (usage == BufferUsages.Read && !isRenderTarget) Debug.ThrowError("Texture2D", "Only RenderTargets may be readable");

                video = parent.FindParentOrSelfWithException<Video>();
                if (isRenderTarget) generateMipmaps = false;

                uint texturesTEMP = 0;
                GL.GenTextures(1, &texturesTEMP);
                Texture = texturesTEMP;
                if (Texture == 0) Debug.ThrowError("Texture2D", "Failed to Generate Texture");

                GL.BindTexture(GL.TEXTURE_2D, Texture);
                if (!generateMipmaps) GL.TexParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR);
                else GL.TexParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR);
                GL.TexParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR);

                hasMipmaps = false;
                if (image != null)
                {
                    var imageType = image.GetType();

                    #if NaCl
                    if (imageType == typeof(ImageBMPC))
                    #else
                    if (imageType == typeof(ImagePNG) || imageType == typeof(ImageJPG) || imageType == typeof(ImageBMP) || imageType == typeof(ImageBMPC))
                    #endif
                    {
                        uint pixelOrder, byteSizes;
                        int format = Video.surfaceFormat(surfaceFormat, out pixelOrder, out byteSizes);
                        var mipmap = image.Mipmaps[0];
                        fixed (byte* data = mipmap.Data)
                        {
                            GL.TexImage2D(GL.TEXTURE_2D, 0, format, mipmap.Size.Width, mipmap.Size.Height, 0, pixelOrder, byteSizes, data);
                            if (generateMipmaps)
                            {
                                hasMipmaps = true;
                                GL.GenerateMipmap(GL.TEXTURE_2D);
                            }
                        }
                    }
                    else if (imageType == typeof(ImageDDS) || imageType == typeof(ImagePVR))
                    {
                        if (image.Mipmaps.Length != Image.Mipmap.CalculateMipmapLvls(image.Size.Width, image.Size.Height))
                        {
                            Debug.ThrowError("Texture2D", "Compressed Textures require full mipmap chain");
                        }
                        GL.TexParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR);
                        hasMipmaps = true;

                        bool compressed = false;
                        uint format = 0;
                        string errorType = null;
                        if (imageType == typeof(ImageDDS))
                        {
                            var imageDDS = (ImageDDS)image;
                            compressed = imageDDS.Compressed;
                            format = imageDDS.FormatGL;
                            errorType = "DDS";
                        }
                        else if (imageType == typeof(ImagePVR))
                        {
                            var imagePVR = (ImagePVR)image;
                            compressed = imagePVR.Compressed;
                            format = imagePVR.FormatGL;
                            errorType = "PVR";
                        }

                        if (compressed)
                        {
                            for (int i = 0; i < image.Mipmaps.Length; ++i)
                            {
                                var mipmap = image.Mipmaps[i];
                                fixed (byte* data = mipmap.Data)
                                {
                                    // look up:: libtxc_dxtn.so for linux with mesa
                                    GL.CompressedTexImage2D(GL.TEXTURE_2D, i, format, mipmap.Size.Width, mipmap.Size.Height, 0, mipmap.Data.Length, data);
                                }
                            }
                        }
                        else
                        {
                            Debug.ThrowError("Texture2D", "Loading uncompresed " + errorType + " textures not supported");
                        }
                    }

                    Size = image.Size;
                    PixelByteSize = image.CalculatePixelByteSize();
                }
                else
                {
                    //GL.TexImage2D(GL.TEXTURE_2D, 0, Video.surfaceFormat(surfaceFormat), width, height, 0, GL.RGBA, GL.UNSIGNED_BYTE, IntPtr.Zero.ToPointer());
                    uint pixelOrder, byteSizes;
                    int format = Video.surfaceFormat(surfaceFormat, out pixelOrder, out byteSizes);
                    GL.TexImage2D(GL.TEXTURE_2D, 0, format, width, height, 0, pixelOrder, byteSizes, IntPtr.Zero.ToPointer());
                    if (generateMipmaps)
                    {
                        hasMipmaps = true;
                        GL.GenerateMipmap(GL.TEXTURE_2D);
                    }
                    Size = new Size2(width, height);
                    PixelByteSize = Image.CalculatePixelByteSize((surfaceFormat == SurfaceFormats.Defualt ? Video.DefaultSurfaceFormat() : surfaceFormat), width, height);
                }

                SizeF = Size.ToVector2();

                uint error;
                string errorName;
                if (Video.checkForError(out error, out errorName))
                {
                    Debug.ThrowError("Texture2D", string.Format("{0} {1}: Failed to load/create texture", error, errorName));
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null) loadedCallback(this, false);
                return false;
            }

            if (!isRenderTarget)
            {
                Loaded = true;
                if (loadedCallback != null) loadedCallback(this, true);
            }
            return true;
        }
Beispiel #37
0
        protected virtual bool init(DisposableI parent, string fileName, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, Loader.LoadedCallbackMethod loadedCallback)
        {
            try
            {
                if (usage == BufferUsages.Read && !isRenderTarget) Debug.ThrowError("Texture2D", "Only RenderTargets may be readable");

                video = parent.FindParentOrSelfWithException<Video>();

                if (!isRenderTarget)
                {
                    if (fileName != null || image != null)
                    {
                        #if SILVERLIGHT
                        texture = new X.Texture2D(video.Device, image.Size.Width, image.Size.Height, image.Mipmaps.Length != 0, Video.surfaceFormat(surfaceFormat));
                        for (int i = 0; i != image.Mipmaps.Length; ++i)
                        {
                            var mipmap = image.Mipmaps[i];
                            texture.SetData<byte>(i, null, mipmap.Data, 0, mipmap.Data.Length);
                        }
                        #else
                        texture = parent.FindParentOrSelfWithException<RootDisposable>().Content.Load<X.Texture2D>(Streams.StripFileExt(fileName));
                        loadedFromContentManager = true;
                        #endif
                    }
                    else
                    {
                        texture = new X.Texture2D(video.Device, width, height, generateMipmaps, Video.surfaceFormat(surfaceFormat));
                    }

                    Size = new Size2(texture.Width, texture.Height);
                    PixelByteSize = Image.CalculatePixelByteSize(surfaceFormat, texture.Width, texture.Height);
                }
                else
                {
                    Size = new Size2(width, height);
                    PixelByteSize = Image.CalculatePixelByteSize(surfaceFormat, width, height);
                }

                TexelOffset = (1 / Size.ToVector2()) * .5f;
                SizeF = Size.ToVector2();
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null) loadedCallback(this, false);
                return false;
            }

            if (!isRenderTarget)
            {
                Loaded = true;
                if (loadedCallback != null) loadedCallback(this, true);
            }
            return true;
        }
Beispiel #38
0
 public static ITexture2D New(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
 {
     return New(VideoAPI.DefaultAPI, parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, loadedCallback);
 }
Beispiel #39
0
 public static ITexture2D New(IDisposableResource parent, string filename, bool generateMipmaps, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
 {
     return New(VideoAPI.DefaultAPI, parent, filename, 0, 0, generateMipmaps, MultiSampleTypes.None, SurfaceFormats.Defualt, RenderTargetUsage.PlatformDefault, usage, loadedCallback);
 }
Beispiel #40
0
        protected virtual bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, bool lockable, Loader.LoadedCallbackMethod loadedCallback)
        {
            byte[][] mipmaps     = null;
            int[]    mipmapSizes = null, mipmapPitches = null;
            try
            {
                video = parent.FindParentOrSelfWithException <Video>();
                if (isRenderTarget)
                {
                    generateMipmaps = false;
                }

                // load image data
                if (image != null)
                {
                    mipmaps       = new byte[image.Mipmaps.Length][];
                    mipmapSizes   = new int[image.Mipmaps.Length];
                    mipmapPitches = new int[image.Mipmaps.Length];
                    for (int i = 0; i != mipmaps.Length; ++i)
                    {
                        var imageMipmap = image.Mipmaps[i];
                        mipmaps[i]       = image.Compressed ? imageMipmap.Data : imageMipmap.SwapRBColorChannels();
                        mipmapSizes[i]   = imageMipmap.Data.Length;
                        mipmapPitches[i] = imageMipmap.Pitch;
                    }

                    Size          = image.Size;
                    surfaceFormat = image.SurfaceFormat;
                    PixelByteSize = image.CalculatePixelByteSize();
                }
                else
                {
                    if (width == 0 || height == 0)
                    {
                        Debug.ThrowError("Texture2D", "Width or Height cannot be 0");
                    }
                    Size          = new Size2(width, height);
                    PixelByteSize = Image.CalculatePixelByteSize((surfaceFormat == SurfaceFormats.Defualt ? Video.DefaultSurfaceFormat() : surfaceFormat), width, height);
                }
                TexelOffset = (1 / Size.ToVector2()) * .5f;
                SizeF       = Size.ToVector2();

                // init texture
                REIGN_D3DUSAGE nativeUsage = isRenderTarget ? REIGN_D3DUSAGE.RENDERTARGET : REIGN_D3DUSAGE.NONE;
                REIGN_D3DPOOL  nativePool  = (mipmaps != null && !video.Caps.ExDevice) ? REIGN_D3DPOOL.MANAGED : REIGN_D3DPOOL.DEFAULT;
                if (usage == BufferUsages.Read)
                {
                    if (!isRenderTarget)
                    {
                        Debug.ThrowError("Texture2D", "Only RenderTargets may be readable");
                    }
                    // NOTE: Staging texture and states will be created in the RenderTarget
                }
                if (usage == BufferUsages.Write)
                {
                    if (mipmaps != null)
                    {
                        if (video.Caps.ExDevice)
                        {
                            nativeUsage |= REIGN_D3DUSAGE.DYNAMIC;
                        }
                    }
                    else
                    {
                        nativeUsage |= REIGN_D3DUSAGE.DYNAMIC;
                    }
                }
                com = new Texture2DCom();
                var error = com.Init(video.com, Size.Width, Size.Height, generateMipmaps, mipmaps, mipmapSizes, mipmapPitches, 0, nativePool, nativeUsage, video.surfaceFormat(surfaceFormat), isRenderTarget);

                switch (error)
                {
                case TextureError.Texture: Debug.ThrowError("Texture2D", "Failed to create Texture2D"); break;

                case TextureError.SystemTexture: Debug.ThrowError("Texture2D", "Failed to create System Texture2D"); break;
                }

                if (!video.Caps.ExDevice && nativePool != REIGN_D3DPOOL.MANAGED)
                {
                    LostDevice_image             = image;
                    LostDevice_width             = width;
                    LostDevice_height            = height;
                    LostDevice_generateMipmaps   = generateMipmaps;
                    LostDevice_multiSampleType   = multiSampleType;
                    LostDevice_surfaceFormat     = surfaceFormat;
                    LostDevice_renderTargetUsage = renderTargetUsage;
                    LostDevice_usage             = usage;
                    LostDevice_isRenderTarget    = isRenderTarget;
                    LostDevice_lockable          = lockable;
                    LostDevice_pool = nativePool;
                }

                if (nativePool == REIGN_D3DPOOL.DEFAULT && !video.Caps.ExDevice && !video.deviceReseting)
                {
                    video.DeviceLost  += deviceLost;
                    video.DeviceReset += deviceReset;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return(false);
            }

            if (!isRenderTarget)
            {
                Loaded = true;
                if (loadedCallback != null)
                {
                    loadedCallback(this, true);
                }
            }
            return(true);
        }
Beispiel #41
0
 public Texture2D(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent)
 {
     init(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, false, false, loadedCallback);
 }
Beispiel #42
0
 public Texture2D(IDisposableResource parent, string filename, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent)
 {
     Image.New(filename, false,
               delegate(object sender, bool succeeded)
     {
         if (succeeded)
         {
             var image = (Image)sender;
             init(parent, image, image.Size.Width, image.Size.Height, generateMipmaps, MultiSampleTypes.None, image.SurfaceFormat, RenderTargetUsage.PlatformDefault, usage, false, false, loadedCallback);
         }
         else
         {
             FailedToLoad = true;
             Dispose();
             if (loadedCallback != null)
             {
                 loadedCallback(this, false);
             }
         }
     });
 }
Beispiel #43
0
 public static IIndexBuffer New(IDisposableResource parent, BufferUsages usage, int[] indices)
 {
     return(New(VideoAPI.DefaultAPI, parent, usage, indices));
 }
Beispiel #44
0
 public Texture2D(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent)
 {
     init(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, false, loadedCallback);
 }
Beispiel #45
0
        protected override bool init(DisposableI parent, string fileName, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, Loader.LoadedCallbackMethod loadedCallback)
        {
            if (!base.init(parent, fileName, width, height, false, multiSampleType, surfaceFormat, renderTargetUsage, usage, true, loadedCallback)) return false;

            try
            {
                if (usage == BufferUsages.Write) Debug.ThrowError("RenderTarget", "Only Textures may be writable");

                frameBuffer = new G.FrameBuffer();
                frameBuffer.SetColorTarget(texture, 0);
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null) loadedCallback(this, false);
                return false;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
            return true;
        }
Beispiel #46
0
 public VertexBuffer(IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, VertexBufferTopologys topology, float[] vertices, int[] indices)
     : base(parent, bufferLayoutDesc, usage, vertices)
 {
     init(parent, bufferLayoutDesc, usage, topology, vertices, indices);
 }
Beispiel #47
0
 public static Texture2D New(DisposableI parent, int width, int height, SurfaceFormats surfaceFormat, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
 {
     return new Texture2D(parent, width, height, surfaceFormat, usage, loadedCallback);
 }
Beispiel #48
0
 public static RenderTarget New(DisposableI parent, string fileName, MultiSampleTypes multiSampleType, BufferUsages usage, RenderTargetUsage renderTargetUsage, Loader.LoadedCallbackMethod loadedCallback)
 {
     return new RenderTarget(parent, fileName, multiSampleType, usage, renderTargetUsage, loadedCallback);
 }
Beispiel #49
0
        private void init(IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, VertexBufferTopologys topology, float[] vertices, int[] indices)
        {
            try
            {
                var video = parent.FindParentOrSelfWithException<Video>();

                this.topology = topology;
                REIGN_D3D_PRIMITIVE_TOPOLOGY topologyType = REIGN_D3D_PRIMITIVE_TOPOLOGY.TRIANGLELIST;
                switch (topology)
                {
                    case VertexBufferTopologys.Triangle: topologyType = REIGN_D3D_PRIMITIVE_TOPOLOGY.TRIANGLELIST; break;
                    case VertexBufferTopologys.Line: topologyType = REIGN_D3D_PRIMITIVE_TOPOLOGY.LINELIST; break;
                    case VertexBufferTopologys.Point: topologyType = REIGN_D3D_PRIMITIVE_TOPOLOGY.POINTLIST; break;
                }

                com = new VertexBufferCom(video.com, topologyType);
                var bufferUsage = (usage == BufferUsages.Write) ? REIGN_D3D11_USAGE.DYNAMIC : REIGN_D3D11_USAGE.DEFAULT;
                var cpuUsage = (usage == BufferUsages.Write) ? REIGN_D3D11_CPU_ACCESS_FLAG.WRITE : (REIGN_D3D11_CPU_ACCESS_FLAG)0;
                var error = com.Init(vertices, vertexCount, vertexByteSize, bufferUsage, cpuUsage);
                if (error == VertexBufferErrors.Buffer) Debug.ThrowError("VertexBuffer", "Failed to create Buffer");

                if (indices != null && indices.Length != 0) indexBuffer = new IndexBuffer(this, usage, indices);
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Beispiel #50
0
        protected virtual bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, bool lockable, Loader.LoadedCallbackMethod loadedCallback)
        {
            byte[][] mipmaps = null;
            int[] mipmapSizes = null, mipmapPitches = null;
            try
            {
                video = parent.FindParentOrSelfWithException<Video>();
                if (isRenderTarget) generateMipmaps = false;

                // load image data
                if (image != null)
                {
                    mipmaps = new byte[image.Mipmaps.Length][];
                    mipmapSizes = new int[image.Mipmaps.Length];
                    mipmapPitches = new int[image.Mipmaps.Length];
                    for (int i = 0; i != mipmaps.Length; ++i)
                    {
                        var imageMipmap = image.Mipmaps[i];
                        mipmaps[i] = image.Compressed ? imageMipmap.Data : imageMipmap.SwapRBColorChannels();
                        mipmapSizes[i] = imageMipmap.Data.Length;
                        mipmapPitches[i] = imageMipmap.Pitch;
                    }

                    Size = image.Size;
                    surfaceFormat = image.SurfaceFormat;
                    PixelByteSize = image.CalculatePixelByteSize();
                }
                else
                {
                    if (width == 0 || height == 0) Debug.ThrowError("Texture2D", "Width or Height cannot be 0");
                    Size = new Size2(width, height);
                    PixelByteSize = Image.CalculatePixelByteSize((surfaceFormat == SurfaceFormats.Defualt ? Video.DefaultSurfaceFormat() : surfaceFormat), width, height);
                }
                TexelOffset = (1 / Size.ToVector2()) * .5f;
                SizeF = Size.ToVector2();

                // init texture
                REIGN_D3DUSAGE nativeUsage = isRenderTarget ? REIGN_D3DUSAGE.RENDERTARGET : REIGN_D3DUSAGE.NONE;
                REIGN_D3DPOOL nativePool = (mipmaps != null && !video.Caps.ExDevice) ? REIGN_D3DPOOL.MANAGED : REIGN_D3DPOOL.DEFAULT;
                if (usage == BufferUsages.Read)
                {
                    if (!isRenderTarget) Debug.ThrowError("Texture2D", "Only RenderTargets may be readable");
                    // NOTE: Staging texture and states will be created in the RenderTarget
                }
                if (usage == BufferUsages.Write)
                {
                    if (mipmaps != null)
                    {
                        if (video.Caps.ExDevice) nativeUsage |= REIGN_D3DUSAGE.DYNAMIC;
                    }
                    else
                    {
                        nativeUsage |= REIGN_D3DUSAGE.DYNAMIC;
                    }
                }
                com = new Texture2DCom();
                var error = com.Init(video.com, Size.Width, Size.Height, generateMipmaps, mipmaps, mipmapSizes, mipmapPitches, 0, nativePool, nativeUsage, video.surfaceFormat(surfaceFormat), isRenderTarget);

                switch (error)
                {
                    case TextureError.Texture: Debug.ThrowError("Texture2D", "Failed to create Texture2D"); break;
                    case TextureError.SystemTexture: Debug.ThrowError("Texture2D", "Failed to create System Texture2D"); break;
                }

                if (!video.Caps.ExDevice && nativePool != REIGN_D3DPOOL.MANAGED)
                {
                    LostDevice_image = image;
                    LostDevice_width = width;
                    LostDevice_height = height;
                    LostDevice_generateMipmaps = generateMipmaps;
                    LostDevice_multiSampleType = multiSampleType;
                    LostDevice_surfaceFormat = surfaceFormat;
                    LostDevice_renderTargetUsage = renderTargetUsage;
                    LostDevice_usage = usage;
                    LostDevice_isRenderTarget = isRenderTarget;
                    LostDevice_lockable = lockable;
                    LostDevice_pool = nativePool;
                }

                if (nativePool == REIGN_D3DPOOL.DEFAULT && !video.Caps.ExDevice && !video.deviceReseting)
                {
                    video.DeviceLost += deviceLost;
                    video.DeviceReset += deviceReset;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null) loadedCallback(this, false);
                return false;
            }

            if (!isRenderTarget)
            {
                Loaded = true;
                if (loadedCallback != null) loadedCallback(this, true);
            }
            return true;
        }
Beispiel #51
0
 public RenderTarget(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, DepthStencilFormats depthStencilFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, loadedCallback)
 {
     if (initSuccess) initDepthStencil(width, height, depthStencilFormat);
 }
Beispiel #52
0
 public VertexBuffer(IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, VertexBufferTopologys topology, float[] vertices, int[] indices)
     : base(parent, bufferLayoutDesc, usage, vertices)
 {
     init(parent, bufferLayoutDesc, usage, topology, vertices, indices);
 }
Beispiel #53
0
 public RenderTarget(DisposableI parent, int width, int height, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, DepthStencilFormats depthStencilFormat, BufferUsages usage, RenderTargetUsage renderTargetUsage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent, width, height, surfaceFormat, usage, loadedCallback)
 {
     initDepthStencilFormat = depthStencilFormat;
 }
Beispiel #54
0
        private void init(IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, VertexBufferTopologys topology, float[] vertices, int[] indices)
        {
            try
            {
                var video = parent.FindParentOrSelfWithException <Video>();

                this.topology = topology;
                REIGN_D3D_PRIMITIVE_TOPOLOGY topologyType = REIGN_D3D_PRIMITIVE_TOPOLOGY.TRIANGLELIST;
                switch (topology)
                {
                case VertexBufferTopologys.Triangle: topologyType = REIGN_D3D_PRIMITIVE_TOPOLOGY.TRIANGLELIST; break;

                case VertexBufferTopologys.Line: topologyType = REIGN_D3D_PRIMITIVE_TOPOLOGY.LINELIST; break;

                case VertexBufferTopologys.Point: topologyType = REIGN_D3D_PRIMITIVE_TOPOLOGY.POINTLIST; break;
                }

                com = new VertexBufferCom(video.com, topologyType);
                var bufferUsage = (usage == BufferUsages.Write) ? REIGN_D3D11_USAGE.DYNAMIC : REIGN_D3D11_USAGE.DEFAULT;
                var cpuUsage    = (usage == BufferUsages.Write) ? REIGN_D3D11_CPU_ACCESS_FLAG.WRITE : (REIGN_D3D11_CPU_ACCESS_FLAG)0;
                var error       = com.Init(vertices, vertexCount, vertexByteSize, bufferUsage, cpuUsage);
                if (error == VertexBufferErrors.Buffer)
                {
                    Debug.ThrowError("VertexBuffer", "Failed to create Buffer");
                }

                if (indices != null && indices.Length != 0)
                {
                    indexBuffer = new IndexBuffer(this, usage, indices);
                }
            }
            catch (Exception e)
            {
                Dispose();
                throw e;
            }
        }
Beispiel #55
0
 public static RenderTarget New(DisposableI parent, int width, int height, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, DepthStencilFormats depthStencilFormat, BufferUsages usage, RenderTargetUsage renderTargetUsage, Loader.LoadedCallbackMethod loadedCallback)
 {
     return new RenderTarget(parent, width, height, multiSampleType, surfaceFormat, depthStencilFormat, usage, renderTargetUsage, loadedCallback);
 }
Beispiel #56
0
 public Texture2D(DisposableI parent, int width, int height, SurfaceFormats surfaceFormat, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent)
 {
     init(parent, null, null, width, height, false, MultiSampleTypes.None, surfaceFormat, RenderTargetUsage.PlatformDefault, usage, false, loadedCallback);
 }
Beispiel #57
0
        protected override bool init(DisposableI parent, string fileName, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, Loader.LoadedCallbackMethod loadedCallback)
        {
            if (!base.init(parent, fileName, image, width, height, false, multiSampleType, surfaceFormat, renderTargetUsage, usage, true, loadedCallback)) return false;

            try
            {
                if (usage == BufferUsages.Write) Debug.ThrowError("RenderTarget", "Only Textures may be writable");

                if (fileName == null)
                {
                    // TODO: handle multiSampleType types
                    #if SILVERLIGHT
                    var xUsage = X.RenderTargetUsage.PreserveContents;
                    #else
                    var xUsage = X.RenderTargetUsage.PlatformContents;
                    #endif
                    switch (renderTargetUsage)
                    {
                        case RenderTargetUsage.PlatformDefault:
                            #if SILVERLIGHT
                            xUsage = X.RenderTargetUsage.PreserveContents;
                            #else
                            xUsage = X.RenderTargetUsage.PlatformContents;
                            #endif
                            break;

                        case RenderTargetUsage.PreserveContents: xUsage = X.RenderTargetUsage.PreserveContents; break;
                        case RenderTargetUsage.DiscardContents: xUsage = X.RenderTargetUsage.DiscardContents; break;
                    }

                    X.DepthFormat format = X.DepthFormat.None;
                    if (initDepthStencilFormat != DepthStencilFormats.None)
                    {
                        switch (initDepthStencilFormat)
                        {
                            case DepthStencilFormats.Defualt: format = X.DepthFormat.Depth24; break;
                            case DepthStencilFormats.Depth24Stencil8: format = X.DepthFormat.Depth24Stencil8; break;
                            case DepthStencilFormats.Depth16: format = X.DepthFormat.Depth16; break;
                            case DepthStencilFormats.Depth24: format = X.DepthFormat.Depth24; break;

                            default:
                                Debug.ThrowError("RenderTarget", "Unsuported DepthStencilFormat type");
                                break;
                        }
                    }

                    renderTarget = new X.RenderTarget2D(video.Device, width, height, false, Video.surfaceFormat(surfaceFormat), format, 0, xUsage);
                }
                else
                {
                    Debug.ThrowError("RenderTarget", "(Load image data into RenderTarget Texture) -- Not implemented yet...");
                }

                texture = renderTarget;
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null) loadedCallback(this, false);
                return false;
            }

            Loaded = true;
            if (loadedCallback != null) loadedCallback(this, true);
            return true;
        }
Beispiel #58
0
 public RenderTarget(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, DepthStencilFormats depthStencilFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, Loader.LoadedCallbackMethod loadedCallback)
     : base(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, loadedCallback)
 {
     if (initSuccess)
     {
         initDepthStencil(width, height, depthStencilFormat);
     }
 }
Beispiel #59
0
 public static IVertexBuffer New(IDisposableResource parent, IBufferLayoutDesc bufferLayoutDesc, BufferUsages usage, VertexBufferTopologys topology, float[] vertices)
 {
     return(New(VideoAPI.DefaultAPI, parent, bufferLayoutDesc, usage, topology, vertices, null));
 }
Beispiel #60
0
        protected override bool init(IDisposableResource parent, Image image, int width, int height, bool generateMipmaps, MultiSampleTypes multiSampleType, SurfaceFormats surfaceFormat, RenderTargetUsage renderTargetUsage, BufferUsages usage, bool isRenderTarget, bool lockable, Loader.LoadedCallbackMethod loadedCallback)
        {
            initSuccess = base.init(parent, image, width, height, generateMipmaps, multiSampleType, surfaceFormat, renderTargetUsage, usage, true, lockable, loadedCallback);
            if (!initSuccess)
            {
                return(false);
            }

            try
            {
                if (usage == BufferUsages.Write)
                {
                    Debug.ThrowError("RenderTarget", "Only Textures may be writable");
                }

                if (image != null)
                {
                    width         = image.Size.Width;
                    height        = image.Size.Height;
                    surfaceFormat = image.SurfaceFormat;
                }

                renderTargetCom = new RenderTargetCom();
                var error = renderTargetCom.Init(video.com, com, width, height, 0, video.surfaceFormat(surfaceFormat), usage == BufferUsages.Read, lockable);

                switch (error)
                {
                case RenderTargetError.RenderTarget: Debug.ThrowError("RenderTarget", "Failed to create RenderTarget"); break;

                case RenderTargetError.StagingTexture: Debug.ThrowError("RenderTarget", "Failed to create Staging Texture"); break;
                }
            }
            catch (Exception e)
            {
                FailedToLoad = true;
                Loader.AddLoadableException(e);
                Dispose();
                if (loadedCallback != null)
                {
                    loadedCallback(this, false);
                }
                return(false);
            }

            Loaded = true;
            if (loadedCallback != null)
            {
                loadedCallback(this, true);
            }
            return(true);
        }