Пример #1
0
    private Material CreateLineMaterial(bool occludedOnly)
    {
        var blendState = new BlendSpec();

        blendState.blendEnable = true;
        blendState.srcBlend    = BlendOperand.SrcAlpha;
        blendState.destBlend   = BlendOperand.InvSrcAlpha;
        var depthState = new DepthStencilSpec();

        depthState.depthEnable = true;
        depthState.depthWrite  = false;
        if (occludedOnly)
        {
            depthState.depthFunc = ComparisonFunc.GreaterEqual;
        }

        using var vs = _device.GetShaders().LoadVertexShader("line_vs");
        using var ps = _device.GetShaders().LoadPixelShader("diffuse_only_ps");

        return(_device.CreateMaterial(
                   blendState,
                   depthState,
                   null,
                   null,
                   vs.Resource,
                   ps.Resource
                   ));
    }
Пример #2
0
    private static Material CreateMaterial(RenderingDevice device)
    {
        var blendState = new BlendSpec
        {
            blendEnable = true,
            srcBlend    = BlendOperand.SrcAlpha,
            destBlend   = BlendOperand.InvSrcAlpha
        };

        var depthStencilState = new DepthStencilSpec();

        depthStencilState.depthEnable = false;

        var vertexShader = device.GetShaders().LoadVertexShader("font_vs");
        var pixelShader  = device.GetShaders().LoadPixelShader("textured_simple_ps");

        return(device.CreateMaterial(
                   blendState,
                   depthStencilState,
                   null,
                   null,
                   vertexShader,
                   pixelShader
                   ));
    }
Пример #3
0
    private ResourceRef <Material> CreateMaterial(RenderingDevice device)
    {
        var blendState = new BlendSpec
        {
            blendEnable = true,
            srcBlend    = BlendOperand.SrcAlpha,
            destBlend   = BlendOperand.InvSrcAlpha
        };
        var depthState = new DepthStencilSpec();

        depthState.depthEnable = false;
        depthState.depthWrite  = false;

        var rasterizerState = new RasterizerSpec();
        var vs = _device.GetShaders().LoadVertexShader("diffuse_only_vs");
        var ps = _device.GetShaders().LoadPixelShader("diffuse_only_ps");

        return(device.CreateMaterial(
                   blendState,
                   depthState,
                   rasterizerState,
                   Array.Empty <MaterialSamplerSpec>(),
                   vs,
                   ps).Ref());
    }
Пример #4
0
    private static Material CreateMaterial(RenderingDevice device,
                                           string pixelShaderName,
                                           bool forLine  = false,
                                           bool blending = true)
    {
        BlendSpec blendSpec = new BlendSpec();

        blendSpec.blendEnable = blending;
        blendSpec.srcBlend    = BlendOperand.SrcAlpha;
        blendSpec.destBlend   = BlendOperand.InvSrcAlpha;
        DepthStencilSpec depthStencilSpec = new DepthStencilSpec();

        depthStencilSpec.depthEnable = false;
        var vsDefines = new Dictionary <string, string>();

        if (forLine)
        {
            vsDefines["DRAW_LINES"] = "1";
        }

        using var vertexShader = device.GetShaders().LoadVertexShader("gui_vs", vsDefines);
        using var pixelShader  = device.GetShaders().LoadPixelShader(pixelShaderName);

        return(device.CreateMaterial(blendSpec, depthStencilSpec, null,
                                     null, vertexShader.Resource, pixelShader.Resource));
    }
Пример #5
0
    private static ResourceRef <Material> CreateGaussBlurMaterial(RenderingDevice device,
                                                                  RenderTargetTexture texture,
                                                                  bool horizontal)
    {
        var samplerState = new SamplerSpec();

        samplerState.addressU  = TextureAddress.Clamp;
        samplerState.addressV  = TextureAddress.Clamp;
        samplerState.magFilter = TextureFilterType.Linear;
        samplerState.minFilter = TextureFilterType.Linear;
        samplerState.mipFilter = TextureFilterType.Linear;
        var rasterizerState = new RasterizerSpec();

        rasterizerState.cullMode = CullMode.None;
        var depthStencilState = new DepthStencilSpec();

        depthStencilState.depthEnable = false;

        var vs         = device.GetShaders().LoadVertexShader("gaussian_blur_vs");
        var horDefines = new Dictionary <string, string>();

        if (horizontal)
        {
            horDefines["HOR"] = "1";
        }

        var ps = device.GetShaders().LoadPixelShader("gaussian_blur_ps", horDefines);

        var samplers = new MaterialSamplerSpec[]
        {
            new (new ResourceRef <ITexture>(texture), samplerState)
        };

        return(device.CreateMaterial(null, depthStencilState, rasterizerState, samplers, vs, ps).Ref());
    }
