public RenderToTextureScene(IEye eye, DisplayMode desctopDisplayMode)
            : base(eye, desctopDisplayMode)
        {
            var vertexShader = Device.Create.VertexShader(ShaderParser.Parse(VertexShaderText));
            var pixelShader = Device.Create.PixelShader(ShaderParser.Parse(PixelShaderText));
            shaderCombination = Device.Create.ShaderCombination(vertexShader, null, null, null, pixelShader);

            var meshFactory = new MeshFactory(Device, Handedness.Right, Winding.Clockwise);
            cubeMesh = meshFactory.CreateCube(2.0f);

            vertexLayout = Device.Create.VertexLayout(vertexShader, new[]
            {
                new VertexLayoutElement(ExplicitFormat.R32G32B32_FLOAT, 0, 0),
                new VertexLayoutElement(ExplicitFormat.R32G32B32_FLOAT, 0, 12),
                new VertexLayoutElement(ExplicitFormat.R32G32_FLOAT, 0, 24)
            });

            transformBuffer = Device.Create.Buffer(
                new BufferDescription { SizeInBytes = Transform.SizeInBytes, BindFlags = BindFlags.UniformBuffer, Usage = Usage.Dynamic });
            cameraVertexBuffer = Device.Create.Buffer(
                new BufferDescription { SizeInBytes = CameraVertex.SizeInBytes, BindFlags = BindFlags.UniformBuffer, Usage = Usage.Dynamic });
            lightBuffer = Device.Create.Buffer(
                new BufferDescription { SizeInBytes = Light.SizeInBytes, BindFlags = BindFlags.UniformBuffer, Usage = Usage.Dynamic });

            var renderTargetFormats = Eye.Adapters[0]
                .GetSupportedFormats(FormatSupport.Texture2D | FormatSupport.RenderTarget | FormatSupport.MipAutogen)
                .Where(fi => fi.ColorBits <= 24);
            if (!renderTargetFormats.Any()) throw new NotSupportedException("Render target textures are not supported.");
            var renderTargetFormatInfo = renderTargetFormats
                .OrderByDescending(fi => fi.ColorBits)
                .ThenBy(fi => fi.TotalBits)
                .First();

            var renderTargetTexture = Device.Create.Texture2D(new Texture2DDescription
            {
                Width = TargetSize,
                Height = TargetSize,
                MipLevels = TextureHelper.MipLevels(TargetSize, TargetSize, 1),
                ArraySize = 1,
                FormatID = renderTargetFormatInfo.ID,
                Sampling = Sampling.NoMultisampling,
                Usage = Usage.Default,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                MiscFlags = MiscFlags.GenerateMips,
                ExtraFlags = ExtraFlags.None
            });
            targetRtv = renderTargetTexture.ViewAsRenderTarget(renderTargetFormatInfo.ID, 0);
            targetSrv = renderTargetTexture.ViewAsShaderResource(renderTargetFormatInfo.ID, 0, renderTargetTexture.MipLevels);

            var dsFormats = Eye.Adapters[0].GetSupportedFormats(FormatSupport.Texture2D | FormatSupport.DepthStencil);
            var dsFormatInfo = dsFormats
                .OrderBy(fi => (fi.ColorBits == 24 && fi.AlphaBits == 8) ? 0 : 1)
                .ThenByDescending(fi => fi.ColorBits)
                .ThenBy(fi => fi.TotalBits)
                .First();

            var depthStencilTexture = Device.Create.Texture2D(new Texture2DDescription
            {
                Width = TargetSize,
                Height = TargetSize,
                MipLevels = 1,
                ArraySize = 1,
                FormatID = dsFormatInfo.ID,
                Sampling = Sampling.NoMultisampling,
                Usage = Usage.Default,
                BindFlags = BindFlags.DepthStencil,
                MiscFlags = MiscFlags.None,
                ExtraFlags = ExtraFlags.None
            });
            targetDsv = depthStencilTexture.ViewAsDepthStencil(dsFormatInfo.ID, DepthStencilViewFlags.None, 0);

            var textureLoader = new TextureLoader(Device);

            var diffuseTexture = textureLoader.Load("../Textures/DiffuseTest.png");
            diffuseView = diffuseTexture.ViewAsShaderResource(diffuseTexture.FormatID, 0, diffuseTexture.MipLevels);

            samplerState = Device.Create.SamplerState(SamplerDescription.Anisotropic);
            depthStencilState = Device.Create.DepthStencilState(DepthStencilDescription.Enabled);
        }
Beispiel #2
0
        public CubeScene(IEye eye, DisplayMode desctopDisplayMode)
            : base(eye, desctopDisplayMode)
        {
            var vertexShader = Device.Create.VertexShader(ShaderParser.Parse(VertexShaderText));
            var pixelShader = Device.Create.PixelShader(ShaderParser.Parse(PixelShaderText));
            shaderCombination = Device.Create.ShaderCombination(vertexShader, null, null, null, pixelShader);

            var meshFactory = new MeshFactory(Device, Handedness.Right, Winding.Clockwise);
            cubeMesh = meshFactory.CreateCube(2.0f);

            vertexLayout = Device.Create.VertexLayout(vertexShader, new[]
            {
                new VertexLayoutElement(ExplicitFormat.R32G32B32_FLOAT, 0, 0),
                new VertexLayoutElement(ExplicitFormat.R32G32B32_FLOAT, 0, 12),
                new VertexLayoutElement(ExplicitFormat.R32G32_FLOAT, 0, 24)
            });

            transformBuffer = Device.Create.Buffer(
                new BufferDescription { SizeInBytes = Transform.SizeInBytes, BindFlags = BindFlags.UniformBuffer, Usage = Usage.Dynamic });
            cameraVertexBuffer = Device.Create.Buffer(
                new BufferDescription { SizeInBytes = CameraVertex.SizeInBytes, BindFlags = BindFlags.UniformBuffer, Usage = Usage.Dynamic });
            cameraPixelBuffer = Device.Create.Buffer(
                new BufferDescription { SizeInBytes = CameraPixel.SizeInBytes, BindFlags = BindFlags.UniformBuffer, Usage = Usage.Dynamic });
            lightBuffer = Device.Create.Buffer(
                new BufferDescription { SizeInBytes = Light.SizeInBytes, BindFlags = BindFlags.UniformBuffer, Usage = Usage.Dynamic });

            var textureLoader = new TextureLoader(Device);

            var diffuseTexture = textureLoader.Load("../Textures/DiffuseTest.png");
            diffuseView = diffuseTexture.ViewAsShaderResource(diffuseTexture.FormatID, 0, diffuseTexture.MipLevels);

            var specularTexture = textureLoader.Load("../Textures/SpecularTest.png");
            specualrView = specularTexture.ViewAsShaderResource(specularTexture.FormatID, 0, specularTexture.MipLevels);

            samplerState = Device.Create.SamplerState(SamplerDescription.Default);
            depthStencilState = Device.Create.DepthStencilState(DepthStencilDescription.Enabled);
        }