Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DeviceHandlerD3D11" /> class.
        /// </summary>
        internal DeviceHandlerD2D(GraphicsDeviceConfiguration deviceConfig, EngineFactory engineFactory, EngineDevice engineDevice)
        {
            try
            {
                // Simulate exception if requested
                if (deviceConfig.CoreConfiguration.ThrowD2DInitDeviceError)
                {
                    throw new SeeingSharpGraphicsException("Simulation Direct2D device init exception");
                }

                using (var dxgiDevice = engineDevice.DeviceD3D11_1.QueryInterface <IDXGIDevice>())
                {
                    _deviceD2D        = engineFactory.FactoryD2D_2.CreateDevice(dxgiDevice);
                    _deviceContextD2D = _deviceD2D
                                        .CreateDeviceContext(D2D.DeviceContextOptions.None);
                    _renderTarget = _deviceContextD2D;
                }
            }
            catch (Exception)
            {
                SeeingSharpUtil.SafeDispose(ref _deviceContextD2D);
                SeeingSharpUtil.SafeDispose(ref _deviceD2D);
                SeeingSharpUtil.SafeDispose(ref _renderTarget);
            }
        }
        /// <summary>
        /// Unloads the resource.
        /// </summary>
        protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources)
        {
            SeeingSharpUtil.SafeDispose(ref _inputLayout);

            _vertexShader = null;
            _pixelShader  = null;
        }
        public override void OnSampleClosed()
        {
            base.OnSampleClosed();

            SeeingSharpUtil.SafeDispose(ref _solidBrush);
            SeeingSharpUtil.SafeDispose(ref _animatedRectBrush);
            SeeingSharpUtil.SafeDispose(ref _textFormat);
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DeviceHandlerD3D9"/> class.
        /// </summary>
        /// <param name="dxgiAdapter">The target adapter.</param>
        /// <param name="isSoftwareAdapter">Are we in software mode?</param>
        internal DeviceHandlerD3D9(IDXGIAdapter1 dxgiAdapter, bool isSoftwareAdapter)
        {
            try
            {
                // Just needed when on true hardware
                if (!isSoftwareAdapter)
                {
                    //Prepare device creation
                    const D3D9.CreateFlags CREATE_FLAGS = D3D9.CreateFlags.HardwareVertexProcessing |
                                                          D3D9.CreateFlags.PureDevice |
                                                          D3D9.CreateFlags.FpuPreserve |
                                                          D3D9.CreateFlags.Multithreaded;

                    var presentparams = new D3D9.PresentParameters
                    {
                        Windowed             = true,
                        SwapEffect           = D3D9.SwapEffect.Discard,
                        DeviceWindowHandle   = GetDesktopWindow(),
                        PresentationInterval = D3D9.PresentInterval.Default,
                        BackBufferCount      = 1
                    };

                    //Create the device finally
                    var result = Create9Ex(out _direct3DEx);
                    result.CheckError();

                    // Try to find the Direct3D9 adapter that matches given DXGI adapter
                    var adapterIndex = -1;
                    for (var loop = 0; loop < _direct3DEx.AdapterCount; loop++)
                    {
                        var d3d9AdapterInfo = _direct3DEx.GetAdapterIdentifier(loop);
                        if (d3d9AdapterInfo.DeviceId == dxgiAdapter.Description1.DeviceId)
                        {
                            adapterIndex = loop;
                            break;
                        }
                    }

                    // Direct3D 9 is only relevant on the primary device
                    if (adapterIndex < 0)
                    {
                        return;
                    }

                    // Try to create the device
                    _deviceEx = _direct3DEx.CreateDeviceEx(
                        adapterIndex, D3D9.DeviceType.Hardware, IntPtr.Zero,
                        CREATE_FLAGS, presentparams);
                }
            }
            catch (Exception)
            {
                // No direct3d 9 interface support
                SeeingSharpUtil.SafeDispose(ref _direct3DEx);
                SeeingSharpUtil.SafeDispose(ref _deviceEx);
            }
        }
Пример #5
0
        /// <inheritdoc />
        public void Dispose()
        {
            SeeingSharpUtil.SafeDispose(ref _uploaderColor);

            foreach (var actDumpResult in _dumpResults)
            {
                SeeingSharpUtil.DisposeObject(actDumpResult);
            }
            _dumpResults.Clear();

            _isDisposed = true;
        }
Пример #6
0
 /// <summary>
 /// Disposes all resources of this object completely.
 /// </summary>
 public void Dispose()
 {
     // Dispose all created objects
     if (_renderTarget2D != null)
     {
         SeeingSharpUtil.SafeDispose(ref _renderTargetBitmap);
         _renderTarget2D = null;
     }
     else
     {
         SeeingSharpUtil.SafeDispose(ref _renderTarget2D);
     }
 }
Пример #7
0
        /// <summary>
        /// Unloads all resources.
        /// </summary>
        /// <param name="device">The device on which the resources where loaded.</param>
        /// <param name="resources">The current ResourceDictionary.</param>
        protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources)
        {
            if (device.Supports2D)
            {
                _graphics2D = null;
                SeeingSharpUtil.SafeDispose(ref _overlayRenderer);
            }
            else
            {
                _notLoadedDueToMissingSupport = false;
                return;
            }

            SeeingSharpUtil.SafeDispose(ref _renderTargetTextureView);
            SeeingSharpUtil.SafeDispose(ref _renderTargetTexture);
        }