Пример #6
0
    private ResourceRef <Material> ParseMaterial(JsonElement root)
    {
        if (!root.TryGetProperty("vertexShader", out var vertexShaderEl))
        {
            throw new InvalidMaterialException(_path, "A vertexShader is required.");
        }

        var vertexShader = _device.GetShaders().LoadVertexShader(vertexShaderEl.GetString());

        if (!root.TryGetProperty("fragmentShader", out var fragmentShaderEl))
        {
            throw new InvalidMaterialException(_path, "A fragmentShader is required.");
        }

        var fragmentShader = _device.GetShaders().LoadPixelShader(fragmentShaderEl.GetString());

        var blendState = new BlendSpec();

        if (root.TryGetProperty("blend", out var blendEl))
        {
            ParseBlendSpec(blendEl, blendState);
        }

        var depthStencil = new DepthStencilSpec();

        if (root.TryGetProperty("depthStencil", out var depthStencilEl))
        {
            ParseDepthStencil(depthStencilEl, depthStencil);
        }

        var rasterizer = new RasterizerSpec();

        if (root.TryGetProperty("rasterizer", out var rasterizerEl))
        {
            ParseRasterizer(rasterizerEl, rasterizer);
        }

        var samplers = new List <MaterialSamplerSpec>();

        if (root.TryGetProperty("samplers", out var samplersEl))
        {
            foreach (var samplerEl in samplersEl.EnumerateArray())
            {
                var samplerState = new SamplerSpec();
                ParseSampler(samplerEl, samplerState, out var texture);
                samplers.Add(new MaterialSamplerSpec(texture, samplerState));
            }
        }

        return(_device.CreateMaterial(
                   blendState,
                   depthStencil,
                   rasterizer,
                   samplers.ToArray(),
                   vertexShader,
                   fragmentShader
                   ).Ref());
    }
Пример #7
0
    private static Material CreateOutlineMaterial(RenderingDevice device)
    {
        BlendSpec blendSpec = new BlendSpec();

        blendSpec.blendEnable = true;
        blendSpec.srcBlend    = BlendOperand.SrcAlpha;
        blendSpec.destBlend   = BlendOperand.InvSrcAlpha;
        DepthStencilSpec depthStencilSpec = new DepthStencilSpec();

        depthStencilSpec.depthEnable = false;
        using var vertexShader       = device.GetShaders().LoadVertexShader("line_vs");
        using var pixelShader        = device.GetShaders().LoadPixelShader("diffuse_only_ps");

        return(device.CreateMaterial(blendSpec, depthStencilSpec, null, null, vertexShader, pixelShader));
    }
Пример #8
0
    private static ResourceRef <Material> CreateShadowMapMaterial(RenderingDevice device)
    {
        DepthStencilSpec depthStencilState = new DepthStencilSpec();

        depthStencilState.depthEnable = false;
        using var vs = device.GetShaders().LoadVertexShader("shadowmap_geom_vs");
        using var ps = device.GetShaders().LoadPixelShader("diffuse_only_ps");

        BlendSpec blendSpec = new BlendSpec();

        blendSpec.blendEnable    = true;
        blendSpec.destBlend      = BlendOperand.Zero;
        blendSpec.srcBlend       = BlendOperand.SrcAlpha;
        blendSpec.destAlphaBlend = BlendOperand.Zero;
        blendSpec.srcAlphaBlend  = BlendOperand.One;

        return(device.CreateMaterial(blendSpec, depthStencilState, null, null, vs, ps).Ref());
    }
Пример #9
0
    private static ResourceRef <Material> CreateGeometryShadowMaterial(RenderingDevice device)
    {
        BlendSpec blendState = new BlendSpec();

        blendState.blendEnable = true;
        blendState.srcBlend    = BlendOperand.SrcAlpha;
        blendState.destBlend   = BlendOperand.InvSrcAlpha;
        RasterizerSpec rasterizerState = new RasterizerSpec();

        rasterizerState.cullMode = CullMode.None;
        DepthStencilSpec depthStencilState = new DepthStencilSpec();

        depthStencilState.depthWrite = false;

        using var vs = device.GetShaders().LoadVertexShader("shadow_geom_vs");
        using var ps = device.GetShaders().LoadPixelShader("diffuse_only_ps");

        return(device.CreateMaterial(blendState, depthStencilState, rasterizerState, null, vs, ps).Ref());
    }
Пример #10
0
    private Material CreatePieFillMaterial(RenderingDevice device)
    {
        BlendSpec blendSpec = new BlendSpec();

        blendSpec.blendEnable = true;
        blendSpec.srcBlend    = BlendOperand.SrcAlpha;
        blendSpec.destBlend   = BlendOperand.InvSrcAlpha;
        DepthStencilSpec depthStencilSpec = new DepthStencilSpec();

        depthStencilSpec.depthEnable = false;
        RasterizerSpec rasterizerSpec = new RasterizerSpec();

        rasterizerSpec.cullMode = CullMode.None;

        using var vertexShader = _device.GetShaders().LoadVertexShader("gui_vs");
        using var pixelShader  = _device.GetShaders().LoadPixelShader("diffuse_only_ps");

        return(_device.CreateMaterial(blendSpec, depthStencilSpec, rasterizerSpec,
                                      null, vertexShader, pixelShader));
    }
