Ejemplo n.º 1
0
        protected override void Initialize(DeviceChild view)
        {
            // The initialize method will override the view.Tag, so we are setting it back
            base.Initialize(view);

            IsRenderView = view is RenderTargetView;

            var shaderResourceView = view as ShaderResourceView;
            int mipLevel           = 0;

            if (shaderResourceView != null)
            {
                var description = shaderResourceView.Description;
                mipLevel = description.Texture1D.MostDetailedMip;
            }
            else
            {
                var renderTargetView = view as RenderTargetView;
                if (renderTargetView == null)
                {
                    throw new ArgumentException("Expecting argument to be a ShaderResourceView or RenderTargetView", "view");
                }
                mipLevel = renderTargetView.Description.Texture1D.MipSlice;
            }
            Size = new Size2(Math.Max(1, Texture.Width >> mipLevel), Math.Max(1, Texture.Height >> mipLevel));

            TexelSize = new Size2F
            {
                Width  = 1.0f / Size.Width,
                Height = 1.0f / Size.Height
            };
        }
Ejemplo n.º 2
0
        public static unsafe void SetDebugName(this DeviceChild child, string debugName)
        {
            byte[] debugNameBytes = Encoding.ASCII.GetBytes(debugName);

            fixed(byte *debugNameBytesPtr = debugNameBytes)
            child.SetPrivateData(CommonGuid.DebugObjectName, debugNameBytes.Length, new IntPtr(debugNameBytesPtr));
        }