Пример #8
0
        private static byte[] GetShaderBytecode(EngineDevice device, ResourceLink resourceLink, ShaderResourceKind resourceKind, string shaderModel)
        {
            switch (resourceKind)
            {
            case ShaderResourceKind.Bytecode:
                using (var inStream = resourceLink.OpenInputStream())
                {
                    return(inStream.ReadAllBytes());
                }

            case ShaderResourceKind.HlsFile:
                using (ReusableStringBuilders.Current.UseStringBuilder(out var singleShaderSourceBuilder, 10024))
                {
                    SingleShaderFileBuilder.ReadShaderFileAndResolveIncludes(
                        resourceLink,
                        singleShaderSourceBuilder);

                    // device.DebugEnabled ? D3DCompiler.ShaderFlags.Debug : D3DCompiler.ShaderFlags.None
                    var shaderSource  = singleShaderSourceBuilder.ToString();
                    var compileResult = Compiler.Compile(
                        shaderSource,
                        Array.Empty <D3D.ShaderMacro>(),
                        null !,
                        "main",
                        resourceLink.ToString() ?? "",
                        shaderModel,
                        device.DebugEnabled ? ShaderFlags.Debug : ShaderFlags.None,
                        out var compBlob, out var compErrorBlob);
                    try
                    {
                        if (compileResult.Failure)
                        {
                            throw new SeeingSharpGraphicsException($"Unable to compile shader from {resourceLink}: {compileResult.Code} - {compileResult.ToString()}");
                        }
                        return(compBlob.GetBytes());
                    }
                    finally
                    {
                        SeeingSharpUtil.SafeDispose(ref compBlob);
                        SeeingSharpUtil.SafeDispose(ref compErrorBlob);
                    }
                }

            default:
                throw new SeeingSharpException($"Unhandled {nameof(ShaderResourceKind)}: {resourceKind}");
            }
        }
Пример #9
0
        /// <summary>
        /// Sets the content to all lines in the given polygon.
        /// </summary>
        /// <param name="center">The center of the ellipse.</param>
        /// <param name="radiusX">The radius in x direction.</param>
        /// <param name="radiusY">The radius in y direction.</param>
        public unsafe void SetContent(Vector2 center, float radiusX, float radiusY)
        {
            GraphicsCore.EnsureGraphicsSupportLoaded();
            radiusX.EnsurePositiveOrZero(nameof(radiusX));
            radiusY.EnsurePositiveOrZero(nameof(radiusY));

            _center  = center;
            _radiusX = radiusX;
            _radiusY = radiusY;

            SeeingSharpUtil.SafeDispose(ref _geometry);

            _geometry = GraphicsCore.Current.FactoryD2D !.CreateEllipseGeometry(
                new D2D.Ellipse(
                    *(PointF *)&center,
                    radiusX, radiusY));
        }
Пример #10
0
        /// <summary>
        /// Unloads all resources of this object.
        /// </summary>
        public override void UnloadResources()
        {
            base.UnloadResources();

            // Dispose all locally created resources
            foreach (var actLocalResource in _localResources)
            {
                if (actLocalResource == null)
                {
                    continue;
                }

                SeeingSharpUtil.SafeDispose(ref actLocalResource.IndexBuffer);
                SeeingSharpUtil.SafeDispose(ref actLocalResource.VertexBuffer);
            }

            // Clear local resource container
            _localResources.Clear();
        }