Пример #11
0
    private void CreateMaterial()
    {
        using var ps = _device.GetShaders().LoadPixelShader("fogofwar_ps");
        var blendState = new BlendSpec();

        blendState.blendEnable = true;
        blendState.srcBlend    = BlendOperand.SrcAlpha;
        blendState.destBlend   = BlendOperand.InvSrcAlpha;
        DepthStencilSpec depthStencilState = new DepthStencilSpec();

        depthStencilState.depthEnable = false;
        RasterizerSpec rasterizerState = new RasterizerSpec();
        SamplerSpec    samplerState    = new SamplerSpec();

        samplerState.addressU  = TextureAddress.Clamp;
        samplerState.addressV  = TextureAddress.Clamp;
        samplerState.magFilter = TextureFilterType.Linear;
        samplerState.minFilter = TextureFilterType.Linear;

        MaterialSamplerSpec[] samplers =
        {
            new(new ResourceRef <ITexture>(_blurredFogTexture.Resource), samplerState)
        };
Пример #12
0
    public FogOfWarRenderer(MapFoggingSystem fogSystem, RenderingDevice device)
    {
        _device    = device;
        _fogSystem = fogSystem;

        _vertexShader  = device.GetShaders().LoadVertexShader("fogofwar_vs");
        _bufferBinding = new BufferBinding(device, _vertexShader).Ref();

        UpdateBufferSize(true);

        Span <ushort> indices = stackalloc ushort[]
        {
            0, 2, 1, 2, 0, 3
        };

        _indexBuffer  = device.CreateIndexBuffer(indices, debugName: "FogOfWar");
        _vertexBuffer = device.CreateEmptyVertexBuffer(FogOfWarVertex.Size * 4, debugName: "FogOfWar");

        _bufferBinding.Resource
        .AddBuffer <FogOfWarVertex>(_vertexBuffer, 0)
        .AddElement(VertexElementType.Float3, VertexElementSemantic.Position)
        .AddElement(VertexElementType.Float2, VertexElementSemantic.TexCoord);
    }
Пример #13
0
    public static Material CreateMaterial(RenderingDevice device,
                                          PartSysEmitter emitter,
                                          bool pointSprites)
    {
        var blendState = new BlendSpec();

        blendState.blendEnable = true;

        switch (emitter.GetSpec().GetBlendMode())
        {
        case PartSysBlendMode.Add:
            blendState.srcBlend  = BlendOperand.SrcAlpha;
            blendState.destBlend = BlendOperand.One;
            break;

        case PartSysBlendMode.Subtract:
            blendState.srcBlend  = BlendOperand.Zero;
            blendState.destBlend = BlendOperand.InvSrcAlpha;
            break;

        case PartSysBlendMode.Blend:
            blendState.srcBlend  = BlendOperand.SrcAlpha;
            blendState.destBlend = BlendOperand.InvSrcAlpha;
            break;

        case PartSysBlendMode.Multiply:
            blendState.srcBlend  = BlendOperand.Zero;
            blendState.destBlend = BlendOperand.SrcColor;
            break;

        default:
            break;
        }

        // Particles respect the depth buffer, but do not modify it
        DepthStencilSpec depthStencilState = new DepthStencilSpec();

        depthStencilState.depthEnable = true;
        depthStencilState.depthWrite  = false;
        RasterizerSpec rasterizerState = new RasterizerSpec();

        rasterizerState.cullMode = CullMode.None;


        var samplers = new List <MaterialSamplerSpec>();

        var shaderName  = "diffuse_only_ps";
        var textureName = emitter.GetSpec().GetTextureName();

        if (textureName.Length > 0)
        {
            var samplerState = new SamplerSpec
            {
                addressU  = TextureAddress.Clamp,
                addressV  = TextureAddress.Clamp,
                minFilter = TextureFilterType.Linear,
                magFilter = TextureFilterType.Linear,
                mipFilter = TextureFilterType.Linear
            };
            var texture = device.GetTextures().Resolve(textureName, true);
            samplers.Add(new MaterialSamplerSpec(texture, samplerState));
            shaderName = "textured_simple_ps";
        }

        using var pixelShader = device.GetShaders().LoadPixelShader(shaderName);

        var vsName = pointSprites ? "particles_points_vs" : "particles_quads_vs";

        using var vertexShader = device.GetShaders().LoadVertexShader(vsName);

        return(device.CreateMaterial(blendState, depthStencilState, rasterizerState,
                                     samplers.ToArray(), vertexShader, pixelShader));
    }