Ejemplo n.º 3
0
        protected override void CompileInternal(ShaderStage shaderStage, string filePath, VertexType vertexType)
        {
            var macros = Macros.Select(macro => new ShaderMacro(macro.Key, macro.Value)).ToArray();

            using var includeHandler = new IncludeHandler(null);

            _shaderBytecode = LoadBytecode(shaderStage, filePath, includeHandler, macros);

            if (shaderStage == ShaderStage.Vertex && vertexType != VertexType.Unknown)
            {
                InputLayout = _graphicsFactory.CreateInputLayout(vertexType, _shaderBytecode);
            }

            switch (shaderStage)
            {
            case ShaderStage.Vertex:
                _shaderObject = new VertexShader(_graphicsDevice, _shaderBytecode);
                break;

            case ShaderStage.Pixel:
                _shaderObject = new PixelShader(_graphicsDevice, _shaderBytecode);
                break;

            case ShaderStage.Compute:
                _shaderObject = new ComputeShader(_graphicsDevice, _shaderBytecode);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(shaderStage), shaderStage, null);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes the specified device local.
 /// </summary>
 /// <param name="resource">The resource.</param>
 protected virtual void Initialize(DeviceChild resource)
 {
     Resource = ToDispose(resource);
     if (resource != null)
     {
         resource.Tag = this;
     }
 }
Ejemplo n.º 5
0
 protected override void Dispose(bool disposeManagedResources)
 {
     base.Dispose(disposeManagedResources);
     if (disposeManagedResources)
     {
         Resource = null;
     }
 }
Ejemplo n.º 6
0
 public static int GetID(DeviceChild deviceChild)
 {
     if (deviceChild == null)
     {
         return(-1);
     }
     return(deviceChild.ID);
 }
Ejemplo n.º 7
0
        protected override void CompileStringInternal(ShaderStage shaderStage, string shaderText, VertexType vertexType)
        {
            var macros = Macros.Select(macro => new ShaderMacro(macro.Key, macro.Value)).ToArray();

            using var includeHandler = new IncludeHandler(null);

            _shaderBytecode = CreateBytecode(shaderStage, shaderText, includeHandler, macros);
            _shaderObject   = CreateShader(shaderStage, vertexType);
        }
Ejemplo n.º 8
0
        internal TextureView(Texture texture, DeviceChild view) : base(texture.GraphicsDevice)
        {
            if (view == null)
            {
                throw new ArgumentNullException("view");
            }

            Texture     = texture;
            Description = texture.Description;

            Initialize(view);
        }
Ejemplo n.º 9
0
        protected override void Initialize(DeviceChild view)
        {
            // The initialize method will override the view.Tag, so we are setting it back
            base.Initialize(view);

            var  shaderResourceView = view as ShaderResourceView;
            int  mipLevel           = 0;
            bool isMultisampled     = Texture.Description.SampleDescription.Count > 1;

            if (shaderResourceView != null)
            {
                mipLevel = isMultisampled ? 0 : shaderResourceView.Description.Texture1D.MostDetailedMip;
            }
            else
            {
                var renderTargetView = view as RenderTargetView;
                if (renderTargetView != null)
                {
                    IsRenderView = true;
                    mipLevel     = isMultisampled ? 0 : renderTargetView.Description.Texture1D.MipSlice;
                }
                else
                {
                    var depthStencilView = view as DepthStencilView;
                    if (depthStencilView != null)
                    {
                        IsDepthStencilView = true;
                        mipLevel           = isMultisampled ? 0 : depthStencilView.Description.Texture1D.MipSlice;
                    }
                    else
                    {
                        throw new ArgumentException("Expecting argument to be a ShaderResourceView, RenderTargetView or DepthStencilView", "view");
                    }
                }
            }
            Size = new Size2(Math.Max(1, Texture.Width >> mipLevel), Math.Max(1, Texture.Height >> mipLevel));

            TexelSize = new Size2F
            {
                Width  = 1.0f / Size.Width,
                Height = 1.0f / Size.Height
            };
        }
Ejemplo n.º 10
0
 protected override void Initialize(DeviceChild resource)
 {
     base.Initialize(resource);
     this.InitializeViews();
 }
Ejemplo n.º 11
0
        internal DeviceChild GetOrCompileShader(EffectShaderType shaderType, int index, int soRasterizedStream, StreamOutputElement[] soElements, out string profileError)
        {
            DeviceChild shader = null;

            profileError = null;
            lock (sync)
            {
                shader = compiledShadersGroup[(int)shaderType][index];
                if (shader == null)
                {
                    if (RegisteredShaders[index].Level > graphicsDevice.Features.Level)
                    {
                        profileError = string.Format("{0}", RegisteredShaders[index].Level);
                        return(null);
                    }

                    var bytecodeRaw = RegisteredShaders[index].Bytecode;
                    switch (shaderType)
                    {
                    case EffectShaderType.Vertex:
                        shader = new VertexShader(graphicsDevice, bytecodeRaw);
                        break;

                    case EffectShaderType.Domain:
                        shader = new DomainShader(graphicsDevice, bytecodeRaw);
                        break;

                    case EffectShaderType.Hull:
                        shader = new HullShader(graphicsDevice, bytecodeRaw);
                        break;

                    case EffectShaderType.Geometry:
                        if (soElements != null)
                        {
                            // Calculate the strides
                            var soStrides = new List <int>();
                            foreach (var streamOutputElement in soElements)
                            {
                                for (int i = soStrides.Count; i < (streamOutputElement.Stream + 1); i++)
                                {
                                    soStrides.Add(0);
                                }

                                soStrides[streamOutputElement.Stream] += streamOutputElement.ComponentCount * sizeof(float);
                            }
                            shader = new GeometryShader(graphicsDevice, bytecodeRaw, soElements, soStrides.ToArray(), soRasterizedStream);
                        }
                        else
                        {
                            shader = new GeometryShader(graphicsDevice, bytecodeRaw);
                        }
                        break;

                    case EffectShaderType.Pixel:
                        shader = new PixelShader(graphicsDevice, bytecodeRaw);
                        break;

                    case EffectShaderType.Compute:
                        shader = new ComputeShader(graphicsDevice, bytecodeRaw);
                        break;
                    }
                    compiledShadersGroup[(int)shaderType][index] = ToDispose(shader);
                }
            }
            return(shader);
        }
Ejemplo n.º 12
0
 public static int GetID(DeviceChild deviceChild)
 {
     if (deviceChild == null)
         return -1;
     return deviceChild.ID;
 }
 public GraphicsObjectViewModel(DeviceChild deviceChild)
 {
     _deviceChild = deviceChild;
 }
Ejemplo n.º 14
0
        private void PlatformConstruct(
            GraphicsDevice graphicsDevice,
            string functionName,
            byte[] deviceBytecode,
            out ShaderType shaderType,
            out ShaderResourceBinding[] resourceBindings)
        {
            DeviceBytecode = deviceBytecode;

            using (var shaderBytecode = new ShaderBytecode(DeviceBytecode))
            {
                switch (shaderBytecode.GetVersion().Version)
                {
                case ShaderVersion.VertexShader:
                    DeviceShader = AddDisposable(new VertexShader(graphicsDevice.Device, DeviceBytecode));
                    shaderType   = ShaderType.VertexShader;
                    break;

                case ShaderVersion.PixelShader:
                    DeviceShader = AddDisposable(new PixelShader(graphicsDevice.Device, DeviceBytecode));
                    shaderType   = ShaderType.PixelShader;
                    break;

                default:
                    throw new NotSupportedException();
                }
            }

            using (var reflection = new ShaderReflection(DeviceBytecode))
            {
                resourceBindings = new ShaderResourceBinding[reflection.Description.BoundResources];

                for (var i = 0; i < resourceBindings.Length; i++)
                {
                    var resourceDescription = reflection.GetResourceBindingDescription(i);

                    int constantBufferSizeInBytes;
                    ConstantBufferField[] constantBufferFields;
                    if (resourceDescription.Type == ShaderInputType.ConstantBuffer)
                    {
                        using (var constantBufferDesc = reflection.GetConstantBuffer(resourceDescription.Name))
                        {
                            constantBufferSizeInBytes = constantBufferDesc.Description.Size;
                            constantBufferFields      = new ConstantBufferField[constantBufferDesc.Description.VariableCount];
                            for (var j = 0; j < constantBufferFields.Length; j++)
                            {
                                var variable = constantBufferDesc.GetVariable(j);

                                constantBufferFields[j] = new ConstantBufferField(
                                    variable.Description.Name,
                                    variable.Description.StartOffset,
                                    variable.Description.Size);
                            }
                        }
                    }
                    else
                    {
                        constantBufferSizeInBytes = 0;
                        constantBufferFields      = null;
                    }

                    resourceBindings[i] = new ShaderResourceBinding(
                        resourceDescription.Name,
                        GetResourceType(resourceDescription.Type),
                        shaderType,
                        resourceDescription.BindPoint,
                        constantBufferSizeInBytes,
                        constantBufferFields);
                }
            }
        }
Ejemplo n.º 15
0
 public GraphicsObjectViewModel(DeviceChild deviceChild)
 {
     _deviceChild = deviceChild;
 }