Пример #11
0
        /// <summary>
        /// Unloads the resource.
        /// </summary>
        /// <param name="device">The device.</param>
        /// <param name="resources">Parent ResourceDictionary.</param>
        protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources)
        {
            SeeingSharpUtil.SafeDispose(ref _depthBufferView);
            SeeingSharpUtil.SafeDispose(ref _colorBufferRenderTargetView);
            SeeingSharpUtil.SafeDispose(ref _colorBufferShaderResourceView);
            SeeingSharpUtil.SafeDispose(ref _depthBuffer);
            SeeingSharpUtil.SafeDispose(ref _colorBuffer);
            SeeingSharpUtil.SafeDispose(ref _normalDepthBufferRenderTargetView);
            SeeingSharpUtil.SafeDispose(ref _normalDepthBufferShaderResourceView);
            SeeingSharpUtil.SafeDispose(ref _normalDepthBuffer);

            // Unload shader resource if it was created explicitly
            if (_shaderResourceCreated)
            {
                SeeingSharpUtil.SafeDispose(ref _colorBufferShaderResource);
                SeeingSharpUtil.SafeDispose(ref _normalDepthBufferShaderResource);
                _shaderResourceCreated = false;
            }
        }
Пример #12
0
        public async Task Collision_Ellipse_to_Polygon()
        {
            await TestUtilities.InitializeWithGraphicsAsync();

            var ellipseGeometry01 = new EllipseGeometryResource(
                new Vector2(50, 50), 10f, 10f);
            var ellipseGeometry02 = new EllipseGeometryResource(
                new Vector2(50, 80), 10f, 10f);

            var polygonGeometry = new PolygonGeometryResource(new Polygon2D(new Vector2(55f, 50f), new Vector2(60f, 50f), new Vector2(55f, 55f)));

            try
            {
                Assert.IsTrue(ellipseGeometry01.IntersectsWith(polygonGeometry));
                Assert.IsFalse(ellipseGeometry02.IntersectsWith(polygonGeometry));
            }
            finally
            {
                SeeingSharpUtil.SafeDispose(ref ellipseGeometry01);
                SeeingSharpUtil.SafeDispose(ref ellipseGeometry02);
                SeeingSharpUtil.SafeDispose(ref polygonGeometry);
            }
        }
Пример #13
0
        public async Task Collision_Ellipse_to_Ellipse()
        {
            await TestUtilities.InitializeWithGraphicsAsync();

            var ellipseGeometry01 = new EllipseGeometryResource(
                new Vector2(50, 50), 10f, 10f);
            var ellipseGeometry02 = new EllipseGeometryResource(
                new Vector2(50, 80), 10f, 10f);
            var ellipseGeometry03 = new EllipseGeometryResource(
                new Vector2(50, 70), 10f, 11f);

            try
            {
                Assert.IsFalse(ellipseGeometry01.IntersectsWith(ellipseGeometry02));
                Assert.IsTrue(ellipseGeometry03.IntersectsWith(ellipseGeometry02));
                Assert.IsTrue(ellipseGeometry02.IntersectsWith(ellipseGeometry03));
            }
            finally
            {
                SeeingSharpUtil.SafeDispose(ref ellipseGeometry01);
                SeeingSharpUtil.SafeDispose(ref ellipseGeometry02);
                SeeingSharpUtil.SafeDispose(ref ellipseGeometry03);
            }
        }
Пример #14
0
 public void Dispose()
 {
     SeeingSharpUtil.SafeDispose(ref _textFormat);
     SeeingSharpUtil.SafeDispose(ref _solidBrushBackground);
     SeeingSharpUtil.SafeDispose(ref _solidBrushForeground);
 }
Пример #15
0
        public override void OnSampleClosed()
        {
            base.OnSampleClosed();

            SeeingSharpUtil.SafeDispose(ref _bitmap);
        }
