public override RenderableLightPrimitive GetRenderablePrimitive() { LightShader shader = new LightShader(Renderer); using (ShaderBytecode bytecode = new ShaderBytecode(new DataStream(File.ReadAllBytes("Shaders\\DeferredPointLight.vs"), true, false))) { shader.VertexShader = new VertexShader(Renderer.Device, bytecode); shader.InputLayout = new InputLayout(Renderer.Device, bytecode, new[] { new InputElement("Position", 0, Format.R32G32B32_Float, sizeof(float) * 0, 0), }); } using (ShaderBytecode bytecode = new ShaderBytecode(new DataStream(File.ReadAllBytes("Shaders\\DeferredPointLightShadowless.ps"), true, false))) { shader.PixelShader = new PixelShader(Renderer.Device, bytecode); } using (ShaderBytecode bytecode = new ShaderBytecode(new DataStream(File.ReadAllBytes("Shaders\\DeferredPointLightShadowless.ps"), true, false))) { shader.PixelShaderShadowless = new PixelShader(Renderer.Device, bytecode); } shader.Topology = PrimitiveTopology.TriangleList; ConstantBufferWrapper MatricesCBuffer = new ConstantBufferWrapper(Renderer, sizeof(float) * 32, ShaderType.VertexShader, 0); ConstantBufferWrapper LightCBuffer = new ConstantBufferWrapper(Renderer, sizeof(float) * 64, ShaderType.PixelShader, 0); ConstantBufferWrapper ShadowCBuffer = new ConstantBufferWrapper(Renderer, sizeof(float) * 16 * 4, ShaderType.PixelShader, 1); MatricesCBuffer.Semantics.Add(Semantic.WorldViewProj); MatricesCBuffer.Semantics.Add(Semantic.World); LightCBuffer.Semantics.Add(Semantic.View); LightCBuffer.Semantics.Add(Semantic.ViewInverse); LightCBuffer.Semantics.Add(Semantic.ViewProjInverse); LightCBuffer.Semantics.Add(Semantic.CameraPosition); shader.ConstantBuffers.Add(MatricesCBuffer); shader.ConstantBuffers.Add(LightCBuffer); shader.ConstantBuffers.Add(ShadowCBuffer); GeometricPrimitive prim = new SpherePrimitive(Renderer, 1, 16); DataStream str = new DataStream(prim.GeometryData.Positions, true, false); Buffer vertexBuffer = new Buffer(Renderer.Device, str, new BufferDescription() { SizeInBytes = (int)str.Length, BindFlags = BindFlags.VertexBuffer, StructureByteStride = 3 * sizeof(float), CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default, }); int vertexCount = prim.GeometryData.VertexCount; DataStream IndicesStream = new DataStream(prim.GeometryData.Indices.ToArray(), true, true); int indexCount = prim.GeometryData.IndexCount; Buffer indexBuffer = new Buffer(Renderer.Device, IndicesStream, sizeof(ushort) * indexCount, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, sizeof(ushort)); prim.Dispose(); return new RenderableLightPrimitive(shader, vertexBuffer, vertexCount, indexBuffer, indexCount); }
public override void Initialize() { base.Initialize(); plStruct = new PointLightStruct[maxLights]; pLightBuffer = new StructuredBuffer<PointLightStruct>(maxLights, false, true); cBuffer = new ConstantBufferWrapper(Renderer, sizeof(float) * 32, ShaderType.PixelShader, 0); cBuffer.Semantics.Add(Semantic.Projection); cBuffer.Semantics.Add(Semantic.CameraNearFar); GBuffer = new GBuffer(Renderer); BlendStateDescription disabledBlendDesc = States.BlendDefault(); BlendStateDescription additiveDesc = States.BlendDefault(); additiveDesc.RenderTargets[0] = new RenderTargetBlendDescription() { BlendEnable = true, SourceBlend = BlendOption.One, DestinationBlend = BlendOption.One, BlendOperation = BlendOperation.Add, SourceBlendAlpha = BlendOption.One, DestinationBlendAlpha = BlendOption.One, BlendOperationAlpha = BlendOperation.Add, RenderTargetWriteMask = ColorWriteMaskFlags.All, }; DepthStencilStateDescription defaultDepthDesc = States.DepthDefault(); defaultDepthDesc.DepthComparison = Comparison.GreaterEqual; DepthStencilStateDescription equalDesc = new DepthStencilStateDescription() { IsDepthEnabled = true, DepthWriteMask = DepthWriteMask.Zero, DepthComparison = Comparison.GreaterEqual, IsStencilEnabled = true, StencilWriteMask = 0xFF, StencilReadMask = 0xFF, FrontFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Keep, PassOperation = StencilOperation.Keep, Comparison = Comparison.Equal, }, BackFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Keep, DepthFailOperation = StencilOperation.Keep, PassOperation = StencilOperation.Keep, Comparison = Comparison.Equal, } }; DepthStencilStateDescription writeDesc = new DepthStencilStateDescription() { IsDepthEnabled = false, DepthWriteMask = DepthWriteMask.Zero, DepthComparison = Comparison.GreaterEqual, IsStencilEnabled = true, StencilWriteMask = 0xFF, StencilReadMask = 0xFF, FrontFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Replace, DepthFailOperation = StencilOperation.Replace, PassOperation = StencilOperation.Replace, Comparison = Comparison.Always, }, BackFace = new DepthStencilOperationDescription() { FailOperation = StencilOperation.Replace, DepthFailOperation = StencilOperation.Replace, PassOperation = StencilOperation.Replace, Comparison = Comparison.Always, } }; RasterizerStateDescription rsDesc = States.RasterizerDefault(); mGeometryBlendState = BlendState.FromDescription(Renderer.Device, disabledBlendDesc); mLightingBlendState = BlendState.FromDescription(Renderer.Device, additiveDesc); mDepthState = DepthStencilState.FromDescription(Renderer.Device, defaultDepthDesc); mEqualStencilState = DepthStencilState.FromDescription(Renderer.Device, equalDesc); mWriteStencilState = DepthStencilState.FromDescription(Renderer.Device, writeDesc); mRasterizerState = RasterizerState.FromDescription(Renderer.Device, rsDesc); InitializeBuffers(); InitializeShaders(); }