Helper class for Direct3D that includes conversion functions and things that are specific to D3D.
 //---------------------------------------------------------------------
 public bool RecreateIfDefaultPool(D3D.Device device)
 {
     if (d3dPool == Pool.Default)
     {
         // Create the d3d vertex buffer
         d3dBuffer = new D3D.VertexBuffer(
             typeof(byte),
             sizeInBytes,
             device,
             D3DHelper.ConvertEnum(usage),
             VertexFormats.None,
             d3dPool);
         return(true);
     }
     return(false);
 }
Beispiel #2
0
        //---------------------------------------------------------------------
        public bool RecreateIfDefaultPool(D3D.Device device)
        {
            if (d3dPool == Pool.Default)
            {
                Type bufferType = (type == IndexType.Size16) ? typeof(short) : typeof(int);
                // Create the Index buffer
                d3dBuffer = new IndexBuffer(
                    bufferType,
                    //sizeInBytes, // sizeInBytes is wrong, because the D3D API is expecting the number of indices
                    numIndices,
                    device,
                    D3DHelper.ConvertEnum(usage),
                    d3dPool);

                return(true);
            }
            return(false);
        }
        //public override Axiom.Core.Texture Create(string name, TextureType type) {
        //    D3DTexture texture = new D3DTexture(name, device, TextureUsage.Default, type);

        //    // Handle 32-bit texture settings
        //    texture.Enable32Bit(is32Bit);

        //    return texture;
        //}

        /// <summary>
        ///    Used to create a blank D3D texture.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="numMipMaps"></param>
        /// <param name="format"></param>
        /// <param name="usage"></param>
        /// <returns></returns>
        //public override Axiom.Core.Texture CreateManual(string name, TextureType type, int width, int height, int numMipMaps, Axiom.Media.PixelFormat format, TextureUsage usage) {
        //    D3DTexture texture = new D3DTexture(name, device, type, width, height, numMipMaps, format, usage);
        //    texture.Enable32Bit(is32Bit);
        //    return texture;
        //}

        // This ends up just discarding the format passed in; the C# methods don't let you supply
        // a "recommended" format.  Ah well.
        public override Axiom.Media.PixelFormat GetNativeFormat(TextureType ttype, Axiom.Media.PixelFormat format, TextureUsage usage)
        {
            // Basic filtering
            D3D.Format d3dPF = D3DHelper.ConvertEnum(D3DHelper.GetClosestSupported(format));

            // Calculate usage
            D3D.Usage d3dusage = 0;
            D3D.Pool  pool     = D3D.Pool.Managed;
            if ((usage & TextureUsage.RenderTarget) != 0)
            {
                d3dusage |= D3D.Usage.RenderTarget;
                pool      = D3D.Pool.Default;
            }
            if ((usage & TextureUsage.Dynamic) != 0)
            {
                d3dusage |= D3D.Usage.Dynamic;
                pool      = D3D.Pool.Default;
            }

            // Use D3DX to adjust pixel format
            switch (ttype)
            {
            case TextureType.OneD:
            case TextureType.TwoD:
                TextureRequirements tReqs;
                TextureLoader.CheckTextureRequirements(device, d3dusage, pool, out tReqs);
                d3dPF = tReqs.Format;
                break;

            case TextureType.ThreeD:
                VolumeTextureRequirements volReqs;
                TextureLoader.CheckVolumeTextureRequirements(device, pool, out volReqs);
                d3dPF = volReqs.Format;
                break;

            case TextureType.CubeMap:
                CubeTextureRequirements cubeReqs;
                TextureLoader.CheckCubeTextureRequirements(device, d3dusage, pool, out cubeReqs);
                d3dPF = cubeReqs.Format;
                break;
            }
            ;
            return(D3DHelper.ConvertEnum(d3dPF));
        }
        public D3DHardwareVertexBuffer(int vertexSize, int numVertices, BufferUsage usage,
                                       D3D.Device device, bool useSystemMemory, bool useShadowBuffer)
            : base(vertexSize, numVertices, usage, useSystemMemory, useShadowBuffer)
        {
#if !NO_OGRE_D3D_MANAGE_BUFFERS
            d3dPool = useSystemMemory? Pool.SystemMemory :
                      // If not system mem, use managed pool UNLESS buffer is discardable
                      // if discardable, keeping the software backing is expensive
                      ((usage & BufferUsage.Discardable) != 0) ? Pool.Default : Pool.Managed;
#else
            d3dPool = useSystemMemory ? Pool.SystemMemory : Pool.Default;
#endif
            // Create the d3d vertex buffer
            d3dBuffer = new D3D.VertexBuffer(device,
                                             sizeInBytes,
                                             D3DHelper.ConvertEnum(usage),
                                             VertexFormats.None,
                                             d3dPool);
        }
