private static void CreateTexture(D3D11Device device, D3D11DeviceContext deviceContext, string fileName, out D3D11ShaderResourceView textureView) { if (!File.Exists(fileName)) { textureView = null; return; } string ext = Path.GetExtension(fileName); if (string.Equals(ext, ".dds", StringComparison.OrdinalIgnoreCase)) { DdsDirectX.CreateTexture(fileName, device, deviceContext, out textureView); } else if (string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".bmp", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".png", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".gif", StringComparison.OrdinalIgnoreCase)) { CreateBitmapTexture(device, fileName, out textureView); } else { throw new NotSupportedException(); } }
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; }