Пример #16
0
        /// <summary>
        /// Loads a ac file from the given uri
        /// </summary>
        internal static ACFileInfo LoadFile(Stream inStream)
        {
            ACFileInfo?  result = null;
            StreamReader?reader = null;

            try
            {
                reader = new StreamReader(inStream);

                //Check for correct header
                var header = reader.ReadLine();
                if (header == null ||
                    !header.StartsWith("AC3D"))
                {
                    throw new SeeingSharpGraphicsException("Header of AC3D file not found!");
                }

                //Create file information object
                result = new ACFileInfo();

                //Create a loaded objects stack
                var       loadedObjects  = new Stack <ACObjectInfo>();
                var       parentObjects  = new Stack <ACObjectInfo>();
                ACSurface?currentSurface = null;

                //Read the file
                while (!reader.EndOfStream)
                {
                    var actLine = reader.ReadLine() !.Trim();

                    var firstWord  = string.Empty;
                    var spaceIndex = actLine.IndexOf(' ');
                    if (spaceIndex == -1)
                    {
                        firstWord = actLine;
                    }
                    else
                    {
                        firstWord = firstWord = actLine.Substring(0, spaceIndex);
                    }

                    switch (firstWord)
                    {
                    //New Material info
                    case "MATERIAL":
                        var materialInfo = new ACMaterialInfo();
                        {
                            //Get the name of the material
                            var materialData = actLine.Split(' ');

                            if (materialData.Length > 1)
                            {
                                materialInfo.Name = materialData[1].Trim(' ', '"');
                            }

                            //Parse components
                            for (var loop = 0; loop < materialData.Length; loop++)
                            {
                                switch (materialData[loop])
                                {
                                case "rgb":
                                    var diffuseColor = materialInfo.Diffuse;
                                    diffuseColor.Alpha   = 1f;
                                    diffuseColor.Red     = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    diffuseColor.Green   = float.Parse(materialData[loop + 2], CultureInfo.InvariantCulture);
                                    diffuseColor.Blue    = float.Parse(materialData[loop + 3], CultureInfo.InvariantCulture);
                                    materialInfo.Diffuse = diffuseColor;
                                    break;

                                case "amb":
                                    var ambientColor = new Color4();
                                    ambientColor.Red     = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    ambientColor.Green   = float.Parse(materialData[loop + 2], CultureInfo.InvariantCulture);
                                    ambientColor.Blue    = float.Parse(materialData[loop + 3], CultureInfo.InvariantCulture);
                                    materialInfo.Ambient = ambientColor;
                                    break;

                                case "emis":
                                    var emissiveColor = new Color4();
                                    emissiveColor.Red     = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    emissiveColor.Green   = float.Parse(materialData[loop + 2], CultureInfo.InvariantCulture);
                                    emissiveColor.Blue    = float.Parse(materialData[loop + 3], CultureInfo.InvariantCulture);
                                    materialInfo.Emissive = emissiveColor;
                                    break;

                                case "spec":
                                    var specularColor = new Color4();
                                    specularColor.Red     = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    specularColor.Green   = float.Parse(materialData[loop + 2], CultureInfo.InvariantCulture);
                                    specularColor.Blue    = float.Parse(materialData[loop + 3], CultureInfo.InvariantCulture);
                                    materialInfo.Specular = specularColor;
                                    break;

                                case "shi":
                                    materialInfo.Shininess = float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture);
                                    break;

                                case "trans":
                                    diffuseColor         = materialInfo.Diffuse;
                                    diffuseColor.Alpha   = 1f - EngineMath.Clamp(float.Parse(materialData[loop + 1], CultureInfo.InvariantCulture), 0f, 1f);
                                    materialInfo.Diffuse = diffuseColor;
                                    break;
                                }
                            }
                            result.Materials.Add(materialInfo);
                        }
                        break;

                    //New object starts here
                    case "OBJECT":
                    {
                        var newObject = new ACObjectInfo();

                        var lineData = actLine.Split(' ');
                        if (lineData[1] == "poly")
                        {
                            newObject.Type = ACObjectType.Poly;
                        }
                        else if (lineData[1] == "group")
                        {
                            newObject.Type = ACObjectType.Group;
                        }
                        else if (lineData[1] == "world")
                        {
                            newObject.Type = ACObjectType.World;
                        }

                        loadedObjects.Push(newObject);
                    }
                    break;

                    //End of an object, kids following
                    case "kids":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            //Parse kid count
                            var kidCount = 0;
                            var lineData = actLine.Split(' ');

                            if (lineData.Length >= 1)
                            {
                                int.TryParse(lineData[1], out kidCount);
                            }

                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                //AddObject object to parent object, if any related
                                var addedToParent = false;

                                if (parentObjects.Count > 0)
                                {
                                    var currentParent = parentObjects.Peek();

                                    if (currentParent.Children.Count < currentParent.KidCount)
                                    {
                                        currentParent.Children.Add(currentObject);
                                        addedToParent = true;
                                    }
                                    else
                                    {
                                        while (parentObjects.Count > 0)
                                        {
                                            parentObjects.Pop();

                                            if (parentObjects.Count == 0)
                                            {
                                                break;
                                            }

                                            currentParent = parentObjects.Peek();

                                            if (currentParent == null)
                                            {
                                                break;
                                            }
                                            if (currentParent.Children.Count < currentParent.KidCount)
                                            {
                                                break;
                                            }
                                        }

                                        if (currentParent != null &&
                                            currentParent.Children.Count < currentParent.KidCount)
                                        {
                                            currentParent.Children.Add(currentObject);
                                            addedToParent = true;
                                        }
                                    }
                                }

                                //Enable this object as parent object
                                currentObject.KidCount = kidCount;

                                if (currentObject.KidCount > 0)
                                {
                                    parentObjects.Push(currentObject);
                                }

                                //AddObject to scene root if this object has no parent
                                loadedObjects.Pop();

                                if (!addedToParent)
                                {
                                    if (loadedObjects.Count == 0)
                                    {
                                        result.Objects.Add(currentObject);
                                    }
                                    else
                                    {
                                        loadedObjects.Peek().Children.Add(currentObject);
                                    }
                                }
                                currentObject = null;
                            }
                        }
                        break;

                    //Current object's name
                    case "name":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();
                            if (currentObject != null)
                            {
                                currentObject.Name = actLine.Replace("name ", "").Replace("\"", "");
                            }
                        }
                        break;

                    case "data":
                        break;

                    case "texture":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');
                                currentObject.Texture = lineData[1].Trim('"');
                            }
                        }
                        break;

                    case "texrep":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');

                                var repetition = new Vector2
                                {
                                    X = float.Parse(lineData[1], CultureInfo.InvariantCulture),
                                    Y = float.Parse(lineData[2], CultureInfo.InvariantCulture)
                                };

                                currentObject.TextureRepeat = repetition;
                            }
                        }
                        break;

                    case "texoff":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');

                                var offset = new Vector2
                                {
                                    X = float.Parse(lineData[1], CultureInfo.InvariantCulture),
                                    Y = float.Parse(lineData[2], CultureInfo.InvariantCulture)
                                };

                                currentObject.TextureRepeat = offset;
                            }
                        }
                        break;

                    case "rot":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');

                                var rotation = Matrix4x4.Identity;
                                rotation.M11 = !string.IsNullOrEmpty(lineData[1]) ? float.Parse(lineData[1], CultureInfo.InvariantCulture) : 0f;
                                rotation.M12 = !string.IsNullOrEmpty(lineData[2]) ? float.Parse(lineData[2], CultureInfo.InvariantCulture) : 0f;
                                rotation.M13 = !string.IsNullOrEmpty(lineData[3]) ? float.Parse(lineData[3], CultureInfo.InvariantCulture) : 0f;
                                rotation.M21 = !string.IsNullOrEmpty(lineData[4]) ? float.Parse(lineData[4], CultureInfo.InvariantCulture) : 0f;
                                rotation.M22 = !string.IsNullOrEmpty(lineData[5]) ? float.Parse(lineData[5], CultureInfo.InvariantCulture) : 0f;
                                rotation.M23 = !string.IsNullOrEmpty(lineData[6]) ? float.Parse(lineData[6], CultureInfo.InvariantCulture) : 0f;
                                rotation.M31 = !string.IsNullOrEmpty(lineData[7]) ? float.Parse(lineData[7], CultureInfo.InvariantCulture) : 0f;
                                rotation.M32 = !string.IsNullOrEmpty(lineData[8]) ? float.Parse(lineData[8], CultureInfo.InvariantCulture) : 0f;
                                rotation.M33 = !string.IsNullOrEmpty(lineData[9]) ? float.Parse(lineData[9], CultureInfo.InvariantCulture) : 0f;

                                currentObject.Rotation = rotation;
                            }
                        }
                        break;

                    case "url":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');
                                currentObject.Url = lineData[1].Trim('"');
                            }
                        }
                        break;

                    //Current object's location
                    case "loc":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData = actLine.Split(' ');

                                var location = new Vector3
                                {
                                    X = float.Parse(lineData[1], CultureInfo.InvariantCulture),
                                    Y = float.Parse(lineData[2], CultureInfo.InvariantCulture),
                                    Z = float.Parse(lineData[3], CultureInfo.InvariantCulture)
                                };

                                currentObject.Translation = location;
                            }
                        }
                        break;

                    case "numvert":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            var currentObject = loadedObjects.Peek();

                            if (currentObject != null)
                            {
                                var lineData         = actLine.Split(' ');
                                var numberOfVertices = int.Parse(lineData[1], CultureInfo.InvariantCulture);

                                for (var loop = 0; loop < numberOfVertices; loop++)
                                {
                                    var actInnerLine   = reader.ReadLine() !.Trim();
                                    var splittedVertex = actInnerLine.Split(' ');

                                    var position = new Vector3
                                    {
                                        X = float.Parse(splittedVertex[0], CultureInfo.InvariantCulture),
                                        Y = float.Parse(splittedVertex[1], CultureInfo.InvariantCulture),
                                        Z = float.Parse(splittedVertex[2], CultureInfo.InvariantCulture)
                                    };

                                    currentObject.Vertices.Add(new ACVertex {
                                        Position = position
                                    });
                                }
                            }
                        }
                        break;

                    //Start of a list of surfaces
                    case "numsurf":
                        break;

                    //New surface starts here
                    case "SURF":
                    {
                        if (currentSurface == null)
                        {
                            currentSurface = new ACSurface();
                        }

                        var lineData = actLine.Split(' ');
                        lineData[1]          = lineData[1].Substring(2);
                        currentSurface.Flags = int.Parse(lineData[1], NumberStyles.HexNumber);
                    }
                    break;

                    //Current surface's material
                    case "mat":
                    {
                        if (currentSurface == null)
                        {
                            currentSurface = new ACSurface();
                        }

                        var lineData = actLine.Split(' ');
                        currentSurface.Material = int.Parse(lineData[1], CultureInfo.InvariantCulture);
                    }
                    break;

                    //Current surface's indices
                    case "refs":
                        if (loadedObjects.Count == 0)
                        {
                            break;
                        }
                        {
                            if (currentSurface == null)
                            {
                                currentSurface = new ACSurface();
                            }

                            var lineData     = actLine.Split(' ');
                            var numberOfRefs = int.Parse(lineData[1], CultureInfo.InvariantCulture);

                            for (var loop = 0; loop < numberOfRefs; loop++)
                            {
                                var actInnerLine = reader.ReadLine() !.Trim();
                                var splittedRef  = actInnerLine.Split(' ');

                                var texCoord        = new Vector2();
                                int vertexReference = ushort.Parse(splittedRef[0], CultureInfo.InvariantCulture);
                                texCoord.X = float.Parse(splittedRef[1], CultureInfo.InvariantCulture);
                                texCoord.Y = float.Parse(splittedRef[2], CultureInfo.InvariantCulture);

                                currentSurface.TextureCoordinates.Add(texCoord);
                                currentSurface.VertexReferences.Add(vertexReference);
                            }

                            var currentObject = loadedObjects.Peek();

                            currentObject?.Surfaces.Add(currentSurface);

                            currentSurface = null;
                        }
                        break;
                    }
                }
            }
            finally
            {
                SeeingSharpUtil.SafeDispose(ref reader);
            }

            return(result);
        }
