/// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { if (!IsDisposed) { //Dispose of managed resources if (disposing) { if (_buffer != null) { if (_renderer != null) { _renderer.Resources.RemoveTrackedObject(_buffer.ComPointer); } _buffer.Dispose(); _buffer = null; } if (_staging != null) { if (_renderer != null) { _renderer.Resources.RemoveTrackedObject(_staging.ComPointer); } _staging.Dispose(); _staging = null; } } base.Dispose(disposing); } }
private void CreateVerticesData(string objectName, List <OpenTK.Vector3> data, int dataSize) { if (!_objectsHandleDictionary.ContainsKey(objectName)) { throw new ApplicationException("Object must be added to the renderer before setting its vertex data"); } var verticesStream = new DataStream(dataSize * OpenTK.Vector3.SizeInBytes, true, true); foreach (var vector in data) { verticesStream.Write(new Vector3(vector.X, vector.Y, vector.Z)); } verticesStream.Position = 0; var bufferDesc = new BufferDescription { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = dataSize * OpenTK.Vector3.SizeInBytes, Usage = ResourceUsage.Dynamic }; var vertexBuffer = new Buffer(_device, verticesStream, bufferDesc); _objectsHandleDictionary[objectName].VertexBuffer = vertexBuffer; _objectsHandleDictionary[objectName].VertexBufferSize = dataSize; }
private void SetupBox() { EffectTechnique technique = m_effect.GetTechniqueByIndex(0); EffectPass pass = technique.GetPassByIndex(0); InputElement[] inputElements = new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }; m_boxLayout = new InputLayout(Device, pass.Description.Signature, inputElements); m_boxStream = new DataStream(m_boxPoints.Length * m_pointSize, true, true); m_boxStream.WriteRange(m_boxPoints); m_boxStream.Position = 0; BufferDescription bufferDesc = new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = m_boxPoints.Length * m_pointSize, Usage = ResourceUsage.Default }; m_boxVertices = new Buffer(Device, m_boxStream, bufferDesc); }
private void CreateIndexData(string objectName, List <int> indices, int indicesSize) { if (!_objectsHandleDictionary.ContainsKey(objectName)) { throw new ApplicationException("Object must be added to the renderer before setting its index data"); } var indicesStream = new DataStream(indicesSize * sizeof(int), true, true); foreach (var index in indices) { indicesStream.Write(index); } indicesStream.Position = 0; var bufferDesc = new BufferDescription { BindFlags = BindFlags.IndexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = indicesSize * sizeof(int), Usage = ResourceUsage.Dynamic }; var indexBuffer = new Buffer(_device, indicesStream, bufferDesc); _objectsHandleDictionary[objectName].IndexBuffer = indexBuffer; _objectsHandleDictionary[objectName].IndexCount = indices.Count; _objectsHandleDictionary[objectName].IndexBufferSize = indicesSize; }
/// <summary> /// Creates a new instance of <see cref="D3D10IndexBufferImplementation"/>. /// </summary> /// <param name="renderer">The D3D renderer.</param> /// <param name="format">The index format.</param> /// <param name="indexCount">The number of indices.</param> /// <param name="usage">The buffer usage specifying what type of memory the buffer should be created in.</param> internal D3D10IndexBufferImplementation(D3D10Renderer renderer, IndexFormat format, int indexCount, ResourceUsage usage) : base(format, indexCount, usage) { _renderer = renderer; _graphicsDevice = _renderer.GraphicsDevice; int formatBytes = (format == IndexFormat.ThirtyTwoBits) ? 4 : 2; try { //Determine appropiate vertex buffer to create if (usage == ResourceUsage.Static) { D3D.ResourceUsage rUsage = D3D.ResourceUsage.Default; D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None; _buffer = new D3D.Buffer(_graphicsDevice, new D3D.BufferDescription(indexCount * formatBytes, rUsage, D3D.BindFlags.IndexBuffer, cpuAccess, D3D.ResourceOptionFlags.None)); } else if (usage == ResourceUsage.Dynamic) { D3D.ResourceUsage rUsage = D3D.ResourceUsage.Dynamic; D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write; _buffer = new D3D.Buffer(_graphicsDevice, new D3D.BufferDescription(indexCount * formatBytes, rUsage, D3D.BindFlags.IndexBuffer, cpuAccess, D3D.ResourceOptionFlags.None)); } //Add to tracker _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this); } catch (Exception e) { Dispose(); throw new TeslaException("Error creating DX10 buffer:\n" + e.Message); } }
private void CreateTextures() { // RGB image texture goes here var descTex = new Texture2DDescription { BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, //Format = SlimDX.DXGI.Format.R32G32B32A32_Float, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Width = _xRes, Height = _yRes, MipLevels = 1, ArraySize = 1, Usage = ResourceUsage.Dynamic }; _imageTexture = new Texture2D(_dxDevice, descTex); _imageTextureRV = new ShaderResourceView(_dxDevice, _imageTexture); // depth map buffer _depthMapBuffer = new Buffer(_dxDevice, _vertexCount * sizeof(UInt16), ResourceUsage.Dynamic, BindFlags.ShaderResource, CpuAccessFlags.Write, ResourceOptionFlags.None); _depthMapBufferRV = new ShaderResourceView(_dxDevice, _depthMapBuffer, new ShaderResourceViewDescription { Dimension = ShaderResourceViewDimension.Buffer, Format = Format.R16_UInt, ElementOffset = 0, ElementWidth = _vertexCount, }); }
/// <summary> /// Creates a new instance of <see cref="D3D10VertexBufferImplementation"/> initialized with the interleaved data. /// </summary> /// <param name="renderer">The D3D10 renderer.</param> /// <param name="decl">The vertex declaration.</param> /// <param name="usage">The resource usage.</param> /// <param name="data">The interleaved data.</param> /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying D3D10 buffer or writing to it failed.</exception> internal D3D10VertexBufferImplementation(D3D10Renderer renderer, VertexDeclaration decl, ResourceUsage usage, DataBuffer data) : base(decl, usage, data) { _renderer = renderer; _graphicsDevice = _renderer.GraphicsDevice; //Determine appropiate buffer, let any exceptions bubble up. try { using (SDX.DataStream ds = new SDX.DataStream(data.ByteDataCopy, true, false)) { //Now create and populate appropiate D3D10 buffer if (base.BufferUsage == ResourceUsage.Static) { D3D.ResourceUsage rUsage = D3D.ResourceUsage.Default; D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None; _buffer = new D3D.Buffer(_graphicsDevice, ds, new D3D.BufferDescription(data.SizeInBytes, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None)); } else if (base.BufferUsage == ResourceUsage.Dynamic) { D3D.ResourceUsage rUsage = D3D.ResourceUsage.Dynamic; D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write; _buffer = new D3D.Buffer(_graphicsDevice, ds, new D3D.BufferDescription(data.SizeInBytes, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None)); } } //Add to tracker _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this); } catch (Exception e) { Dispose(); throw new TeslaException("Error creating D3D10 buffer: \n" + e.Message, e); } }
private void CreateBatch(PositionTexCoordNormalVertex[] vertices, short[] indices, Renderer renderer) { Debug.Assert(vertices != null && indices != null); int vertexBufferSize = PositionTexCoordNormalVertex.SizeInBytes * vertices.Length; var verticesData = new DataStream(vertexBufferSize, true, true); verticesData.WriteRange(vertices); verticesData.Seek(0, SeekOrigin.Begin); var vertexBuffer = new Buffer(renderer.Device, verticesData, vertexBufferSize, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); verticesData.Close(); int indexBufferSize = sizeof(short) * indices.Length; var indicesData = new DataStream(indexBufferSize, true, true); indicesData.WriteRange(indices); indicesData.Seek(0, SeekOrigin.Begin); var indexBuffer = new Buffer(renderer.Device, indicesData, indexBufferSize, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); indicesData.Close(); // Create batch var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, PositionTexCoordNormalVertex.SizeInBytes, 0); var inputLayout = new InputLayout(renderer.Device, PositionTexCoordNormalVertex.InputElements, effect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature); batch = new DrawIndexedBatch(renderer.Device, effect, new[] { vertexBufferBinding }, inputLayout, indexBuffer, Format.R16_UInt, indices.Length, 0, 0); batches.Add(batch); }
private void CreateBuffer(BindFlags bindFlags) { int sizeInBytes = mData.Length * ElementSize; var stream = new DataStream(sizeInBytes, canRead: true, canWrite: true); foreach (var element in mData) { stream.Write(element); } // Important: when specifying initial buffer data like this, the buffer will // read from the current DataStream position; we must rewind the stream to // the start of the data we just wrote. stream.Position = 0; var bufferDescription = new BufferDescription { BindFlags = bindFlags, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = sizeInBytes, Usage = ResourceUsage.Default }; Buffer = new Buffer(Device, stream, bufferDescription); stream.Dispose(); }
private void CreateIndexBuffer() { // Why 36 * sizeof(short) ? : See above there are 36 Indices * sizeof(short) : short = 16bits = 2 bytes _indexBuffer = new Buffer(_dxDevice, 36 * sizeof(short), ResourceUsage.Dynamic, BindFlags.IndexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); DataStream indexStream = _indexBuffer.Map(MapMode.WriteDiscard, MapFlags.None); indexStream.WriteRange(new short[] { 3, 1, 0, // Top Face 1 Triangle 1 2, 1, 3, // Triangle 2 0, 5, 4, // Front Face 2 Triangle 3 1, 5, 0, // Triangle 4 3, 4, 7, // Left Side Face 3 Triangle 5 0, 4, 3, // Triangle 6 1, 6, 5, // Right Side Face 4 Triangle 7 2, 6, 1, // Triangle 8 2, 7, 6, // Back Face 5 Triangle 9 3, 7, 2, // Triangle 10 6, 4, 5, // Bottom Face 6 Triangle 11 7, 4, 6, // Triangle 12 }); _indexBuffer.Unmap(); }
// Constructor public static void Initialize() { QuadVertex[] svQuad = new QuadVertex[4]; svQuad[0].pos = new Vector4(-1.0f, 1.0f, 0.5f, 1.0f); svQuad[0].tex = new Vector2(0.0f, 0.0f); svQuad[1].pos = new Vector4(1.0f, 1.0f, 0.5f, 1.0f); svQuad[1].tex = new Vector2(1.0f, 0.0f); svQuad[2].pos = new Vector4(-1.0f, -1.0f, 0.5f, 1.0f); svQuad[2].tex = new Vector2(0.0f, 1.0f); svQuad[3].pos = new Vector4(1.0f, -1.0f, 0.5f, 1.0f); svQuad[3].tex = new Vector2(1.0f, 1.0f); DataStream s = new DataStream(4 * Marshal.SizeOf(typeof(QuadVertex)), true, true); s.WriteRange(svQuad); s.Position = 0; BufferDescription bufferDescription = new BufferDescription(); bufferDescription.BindFlags = BindFlags.VertexBuffer; bufferDescription.CpuAccessFlags = CpuAccessFlags.None; bufferDescription.OptionFlags = ResourceOptionFlags.None; bufferDescription.SizeInBytes = 4 * Marshal.SizeOf(typeof(QuadVertex)); bufferDescription.Usage = ResourceUsage.Default; quadVerts = new D3D10.Buffer(Game.Device, s, bufferDescription); s.Close(); }
private void CreateIndexBuffer() { // Why 36 * sizeof(short) ? : See above there are 36 Indices * sizeof(short) : short = 16bits = 2 bytes _indexBuffer = new Buffer(_dxDevice, 36 * sizeof(short), ResourceUsage.Dynamic, BindFlags.IndexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); DataStream indexStream = _indexBuffer.Map(MapMode.WriteDiscard, MapFlags.None); indexStream.WriteRange(new short[] { 3,1,0, // Top Face 1 Triangle 1 2,1,3, // Triangle 2 0,5,4, // Front Face 2 Triangle 3 1,5,0, // Triangle 4 3,4,7, // Left Side Face 3 Triangle 5 0,4,3, // Triangle 6 1,6,5, // Right Side Face 4 Triangle 7 2,6,1, // Triangle 8 2,7,6, // Back Face 5 Triangle 9 3,7,2, // Triangle 10 6,4,5, // Bottom Face 6 Triangle 11 7,4,6, // Triangle 12 }); _indexBuffer.Unmap(); }
private void CreateVertexBuffer() { _vertexCount = _xRes * _yRes; var vertexStream = new DataStream(_vertexCount * PointVertex.SizeOf, true, true); // store pixel coordinates in each vertex for (short y = 0; y < _yRes; y++) { for (short x = 0; x < _xRes; x++) { var pt = new PointVertex(x, y); vertexStream.Write(pt); } } vertexStream.Position = 0; // create vertex buffer _vertexBuffer = new Buffer(_dxDevice, vertexStream, _vertexCount * PointVertex.SizeOf, ResourceUsage.Immutable, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); vertexStream.Close(); }
private void CreateIndexBuffer() { _indexCount = (_xRes - 1) * (_yRes - 1) * 6; var indexStream = new DataStream(_indexCount * sizeof(uint), true, true); // create index buffer for (int y = 0; y < _yRes - 1; y++) { for (int x = 0; x < _xRes - 1; x++) { // first triangle indexStream.Write(y * _xRes + x); indexStream.Write((y + 1) * _xRes + x); indexStream.Write(y * _xRes + x + 1); // second triangle indexStream.Write((y + 1) * _xRes + x); indexStream.Write((y + 1) * _xRes + x + 1); indexStream.Write(y * _xRes + x + 1); } } indexStream.Position = 0; _indexBuffer = new Buffer(_dxDevice, indexStream, _indexCount * sizeof(uint), ResourceUsage.Immutable, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); indexStream.Close(); }
/// <summary> /// Creates a new instance of <see cref="D3D10VertexBufferImplementation"/>. /// </summary> /// <param name="renderer">The D3D10 renderer.</param> /// <param name="decl">The vertex declaration.</param> /// <param name="vertexCount">The vertex count.</param> /// <param name="usage">The resource usage.</param> /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying D3D10 buffer failed.</exception> internal D3D10VertexBufferImplementation(D3D10Renderer renderer, VertexDeclaration decl, int vertexCount, ResourceUsage usage) : base(decl, vertexCount, usage) { _renderer = renderer; _graphicsDevice = _renderer.GraphicsDevice; try { //Determine appropiate vertex buffer to create, let any exceptions bubble up. if (base.BufferUsage == ResourceUsage.Static) { D3D.ResourceUsage rUsage = D3D.ResourceUsage.Default; D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None; _buffer = new D3D.Buffer(_graphicsDevice, new D3D.BufferDescription(vertexCount * decl.VertexStride, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None)); } else if (base.BufferUsage == ResourceUsage.Dynamic) { D3D.ResourceUsage rUsage = D3D.ResourceUsage.Dynamic; D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write; _buffer = new D3D.Buffer(_graphicsDevice, new D3D.BufferDescription(vertexCount * decl.VertexStride, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None)); } //Add to tracker _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this); } catch (Exception e) { Dispose(); throw new TeslaException("Error creating D3D10 buffer: \n" + e.Message, e); } }
void DestroyD3D() { if (SampleVertices != null) { SampleVertices.Dispose(); SampleVertices = null; } if (SampleLayout != null) { SampleLayout.Dispose(); SampleLayout = null; } if (SampleEffect != null) { SampleEffect.Dispose(); SampleEffect = null; } if (SampleStream != null) { SampleStream.Dispose(); SampleStream = null; } if (SampleLayout != null) { SampleLayout.Dispose(); SampleLayout = null; } }
public void DrawSimple(Buffer vertexBuffer, Buffer indexBuffer, int indexCount) { GraphicsDevice.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 0, 0)); GraphicsDevice.InputAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0); GraphicsDevice.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.LineList); GraphicsDevice.DrawIndexed(indexCount, 0, 0); }
public static void Dispose() { if (vertices != null && !vertices.Disposed) { vertices.Dispose(); vertices = null; } }
/// <summary> /// Disposes of our managed resources. /// </summary> protected override void DisposeManaged() { { if (m_sampleVertices != null) { m_sampleVertices.Dispose(); m_sampleVertices = null; } if (m_sampleLayout != null) { m_sampleLayout.Dispose(); m_sampleLayout = null; } if (m_sampleStream != null) { m_sampleStream.Dispose(); m_sampleStream = null; } } { if (m_boxVertices != null) { m_boxVertices.Dispose(); m_boxVertices = null; } if (m_boxLayout != null) { m_boxLayout.Dispose(); m_boxLayout = null; } if (m_boxStream != null) { m_boxStream.Dispose(); m_boxStream = null; } } if (m_effect != null) { m_effect.Dispose(); m_effect = null; } if (SharedTexture != null) { SharedTexture.Dispose(); SharedTexture = null; } base.DisposeManaged(); return; }
public override void Setup(Device device, ShaderLibrary shaderLibrary, string basePath) { this._MaterialLoader.Setup(device, basePath, this.Block.Material); string vertexShaderName = "deformablewindow"; string pixelShaderName = "deformablewindow"; this._ShaderLoader.Setup( device, shaderLibrary.GetVertexShaderData(vertexShaderName), shaderLibrary.GetFragmentShaderData(pixelShaderName), new[] { new InputElement("POSITION", 0, DXGI.Format.R32G32B32A32_Float, 0, 0, IC.PerVertexData, 0), new InputElement("TEXCOORD", 0, DXGI.Format.R16G16B16A16_SNorm, 16, 0, IC.PerVertexData, 0), new InputElement("TEXCOORD", 1, DXGI.Format.R32G32B32A32_Float, 24, 0, IC.PerVertexData, 0), new InputElement("TEXCOORD", 2, DXGI.Format.R32G32_Float, 40, 0, IC.PerVertexData, 0), }); var vertexBuffer = new Buffer(device, 48 * this.Block.VertexData0.Count, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); using (var stream = vertexBuffer.Map(MapMode.WriteDiscard, MapFlags.None)) { stream.WriteRange(this.Block.VertexData0.ToArray()); vertexBuffer.Unmap(); } this._VertexData0Buffer = vertexBuffer; var indexBuffer = new Buffer(device, 2 * this.Block.Faces.Count, ResourceUsage.Dynamic, BindFlags.IndexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); using (var stream = indexBuffer.Map(MapMode.WriteDiscard, MapFlags.None)) { stream.WriteRange(this.Block.Faces.ToArray()); indexBuffer.Unmap(); } this._IndexBuffer = indexBuffer; this._VertexShaderConstantBuffer1 = new ConstantBuffer <VertexShaderGlobals>(device); this._PixelShaderConstantBuffer0 = new ConstantBuffer <PixelShaderGlobalConstants>(device); this._PixelShaderConstantBuffer1 = new ConstantBuffer <PixelShaderInstanceConstants>(device); this._PixelShaderConstantBuffer2 = new ConstantBuffer <PixelShaderMaterialConstants>(device); this._PixelShaderConstantBuffer4 = new ConstantBuffer <PixelShaderBooleans>(device); }
public DrawIndexedBatch(Device device, Effect effect, IEnumerable<VertexBufferBinding> vertexBuffers, InputLayout inputLayout, Buffer indexBuffer, Format indexBufferFormat, int indexCount, int startIndex, int baseVertex) : base(device, effect, vertexBuffers, inputLayout) { IndexBuffer = indexBuffer; IndexBufferFormat = indexBufferFormat; IndexCount = indexCount; StartIndex = startIndex; BaseVertex = baseVertex; }
public ConstantBuffer(Device device) { var size = Marshal.SizeOf(typeof(TStructure)); this.Buffer = new Buffer(device, size.Align(16), ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); }
public DrawIndexedBatch(Device device, Effect effect, IEnumerable <VertexBufferBinding> vertexBuffers, InputLayout inputLayout, Buffer indexBuffer, Format indexBufferFormat, int indexCount, int startIndex, int baseVertex) : base(device, effect, vertexBuffers, inputLayout) { IndexBuffer = indexBuffer; IndexBufferFormat = indexBufferFormat; IndexCount = indexCount; StartIndex = startIndex; BaseVertex = baseVertex; }
public void Dispose() { if (_vertexBuffer != null) { _vertexBuffer.Dispose(); _vertexBuffer = null; } if (_indexBuffer != null) { _indexBuffer.Dispose(); _indexBuffer = null; } }
public static void RenderMesh2(Model m, Vector3 position, string effectName, string technique, Matrix mWorld, int stride) { if (m == null || m.MeshObj == null) { return; } Shader e = WorldData.GetObject(effectName) as Shader; Matrix world = Matrix.Identity + Matrix.Translation(position); if (mWorld != null) { world = mWorld; } ShaderHelper.UpdateCommonEffectVars(e, world); EffectTechnique t = e.EffectObj.GetTechniqueByName(technique); D3D10.Buffer indexBuffer = m.MeshObj.GetDeviceIndexBuffer(); D3D10.Buffer vertextBuffer = m.MeshObj.GetDeviceVertexBuffer(0); Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); Game.Device.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0); Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertextBuffer, stride, 0)); for (int p = 0; p < t.Description.PassCount; p++) { EffectPass pass = t.GetPassByIndex(p); InputLayout l = ShaderHelper.ConstructInputLayout(m.Mesh3d.inputElements, pass.Description.Signature); Game.Device.InputAssembler.SetInputLayout(l); for (int subset = 0; subset < m.Mesh3d.NumAttributes; subset++) { EffectResourceVariable diffTex = e.GetVar(ShaderHelper.DiffTex).AsResource(); if (diffTex != null) { diffTex.SetResource(m.Mesh3d.textureViews[subset]); } pass.Apply(); MeshAttributeRange r = m.Mesh3d.attrTable[subset]; // * 2 cause adj data is twice as much data //Game.Device.DrawIndexed((r.FaceCount * 3) * 2, (r.FaceStart * 3) * 2, 0); Game.Device.DrawIndexed((r.FaceCount * 3), (r.FaceStart * 3), 0); } } indexBuffer.Dispose(); vertextBuffer.Dispose(); }
/// <summary> /// Creates a new instance of <see cref="D3D10VertexBufferImplementation"/> initialized with the vertex data array. /// </summary> /// <param name="renderer">The D3D10 renderer.</param> /// <param name="decl">The vertex declaration.</param> /// <param name="usage">The resource usage.</param> /// <param name="data">The array of vertex data.</param> /// <exception cref="Tesla.Core.TeslaException">Thrown if creating the underlying D3D10 buffer or writing to it failed.</exception> internal D3D10VertexBufferImplementation(D3D10Renderer renderer, VertexDeclaration decl, ResourceUsage usage, params DataBuffer[] data) : base(decl, usage, data) { _renderer = renderer; _graphicsDevice = renderer.GraphicsDevice; int totalSizeInBytes = base.VertexCount * decl.VertexStride; int vertexStride = decl.VertexStride; try { //Now initialize the buffer with the supplied vertex store using (SDX.DataStream interleaved = new SDX.DataStream(totalSizeInBytes, true, true)) { //Create the interleaved buffer byte[] vertex = new byte[vertexStride]; for (int i = 0; i < base.VertexCount; i++) { int offset = 0; for (int j = 0; j < data.Length; j++) { DataBuffer db = data[j]; int elementSize = db.ElementSizeInBytes; db.Get(vertex, offset, elementSize); offset += elementSize; } interleaved.Write(vertex, 0, vertexStride); } interleaved.Position = 0; //Now create and populate appropiate D3D10 buffer if (base.BufferUsage == ResourceUsage.Static) { D3D.ResourceUsage rUsage = D3D.ResourceUsage.Default; D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.None; _buffer = new D3D.Buffer(_graphicsDevice, interleaved, new D3D.BufferDescription(totalSizeInBytes, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None)); } else if (base.BufferUsage == ResourceUsage.Dynamic) { D3D.ResourceUsage rUsage = D3D.ResourceUsage.Dynamic; D3D.CpuAccessFlags cpuAccess = D3D.CpuAccessFlags.Write; _buffer = new D3D.Buffer(_graphicsDevice, interleaved, new D3D.BufferDescription(totalSizeInBytes, rUsage, D3D.BindFlags.VertexBuffer, cpuAccess, D3D.ResourceOptionFlags.None)); } } //Add to tracker _renderer.Resources.AddTrackedObject(_buffer.ComPointer, this); } catch (Exception e) { Dispose(); throw new TeslaException("Error creating D3D10 buffer: \n" + e.Message, e); } }
private static void DrawClouds(bool renderReflection) { if (skyBoxCloudsMesh == null || skyBoxCloudsMesh.MeshObj == null) { return; } Shader scatterEffect = WorldData.GetObject("scatter.fx") as Shader; EffectTechnique tech = scatterEffect.EffectObj.GetTechniqueByName("RenderClouds"); if (renderReflection) { tech = scatterEffect.EffectObj.GetTechniqueByName("RenderCloudsReflection"); } D3D10.Buffer iBuffer = skyBoxCloudsMesh.MeshObj.GetDeviceIndexBuffer(); D3D10.Buffer vBuffer = skyBoxCloudsMesh.MeshObj.GetDeviceVertexBuffer(0); Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); Game.Device.InputAssembler.SetIndexBuffer(iBuffer, Format.R16_UInt, 0); Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vBuffer, 56, 0)); EffectPass p1 = tech.GetPassByIndex(0); InputLayout layout = ShaderHelper.ConstructInputLayout(MeshInputElements10.NormalMesh, p1.Description.Signature); Game.Device.InputAssembler.SetInputLayout(layout); Matrix mWorld = Matrix.Translation(Camera.Position); if (renderReflection) { mWorld = Matrix.Scaling(1, -1, 1) * Matrix.Translation(Camera.Position); } scatterEffect.GetVar("WorldViewProj").AsMatrix().SetMatrix(mWorld * Camera.ViewMatrix * Camera.ProjectionMatrix); if (settings != null && settings.SkySettings != null) { scatterEffect.GetVar("cloud1Tile").AsScalar().Set(settings.SkySettings.CloudTile.X); scatterEffect.GetVar("cloud2Tile").AsScalar().Set(settings.SkySettings.CloudTile.Y); scatterEffect.GetVar("cloudCover").AsScalar().Set(settings.SkySettings.CloudCover); } scatterEffect.GetVar("scroll").AsVector().Set(t); scatterEffect.GetVar("clouds1Tex").AsResource().SetResource(skyClouds1); scatterEffect.GetVar("clouds2Tex").AsResource().SetResource(skyClouds2); scatterEffect.GetVar("SunColor").AsVector().Set(SunColor); p1.Apply(); Game.Device.DrawIndexed((skyBoxCloudsMesh.Mesh3d.attrTable[0].FaceCount * 3), 0, 0); iBuffer.Dispose(); vBuffer.Dispose(); }
/// <summary> /// In a derived class, implements logic to resize the buffer. /// During resize, the existing buffer contents need not be preserved. /// </summary> /// <param name="sizeInBytes">The new size, in bytes.</param> protected override void ResizeBuffer(int sizeInBytes) { if (buffer != null) { buffer.Dispose(); } buffer = new D3D.Buffer(device, new D3D.BufferDescription { BindFlags = D3D.BindFlags.VertexBuffer, CpuAccessFlags = D3D.CpuAccessFlags.Write, OptionFlags = D3D.ResourceOptionFlags.None, SizeInBytes = sizeInBytes, Usage = D3D.ResourceUsage.Dynamic }); }
private void CreateStaging() { if (_staging == null) { D3D.BufferDescription desc = new D3D.BufferDescription(); desc.BindFlags = D3D.BindFlags.None; desc.CpuAccessFlags = D3D.CpuAccessFlags.Write | D3D.CpuAccessFlags.Read; desc.OptionFlags = D3D.ResourceOptionFlags.None; desc.SizeInBytes = base.IndexCount * ((base.IndexFormat == IndexFormat.SixteenBits) ? 2 : 4); desc.Usage = D3D.ResourceUsage.Staging; _staging = new D3D.Buffer(_graphicsDevice, desc); //Add to tracker _renderer.Resources.AddTrackedObject(_staging.ComPointer, this); } }
private void CreateStaging() { if (_staging == null) { D3D.BufferDescription desc = new D3D.BufferDescription(); desc.BindFlags = D3D.BindFlags.None; desc.CpuAccessFlags = D3D.CpuAccessFlags.Write | D3D.CpuAccessFlags.Read; desc.OptionFlags = D3D.ResourceOptionFlags.None; desc.SizeInBytes = base.VertexCount * base.VertexDeclaration.VertexStride; desc.Usage = D3D.ResourceUsage.Staging; _staging = new D3D.Buffer(_graphicsDevice, desc); //Add staging _renderer.Resources.AddTrackedObject(_staging.ComPointer, this); } }
private void renderLoop() { instanceBuffer = new SlimDX.Direct3D10.Buffer(device, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = (sizeof(float) * 4 * 4 + sizeof(int)) * InstanceCount, Usage = ResourceUsage.Dynamic }); while (IsRendering || HandleCommands) { try { if (HandleCommands) { commandQueue.TryExecute(CommandsPerFrame); } if (IsRendering) { render(); } } catch (SlimDX.DXGI.DXGIException) { IsRendering = false; } if (stateChanged) { stateChanged = false; evt.Finish(); } } instanceBuffer.Dispose(); if (stateChanged) { stateChanged = false; evt.Finish(); } }
void UpdateOcclusion() { if (gm == null) { return; } if (occlusionQueryActive) { // If the previous query has not yet completed, wait until it does. if (!occQuery.IsDataAvailable) { return; } UInt64 pixelCount = occQuery.GetData().Read <UInt64>(); occlusionAlpha = Math.Min(pixelCount / 25000.0f, 1); } occQuery.Begin(); Matrix m = Matrix.Scaling(3000, 3000, 3000) * Matrix.Translation(-SkyDome.LightDirection * 25000); Shader e = WorldData.GetObject("scatter.fx") as Shader; EffectTechnique t = e.EffectObj.GetTechniqueByName("RenderOccluder"); D3D10.Buffer indexBuffer = gm.MeshObj.GetDeviceIndexBuffer(); D3D10.Buffer vertextBuffer = gm.MeshObj.GetDeviceVertexBuffer(0); Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); Game.Device.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0); Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertextBuffer, 16, 0)); EffectPass pass = t.GetPassByIndex(0); InputLayout l = ShaderHelper.ConstructInputLayout(gm.Mesh3d.inputElements, pass.Description.Signature); Game.Device.InputAssembler.SetInputLayout(l); Matrix proj = Matrix.PerspectiveFovLH(Camera.Fovx, Camera.AspectRatio, 1.0f, 1000000.0f); e.GetVar("WorldViewProjection").AsMatrix().SetMatrix(m * Camera.ViewMatrix * proj); pass.Apply(); Game.Device.DrawIndexed((gm.Mesh3d.attrTable[0].FaceCount * 3), (gm.Mesh3d.attrTable[0].FaceStart * 3), 0); indexBuffer.Dispose(); vertextBuffer.Dispose(); occQuery.End(); occlusionQueryActive = true; }
public static void DrawInstancedSpheres(GraphicsDeviceManager gdm, List <Matrix> instances) { Model sphere = WorldData.GetObject("sphereInstanced.mesh") as Model; D3D10.Buffer indexBuffer = sphere.MeshObj.GetDeviceIndexBuffer(); D3D10.Buffer vertextBuffer = sphere.MeshObj.GetDeviceVertexBuffer(0); Shader e = WorldData.GetObject("Instanced.fx") as Shader; EffectTechnique t = e.EffectObj.GetTechniqueByName("RenderInstanced"); InputLayout l = ShaderHelper.ConstructInputLayout(MeshInputElements10.PositionOnlyInstanced, t.GetPassByIndex(0).Description.Signature); BufferDescription bd = new BufferDescription(); bd.SizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf(instances[0]) * instances.Count; bd.Usage = ResourceUsage.Dynamic; bd.CpuAccessFlags = CpuAccessFlags.Write; bd.BindFlags = BindFlags.VertexBuffer; D3D10.Buffer instanceData = new D3D10.Buffer(gdm.Direct3D10.Device, bd); DataStream ds = instanceData.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None); ds.Position = 0; ds.WriteRange(instances.ToArray()); instanceData.Unmap(); Game.Device.InputAssembler.SetInputLayout(l); Game.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertextBuffer, 16, 0), new VertexBufferBinding(instanceData, System.Runtime.InteropServices.Marshal.SizeOf(instances[0]), 0)); Game.Device.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0); Game.Device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); e.GetVar("WorldViewProj").AsMatrix().SetMatrix(Camera.ViewMatrix * Camera.ProjectionMatrix); t.GetPassByIndex(0).Apply(); Game.Device.DrawIndexedInstanced((sphere.Mesh3d.attrTable[0].FaceCount * 3), instances.Count, 0, 0, 0); //Game.Device.DrawIndexed((sphere.attrTable[0].FaceCount * 3) * 2, 0, 0); indexBuffer.Dispose(); vertextBuffer.Dispose(); sphere.Dispose(); }
public static void Create() { //without texcoord DataStream stream = new DataStream(12 * 6, true, true); stream.Write<Vector3>(new Vector3(-1, -1, 1)); stream.Write<Vector3>(new Vector3(-1, 1, 1)); stream.Write<Vector3>(new Vector3(1, 1, 1)); stream.Write<Vector3>(new Vector3(1, 1, 1)); stream.Write<Vector3>(new Vector3(1, -1, 1)); stream.Write<Vector3>(new Vector3(-1, -1, 1)); elements[0] = new SlimDX.Direct3D10.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0, 0); stream.Position = 0; buffer = new SlimDX.Direct3D10.Buffer(Game.gameClass.GetDevice(), stream, (int)stream.Length, D3D10.ResourceUsage.Immutable, D3D10.BindFlags.VertexBuffer, D3D10.CpuAccessFlags.None, D3D10.ResourceOptionFlags.None); binding = new SlimDX.Direct3D10.VertexBufferBinding(buffer, 12, 0); stream.Close(); //with texcoord stream = new DataStream(12 * 6*2, true, true); stream.Write<Vector3>(new Vector3(-1, -1, 1)); stream.Write<Vector3>(Vector3.Zero); stream.Write<Vector3>(new Vector3(-1, 1, 1)); stream.Write<Vector3>(Vector3.Zero); stream.Write<Vector3>(new Vector3(1, 1, 1)); stream.Write<Vector3>(Vector3.Zero); stream.Write<Vector3>(new Vector3(1, 1, 1)); stream.Write<Vector3>(Vector3.Zero); stream.Write<Vector3>(new Vector3(1, -1, 1)); stream.Write<Vector3>(Vector3.Zero); stream.Write<Vector3>(new Vector3(-1, -1, 1)); stream.Write<Vector3>(Vector3.Zero); elementsWithTexCoord[0] = new SlimDX.Direct3D10.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0, 0); elementsWithTexCoord[1] = new SlimDX.Direct3D10.InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32B32_Float, 12, 0); stream.Position = 0; bufferWithTexCoord = new SlimDX.Direct3D10.Buffer(Game.gameClass.GetDevice(), stream, (int)stream.Length, D3D10.ResourceUsage.Dynamic, D3D10.BindFlags.VertexBuffer, D3D10.CpuAccessFlags.Write, D3D10.ResourceOptionFlags.None); bindingWithTexCoord = new SlimDX.Direct3D10.VertexBufferBinding(bufferWithTexCoord, 24, 0); stream.Close(); }
private void LoadMesh(string meshName) { var meshData = Smd.FromFile(meshName); using (var data = new DataStream(meshData.Indices.ToArray(), true, false)) { indices = new Buffer(device, data, new BufferDescription(meshData.Indices.Count * 4, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None)); indexCount = meshData.Indices.Count; } using (var data = new DataStream(meshData.Positions.ToArray(), true, false)) { vertices = new Buffer(device, data, new BufferDescription(meshData.Positions.Count * 4 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None)); } using (var data = new DataStream(meshData.Normals.ToArray(), true, false)) { normals = new Buffer(device, data, new BufferDescription(meshData.Normals.Count * 4 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None)); } using (var data = new DataStream(meshData.TextureCoordinates.ToArray(), true, false)) { texCoords = new Buffer(device, data, new BufferDescription(meshData.TextureCoordinates.Count * 4 * 2, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None)); } }
static void BuildQuad() { ushort[] idx; TexturedVertex[] quad = CreateTexturedQuad(Vector3.Zero, size.Width, size.Height, out idx); // fill vertex and index buffers DataStream stream = new DataStream(4 * 24, true, true); stream.WriteRange(quad); stream.Position = 0; vertices = new SlimDX.Direct3D10.Buffer(device, stream, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = 4 * 24, Usage = ResourceUsage.Default }); stream.Close(); stream = new DataStream(6 * sizeof(ushort), true, true); stream.WriteRange(idx); stream.Position = 0; indices = new SlimDX.Direct3D10.Buffer(device, stream, new BufferDescription() { BindFlags = BindFlags.IndexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = 6 * sizeof(ushort), Usage = ResourceUsage.Default }); }
protected void Dispose(bool disposing) { if (_depthMapBuffer != null) { _depthMapBuffer.Dispose(); _depthMapBuffer = null; } if (_depthMapBufferRV != null) { _depthMapBufferRV.Dispose(); _depthMapBufferRV = null; } if (_imageTexture != null) { _imageTexture.Dispose(); _imageTexture = null; } if (_imageTextureRV != null) { _imageTextureRV.Dispose(); _imageTextureRV = null; } if (_effect != null) { _effect.Dispose(); _effect = null; } if (_inputLayout != null) { _inputLayout.Dispose(); _inputLayout = null; } if (_vertexBuffer != null) { _vertexBuffer.Dispose(); _vertexBuffer = null; } }
void InitD3D() { D3DDevice = new Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_0); Texture2DDescription colordesc = new Texture2DDescription(); colordesc.BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource; colordesc.Format = Format.B8G8R8A8_UNorm; colordesc.Width = WindowWidth; colordesc.Height = WindowHeight; colordesc.MipLevels = 1; colordesc.SampleDescription = new SampleDescription(1, 0); colordesc.Usage = ResourceUsage.Default; colordesc.OptionFlags = ResourceOptionFlags.Shared; colordesc.CpuAccessFlags = CpuAccessFlags.None; colordesc.ArraySize = 1; Texture2DDescription depthdesc = new Texture2DDescription(); depthdesc.BindFlags = BindFlags.DepthStencil; depthdesc.Format = Format.D32_Float_S8X24_UInt; depthdesc.Width = WindowWidth; depthdesc.Height = WindowHeight; depthdesc.MipLevels = 1; depthdesc.SampleDescription = new SampleDescription(1, 0); depthdesc.Usage = ResourceUsage.Default; depthdesc.OptionFlags = ResourceOptionFlags.None; depthdesc.CpuAccessFlags = CpuAccessFlags.None; depthdesc.ArraySize = 1; SharedTexture = new Texture2D(D3DDevice, colordesc); DepthTexture = new Texture2D(D3DDevice, depthdesc); SampleRenderView = new RenderTargetView(D3DDevice, SharedTexture); SampleDepthView = new DepthStencilView(D3DDevice, DepthTexture); SampleEffect = Effect.FromFile(D3DDevice, "MiniTri.fx", "fx_4_0"); EffectTechnique technique = SampleEffect.GetTechniqueByIndex(0); ; EffectPass pass = technique.GetPassByIndex(0); SampleLayout = new InputLayout(D3DDevice, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }); SampleStream = new DataStream(3 * 32, true, true); SampleStream.WriteRange(new[] { new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f) }); SampleStream.Position = 0; SampleVertices = new Buffer(D3DDevice, SampleStream, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = 3 * 32, Usage = ResourceUsage.Default }); D3DDevice.Flush(); }
// Create VertexBuffer private void CreateVertexBuffer() { // 8 * 32 WHY ? : 1 Float = 4bytes, so Vector4(floatX,floatY,floatZ,floatW) = 4 float = 16 bytes. // See above there are 8 vertices and each Vertex contain 2 * Vector4. Then 8 * (16 bytes + 16 bytes) = 8 * 32 // _vertexBuffer = new Buffer(_dxDevice, 8 * 32, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); /* vertexStream.WriteRange(new[] { // Color ((R)ed, (G)reen, (B)lue, (A)lpha) *Note Alpha used to blending (Transparency) // Position X Y Z W and Color R G B A new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // First Square Vertex 1 (0) new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Vertex 2 (1) new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Vertex 3 (2) new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Vertex 4 (3) new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // Second Square Vertex 5 (4) new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // Vertex 6 (5) new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // Vertex 7 (6) new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f) // Vertex 8 (7) });*/ var numVertices = 8; var numFaces = 12; _vertexBuffer = new Buffer(_dxDevice, numVertices * Vertex.Size, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); DataStream vertexStream = _vertexBuffer.Map(MapMode.WriteDiscard, MapFlags.None); vertexStream.WriteRange(new[] { new Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f), new Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f), new Vertex( 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f), new Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f), new Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f), new Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f), new Vertex( 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f), new Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f) }); _vertexBuffer.Unmap(); }
private void CreateVertexBuffer() { _vertexCount = _xRes*_yRes; var vertexStream = new DataStream(_vertexCount*PointVertex.SizeOf, true, true); // store pixel coordinates in each vertex for (short y = 0; y < _yRes; y++) { for (short x = 0; x < _xRes; x++) { var pt = new PointVertex(x, y); vertexStream.Write(pt); } } vertexStream.Position = 0; // create vertex buffer _vertexBuffer = new Buffer(_dxDevice, vertexStream, _vertexCount*PointVertex.SizeOf, ResourceUsage.Immutable, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); vertexStream.Close(); }
void DestroyD3D() { if (SampleVertices != null) { SampleVertices.Dispose(); SampleVertices = null; } if (SampleLayout != null) { SampleLayout.Dispose(); SampleLayout = null; } if (SampleEffect != null) { SampleEffect.Dispose(); SampleEffect = null; } if (SampleRenderView != null) { SampleRenderView.Dispose(); SampleRenderView = null; } if (SampleDepthView != null) { SampleDepthView.Dispose(); SampleDepthView = null; } if (SampleStream != null) { SampleStream.Dispose(); SampleStream = null; } if (SampleLayout != null) { SampleLayout.Dispose(); SampleLayout = null; } if (SharedTexture != null) { SharedTexture.Dispose(); SharedTexture = null; } if (DepthTexture != null) { DepthTexture.Dispose(); DepthTexture = null; } if (D3DDevice != null) { D3DDevice.Dispose(); D3DDevice = null; } }
private void GenerateGeometry() { using (var stream = new DataStream(4 * InputStride, true, true)) { for (int i = 0; i < 4; i++) { float x = (i % 2 == 0) ? -0.5f : 0.5f; float y = (i / 2 == 0) ? -0.5f : 0.5f; stream.Write(new Vector4(x, y, 0.0f, 1.0f)); stream.Write(new Vector2(0.5f - x * 1.03f, 0.5f - y * 1.03f)); } stream.Position = 0; m_Vertices = new SlimDX.Direct3D10.Buffer(m_D3DDevice, stream, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = 4 * InputStride, Usage = ResourceUsage.Default }); } }
private void CreateIndexBuffer() { _indexCount = (_xRes - 1)*(_yRes - 1)*6; var indexStream = new DataStream(_indexCount* sizeof(uint), true, true); // create index buffer for (int y = 0; y < _yRes -1; y++) { for (int x = 0; x < _xRes -1; x++) { // first triangle indexStream.Write(y*_xRes + x); indexStream.Write((y+1)*_xRes + x); indexStream.Write(y * _xRes + x + 1); // second triangle indexStream.Write((y + 1)*_xRes + x); indexStream.Write((y + 1)*_xRes + x + 1); indexStream.Write(y * _xRes + x + 1); } } indexStream.Position = 0; _indexBuffer = new Buffer(_dxDevice, indexStream, _indexCount*sizeof (uint), ResourceUsage.Immutable, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); indexStream.Close(); }
/// <summary> /// Creates the meshes we will display /// </summary> private void InitializeMeshes() { var vertices = new DataStream(12 * 3, canRead: true, canWrite: true); vertices.Write(new Vector3(0.0f, 0.5f, 0.5f)); vertices.Write(new Vector3(0.5f, -0.5f, 0.5f)); vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f)); vertices.Position = 0; // always remember to rewind _vertexBuffer = new Buffer(_device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); _device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); _device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_vertexBuffer, 12, 0)); var closerVertices = new DataStream(12 * 3, true, true); closerVertices.Write(new Vector3(0.0f, -0.5f, 0.3f)); closerVertices.Write(new Vector3(0.5f, 0.5f, 0.3f)); closerVertices.Write(new Vector3(-0.5f, 0.5f, 0.8f)); closerVertices.Position = 0; _closerTriangleBuffer = new Buffer(_device, closerVertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); }
public void SetFacesData(string objectName, List<int> indices) { if (!_objectsHandleDictionary.ContainsKey(objectName)) throw new ApplicationException("Object must be added to the renderer before setting its index data"); var indicesStream = new DataStream(indices.Count * sizeof(int), true, true); foreach (var index in indices) { indicesStream.Write(index); } indicesStream.Position = 0; var bufferDesc = new BufferDescription { BindFlags = BindFlags.IndexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = indices.Count * sizeof(int), Usage = ResourceUsage.Default }; var indexBuffer = new Buffer(_device, indicesStream, bufferDesc); _objectsHandleDictionary[objectName].IndexBuffer = indexBuffer; _objectsHandleDictionary[objectName].IndexCount = indices.Count; }
private void SetupSamples() { EffectTechnique technique = m_effect.GetTechniqueByIndex(0); EffectPass pass = technique.GetPassByIndex(0); InputElement[] inputElements = new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }; m_sampleLayout = new InputLayout(Device, pass.Description.Signature, inputElements); m_sampleStream = new DataStream(m_samplePoints.Length * m_pointSize, true, true); m_sampleStream.WriteRange(m_samplePoints); m_sampleStream.Position = 0; BufferDescription bufferDesc = new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = m_samplePoints.Length * m_pointSize, Usage = ResourceUsage.Default }; m_sampleVertices = new Buffer(Device, m_sampleStream, bufferDesc); }
private void CreateVertexBuffer() { //create vertex buffer int size = 8 * quadSize * quadSize * 6; //end size of each quad will be quadSize+1 DataStream stream = new DataStream(size, true, true); for (int i = 0; i < quadSize; i++) for (int j = 0; j < quadSize; j++) { stream.Write(new Vector2(i, j)); stream.Write(new Vector2(i, j + 1)); stream.Write(new Vector2(i + 1, j + 1)); stream.Write(new Vector2(i + 1, j + 1)); stream.Write(new Vector2(i + 1, j)); stream.Write(new Vector2(i, j)); } stream.Position = 0; vertexBuffer = new SlimDX.Direct3D10.Buffer(Game.gameClass.GetDevice(), stream, size, SlimDX.Direct3D10.ResourceUsage.Immutable, SlimDX.Direct3D10.BindFlags.VertexBuffer, SlimDX.Direct3D10.CpuAccessFlags.None, SlimDX.Direct3D10.ResourceOptionFlags.None); stream.Close(); instances = new Vector4[(Globals.mapSize / quadSize) * (Globals.mapSize / quadSize)]; vertexBufferBinding = new SlimDX.Direct3D10.VertexBufferBinding(vertexBuffer, sizeof(float) * 2, 0); }
public void SetVerticesData(string objectName, List<OpenTK.Vector3> data) { if (!_objectsHandleDictionary.ContainsKey(objectName)) throw new ApplicationException("Object must be added to the renderer before setting its vertex data"); var verticesStream = new DataStream(data.Count * OpenTK.Vector3.SizeInBytes, true, true); foreach (var vector in data) { verticesStream.Write(new Vector3(vector.X, vector.Y, vector.Z)); } verticesStream.Position = 0; var bufferDesc = new BufferDescription { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = data.Count * OpenTK.Vector3.SizeInBytes, Usage = ResourceUsage.Default }; var vertexBuffer = new Buffer(_device, verticesStream, bufferDesc); _objectsHandleDictionary[objectName].VertexBuffer = vertexBuffer; }
private Buffer CreateBuffer(Device device) { Vertex[] vertices = CreateVertices(); var stream = new DataStream(vertices.Length * Marshal.SizeOf(typeof(Vertex)), true, true); foreach (var vertex in vertices) { stream.Write(vertex.Position); stream.Write(vertex.Color); stream.Write(vertex.Normal); } // Important: when specifying initial buffer data like this, the buffer will // read from the current DataStream position; we must rewind the stream to // the start of the data we just wrote. stream.Position = 0; var bufferDescription = new BufferDescription { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = (3 * Marshal.SizeOf(typeof(Vertex))), Usage = ResourceUsage.Default }; var buffer = new Buffer(device, stream, bufferDescription); stream.Close(); return buffer; }
static void Main() { var form = new RenderForm("SlimDX - MiniTri Direct3D 10 Sample"); var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; Device device; SwapChain swapChain; Device.CreateWithSwapChain(null, DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain); //Stops Alt+enter from causing fullscreen skrewiness. device.Factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll); Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0); var renderView = new RenderTargetView(device, backBuffer); var effect = Effect.FromFile(device, "MiniTri.fx", "fx_4_0"); var technique = effect.GetTechniqueByIndex(0); var pass = technique.GetPassByIndex(0); var layout = new InputLayout(device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }); var stream = new DataStream(3 * 32, true, true); stream.WriteRange(new[] { new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f) }); stream.Position = 0; var vertices = new SlimDX.Direct3D10.Buffer(device, stream, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = 3 * 32, Usage = ResourceUsage.Default }); stream.Dispose(); device.OutputMerger.SetTargets(renderView); device.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f)); MessagePump.Run(form, () => { device.ClearRenderTargetView(renderView, Color.Black); device.InputAssembler.SetInputLayout(layout); device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 32, 0)); for (int i = 0; i < technique.Description.PassCount; ++i) { pass.Apply(); device.Draw(3, 0); } swapChain.Present(0, PresentFlags.None); }); vertices.Dispose(); layout.Dispose(); effect.Dispose(); renderView.Dispose(); backBuffer.Dispose(); device.Dispose(); swapChain.Dispose(); //foreach (var item in ObjectTable.Objects) // item.Dispose(); }
private void InitScene() { _effect = Effect.FromFile(_dev_manager.Device, @"Shader\MiniTri.fx", "fx_4_0"); EffectTechnique technique = _effect.GetTechniqueByIndex(0); EffectPass pass = technique.GetPassByIndex(0); _input_layout = new InputLayout(_dev_manager.Device, pass.Description.Signature, ColoredVertex.InputElements); //Triangle triangle = new Triangle(new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0) ); Vector3[] quad = { new Vector3(-0.5f, 0.5f, 0), // top left new Vector3(0.5f, 0.5f, 0), // top right new Vector3(-0.5f, -0.5f, 0), // bottom left new Vector3(0.5f, -0.5f, 0), // bottom right }; ColoredVertex[] colored_quad = { new ColoredVertex(quad[0], new Color4(1, 0, 0, 1)), new ColoredVertex(quad[1], new Color4(1, 0, 0, 1)), new ColoredVertex(quad[2], new Color4(1, 0, 0, 1)), new ColoredVertex(quad[3], new Color4(1, 0, 0, 1)), }; // size in bytes (quad: (3x4)x4 + color:(4x4)x4 DataStream quad_stream = new DataStream(ColoredVertex.SizeOf * quad.Length, true, true); quad_stream.WriteRange(colored_quad); quad_stream.Position = 0; _vbuffer = new Buffer(_dev_manager.Device, quad_stream, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Default, SizeInBytes = ColoredVertex.SizeOf * colored_quad.Length }); }
public void Run() { using (var form = EmptyWindow.CreateForm()) { Device device; SwapChain swapChain; RenderTargetView renderTarget; EmptyWindow.CreateDeviceSwapChainAndRenderTarget(form, out device, out swapChain, out renderTarget); Vertex[] vertices = CreateVertices(); var stream = new DataStream(vertices.Length * Marshal.SizeOf(typeof(Vertex)), true, true); foreach (var vertex in vertices) { stream.Write(vertex.Position); stream.Write(vertex.Color); } // Important: when specifying initial buffer data like this, the buffer will // read from the current DataStream position; we must rewind the stream to // the start of the data we just wrote. stream.Position = 0; var bufferDescription = new BufferDescription { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = (3 * Marshal.SizeOf(typeof (Vertex))), Usage = ResourceUsage.Default }; var buffer = new Buffer(device, stream, bufferDescription); stream.Close(); Effect effect; string errors = string.Empty; try { effect = Effect.FromFile(device, "MyShader10.fx", "fx_4_0", ShaderFlags.Debug, EffectFlags.None, null, null, out errors); Console.WriteLine(errors); } catch (Exception) { TestLog.Warnings.WriteLine(errors); throw; } var technique = effect.GetTechniqueByIndex(0); var pass = technique.GetPassByIndex(0); var inputElements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0) }; var inputLayout = new InputLayout(device, inputElements, pass.Description.Signature); Application.Idle += delegate { device.ClearRenderTargetView(renderTarget, new Color4(0, 0, 0)); device.InputAssembler.SetInputLayout(inputLayout); device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList); device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(buffer, Marshal.SizeOf(typeof(Vertex)), 0)); Matrix view = Matrix.LookAtRH(new Vector3(0, 0, 3), new Vector3(), new Vector3(0, 1, 0)); Matrix projection = Matrix.PerspectiveFovRH((float)(System.Math.PI / 3), 800f / 600.0f, 0.01f, 100f); Matrix world = Matrix.Identity; Matrix worldViewProjection = world * view * projection; effect.GetVariableBySemantic("WorldViewProjection") .AsMatrix().SetMatrix(worldViewProjection); for (int actualPass = 0; actualPass < technique.Description.PassCount; ++actualPass) { pass.Apply(); device.Draw(vertices.Length, 0); } swapChain.Present(0, PresentFlags.None); Application.DoEvents(); }; Application.Run(form); } }
void InitScene() { SampleEffect = Effect.FromFile(_dev_manager.Device, "MiniTri.fx", "fx_4_0"); EffectTechnique technique = SampleEffect.GetTechniqueByIndex(0); ; EffectPass pass = technique.GetPassByIndex(0); SampleLayout = new InputLayout(_dev_manager.Device, pass.Description.Signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) }); SampleStream = new DataStream(3 * 32, true, true); SampleStream.WriteRange(new[] { new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f) }); SampleStream.Position = 0; SampleVertices = new Buffer(_dev_manager.Device, SampleStream, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = 3 * 32, Usage = ResourceUsage.Default }); }