Beispiel #5
0
        ///<summary>
        ///    Call this to associate a D3D volume with this pixel buffer
        ///</summary>
        public void Bind(D3D.Device device, D3D.Volume volume, bool update)
        {
            this.device = device;
            this.volume = volume;

            D3D.VolumeDescription desc = volume.Description;
            width  = desc.Width;
            height = desc.Height;
            depth  = desc.Depth;
            format = D3DHelper.ConvertEnum(desc.Format);
            // Default
            rowPitch    = width;
            slicePitch  = height * width;
            sizeInBytes = PixelUtil.GetMemorySize(width, height, depth, format);

            if (((int)usage & (int)TextureUsage.RenderTarget) != 0)
            {
                CreateRenderTextures(update);
            }
        }
Beispiel #6
0
        public D3DHardwareIndexBuffer(IndexType type, int numIndices, BufferUsage usage,
                                      D3D.Device device, bool useSystemMemory, bool useShadowBuffer)
            : base(type, numIndices, usage, useSystemMemory, useShadowBuffer)
        {
#if !NO_OGRE_D3D_MANAGE_BUFFERS
            d3dPool = useSystemMemory? Pool.SystemMemory :
                      // If not system mem, use managed pool UNLESS buffer is discardable
                      // if discardable, keeping the software backing is expensive
                      ((usage & BufferUsage.Discardable) != 0) ? Pool.Default : Pool.Managed;
#else
            d3dPool = useSystemMemory ? Pool.SystemMemory : Pool.Default;
#endif

            Type bufferType = (type == IndexType.Size16) ? typeof(short) : typeof(int);

            // create the buffer
            d3dBuffer = new IndexBuffer(
                bufferType,
                //sizeInBytes, // sizeInBytes is wrong, because the D3D API is expecting the number of indices
                numIndices,
                device,
                D3DHelper.ConvertEnum(usage),
                d3dPool);
        }
Beispiel #7
0
        ///<summary>
        ///    @copydoc HardwarePixelBuffer.BlitToMemory
        ///</summary>
        public override void BlitToMemory(BasicBox srcBox, PixelBox dst)
        {
            // Decide on pixel format of temp surface
            PixelFormat tmpFormat = format;

            if (D3DHelper.ConvertEnum(dst.Format) == D3D.Format.Unknown)
            {
                tmpFormat = dst.Format;
            }
            if (surface != null)
            {
                Debug.Assert(srcBox.Depth == 1 && dst.Depth == 1);
                // Create temp texture
                D3D.Texture tmp =
                    new D3D.Texture(device, dst.Width, dst.Height,
                                    1, // 1 mip level ie topmost, generate no mipmaps
                                    0, D3DHelper.ConvertEnum(tmpFormat),
                                    Pool.Scratch);
                D3D.Surface subSurface = tmp.GetSurfaceLevel(0);
                // Copy texture to this temp surface
                Rectangle destRect, srcRect;
                srcRect  = ToD3DRectangle(srcBox);
                destRect = ToD3DRectangleExtent(dst);

                SurfaceLoader.FromSurface(subSurface, destRect, surface, srcRect, Filter.None, 0);

                // Lock temp surface and copy it to memory
                int            pitch; // Filled in by D3D
                GraphicsStream data = subSurface.LockRectangle(D3D.LockFlags.ReadOnly, out pitch);
                // Copy it
                PixelBox locked = new PixelBox(dst.Width, dst.Height, dst.Depth, tmpFormat);
                FromD3DLock(locked, pitch, data);
                PixelUtil.BulkPixelConversion(locked, dst);
                subSurface.UnlockRectangle();
                // Release temporary surface and texture
                subSurface.Dispose();
                tmp.Dispose();
            }
            else
            {
                // Create temp texture
                D3D.VolumeTexture tmp =
                    new D3D.VolumeTexture(device, dst.Width, dst.Height, dst.Depth,
                                          0, D3D.Usage.None,
                                          D3DHelper.ConvertEnum(tmpFormat),
                                          Pool.Scratch);
                D3D.Volume subVolume = tmp.GetVolumeLevel(0);
                // Volume
                D3D.Box ddestBox = ToD3DBoxExtent(dst);
                D3D.Box dsrcBox  = ToD3DBox(srcBox);

                VolumeLoader.FromVolume(subVolume, ddestBox, volume, dsrcBox, Filter.None, 0);
                // Lock temp surface and copy it to memory
                D3D.LockedBox  lbox;                // Filled in by D3D
                GraphicsStream data = subVolume.LockBox(LockFlags.ReadOnly, out lbox);
                // Copy it
                PixelBox locked = new PixelBox(dst.Width, dst.Height, dst.Depth, tmpFormat);
                FromD3DLock(locked, lbox, data);
                PixelUtil.BulkPixelConversion(locked, dst);
                subVolume.UnlockBox();
                // Release temporary surface and texture
                subVolume.Dispose();
                tmp.Dispose();
            }
        }
