public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; var loader = new BasicLoader(this.deviceResources.D3DDevice); loader.LoadShader("Components.VertexShader.cso", null, out this.vertexShader, out this.inputLayout); var shapes = new BasicShapes(this.deviceResources.D3DDevice); shapes.CreateReferenceAxis(out this.vertexBuffer, out this.indexBuffer, out this.vertexCount, out this.indexCount); var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer); this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc); loader.LoadShader("Components.PixelShader.cso", out this.pixelShader); loader.LoadTexture("texturedata.bin", 256, 256, out this.texture, out this.textureView); D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc( D3D11Filter.Anisotropic, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, 0.0f, this.deviceResources.D3DFeatureLevel > D3D11FeatureLevel.FeatureLevel91 ? D3D11Constants.DefaultMaxAnisotropy : D3D11Constants.FeatureLevel91DefaultMaxAnisotropy, D3D11ComparisonFunction.Never, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0.0f, float.MaxValue); this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); this.camera = new BasicCamera(); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; // Create the shaders byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso"); this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); // Create a layout for the object data D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "NORMAL", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "TEXCOORD", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 24, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); // Create state objects D3D11SamplerDesc samplerDesc = new( D3D11Filter.MinMagMipLinear, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, 0.0f, 1, D3D11ComparisonFunction.Always, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0.0f, float.MaxValue); this.samplerLinear = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); // Create constant buffers this.perObjectConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(PerObjectConstantBuffer.Size, D3D11BindOptions.ConstantBuffer)); this.perFrameConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(PerFrameConstantBuffer.Size, D3D11BindOptions.ConstantBuffer)); // Create other render resources here }
protected override void CreateDeviceDependentResources() { base.CreateDeviceDependentResources(); byte[] vertexShaderBytecode = File.ReadAllBytes(Lesson4Game.ShadersDirectory + "Textures.VertexShader.cso"); this.vertexShader = this.DeviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); D3D11InputElementDesc[] basicVertexLayoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "NORMAL", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "TEXCOORD", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 24, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.DeviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes(Lesson4Game.ShadersDirectory + "Textures.PixelShader.cso"); this.pixelShader = this.DeviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); var vertexBufferDesc = D3D11BufferDesc.From(cubeVertices, D3D11BindOptions.VertexBuffer); this.vertexBuffer = this.DeviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, cubeVertices, 0, 0); var indexBufferDesc = D3D11BufferDesc.From(cubeIndices, D3D11BindOptions.IndexBuffer); this.indexBuffer = this.DeviceResources.D3DDevice.CreateBuffer(indexBufferDesc, cubeIndices, 0, 0); var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer); this.constantBuffer = this.DeviceResources.D3DDevice.CreateBuffer(constantBufferDesc); this.constantBufferData.View = new Float4X4( -1.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, 0.89442718f, 0.44721359f, 0.00000000f, 0.00000000f, 0.44721359f, -0.89442718f, -2.23606800f, 0.00000000f, 0.00000000f, 0.00000000f, 1.00000000f ); byte[] textureData = File.ReadAllBytes("../../texturedata.bin"); D3D11Texture2DDesc textureDesc = new D3D11Texture2DDesc(DxgiFormat.R8G8B8A8UNorm, 256, 256, 1, 1); D3D11SubResourceData[] textureSubResData = new[] { new D3D11SubResourceData(textureData, 1024) }; using (var texture = this.DeviceResources.D3DDevice.CreateTexture2D(textureDesc, textureSubResData)) { D3D11ShaderResourceViewDesc textureViewDesc = new D3D11ShaderResourceViewDesc { Format = textureDesc.Format, ViewDimension = D3D11SrvDimension.Texture2D, Texture2D = new D3D11Texture2DSrv { MipLevels = textureDesc.MipLevels, MostDetailedMip = 0 } }; this.textureView = this.DeviceResources.D3DDevice.CreateShaderResourceView(texture, textureViewDesc); } D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc( D3D11Filter.Anisotropic, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, 0.0f, this.DeviceResources.D3DFeatureLevel > D3D11FeatureLevel.FeatureLevel91 ? D3D11Constants.DefaultMaxAnisotropy : D3D11Constants.FeatureLevel91DefaultMaxAnisotropy, D3D11ComparisonFunction.Never, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0.0f, float.MaxValue); this.sampler = this.DeviceResources.D3DDevice.CreateSamplerState(samplerDesc); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; //string fileName = Path.GetDirectoryName(this.OptFileName) + "\\" + Path.GetFileNameWithoutExtension(this.OptFileName) + "Exterior.opt"; //OptFile opt; //if (File.Exists(fileName)) //{ // opt = OptFile.FromFile(fileName); //} //else //{ // opt = OptFile.FromFile(this.OptFileName); //} OptFile opt = OptFile.FromFile(this.OptFileName); this.OptSize = opt.Size * OptFile.ScaleFactor; this.OptSpanSize = opt.SpanSize.Scale(OptFile.ScaleFactor, OptFile.ScaleFactor, OptFile.ScaleFactor); Vector max = opt.MaxSize; Vector min = opt.MinSize; this.OptCenter = new Vector() { X = (max.X + min.X) / 2, Y = (max.Y + min.Y) / 2, Z = (max.Z + min.Z) / 2 }.Scale(OptFile.ScaleFactor, OptFile.ScaleFactor, OptFile.ScaleFactor); this.CreateTextures(opt); this.CreateMeshes(opt); byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso"); this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); D3D11InputElementDesc[] basicVertexLayoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "NORMAL", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "TEXCOORD", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 24, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer); this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc); D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc( D3D11Filter.Anisotropic, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, 0.0f, this.deviceResources.D3DFeatureLevel > D3D11FeatureLevel.FeatureLevel91 ? D3D11Constants.DefaultMaxAnisotropy : D3D11Constants.FeatureLevel91DefaultMaxAnisotropy, D3D11ComparisonFunction.Never, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0.0f, float.MaxValue); this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); D3D11RasterizerDesc rasterizerDesc = D3D11RasterizerDesc.Default; rasterizerDesc.CullMode = D3D11CullMode.None; this.rasterizerState = this.deviceResources.D3DDevice.CreateRasterizerState(rasterizerDesc); this.depthStencilState0 = this.deviceResources.D3DDevice.CreateDepthStencilState(D3D11DepthStencilDesc.Default); D3D11DepthStencilDesc depthStencilDesc = D3D11DepthStencilDesc.Default; depthStencilDesc.DepthWriteMask = D3D11DepthWriteMask.Zero; this.depthStencilState1 = this.deviceResources.D3DDevice.CreateDepthStencilState(depthStencilDesc); this.blendState0 = this.deviceResources.D3DDevice.CreateBlendState(D3D11BlendDesc.Default); D3D11BlendDesc blendDesc = D3D11BlendDesc.Default; D3D11RenderTargetBlendDesc[] blendDescRenderTargets = blendDesc.GetRenderTargets(); blendDescRenderTargets[0].IsBlendEnabled = true; blendDescRenderTargets[0].SourceBlend = D3D11BlendValue.SourceAlpha; blendDescRenderTargets[0].DestinationBlend = D3D11BlendValue.InverseSourceAlpha; blendDescRenderTargets[0].BlendOperation = D3D11BlendOperation.Add; blendDescRenderTargets[0].SourceBlendAlpha = D3D11BlendValue.One; blendDescRenderTargets[0].DestinationBlendAlpha = D3D11BlendValue.InverseSourceAlpha; blendDescRenderTargets[0].BlendOperationAlpha = D3D11BlendOperation.Add; blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All; blendDesc.SetRenderTargets(blendDescRenderTargets); this.blendState1 = this.deviceResources.D3DDevice.CreateBlendState(blendDesc); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; XMFloat3 vCenter = new XMFloat3(0.25767413f, -28.503521f, 111.00689f); XMMatrix m = XMMatrix.Translation(-vCenter.X, -vCenter.Y, -vCenter.Z); m *= XMMatrix.RotationY(XMMath.PI); m *= XMMatrix.RotationX(XMMath.PIDivTwo); this.centerMesh = m; // Load the mesh this.mesh = SdkMeshFile.FromFile( this.deviceResources.D3DDevice, this.deviceResources.D3DContext, "Tiny\\Tiny.sdkmesh"); // Create the shaders byte[] vertexShaderBytecode = File.ReadAllBytes("BasicHLSL11_VS.cso"); this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "NORMAL", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "TEXCOORD", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 24, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("BasicHLSL11_PS.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); // Create a sampler state D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc( D3D11Filter.MinMagMipLinear, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, 0.0f, 1, D3D11ComparisonFunction.Always, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0.0f, float.MaxValue); this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); // Setup constant buffers this.constantBufferVSPerObject = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferVSPerObject.Size, D3D11BindOptions.ConstantBuffer)); this.constantBufferPSPerObject = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferPSPerObject.Size, D3D11BindOptions.ConstantBuffer)); this.constantBufferPSPerFrame = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferPSPerFrame.Size, D3D11BindOptions.ConstantBuffer)); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; var d3dDevice = this.deviceResources.D3DDevice; // Create the shaders byte[] renderParticlesVSBytecode = File.ReadAllBytes("ParticleDrawVS.cso"); this.g_pRenderParticlesVS = d3dDevice.CreateVertexShader(renderParticlesVSBytecode, null); this.g_pRenderParticlesGS = d3dDevice.CreateGeometryShader(File.ReadAllBytes("ParticleDrawGS.cso"), null); this.g_pRenderParticlesPS = d3dDevice.CreatePixelShader(File.ReadAllBytes("ParticleDrawPS.cso"), null); this.g_pCalcCS = d3dDevice.CreateComputeShader(File.ReadAllBytes("NBodyGravityCS.cso"), null); // Create our vertex input layout D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "COLOR", SemanticIndex = 0, Format = DxgiFormat.R32G32B32A32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.g_pParticleVertexLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, renderParticlesVSBytecode); this.CreateParticleBuffer(); this.CreateParticlePosVeloBuffers(); // Setup constant buffer this.g_pcbGS = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferGS.Size, D3D11BindOptions.ConstantBuffer)); this.g_pcbCS = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferCS.Size, D3D11BindOptions.ConstantBuffer)); // Load the Particle Texture DdsDirectX.CreateTexture( "Particle.dds", this.deviceResources.D3DDevice, this.deviceResources.D3DContext, out this.g_pParticleTexRV); D3D11SamplerDesc SamplerDesc = D3D11SamplerDesc.Default; SamplerDesc.AddressU = D3D11TextureAddressMode.Clamp; SamplerDesc.AddressV = D3D11TextureAddressMode.Clamp; SamplerDesc.AddressW = D3D11TextureAddressMode.Clamp; SamplerDesc.Filter = D3D11Filter.MinMagMipLinear; this.g_pSampleStateLinear = d3dDevice.CreateSamplerState(SamplerDesc); D3D11BlendDesc BlendStateDesc = D3D11BlendDesc.Default; D3D11RenderTargetBlendDesc[] BlendStateDescRenderTargets = BlendStateDesc.GetRenderTargets(); BlendStateDescRenderTargets[0].IsBlendEnabled = true; BlendStateDescRenderTargets[0].BlendOperation = D3D11BlendOperation.Add; BlendStateDescRenderTargets[0].SourceBlend = D3D11BlendValue.SourceAlpha; BlendStateDescRenderTargets[0].DestinationBlend = D3D11BlendValue.One; BlendStateDescRenderTargets[0].BlendOperationAlpha = D3D11BlendOperation.Add; BlendStateDescRenderTargets[0].SourceBlendAlpha = D3D11BlendValue.Zero; BlendStateDescRenderTargets[0].DestinationBlendAlpha = D3D11BlendValue.Zero; BlendStateDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All; BlendStateDesc.SetRenderTargets(BlendStateDescRenderTargets); this.g_pBlendingStateParticle = d3dDevice.CreateBlendState(BlendStateDesc); D3D11DepthStencilDesc DepthStencilDesc = D3D11DepthStencilDesc.Default; DepthStencilDesc.IsDepthEnabled = false; DepthStencilDesc.DepthWriteMask = D3D11DepthWriteMask.Zero; this.g_pDepthStencilState = d3dDevice.CreateDepthStencilState(DepthStencilDesc); XMFloat3 eye = new XMFloat3(-Spread * 2, Spread * 4, -Spread * 3); XMFloat3 at = new XMFloat3(0.0f, 0.0f, 0.0f); XMFloat3 up = new XMFloat3(0.0f, 1.0f, 0.0f); this.ViewMatrix = XMMatrix.LookAtLH(eye, at, up); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso"); this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "TEXCOORD", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); var vertexBufferDesc = D3D11BufferDesc.From(MainGameComponent.Vertices, D3D11BindOptions.VertexBuffer); this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, MainGameComponent.Vertices, 0, 0); var indexBufferDesc = D3D11BufferDesc.From(MainGameComponent.Indices, D3D11BindOptions.IndexBuffer); this.indexBuffer = this.deviceResources.D3DDevice.CreateBuffer(indexBufferDesc, MainGameComponent.Indices, 0, 0); this.constantBufferNeverChanges = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferNeverChangesData.Size, D3D11BindOptions.ConstantBuffer)); this.constantBufferChangesOnResize = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferChangesOnResizeData.Size, D3D11BindOptions.ConstantBuffer)); this.constantBufferChangesEveryFrame = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferChangesEveryFrameData.Size, D3D11BindOptions.ConstantBuffer)); DdsDirectX.CreateTexture( "seafloor.dds", this.deviceResources.D3DDevice, this.deviceResources.D3DContext, out this.textureView); D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc( D3D11Filter.MinMagMipLinear, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, 0.0f, 0, D3D11ComparisonFunction.Never, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0.0f, float.MaxValue); this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); this.worldMatrix = XMMatrix.Identity; XMVector eye = new XMVector(0.0f, 3.0f, -6.0f, 0.0f); XMVector at = new XMVector(0.0f, 1.0f, 0.0f, 0.0f); XMVector up = new XMVector(0.0f, 1.0f, 0.0f, 0.0f); this.viewMatrix = XMMatrix.LookAtLH(eye, at, up); ConstantBufferNeverChangesData cbNeverChanges; cbNeverChanges.View = this.viewMatrix.Transpose(); this.deviceResources.D3DContext.UpdateSubresource(this.constantBufferNeverChanges, 0, null, cbNeverChanges, 0, 0); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso"); this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "NORMAL", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "TEXCOORD", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 24, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); this.constantBufferPerObject = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferPerObject.Size, D3D11BindOptions.ConstantBuffer)); this.constantBufferPerFrame = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferPerFrame.Size, D3D11BindOptions.ConstantBuffer)); D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc( D3D11Filter.MinMagMipLinear, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, 0.0f, 1, D3D11ComparisonFunction.Always, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0.0f, float.MaxValue); this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); DdsDirectX.CreateTexture( "seafloor.dds", this.deviceResources.D3DDevice, this.deviceResources.D3DContext, out this.textureView); this.mesh = SdkMeshFile.FromFile( this.deviceResources.D3DDevice, this.deviceResources.D3DContext, "ball.sdkmesh"); XMVector eye = new XMVector(0.0f, 0.0f, -5.0f, 0.0f); XMVector at = new XMVector(0.0f, 0.0f, -0.0f, 0.0f); XMVector up = new XMVector(0.0f, 1.0f, 0.0f, 0.0f); this.ViewMatrix = XMMatrix.LookAtLH(eye, at, up); this.WorldMatrix = XMMatrix.Identity; }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; byte[] renderSceneVertexShaderBytecode = File.ReadAllBytes("RenderSceneVertexShader.cso"); this.g_pSceneVS = this.deviceResources.D3DDevice.CreateVertexShader(renderSceneVertexShaderBytecode, null); D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "NORMAL", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "TEXTURE", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 24, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.g_pSceneVertexLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, renderSceneVertexShaderBytecode); byte[] renderScenePixelShaderBytecode = File.ReadAllBytes("RenderScenePixelShader.cso"); this.g_pScenePS = this.deviceResources.D3DDevice.CreatePixelShader(renderScenePixelShaderBytecode, null); byte[] renderSceneShadowMapVertexShaderBytecode = File.ReadAllBytes("RenderSceneShadowMapVertexShader.cso"); this.g_pShadowMapVS = this.deviceResources.D3DDevice.CreateVertexShader(renderSceneShadowMapVertexShaderBytecode, null); this.g_SceneMesh = SdkMeshFile.FromFile(this.deviceResources.D3DDevice, this.deviceResources.D3DContext, @"ColumnScene\scene.sdkmesh"); this.g_Poles = SdkMeshFile.FromFile(this.deviceResources.D3DDevice, this.deviceResources.D3DContext, @"ColumnScene\poles.sdkmesh"); this.g_pcbConstants = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferConstants.Size, D3D11BindOptions.ConstantBuffer)); D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc( D3D11Filter.ComparisonMinMagMipPoint, D3D11TextureAddressMode.Border, D3D11TextureAddressMode.Border, D3D11TextureAddressMode.Border, 0.0f, 1, D3D11ComparisonFunction.LessEqual, new float[] { 1.0f, 1.0f, 1.0f, 1.0f }, 0.0f, float.MaxValue); // PointCmp this.g_pSamplePointCmp = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); // Point samplerDesc.Filter = D3D11Filter.MinMagMipPoint; samplerDesc.ComparisonFunction = D3D11ComparisonFunction.Always; this.g_pSamplePoint = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); // Linear samplerDesc.Filter = D3D11Filter.MinMagMipLinear; samplerDesc.AddressU = D3D11TextureAddressMode.Wrap; samplerDesc.AddressV = D3D11TextureAddressMode.Wrap; samplerDesc.AddressW = D3D11TextureAddressMode.Wrap; this.g_pSampleLinear = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); // Create a blend state to disable alpha blending D3D11BlendDesc blendDesc = D3D11BlendDesc.Default; blendDesc.IsIndependentBlendEnabled = false; D3D11RenderTargetBlendDesc[] blendDescRenderTargets = blendDesc.GetRenderTargets(); blendDescRenderTargets[0].IsBlendEnabled = false; blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All; blendDesc.SetRenderTargets(blendDescRenderTargets); this.g_pBlendStateNoBlend = this.deviceResources.D3DDevice.CreateBlendState(blendDesc); blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.None; blendDesc.SetRenderTargets(blendDescRenderTargets); this.g_pBlendStateColorWritesOff = this.deviceResources.D3DDevice.CreateBlendState(blendDesc); // textures / rts D3D11Texture2DDesc TDesc = new D3D11Texture2DDesc( DxgiFormat.R16Typeless, (uint)g_fShadowMapWidth, (uint)g_fShadowMapHeight, 1, 1, D3D11BindOptions.DepthStencil | D3D11BindOptions.ShaderResource); this.g_pShadowMapDepthStencilTexture = this.deviceResources.D3DDevice.CreateTexture2D(TDesc); D3D11ShaderResourceViewDesc SRVDesc = new D3D11ShaderResourceViewDesc { Format = DxgiFormat.R16UNorm, ViewDimension = D3D11SrvDimension.Texture2D, Texture2D = new D3D11Texture2DSrv { MipLevels = 1, MostDetailedMip = 0 } }; this.g_pDepthTextureSRV = this.deviceResources.D3DDevice.CreateShaderResourceView(this.g_pShadowMapDepthStencilTexture, SRVDesc); D3D11DepthStencilViewDesc DSVDesc = new D3D11DepthStencilViewDesc { Format = DxgiFormat.D16UNorm, ViewDimension = D3D11DsvDimension.Texture2D, Options = D3D11DepthStencilViewOptions.None, Texture2D = new D3D11Texture2DDsv { MipSlice = 0 } }; this.g_pDepthStencilTextureDSV = this.deviceResources.D3DDevice.CreateDepthStencilView(this.g_pShadowMapDepthStencilTexture, DSVDesc); XMFloat3 vecEye = new XMFloat3(0.95f, 5.83f, -14.48f); XMFloat3 vecAt = new XMFloat3(0.90f, 5.44f, -13.56f); XMFloat3 vecUp = new XMFloat3(0.0f, 1.0f, 0.0f); this.ViewMatrix = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); this.WorldMatrix = XMMatrix.Identity; XMFloat3 vecEyeL = new XMFloat3(0, 0, 0); XMFloat3 vecAtL = new XMFloat3(0, -0.5f, 1); XMFloat3 vecUUpL = new XMFloat3(0.0f, 1.0f, 0.0f); this.LightViewMatrix = XMMatrix.LookAtLH(vecEyeL, vecAtL, vecUUpL); this.LightWorldMatrix = XMMatrix.Identity; }
public void Render() { var context = this.deviceResources.D3DContext; D3D11RenderTargetView[] pRTV = new D3D11RenderTargetView[2]; D3D11ShaderResourceView[] pSRV = new D3D11ShaderResourceView[8]; // Array of our samplers D3D11SamplerState[] ppSamplerStates = new D3D11SamplerState[3] { this.g_pSamplePoint, this.g_pSampleLinear, this.g_pSamplePointCmp }; context.PixelShaderSetSamplers(0, ppSamplerStates); // Store off original render target, this is the back buffer of the swap chain D3D11RenderTargetView pOrigRTV = this.deviceResources.D3DRenderTargetView; D3D11DepthStencilView pOrigDSV = this.deviceResources.D3DDepthStencilView; // Clear the render target float[] ClearColor = { 0.0f, 0.25f, 0.25f, 1.0f }; context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, ClearColor); context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth | D3D11ClearOptions.Stencil, 1.0f, 0); // disable color writes context.OutputMergerSetBlendState(this.g_pBlendStateColorWritesOff, null, 0xffffffff); this.RenderShadowMap(out XMFloat4X4 mViewProjLight, out XMFloat3 vLightDir); // enable color writes context.OutputMergerSetBlendState(this.g_pBlendStateNoBlend, null, 0xffffffff); // Get the projection & view matrix from the camera class XMMatrix mView = this.ViewMatrix; XMMatrix mProj = this.ProjectionMatrix; XMMatrix mWorldViewProjection = mView * mProj; // Setup the constant buffer for the scene vertex shader ConstantBufferConstants pConstants = new ConstantBufferConstants { WorldViewProjection = mWorldViewProjection.Transpose(), WorldViewProjLight = mViewProjLight.ToMatrix().Transpose(), ShadowMapDimensions = new XMFloat4( g_fShadowMapWidth, g_fShadowMapHeight, 1.0f / g_fShadowMapWidth, 1.0f / g_fShadowMapHeight), LightDir = new XMFloat4(vLightDir.X, vLightDir.Y, vLightDir.Z, 0.0f), SunWidth = this.SunWidth }; context.UpdateSubresource(this.g_pcbConstants, 0, null, pConstants, 0, 0); context.VertexShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants }); context.PixelShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants }); // Set the shaders context.VertexShaderSetShader(this.g_pSceneVS, null); context.PixelShaderSetShader(this.g_pScenePS, null); // Set the vertex buffer format context.InputAssemblerSetInputLayout(this.g_pSceneVertexLayout); // Rebind to original back buffer and depth buffer pRTV[0] = pOrigRTV; context.OutputMergerSetRenderTargets(pRTV, pOrigDSV); // set the shadow map context.PixelShaderSetShaderResources(1, new[] { this.g_pDepthTextureSRV }); // Render the scene this.g_SceneMesh.Render(0, -1, -1); this.g_Poles.Render(0, -1, -1); // restore resources context.PixelShaderSetShaderResources(0, pSRV); }