public IntPtr GetPtr(ID3D11Device device) { if (!m_buffer) { var desc = new D3D11_BUFFER_DESC { ByteWidth = (uint)m_bytes.Length, Usage = D3D11_USAGE._DEFAULT, BindFlags = (uint)m_bind, }; if (MemoryMarshal.TryGetArray(m_bytes, out ArraySegment <byte> bytes)) { using (var pin = PinPtr.Create(bytes)) { var data = new D3D11_SUBRESOURCE_DATA { pSysMem = pin.Ptr }; device.CreateBuffer(ref desc, ref data, out m_buffer).ThrowIfFailed(); } } } return(m_buffer.Ptr); }
public void Draw(ID3D11Device device, ID3D11DeviceContext context, Span <D3D11_INPUT_ELEMENT_DESC> _layout) { if (!m_vertexBuffer) { using (var pin = PinPtr.Create(m_vertices)) { var desc = new D3D11_BUFFER_DESC { ByteWidth = (uint)m_vertices.Length, Usage = D3D11_USAGE._DEFAULT, BindFlags = (uint)D3D11_BIND_FLAG._VERTEX_BUFFER, }; var data = new D3D11_SUBRESOURCE_DATA { pSysMem = pin.Ptr }; device.CreateBuffer(ref desc, ref data, out m_vertexBuffer).ThrowIfFailed(); } } Span <IntPtr> pBufferTbl = stackalloc IntPtr[] { m_vertexBuffer.Ptr }; Span <uint> SizeTbl = stackalloc uint[] { (uint)m_vertexSize }; Span <uint> OffsetTbl = stackalloc uint[] { 0 }; context.IASetVertexBuffers(0, 1, ref MemoryMarshal.GetReference(pBufferTbl), ref MemoryMarshal.GetReference(SizeTbl), ref MemoryMarshal.GetReference(OffsetTbl)); if (!m_indexBuffer) { using (var pin = PinPtr.Create(m_indices)) { var desc = new D3D11_BUFFER_DESC { ByteWidth = (uint)m_indices.Length, Usage = D3D11_USAGE._DEFAULT, BindFlags = (uint)D3D11_BIND_FLAG._INDEX_BUFFER, }; var data = new D3D11_SUBRESOURCE_DATA { pSysMem = pin.Ptr }; device.CreateBuffer(ref desc, ref data, out m_indexBuffer).ThrowIfFailed(); } } context.IASetIndexBuffer(m_indexBuffer, m_indexFormat, 0); context.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY._TRIANGLELIST); context.DrawIndexed((uint)m_indexCount, 0, 0); } } }
public void UpdateVertexAttribute(ID3D11Device device, ID3D11DeviceContext context, Semantics semantic, Memory <byte> bytes) { var buffer = m_vertexBufferMap[semantic]; if (MemoryMarshal.TryGetArray(bytes, out ArraySegment <byte> segment)) { using (var pin = PinPtr.Create(segment)) { var box = new D3D11_BOX { }; context.UpdateSubresource(buffer.Buffer, 0, ref box, pin.Ptr, 0, 0); } } }
static void Compile(string source, string name, string entryPoint, string target, uint flag1, uint flag2, out ID3D10Blob dst) { var source_utf8 = Encoding.UTF8.GetBytes(source); // var name_utf8 = Encoding.UTF8.GetBytes(name + "\0\0"); var entryPoint_utf8 = Encoding.UTF8.GetBytes(entryPoint + "\0\0"); var target_utf8 = Encoding.UTF8.GetBytes(target + "\0\0"); var def = new _D3D_SHADER_MACRO[] { new _D3D_SHADER_MACRO { } }; // using (var error = new ID3D10Blob()) using (var source_pin = PinPtr.Create(source_utf8)) // using (var name_pin = PinPtr.Create(name_utf8)) using (var entryPoint_pin = PinPtr.Create(entryPoint_utf8)) using (var target_pin = PinPtr.Create(target_utf8)) { var hr = d3dcompiler.D3DCompile( source_pin.Ptr, (ulong)source.Length, name, ref def[0], new IntPtr(1), entryPoint, target, flag1, flag2, out dst, out ID3D10Blob error ); if (hr.Failed()) { using (error) { var buffer = new Byte[error.GetBufferSize()]; Marshal.Copy(error.GetBufferPointer(), buffer, 0, buffer.Length); //var encoding = Encoding.GetEncoding(932); var encoding = Encoding.UTF8; var msg = encoding.GetString(buffer); throw new Exception(msg); } } } }
static SemanticsExtensions() { position_pin = PinPtr.Create(position); color_pin = PinPtr.Create(color); }