Beispiel #8
0
        protected void BlitFromMemoryImpl(PixelBox src, BasicBox dstBox)
        {
            // TODO: This currently does way too many copies.  We copy
            // from src to a converted buffer (if needed), then from
            // converted to a byte array, then into the temporary surface,
            // and finally from the temporary surface to the real surface.
            PixelBox converted   = src;
            IntPtr   bufPtr      = IntPtr.Zero;
            GCHandle bufGCHandle = new GCHandle();

            // convert to pixelbuffer's native format if necessary
            if (D3DHelper.ConvertEnum(src.Format) == D3D.Format.Unknown)
            {
                int    bufSize   = PixelUtil.GetMemorySize(src.Width, src.Height, src.Depth, format);
                byte[] newBuffer = new byte[bufSize];
                bufGCHandle = GCHandle.Alloc(newBuffer, GCHandleType.Pinned);
                bufPtr      = bufGCHandle.AddrOfPinnedObject();
                converted   = new PixelBox(src.Width, src.Height, src.Depth, format, bufPtr);
                PixelUtil.BulkPixelConversion(src, converted);
            }

            // int formatBytes = PixelUtil.GetNumElemBytes(converted.Format);
            Surface tmpSurface = device.CreateOffscreenPlainSurface(converted.Width, converted.Height, D3DHelper.ConvertEnum(converted.Format), Pool.Scratch);
            int     pitch;
            // Ideally I would be using the Array mechanism here, but that doesn't seem to work
            GraphicsStream buf = tmpSurface.LockRectangle(LockFlags.NoSystemLock, out pitch);

            buf.Position = 0;
            unsafe {
                int    bufSize = PixelUtil.GetMemorySize(converted.Width, converted.Height, converted.Depth, converted.Format);
                byte * srcPtr  = (byte *)converted.Data.ToPointer();
                byte[] ugh     = new byte[bufSize];
                for (int i = 0; i < bufSize; ++i)
                {
                    ugh[i] = srcPtr[i];
                }
                buf.Write(ugh);
            }
            tmpSurface.UnlockRectangle();
            buf.Dispose();

            //ImageInformation imageInfo = new ImageInformation();
            //imageInfo.Format = D3DHelper.ConvertEnum(converted.Format);
            //imageInfo.Width = converted.Width;
            //imageInfo.Height = converted.Height;
            //imageInfo.Depth = converted.Depth;
            if (surface != null)
            {
                // I'm trying to write to surface using the data in converted
                Rectangle srcRect  = ToD3DRectangleExtent(converted);
                Rectangle destRect = ToD3DRectangle(dstBox);
                SurfaceLoader.FromSurface(surface, destRect, tmpSurface, srcRect, Filter.None, 0);
            }
            else
            {
                D3D.Box srcBox  = ToD3DBoxExtent(converted);
                D3D.Box destBox = ToD3DBox(dstBox);
                Debug.Assert(false, "Volume textures not yet supported");
                // VolumeLoader.FromStream(volume, destBox, converted.Data, converted.RowPitch * converted.SlicePitch * formatBytes, srcBox, Filter.None, 0);
                VolumeLoader.FromStream(volume, destBox, buf, srcBox, Filter.None, 0);
            }

            tmpSurface.Dispose();

            // If we allocated a buffer for the temporary conversion, free it here
            // If I used bufPtr to store my temporary data while I converted
            // it, I need to free it here.  This invalidates converted.
            // My data has already been copied to tmpSurface and then to the
            // real surface.
            if (bufGCHandle.IsAllocated)
            {
                bufGCHandle.Free();
            }

            if (doMipmapGen)
            {
                GenMipmaps();
            }
        }
Beispiel #9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="offset"></param>
 /// <param name="length"></param>
 /// <param name="locking"></param>
 /// <returns></returns>
 protected override IntPtr LockImpl(int offset, int length, BufferLocking locking)
 {
     D3D.LockFlags d3dLocking           = D3DHelper.ConvertEnum(locking, usage);
     Microsoft.DirectX.GraphicsStream s = d3dBuffer.Lock(offset, length, d3dLocking);
     return(s.InternalData);
 }