public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; byte[] vertexShaderBytecode = File.ReadAllBytes("Triangles.VertexShader.cso"); this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); D3D11InputElementDesc[] basicVertexLayoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("Triangles.PixelShader.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); var vertexBufferDesc = D3D11BufferDesc.From(triangleVertices, D3D11BindOptions.VertexBuffer); this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, triangleVertices, 0, 0); var indexBufferDesc = D3D11BufferDesc.From(triangleIndices, D3D11BindOptions.IndexBuffer); this.indexBuffer = this.deviceResources.D3DDevice.CreateBuffer(indexBufferDesc, triangleIndices, 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 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); pixelShaderBytecode = File.ReadAllBytes("PixelShaderSolid.cso"); this.pixelShaderSolid = 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); var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer); this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc); this.worldMatrix = XMMatrix.Identity; XMVector eye = new XMVector(0.0f, 4.0f, -10.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); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; var d3dDevice = this.deviceResources.D3DDevice; // Create the shaders byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso"); this.g_pVertexShader = d3dDevice.CreateVertexShader(vertexShaderBytecode, null); this.g_pHullShaderInteger = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderInteger.cso"), null); this.g_pHullShaderFracEven = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderFracEven.cso"), null); this.g_pHullShaderFracOdd = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderFracOdd.cso"), null); this.g_pDomainShader = d3dDevice.CreateDomainShader(File.ReadAllBytes("DomainShader.cso"), null); this.g_pPixelShader = d3dDevice.CreatePixelShader(File.ReadAllBytes("PixelShader.cso"), null); this.g_pSolidColorPS = d3dDevice.CreatePixelShader(File.ReadAllBytes("SolidColorPS.cso"), null); // Create our vertex input layout - this matches the BEZIER_CONTROL_POINT structure D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.g_pPatchLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); // Create constant buffers this.g_pcbPerFrame = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferConstants.Size, D3D11BindOptions.ConstantBuffer)); // Create solid and wireframe rasterizer state objects D3D11RasterizerDesc rasterDesc = D3D11RasterizerDesc.Default; rasterDesc.CullMode = D3D11CullMode.None; rasterDesc.IsDepthClipEnabled = true; rasterDesc.FillMode = D3D11FillMode.Solid; this.g_pRasterizerStateSolid = d3dDevice.CreateRasterizerState(rasterDesc); rasterDesc.FillMode = D3D11FillMode.WireFrame; this.g_pRasterizerStateWireframe = d3dDevice.CreateRasterizerState(rasterDesc); D3D11BufferDesc vbdesc = D3D11BufferDesc.From(MobiusStrip.Points, D3D11BindOptions.VertexBuffer); this.g_pControlPointVB = d3dDevice.CreateBuffer(vbdesc, MobiusStrip.Points, 0, 0); XMFloat3 vecEye = new XMFloat3(1.0f, 1.5f, -3.5f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, 0.0f); XMFloat3 vecUp = new XMFloat3(0.0f, 1.0f, 0.0f); this.ViewMatrix = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); this.EyePt = vecEye; }
protected override void CreateDeviceDependentResources() { base.CreateDeviceDependentResources(); byte[] vertexShaderBytecode = File.ReadAllBytes(Lesson3Game.ShadersDirectory + "Cubes.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 = "COLOR", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.DeviceResources.D3DDevice.CreateInputLayout(basicVertexLayoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes(Lesson3Game.ShadersDirectory + "Cubes.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 ); }
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 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); var vertexBufferDesc = new D3D11BufferDesc(MaxVertices * 12, D3D11BindOptions.VertexBuffer); this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc); var indexBufferDesc = new D3D11BufferDesc(MaxIndices * 2, D3D11BindOptions.IndexBuffer); this.indexBuffer = this.deviceResources.D3DDevice.CreateBuffer(indexBufferDesc); var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer); this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc); this.worldMatrix = XMMatrix.Identity; XMVector vecAt = this.cameraOrigins[(int)this.CollisionGroup]; XMVector vecEye = new(vecAt.X, vecAt.Y + 20.0f, (this.CollisionGroup == CollisionGroup.Frustum) ? (vecAt.Z + 20.0f) : (vecAt.Z - 20.0f), 0.0f); XMVector vecUp = new(0.0f, 1.0f, 0.0f, 0.0f); this.viewMatrix = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; this.fragmentCountPS = this.deviceResources.D3DDevice.CreatePixelShader( File.ReadAllBytes("FragmentCountPS.cso"), null); this.createPrefixSumPass0CS = this.deviceResources.D3DDevice.CreateComputeShader( File.ReadAllBytes("CreatePrefixSumPass0CS.cso"), null); this.createPrefixSumPass1CS = this.deviceResources.D3DDevice.CreateComputeShader( File.ReadAllBytes("CreatePrefixSumPass1CS.cso"), null); this.fillDeepBufferPS = this.deviceResources.D3DDevice.CreatePixelShader( File.ReadAllBytes("FillDeepBufferPS.cso"), null); this.sortAndRenderCS = this.deviceResources.D3DDevice.CreateComputeShader( File.ReadAllBytes("SortAndRenderCS.cso"), null); var depthStencilDesc = new D3D11DepthStencilDesc { IsDepthEnabled = false, IsStencilEnabled = false }; this.depthStencilState = this.deviceResources.D3DDevice.CreateDepthStencilState(depthStencilDesc); this.computeShaderConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(OitComputeShaderConstantBufferData.Size, D3D11BindOptions.ConstantBuffer)); this.pixelShaderConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(OitPixelShaderConstantBufferData.Size, D3D11BindOptions.ConstantBuffer)); }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; byte[] vertexShaderBytecode = File.ReadAllBytes("SceneVS.cso"); this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32A32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "COLOR", SemanticIndex = 0, Format = DxgiFormat.R32G32B32A32Float, InputSlot = 0, AlignedByteOffset = 0xffffffff, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.vertexLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("ScenePS.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); // Set up vertex buffer float fRight = -10.0f; float fTop = -10.0f; float fLeft = 10.0f; float fLowH = -5.0f; // Fill the vertex buffer var vertices = new SceneVertex[12]; vertices[0].Pos = new XMFloat4(fLeft, fLowH, 50.0f, 1.0f); vertices[1].Pos = new XMFloat4(fLeft, fTop, 50.0f, 1.0f); vertices[2].Pos = new XMFloat4(fRight, fLowH, 50.0f, 1.0f); vertices[3].Pos = new XMFloat4(fRight, fTop, 50.0f, 1.0f); vertices[0].Color = new XMFloat4(1.0f, 0.0f, 0.0f, 0.5f); vertices[1].Color = new XMFloat4(1.0f, 0.0f, 0.0f, 0.5f); vertices[2].Color = new XMFloat4(1.0f, 0.0f, 0.0f, 0.5f); vertices[3].Color = new XMFloat4(1.0f, 0.0f, 0.0f, 0.5f); vertices[4].Pos = new XMFloat4(fLeft, fLowH, 60.0f, 1.0f); vertices[5].Pos = new XMFloat4(fLeft, fTop, 60.0f, 1.0f); vertices[6].Pos = new XMFloat4(fRight, fLowH, 40.0f, 1.0f); vertices[7].Pos = new XMFloat4(fRight, fTop, 40.0f, 1.0f); vertices[4].Color = new XMFloat4(0.0f, 1.0f, 0.0f, 0.5f); vertices[5].Color = new XMFloat4(0.0f, 1.0f, 0.0f, 0.5f); vertices[6].Color = new XMFloat4(0.0f, 1.0f, 0.0f, 0.5f); vertices[7].Color = new XMFloat4(0.0f, 1.0f, 0.0f, 0.5f); vertices[8].Pos = new XMFloat4(fLeft, fLowH, 40.0f, 1.0f); vertices[9].Pos = new XMFloat4(fLeft, fTop, 40.0f, 1.0f); vertices[10].Pos = new XMFloat4(fRight, fLowH, 60.0f, 1.0f); vertices[11].Pos = new XMFloat4(fRight, fTop, 60.0f, 1.0f); vertices[8].Color = new XMFloat4(0.0f, 0.0f, 1.0f, 0.5f); vertices[9].Color = new XMFloat4(0.0f, 0.0f, 1.0f, 0.5f); vertices[10].Color = new XMFloat4(0.0f, 0.0f, 1.0f, 0.5f); vertices[11].Color = new XMFloat4(0.0f, 0.0f, 1.0f, 0.5f); var vertexBufferDesc = D3D11BufferDesc.From(vertices, D3D11BindOptions.VertexBuffer, D3D11Usage.Immutable); this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, vertices, 0, 0); var constantBufferDesc = new D3D11BufferDesc(SceneVertexShaderConstantBufferData.Size, D3D11BindOptions.ConstantBuffer); this.vertexShaderConstantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc); }
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[] 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 CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; var d3dDevice = this.deviceResources.D3DDevice; this.tessellator.CreateDeviceDependentResources(this.deviceResources); XMFloat4[] initData; // Parse the .obj file. Both triangle faces and quad faces are supported. // Only v and f tags are processed, other tags like vn, vt etc are ignored. { var initFile = ObjFile.FromFile("BaseMesh.obj"); var data = new List <XMFloat4>(); var v = new List <XMFloat4>(); for (int i = 0; i < initFile.Vertices.Count; i++) { ObjVector4 objPosition = initFile.Vertices[i].Position; XMFloat4 pos = new XMFloat4( objPosition.X, objPosition.Y, objPosition.Z, 1.0f); v.Add(pos); } foreach (ObjFace face in initFile.Faces) { if (face.Vertices.Count < 3) { continue; } data.Add(v[face.Vertices[0].Vertex - 1]); data.Add(v[face.Vertices[1].Vertex - 1]); data.Add(v[face.Vertices[2].Vertex - 1]); if (face.Vertices.Count >= 4) { data.Add(v[face.Vertices[2].Vertex - 1]); data.Add(v[face.Vertices[3].Vertex - 1]); data.Add(v[face.Vertices[0].Vertex - 1]); } } initData = data.ToArray(); } this.g_pBaseVB = d3dDevice.CreateBuffer( D3D11BufferDesc.From(initData, D3D11BindOptions.ShaderResource | D3D11BindOptions.VertexBuffer), initData, 0, 0); this.tessellator.SetBaseMesh((uint)initData.Length, this.g_pBaseVB); this.g_pVS = d3dDevice.CreateVertexShader(File.ReadAllBytes("RenderVertexShader.cso"), null); { byte[] shaderBytecode = File.ReadAllBytes("RenderBaseVertexShader.cso"); this.g_pBaseVS = d3dDevice.CreateVertexShader(shaderBytecode, null); D3D11InputElementDesc[] layoutDesc = new[] { new D3D11InputElementDesc( "POSITION", 0, DxgiFormat.R32G32B32A32Float, 0, 0, D3D11InputClassification.PerVertexData, 0) }; this.g_pBaseVBLayout = d3dDevice.CreateInputLayout(layoutDesc, shaderBytecode); } this.g_pPS = d3dDevice.CreatePixelShader(File.ReadAllBytes("RenderPixelShader.cso"), null); // Setup constant buffer this.g_pVSCB = d3dDevice.CreateBuffer(new D3D11BufferDesc { BindOptions = D3D11BindOptions.ConstantBuffer, ByteWidth = 4 * 16 }); // Rasterizer state this.g_pRasWireFrame = d3dDevice.CreateRasterizerState(new D3D11RasterizerDesc { CullMode = D3D11CullMode.None, FillMode = D3D11FillMode.WireFrame }); }
public void LoadShader(string filename, out D3D11PixelShader shader) { byte[] data = File.ReadAllBytes(filename); shader = this.d3dDevice.CreatePixelShader(data, null); }
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[] 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; // 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 }
private void EmitPipeline( IEmitModule emitModule, MidPipelineDecl midPipeline) { var range = midPipeline.Range; // Create the class to represent the pipeline EmitClassFlags ifaceFlags = EmitClassFlags.None; EmitClassFlags implFlags = EmitClassFlags.Hidden | EmitClassFlags.Implementation; if (!midPipeline.IsPrimary) { ifaceFlags |= EmitClassFlags.Mixin; // The "impl" class is treated as always primary } // this is a terrible horrible hack to detect a stdlib clas // (which means we *don't* want it in the generated header... var className = midPipeline.Name.ToString(); if (className.StartsWith("D3D11")) { ifaceFlags |= EmitClassFlags.Internal; implFlags |= EmitClassFlags.Internal; className = string.Format("spark::d3d11::{0}", className); } var baseFacets = (from f in midPipeline.Facets where f != midPipeline.DirectFacet select f).ToArray(); var primaryBaseFacet = (from f in baseFacets where f.OriginalShaderClass.Decl.IsPrimary select f).FirstOrDefault(); ShaderClassInfo primaryBaseInfo = null; IEmitClass primaryBaseClass = null; if (primaryBaseFacet != null) { primaryBaseInfo = _mapShaderClassToInfo[primaryBaseFacet.OriginalShaderClass.Decl]; primaryBaseClass = primaryBaseInfo.InterfaceClass; } var pipelineEnv = new EmitEnv(_moduleEnv); IEmitClass ifaceClass = emitModule.CreateClass( className, midPipeline.IsPrimary ? primaryBaseClass : null, ifaceFlags); var info = new ShaderClassInfo { MidClassDecl = midPipeline, InheritedFacets = new ShaderFacetInfo[midPipeline.Facets.Count() - 1], }; info.InterfaceClass = ifaceClass; _mapShaderClassToInfo[midPipeline] = info; // Before we create fields declared in Spark, we // first create the "hidden" fields that represent // an instance at run-time: if (primaryBaseInfo == null && midPipeline.IsPrimary) { // We need two fields here for any root-of-the-hierarchy // class (which should actually be declared off in a header somewhere). // The first is a "class info" pointer: ifaceClass.AddPrivateField( Target.GetOpaqueType("void*"), "__classInfo"); // The second is the reference-count field: ifaceClass.AddPrivateField( Target.GetBuiltinType("UINT"), "__referenceCount"); } // Create fields to represent the various attributes var constantElement = GetElement(midPipeline, "Constant"); var uniformElement = GetElement(midPipeline, "Uniform"); // Direct facet: var directFacetDecl = midPipeline.DirectFacet; var directFacetInfo = CreateDirectFacet( directFacetDecl, constantElement, uniformElement, ifaceClass, pipelineEnv); info.DirectFacet = directFacetInfo; ifaceClass.WrapperWriteLineProtected( "{0}* _StaticCastImpl( void* ) {{ return this; }}", className); // Base-class facet, if any if (primaryBaseInfo != null) { if (midPipeline.IsPrimary) { AddPrimaryBaseFacet( info, primaryBaseInfo, (b, shaderObj) => shaderObj); } else { var fieldName = "_Base_" + primaryBaseInfo.MidClassDecl.Name.ToString(); var fieldType = primaryBaseInfo.InterfaceClass.Pointer(); var facetField = ifaceClass.AddPrivateField( fieldType, fieldName); info.DirectFacet.Mixins.Add( new ShaderFacetMixinInfo( primaryBaseInfo.MidClassDecl, facetField)); AddPrimaryBaseFacet( info, primaryBaseInfo, (b, shaderObj) => b.GetArrow(shaderObj, facetField)); foreach (var bf in primaryBaseInfo.AllFacets) { var baseFacet = bf; // avoid capture AddForwardingAccessors( info.InterfaceClass, baseFacet); var baseClass = _mapShaderClassToInfo[baseFacet.OriginalClass]; ifaceClass.WrapperWriteLineProtected( "{0}* _StaticCastImpl( {0}* ) {{ return {1}; }}", baseClass.InterfaceClass.GetName(), fieldName); } } } // Inherited facets: foreach (var f in midPipeline.Facets) { // Clearly don't want to embed a facet for ourself... if (f == midPipeline.DirectFacet) { continue; } // Primary classes will already be covered by // the inheritance chain... if (f.OriginalShaderClass.Decl.IsPrimary) { continue; } // We may have inherited an interface // to this facet through our base... if (primaryBaseFacet != null) { if (primaryBaseFacet.OriginalShaderClass.Decl.Facets.Any( (otherF) => otherF.OriginalShaderClass.Decl == f.OriginalShaderClass.Decl)) { continue; } } // Otherwise we need a public field // to expose the facet: var facetClassDecl = f.OriginalShaderClass.Decl; var facetClassInfo = _mapShaderClassToInfo[facetClassDecl]; // \todo: get pointer type... :( var fieldType = facetClassInfo.InterfaceClass.Pointer(); var fieldName = string.Format( "_Mixin_{0}", f.OriginalShaderClass.Decl.Name.ToString()); var field = ifaceClass.AddPrivateField(fieldType, fieldName); info.DirectFacet.Mixins.Add( new ShaderFacetMixinInfo( facetClassDecl, field)); ifaceClass.WrapperWriteLineProtected( "{0} _StaticCastImpl( {0} ) {{ return {1}; }}", fieldType.ToString(), fieldName); AddBaseFacet( info, facetClassInfo.DirectFacet, (b, shaderObj) => b.GetArrow(shaderObj, field)); AddForwardingAccessors( info.InterfaceClass, facetClassInfo.DirectFacet); } /* * * * * foreach (var a in constantElement.Attributes) * { * var attr = a; // Avoid capture. * * if (attr.Exp == null) continue; * * pipelineEnv.Insert(attr, * (b) => EmitExp(attr.Exp, b, pipelineEnv)); * } * foreach (var a in uniformElement.Attributes) * { * var attr = a; // Avoid capture. * * if (attr.Exp != null) continue; * * var attrType = EmitType(attr.Type, pipelineEnv); * var attrName = attr.Name.ToString(); * * var attrField = emitClass.AddPublicField(attrType, attrName); * * pipelineEnv.Insert(attr, * (b) => b.GetArrow( * b.Method.ThisParameter, * attrField)); * } */ var sharedHLSL = new HLSL.SharedContextHLSL(Identifiers, Diagnostics); var emitPass = new PassEmitContext() { EmitContext = this, MidPass = midPipeline, SharedHLSL = sharedHLSL, EmitClass = ifaceClass, ShaderClassEnv = pipelineEnv, ShaderClassInfo = info, }; // Now emit stage-specific code. EmitStageInterface <D3D11VertexShader>(emitPass); EmitStageInterface <D3D11HullShader>(emitPass); EmitStageInterface <D3D11DomainShader>(emitPass); EmitStageInterface <D3D11GeometryShader>(emitPass); EmitStageInterface <D3D11PixelShader>(emitPass); // IA last since it generates the call to Draw*() EmitStageInterface <D3D11InputAssembler>(emitPass); // Do this *after* emitting the per-stage interface, // so that stages of the pipeline can bind attributes too. foreach (var f in info.AllFacets) { foreach (var a in f.Attributes) { if (a == null) { continue; } var attr = a.AttributeDecl; var accessor = a.Accessor; pipelineEnv.Insert( attr, (b) => accessor(b, b.Method.ThisParameter)); } } ifaceClass.WrapperWriteLine(""); ifaceClass.WrapperWriteLine( "// Statically cast shader to base/mixin class"); ifaceClass.WrapperWriteLine( "template<typename TBase>"); ifaceClass.WrapperWriteLine( "TBase* StaticCast() { return _StaticCastImpl(static_cast<TBase*>(nullptr)); }"); ifaceClass.WrapperWriteLine(""); ifaceClass.WrapperWriteLine( "// Spark implementation details follow. Do not depend on these:"); ifaceClass.WrapperWriteLine("//"); // Janky RTTI-like system: use the name of the class ifaceClass.WrapperWriteLine( "static const char* StaticGetShaderClassName() {{ return \"{0}\"; }}", className); ifaceClass.Seal(); if (midPipeline.IsAbstract) { return; } var implBase = ifaceClass; if (!midPipeline.IsPrimary) { implBase = primaryBaseClass; if (implBase == null) { throw new NotImplementedException(); } } var implClass = emitModule.CreateClass( className, implBase, implFlags); ifaceClass.WrapperWriteLine( "static const spark::ShaderClassDesc* GetShaderClassDesc();"); // // The impl class needs a field to hold each of its mixin bases... Dictionary <MidPipelineDecl, Func <IEmitBlock, IEmitVal> > getFacetPointerForBase = new Dictionary <MidPipelineDecl, Func <IEmitBlock, IEmitVal> >(); var facetInfoData = new List <IEmitVal>(); var facetInfoCount = 0; foreach (var f in midPipeline.Facets) { // Clearly don't want to embed a facet for ourself... var facetClassDecl = f.OriginalShaderClass.Decl; if (facetClassDecl.IsPrimary) { // Primary classes will already be covered by // the inheritance chain... getFacetPointerForBase[facetClassDecl] = (b) => b.Method.ThisParameter; facetInfoData.Add(emitModule.LiteralString(_mapShaderClassToInfo[facetClassDecl].InterfaceClass.GetName())); facetInfoData.Add(Target.LiteralU32(0)); facetInfoCount++; } } UInt32 facetOffset = ifaceClass.Size; foreach (var f in midPipeline.Facets) { // Clearly don't want to embed a facet for ourself... var facetClassDecl = f.OriginalShaderClass.Decl; if (!facetClassDecl.IsPrimary) { // Mixin classes will be reflected as fields. var facetClassInfo = _mapShaderClassToInfo[facetClassDecl]; var fieldType = facetClassInfo.InterfaceClass; var fieldName = string.Format( "_MixinImpl_{0}", f.OriginalShaderClass.Decl.Name.ToString()); var field = implClass.AddPrivateField(fieldType, fieldName); getFacetPointerForBase[facetClassDecl] = (b) => b.GetArrow(b.Method.ThisParameter, field).GetAddress(); facetInfoData.Add(emitModule.LiteralString(_mapShaderClassToInfo[facetClassDecl].InterfaceClass.GetName())); facetInfoData.Add(Target.LiteralU32(facetOffset)); facetInfoCount++; facetOffset += fieldType.Size; } } var facetInfoVal = emitModule.EmitGlobalStruct( null, facetInfoData.ToArray()).GetAddress(); // var deviceType = Target.GetOpaqueType("ID3D11Device*"); var contextType = Target.GetOpaqueType("ID3D11DeviceContext*"); // Create constructor var ctor = implClass.CreateCtor(); var ctorDevice = ctor.AddParameter( deviceType, "device"); var facetInitBlock = ctor.EntryBlock.InsertBlock(); var cbInit = ctor.EntryBlock.InsertBlock(); // First things first: wire up all the various facets to // the appropriate pointers: Dictionary <MidPipelineDecl, IEmitVal> initFacetPointers = new Dictionary <MidPipelineDecl, IEmitVal>(); foreach (var p in getFacetPointerForBase) { var facet = p.Key; var accessor = p.Value; var val = accessor(facetInitBlock); initFacetPointers[facet] = val; } foreach (var f in info.AllFacets) { var facetPointer = initFacetPointers[f.OriginalClass]; foreach (var m in f.Mixins) { var mixinPointer = initFacetPointers[m.OriginalClass]; facetInitBlock.SetArrow( facetPointer, m.MixinField, facetInitBlock.CastRawPointer( mixinPointer, m.MixinField.Type)); } } // Create destructor var dtor = implClass.CreateDtor(); var cbFinit = dtor.EntryBlock.InsertBlock(); // Create Submit() method var submit = implClass.CreateMethod( Target.VoidType, "Submit"); var submitDevice = submit.AddParameter( deviceType, "device"); var submitContext = submit.AddParameter( contextType, "context"); var submitEnv = new EmitEnv(pipelineEnv); var cbSubmit = submit.EntryBlock.InsertBlock(); var cbPointerType = Target.GetOpaqueType("ID3D11Buffer*"); var cbField = implClass.AddPrivateField( cbPointerType, "_cb"); emitPass = new PassEmitContext() { EmitContext = this, MidPass = midPipeline, SharedHLSL = sharedHLSL, InitBlock = ctor.EntryBlock, ExecBlock = submit.EntryBlock, DtorBlock = dtor.EntryBlock, EmitClass = implClass, CtorDevice = ctorDevice, SubmitContext = submitContext, CtorThis = ctor.ThisParameter, SubmitThis = submit.ThisParameter, DtorThis = dtor.ThisParameter, CBField = cbField, SubmitEnv = submitEnv, }; // Now emit stage-specific code. var iaStage = new D3D11InputAssembler() { EmitPass = emitPass, Range = range }; var vsStage = new D3D11VertexShader() { EmitPass = emitPass, Range = range }; var hsStage = new D3D11HullShader() { EmitPass = emitPass, Range = range }; var dsStage = new D3D11DomainShader() { EmitPass = emitPass, Range = range }; var gsStage = new D3D11GeometryShader() { EmitPass = emitPass, Range = range }; var psStage = new D3D11PixelShader() { EmitPass = emitPass, Range = range }; vsStage.EmitImplSetup(); iaStage.EmitImplSetup(); // IA after VS for bytecode dependency hsStage.EmitImplSetup(); dsStage.EmitImplSetup(); gsStage.EmitImplSetup(); psStage.EmitImplSetup(); psStage.EmitImplBindOM(); // OM first iaStage.EmitImplBind(); // IA as early as possible vsStage.EmitImplBind(); hsStage.EmitImplBind(); dsStage.EmitImplBind(); gsStage.EmitImplBind(); psStage.EmitImplBind(); iaStage.EmitImplDraw(); // Generate code to fill out CB after all the // stage-specific shader stuff, since these are // what compute the required @Uniform values. var block = cbInit; var cbDescVal = block.Temp( "cbDesc", block.Struct( "D3D11_BUFFER_DESC", block.LiteralU32((UInt32)sharedHLSL.ConstantBufferSize), block.Enum32("D3D11_USAGE", "D3D11_USAGE_DYNAMIC", D3D11Stage.D3D11_USAGE.D3D11_USAGE_DYNAMIC), block.LiteralU32((UInt32)D3D11Stage.D3D11_BIND_FLAG.D3D11_BIND_CONSTANT_BUFFER), block.LiteralU32((UInt32)D3D11Stage.D3D11_CPU_ACCESS_FLAG.D3D11_CPU_ACCESS_WRITE), block.LiteralU32(0), block.LiteralU32(0))); block.SetArrow( ctor.ThisParameter, cbField, cbPointerType.Null()); block.CallCOM( ctorDevice, "ID3D11Device", "CreateBuffer", cbDescVal.GetAddress(), Target.GetBuiltinType("D3D11_SUBRESOURCE_DATA").Pointer().Null(), block.GetArrow(ctor.ThisParameter, cbField).GetAddress()); block = cbSubmit; var mappedType = Target.GetBuiltinType("D3D11_MAPPED_SUBRESOURCE"); var cbMappedVal = block.Local("_cbMapped", mappedType); block.CallCOM( submitContext, "ID3D11DeviceContext", "Map", block.GetArrow(submit.ThisParameter, cbField), block.LiteralU32(0), block.Enum32("D3D11_MAP", "D3D11_MAP_WRITE_DISCARD", D3D11Stage.D3D11_MAP.D3D11_MAP_WRITE_DISCARD), block.LiteralU32(0), cbMappedVal.GetAddress()); IEmitVal mappedData = block.Temp( "cbMappedData", block.CastRawPointer( block.GetBuiltinField(cbMappedVal, "pData", Target.GetOpaqueType("void*")))); foreach (var u in sharedHLSL.Uniforms) { var val = EmitExp(u.Val, block, pipelineEnv); block.StoreRaw( mappedData, (UInt32)u.ByteOffset, val); } block.CallCOM( submitContext, "ID3D11DeviceContext", "Unmap", block.GetArrow(submit.ThisParameter, cbField), block.LiteralU32(0)); cbFinit.CallCOM( cbFinit.GetArrow(dtor.ThisParameter, cbField), "ID3D11Buffer", "Release"); // Now generate calls to bind the depth-stencil and rasterizer states var rsStateAttr = FindAttribute(midPipeline, "Uniform", "RS_State").First(); var rsStateVal = EmitAttributeRef( rsStateAttr, block, pipelineEnv); block.CallCOM( submitContext, "ID3D11DeviceContext", "RSSetState", rsStateVal); var omDepthStencilStateAttr = FindAttribute(midPipeline, "Uniform", "OM_DepthStencilState").First(); var omDepthStencilStateVal = EmitAttributeRef( omDepthStencilStateAttr, block, pipelineEnv); var omStencilRefAttr = FindAttribute(midPipeline, "Uniform", "OM_StencilRef").First(); var omStencilRefVal = EmitAttributeRef( omStencilRefAttr, block, pipelineEnv); block.CallCOM( submitContext, "ID3D11DeviceContext", "OMSetDepthStencilState", omDepthStencilStateVal, omStencilRefVal); implClass.Seal(); // Now we need to constrct the class-info // structure, that will be used as a kind of // virtual function table at runtime. var classInfoVal = emitModule.EmitGlobalStruct( className, new IEmitVal[] { Target.LiteralU32(implClass.Size), Target.LiteralU32((UInt32)facetInfoCount), facetInfoVal, emitModule.GetMethodPointer(ctor), emitModule.GetMethodPointer(dtor), emitModule.GetMethodPointer(submit), }); if (emitModule is Emit.CPlusPlus.EmitModuleCPP) { var moduleCPP = (Emit.CPlusPlus.EmitModuleCPP)emitModule; moduleCPP.SourceSpan.WriteLine( "const spark::ShaderClassDesc* {0}::GetShaderClassDesc() {{ return reinterpret_cast<const spark::ShaderClassDesc*>(&({1})); }}", ifaceClass, classInfoVal); } }