Пример #17
0
 protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources)
 {
     SeeingSharpUtil.SafeDispose(ref _buffer);
 }
 /// <summary>
 /// Unloads all resources.
 /// </summary>
 /// <param name="device">The device on which the resources where loaded.</param>
 /// <param name="resources">The current ResourceDictionary.</param>
 protected override void UnloadResourceInternal(EngineDevice device, ResourceDictionary resources)
 {
     SeeingSharpUtil.SafeDispose(ref _renderTargetTextureView);
     SeeingSharpUtil.SafeDispose(ref _renderTargetTexture);
 }
Пример #19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DeviceHandlerD3D11"/> class.
        /// </summary>
        internal DeviceHandlerD3D11(GraphicsDeviceConfiguration deviceConfig, IDXGIAdapter1 dxgiAdapter)
        {
            _dxgiAdapter = dxgiAdapter;

            // Define possible create flags
            var createFlags = D3D11.DeviceCreationFlags.BgraSupport;

            if (deviceConfig.CoreConfiguration.DebugEnabled)
            {
                createFlags |= D3D11.DeviceCreationFlags.Debug;
            }

            // Define all steps on which we try to initialize Direct3D
            var initParameterQueue =
                new List <Tuple <D3D.FeatureLevel, D3D11.DeviceCreationFlags, HardwareDriverLevel> >();

            // Define all tries for hardware initialization
            initParameterQueue.Add(Tuple.Create(
                                       D3D.FeatureLevel.Level_11_1, createFlags, HardwareDriverLevel.Direct3D11));
            initParameterQueue.Add(Tuple.Create(
                                       D3D.FeatureLevel.Level_11_0, createFlags, HardwareDriverLevel.Direct3D11));
            initParameterQueue.Add(Tuple.Create(
                                       D3D.FeatureLevel.Level_10_0, createFlags, HardwareDriverLevel.Direct3D10));

            // Try to create the device, each defined configuration step by step
            foreach (var(actFeatureLevel, actCreateFlags, actDriverLevel) in initParameterQueue)
            {
                try
                {
                    // Try to create the device using current parameters
                    var result = D3D11CreateDevice(
                        dxgiAdapter, D3D.DriverType.Unknown, createFlags,
                        new D3D.FeatureLevel[] { actFeatureLevel }, out var device);
                    if (!result.Success)
                    {
                        continue;
                    }
                    if (device == null)
                    {
                        continue;
                    }

                    try
                    {
                        _device1 = device.QueryInterface <D3D11.ID3D11Device1>();
                        _device3 = SeeingSharpUtil.TryExecute(() => _device1.QueryInterface <D3D11.ID3D11Device3>());

                        if (_device3 != null)
                        {
                            _immediateContext3 = _device3.ImmediateContext3;
                        }
                    }
                    finally
                    {
                        SeeingSharpUtil.SafeDispose(ref device);
                    }

                    // Device successfully created, save all parameters and break this loop
                    _featureLevel    = actFeatureLevel;
                    _creationFlags   = actCreateFlags;
                    this.DriverLevel = actDriverLevel;
                    break;
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            // Throw exception on failure
            if (_device1 == null)
            {
                throw new SeeingSharpGraphicsException("Unable to initialize d3d11 device!");
            }

            // Get immediate context from the device
            _immediateContext = _device1.ImmediateContext;
        }
 public void Dispose()
 {
     SeeingSharpUtil.SafeDispose(ref IndexBuffer);
     SeeingSharpUtil.SafeDispose(ref VertexBuffer);
 }
Пример #21
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public override void Dispose()
 {
     SeeingSharpUtil.SafeDispose(ref _d2dGeometry);
 }
Пример #22
0
 public void UnloadResources()
 {
     SeeingSharpUtil.SafeDispose(ref _deviceContextD2D);
     SeeingSharpUtil.SafeDispose(ref _deviceD2D);
     SeeingSharpUtil.SafeDispose(ref _renderTarget);
 }
Пример #23
0
 /// <inheritdoc />
 public void Dispose()
 {
     SeeingSharpUtil.SafeDispose(ref _bufferColor);
 }
Пример #24
0
 /// <summary>
 /// Unloads all resources.
 /// </summary>
 public void Dispose()
 {
     SeeingSharpUtil.SafeDispose(ref _factory);
 }
Пример #25
0
        /// <summary>
        /// Applies the given size.
        /// </summary>
        /// <param name="renderState">The render state used for creating all resources.</param>
        public void ApplySize(RenderState renderState)
        {
            var viewInfo   = renderState.ViewInformation !;
            var viewConfig = viewInfo.ViewConfiguration;

            // Get current view size and antialiasing settings
            var currentViewSize            = viewInfo.CurrentViewSize;
            var currentAntialiasingEnabled = viewConfig.AntialiasingEnabled;
            var currentAntialiasingQuality = viewConfig.AntialiasingQuality;

            if (_width != currentViewSize.Width ||
                _height != currentViewSize.Height ||
                _antialiasingEnabled != currentAntialiasingEnabled ||
                _antialiasingQuality != currentAntialiasingQuality ||
                _forceRecreateResources)
            {
                _forceRecreateResources = false;

                // Dispose color-buffer resources
                SeeingSharpUtil.SafeDispose(ref _colorBuffer);
                SeeingSharpUtil.SafeDispose(ref _colorBufferRenderTargetView);
                SeeingSharpUtil.SafeDispose(ref _colorBufferShaderResourceView);
                if (_shaderResourceCreated)
                {
                    SeeingSharpUtil.SafeDispose(ref _colorBufferShaderResource);
                }

                // Dispose depth-buffer resources
                SeeingSharpUtil.SafeDispose(ref _depthBufferView);
                SeeingSharpUtil.SafeDispose(ref _depthBuffer);

                // Dispose object-id buffer
                SeeingSharpUtil.SafeDispose(ref _objectIdBufferRenderTargetView);
                SeeingSharpUtil.SafeDispose(ref _objectIdBuffer);

                // Dispose normal-depth resources
                SeeingSharpUtil.SafeDispose(ref _normalDepthBuffer);
                SeeingSharpUtil.SafeDispose(ref _normalDepthBufferRenderTargetView);
                SeeingSharpUtil.SafeDispose(ref _normalDepthBufferShaderResourceView);
                if (_shaderResourceCreated)
                {
                    SeeingSharpUtil.SafeDispose(ref _normalDepthBufferShaderResource);
                }

                // Create color-buffer resources
                if (_creationMode.HasFlag(RenderTargetCreationMode.Color))
                {
                    _colorBuffer = GraphicsHelper.Internals.CreateRenderTargetTexture(
                        renderState.Device, currentViewSize.Width, currentViewSize.Height, viewInfo.ViewConfiguration);
                    _colorBufferShaderResource = _colorBuffer;
                    if (viewInfo.ViewConfiguration.AntialiasingEnabled)
                    {
                        _colorBufferShaderResource = GraphicsHelper.Internals.CreateTexture(renderState.Device, currentViewSize.Width, currentViewSize.Height);
                        _shaderResourceCreated     = true;
                    }
                    else
                    {
                        _shaderResourceCreated = false;
                    }
                    _colorBufferRenderTargetView   = renderState.Device.DeviceD3D11_1.CreateRenderTargetView(_colorBuffer);
                    _colorBufferShaderResourceView = renderState.Device.DeviceD3D11_1.CreateShaderResourceView(_colorBufferShaderResource);
                }

                // Create depth-buffer resources
                if (_creationMode.HasFlag(RenderTargetCreationMode.Depth))
                {
                    _depthBuffer = GraphicsHelper.Internals.CreateDepthBufferTexture(
                        renderState.Device, currentViewSize.Width, currentViewSize.Height, viewInfo.ViewConfiguration);
                    _depthBufferView = GraphicsHelper.Internals.CreateDepthBufferView(renderState.Device, _depthBuffer);
                }

                // Create object-id resources
                if (_creationMode.HasFlag(RenderTargetCreationMode.ObjectId))
                {
                    _objectIdBuffer = GraphicsHelper.Internals.CreateRenderTargetTextureObjectIds(
                        renderState.Device, currentViewSize.Width, currentViewSize.Height, viewInfo.ViewConfiguration);
                    _objectIdBufferRenderTargetView = renderState.Device.DeviceD3D11_1.CreateRenderTargetView(_objectIdBuffer);
                }

                // Create normal-depth buffer resources
                if (_creationMode.HasFlag(RenderTargetCreationMode.NormalDepth))
                {
                    _normalDepthBuffer = GraphicsHelper.Internals.CreateRenderTargetTextureNormalDepth(
                        renderState.Device, currentViewSize.Width, currentViewSize.Height, viewInfo.ViewConfiguration);
                    _normalDepthBufferShaderResource = _normalDepthBuffer;
                    if (_shaderResourceCreated)
                    {
                        _normalDepthBufferShaderResource = GraphicsHelper.Internals.CreateTexture(
                            renderState.Device, currentViewSize.Width, currentViewSize.Height, GraphicsHelper.Internals.DEFAULT_TEXTURE_FORMAT_NORMAL_DEPTH);
                    }
                    _normalDepthBufferRenderTargetView   = renderState.Device.DeviceD3D11_1.CreateRenderTargetView(_normalDepthBuffer);
                    _normalDepthBufferShaderResourceView = renderState.Device.DeviceD3D11_1.CreateShaderResourceView(_normalDepthBufferShaderResource);
                }

                // Remember values
                _width  = currentViewSize.Width;
                _height = currentViewSize.Height;
                _antialiasingEnabled = currentAntialiasingEnabled;
                _antialiasingQuality = currentAntialiasingQuality;
                _viewportF           = renderState.Viewport;
            }
        }
Пример #26
0
 public void Dispose()
 {
     SeeingSharpUtil.SafeDispose(ref _wicBitmapSource);
 }
Пример #27
0
 public void Dispose()
 {
     SeeingSharpUtil.SafeDispose(ref _copyHelperTextureStaging);
     SeeingSharpUtil.SafeDispose(ref _copyHelperTextureStandard);
     _isDisposed = true;
 }
 /// <summary>
 /// Unloads the resource.
 /// </summary>
 protected internal override void UnloadShader()
 {
     SeeingSharpUtil.SafeDispose(ref _geometryShader);
 }