public D3D11_2D1(Device11 drawdevice, Device10 textdevice) : base(false) // nothing! { if (drawdevice == null || textdevice == null) { using (var dg = new DisposeGroup()) { if (drawdevice == null && textdevice == null) { Adapter a = null; foreach (var item in DeviceUtil.GetAdapters(dg)) { if (!item.IsInterfaceSupported<Device10>()) continue; if (Device11.GetSupportedFeatureLevel(item) < Direct3D.FeatureLevel.Level_10_1) continue; a = item; break; } device = new Device11(a, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.SingleThreaded | DeviceCreationFlags.Debug); device10 = new Device10(a, Direct3D10.DeviceCreationFlags.BgraSupport | Direct3D10.DeviceCreationFlags.Singlethreaded | Direct3D10.DeviceCreationFlags.Debug); } else { if (drawdevice == null) { using (var xgidtext = textdevice.QueryInterface<DeviceXGI>()) device = new Device11(xgidtext.Adapter, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.SingleThreaded | DeviceCreationFlags.Debug); textdevice.AddReference(); device10 = textdevice; } else { using (var xgiddraw = drawdevice.QueryInterface<DeviceXGI>()) device10 = new Device10(xgiddraw.Adapter, Direct3D10.DeviceCreationFlags.BgraSupport | Direct3D10.DeviceCreationFlags.Singlethreaded | Direct3D10.DeviceCreationFlags.Debug); drawdevice.AddReference(); device = drawdevice; } } } } else { using (var xgidev10 = device10.QueryInterface<DeviceXGI>()) using (var xgidev11 = device.QueryInterface<DeviceXGI>()) { if (xgidev10.Adapter.NativePointer != xgidev11.Adapter.NativePointer) throw new ArgumentException("drawdevice.Adapter.NativePointer != textdevice.Adapter.NativePointer"); } textdevice.AddReference(); drawdevice.AddReference(); device = drawdevice; device10 = textdevice; } factory2D = new SharpDX.Direct2D1.Factory(); factoryDW = new FactoryDW(); }
private void RenderM2(string ModelPath) { using (var dg = new DisposeGroup()) { string _BaseDir = ConfigurationManager.AppSettings["basedir"]; //Load Shaders var pVSBlob = dg.Add(ShaderBytecode.CompileFromFile("RenderWithCam.fx", "VS", "vs_4_0")); var inputSignature = dg.Add(ShaderSignature.GetInputSignature(pVSBlob)); m_pVertexShader = new VertexShader(Device, pVSBlob); var pPSBlob = dg.Add(ShaderBytecode.CompileFromFile("RenderWithCam.fx", "PS", "ps_4_0")); m_pPixelShader = new PixelShader(Device, pPSBlob); //Define layout var layout = dg.Add(new InputLayout(Device, inputSignature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 16, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 28, 0) })); M2Loader model = new M2Loader(_BaseDir, ModelPath, Device); model.LoadM2(); WoWM2 m2 = model.m2; //Set count for use in draw later on indicecount = m2.indices.Count(); if (m2.vertices.Count() == 0) { modelLoaded = false; return; } //Create buffers var vertexBuffer = dg.Add(Buffer.Create(Device, BindFlags.VertexBuffer, m2.vertices)); var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, Utilities.SizeOf <Vector4>() + Utilities.SizeOf <Vector3>() + Utilities.SizeOf <Vector2>(), 0); var indexBuffer = dg.Add(Buffer.Create(Device, BindFlags.IndexBuffer, m2.indices)); Device.ImmediateContext.InputAssembler.InputLayout = (layout); Device.ImmediateContext.InputAssembler.SetVertexBuffers(0, vertexBufferBinding); Device.ImmediateContext.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0); Device.ImmediateContext.InputAssembler.PrimitiveTopology = (PrimitiveTopology.TriangleList); M2renderBatches = m2.renderBatches; m2materials = m2.materials; Set(ref m_pConstantBuffer, new ConstantBuffer <Projections>(Device)); Device.ImmediateContext.VertexShader.SetConstantBuffer(0, m_pConstantBuffer.Buffer); } //Make camera Camera = new WoWModelViewerCamera(); Camera.SetProjParams((float)Math.PI / 2, 1, 0.01f, 50.0f); Camera.SetViewParams(new Vector3(10.0f, 10.0f, -10.0f), new Vector3(0.0f, 1.0f, 0.0f)); Camera.Roll(-4f); }
public ShaderManager(Device device, ShaderBytecode vertexShaderByteCode, ShaderBytecode pixelShaderByteCode, ShaderBytecode geometryShaderByteCode) { disposer = new DisposeGroup(); this.vertexShaderByteCode = disposer.Add(vertexShaderByteCode); this.pixelShaderByteCode = disposer.Add(pixelShaderByteCode); this.geometryShaderByteCode = disposer.Add(geometryShaderByteCode); vertexShader = disposer.Add(new VertexShader(device, vertexShaderByteCode)); pixelShader = disposer.Add(new PixelShader(device, pixelShaderByteCode)); geometryShader = disposer.Add(new GeometryShader(device, geometryShaderByteCode)); }
/// <summary> /// /// </summary> /// <param name="dg"></param> /// <returns></returns> public static IEnumerable<Adapter> GetAdapters(DisposeGroup dg) { // NOTE: SharpDX 1.3 requires explicit Dispose() of everything // hence the DisposeGroup, to enforce it using (var f = new Factory()) { int n = AdapterCount; for (int i = 0; i < n; i++) yield return dg.Add(f.GetAdapter(i)); } }
public static Adapter GetCompatibleAdapter(DisposeGroup dg) { foreach (var item in DeviceUtil.GetAdapters(dg)) { if (Device11.GetSupportedFeatureLevel(item) < Direct3D.FeatureLevel.Level_10_1) continue; if (!item.IsInterfaceSupported<Device10>()) continue; return item; } return null; }
/// <summary> /// /// </summary> /// <param name="cFlags"></param> /// <param name="minLevel"></param> /// <returns></returns> public static SharpDX.Direct3D11.Device Create11( Direct3D11.DeviceCreationFlags cFlags = Direct3D11.DeviceCreationFlags.None, Direct3D.FeatureLevel minLevel = Direct3D.FeatureLevel.Level_9_1) { using (var dg = new DisposeGroup()) { var ada = GetBestAdapter(dg); if (ada == null) return null; var level = Direct3D11.Device.GetSupportedFeatureLevel(ada); if (level < minLevel) return null; return new Direct3D11.Device(ada, cFlags, level); } }
public Render() { using (var dg = new DisposeGroup()) { //Load Shaders var pVSBlob = dg.Add(ShaderBytecode.CompileFromFile("RenderWithCam.fx", "VS", "vs_4_0")); var inputSignature = dg.Add(ShaderSignature.GetInputSignature(pVSBlob)); m_pVertexShader = new VertexShader(Device, pVSBlob); var pPSBlob = dg.Add(ShaderBytecode.CompileFromFile("RenderWithCam.fx", "PS", "ps_4_0")); m_pPixelShader = new PixelShader(Device, pPSBlob); //Define layout var layout = dg.Add(new InputLayout(Device, inputSignature, new[]{ new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0) })); //Load model M2Reader reader = new M2Reader("Z:\\18566_full\\"); reader.LoadM2(@"World\ArtTest\Boxtest\xyz.m2"); //Load vertices List<float> verticelist = new List<float>(); for (int i = 0; i < reader.model.vertices.Count(); i++) { verticelist.Add(reader.model.vertices[i].position.X); verticelist.Add(reader.model.vertices[i].position.Z * -1); verticelist.Add(reader.model.vertices[i].position.Y); verticelist.Add(1.0f); verticelist.Add(reader.model.vertices[i].textureCoordX); verticelist.Add(reader.model.vertices[i].textureCoordY); } //Load indices List<ushort> indicelist = new List<ushort>(); for (int i = 0; i < reader.model.skins[0].triangles.Count(); i++) { indicelist.Add(reader.model.skins[0].triangles[i].pt1); indicelist.Add(reader.model.skins[0].triangles[i].pt2); indicelist.Add(reader.model.skins[0].triangles[i].pt3); } //Convert to array ushort[] indices = indicelist.ToArray(); float[] vertices = verticelist.ToArray(); //Set count for use in draw later on indicecount = indices.Count(); //Create buffers var vertexBuffer = dg.Add(Buffer.Create(Device, BindFlags.VertexBuffer, vertices)); var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, Utilities.SizeOf<Vector4>() + Utilities.SizeOf<Vector2>(), 0); var indexBuffer = dg.Add(Buffer.Create(Device, BindFlags.IndexBuffer, indices)); Device.ImmediateContext.InputAssembler.InputLayout = (layout); Device.ImmediateContext.InputAssembler.SetVertexBuffers(0, vertexBufferBinding); Device.ImmediateContext.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0); Device.ImmediateContext.InputAssembler.PrimitiveTopology = (PrimitiveTopology.TriangleList); //Get texture, what a mess this could be much better var blp = new BLPReader("Z:\\18566_full\\"); if (File.Exists(Path.Combine("Z:\\18566_full\\", reader.model.filename.Replace("M2", "blp")))) { blp.LoadBLP(reader.model.filename.Replace("M2", "blp")); } else { if (reader.model.textures.Count() > 0) { blp.LoadBLP(reader.model.textures[0]); } else { throw new Exception("No forking textures, mate."); } } MemoryStream s = new MemoryStream(); blp.bmp.Save(s, System.Drawing.Imaging.ImageFormat.Png); s.Seek(0, SeekOrigin.Begin); Texture2D texture = Texture2D.FromMemory<Texture2D>(Device, s.ToArray()); var textureView = new ShaderResourceView(Device, texture); var sampler = new SamplerState(Device, new SamplerStateDescription() { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, BorderColor = SharpDX.Color.Black, ComparisonFunction = Comparison.Never, MaximumAnisotropy = 16, MipLodBias = 0, MinimumLod = 0, MaximumLod = 16, }); Device.ImmediateContext.PixelShader.SetSampler(0, sampler); Device.ImmediateContext.PixelShader.SetShaderResource(0, textureView); //End of texture stuff, Set(ref m_pConstantBuffer, new ConstantBuffer<Projections>(Device)); Device.ImmediateContext.VertexShader.SetConstantBuffer(0, m_pConstantBuffer.Buffer); } //Make camera Camera = new FirstPersonCamera(); Camera.SetProjParams((float)Math.PI / 2, 1, 0.01f, 100.0f); Camera.SetViewParams(new Vector3(0.0f, 0.0f, -5.0f), new Vector3(0.0f, 1.0f, 0.0f)); }
void Attach() { disposer = new DisposeGroup(); var device = Renderer.Device; string noteShaderData; var assembly = Assembly.GetExecutingAssembly(); using (var stream = assembly.GetManifestResourceStream("Iris.Previews.DX11.notes.fx")) using (var reader = new IO.StreamReader(stream)) noteShaderData = reader.ReadToEnd(); notesShader = disposer.Add(new ShaderManager( device, ShaderBytecode.Compile(noteShaderData, "VS_Note", "vs_4_0", ShaderFlags.None, EffectFlags.None), ShaderBytecode.Compile(noteShaderData, "PS", "ps_4_0", ShaderFlags.None, EffectFlags.None), ShaderBytecode.Compile(noteShaderData, "GS_Note", "gs_4_0", ShaderFlags.None, EffectFlags.None) )); noteLayout = disposer.Add(new InputLayout(device, ShaderSignature.GetInputSignature(notesShader.vertexShaderByteCode), new[] { new InputElement("START", 0, Format.R32_Float, 0, 0), new InputElement("END", 0, Format.R32_Float, 4, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 8, 0), })); noteConstants = new NotesGlobalConstants() { NoteBorder = 0.002f, NoteLeft = -0.2f, NoteRight = 0.0f, ScreenAspect = 1f }; noteBuffer = new Buffer(device, new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = 40 * noteBufferLength, Usage = ResourceUsage.Dynamic, StructureByteStride = 0 }); globalNoteConstants = new Buffer(device, new BufferDescription() { BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, SizeInBytes = 32, Usage = ResourceUsage.Dynamic, StructureByteStride = 0 }); var renderTargetDesc = new RenderTargetBlendDescription(); renderTargetDesc.IsBlendEnabled = true; renderTargetDesc.SourceBlend = BlendOption.SourceAlpha; renderTargetDesc.DestinationBlend = BlendOption.InverseSourceAlpha; renderTargetDesc.BlendOperation = BlendOperation.Add; renderTargetDesc.SourceAlphaBlend = BlendOption.One; renderTargetDesc.DestinationAlphaBlend = BlendOption.One; renderTargetDesc.AlphaBlendOperation = BlendOperation.Add; renderTargetDesc.RenderTargetWriteMask = ColorWriteMaskFlags.All; BlendStateDescription desc = new BlendStateDescription(); desc.AlphaToCoverageEnable = false; desc.IndependentBlendEnable = false; desc.RenderTarget[0] = renderTargetDesc; var blendStateEnabled = new BlendState(device, desc); device.ImmediateContext.OutputMerger.SetBlendState(blendStateEnabled); RasterizerStateDescription renderStateDesc = new RasterizerStateDescription { CullMode = CullMode.None, DepthBias = 0, DepthBiasClamp = 0, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsDepthClipEnabled = false, IsFrontCounterClockwise = false, IsMultisampleEnabled = true, IsScissorEnabled = false, SlopeScaledDepthBias = 0 }; var rasterStateSolid = new RasterizerState(device, renderStateDesc); device.ImmediateContext.Rasterizer.State = rasterStateSolid; }
private void RenderWMO(string ModelPath) { using (var dg = new DisposeGroup()) { string _BaseDir = ConfigurationManager.AppSettings["basedir"]; //Load Shaders var pVSBlob = dg.Add(ShaderBytecode.CompileFromFile("RenderWithCam.fx", "VS", "vs_4_0")); var inputSignature = dg.Add(ShaderSignature.GetInputSignature(pVSBlob)); m_pVertexShader = new VertexShader(Device, pVSBlob); var pPSBlob = dg.Add(ShaderBytecode.CompileFromFile("RenderWithCam.fx", "PS", "ps_4_0")); m_pPixelShader = new PixelShader(Device, pPSBlob); //Define layout var layout = dg.Add(new InputLayout(Device, inputSignature, new[]{ new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 16, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 28, 0) })); WMOLoader model = new WMOLoader(_BaseDir, ModelPath, Device); model.LoadWMO(); WoWWMO wmo = model.wmo; indicecount = wmo.groups[0].indices.Count(); var vertexBuffer = dg.Add(Buffer.Create(Device, BindFlags.VertexBuffer, wmo.groups[0].vertices)); var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, Utilities.SizeOf<Vector4>() + Utilities.SizeOf<Vector3>() + Utilities.SizeOf<Vector2>(), 0); var indexBuffer = dg.Add(Buffer.Create(Device, BindFlags.IndexBuffer, wmo.groups[0].indices)); Device.ImmediateContext.InputAssembler.InputLayout = (layout); Device.ImmediateContext.InputAssembler.SetVertexBuffers(0, vertexBufferBinding); Device.ImmediateContext.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0); Device.ImmediateContext.InputAssembler.PrimitiveTopology = (PrimitiveTopology.TriangleList); WMOrenderBatches = wmo.groups[0].renderBatches; materials = wmo.materials; Set(ref m_pConstantBuffer, new ConstantBuffer<Projections>(Device)); Device.ImmediateContext.VertexShader.SetConstantBuffer(0, m_pConstantBuffer.Buffer); } //Make camera Camera = new WoWModelViewerCamera(); Camera.SetProjParams((float)Math.PI / 2, 1, 0.01f, 4000.0f); Camera.SetViewParams(new Vector3(10.0f, 10.0f, -10.0f), new Vector3(0.0f, 1.0f, 0.0f)); Camera.Roll(-4f); }
/// <summary> /// /// </summary> /// <param name="dg"></param> /// <returns></returns> public static Adapter GetBestAdapter(DisposeGroup dg) { Direct3D.FeatureLevel high = Direct3D.FeatureLevel.Level_9_1; Adapter ada = null; foreach (var item in GetAdapters(dg)) { var level = Direct3D11.Device.GetSupportedFeatureLevel(item); if (ada == null || level > high) { ada = item; high = level; } } return ada; }
public Render() { using (var dg = new DisposeGroup()) { //Load Shaders var pVSBlob = dg.Add(ShaderBytecode.CompileFromFile("RenderWithCam.fx", "VS", "vs_4_0")); var inputSignature = dg.Add(ShaderSignature.GetInputSignature(pVSBlob)); m_pVertexShader = new VertexShader(Device, pVSBlob); var pPSBlob = dg.Add(ShaderBytecode.CompileFromFile("RenderWithCam.fx", "PS", "ps_4_0")); m_pPixelShader = new PixelShader(Device, pPSBlob); //Define layout var layout = dg.Add(new InputLayout(Device, inputSignature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0) })); //Load model M2Reader reader = new M2Reader("Z:\\18566_full\\"); reader.LoadM2(@"World\ArtTest\Boxtest\xyz.m2"); //Load vertices List <float> verticelist = new List <float>(); for (int i = 0; i < reader.model.vertices.Count(); i++) { verticelist.Add(reader.model.vertices[i].position.X); verticelist.Add(reader.model.vertices[i].position.Z * -1); verticelist.Add(reader.model.vertices[i].position.Y); verticelist.Add(1.0f); verticelist.Add(reader.model.vertices[i].textureCoordX); verticelist.Add(reader.model.vertices[i].textureCoordY); } //Load indices List <ushort> indicelist = new List <ushort>(); for (int i = 0; i < reader.model.skins[0].triangles.Count(); i++) { indicelist.Add(reader.model.skins[0].triangles[i].pt1); indicelist.Add(reader.model.skins[0].triangles[i].pt2); indicelist.Add(reader.model.skins[0].triangles[i].pt3); } //Convert to array ushort[] indices = indicelist.ToArray(); float[] vertices = verticelist.ToArray(); //Set count for use in draw later on indicecount = indices.Count(); //Create buffers var vertexBuffer = dg.Add(Buffer.Create(Device, BindFlags.VertexBuffer, vertices)); var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, Utilities.SizeOf <Vector4>() + Utilities.SizeOf <Vector2>(), 0); var indexBuffer = dg.Add(Buffer.Create(Device, BindFlags.IndexBuffer, indices)); Device.ImmediateContext.InputAssembler.InputLayout = (layout); Device.ImmediateContext.InputAssembler.SetVertexBuffers(0, vertexBufferBinding); Device.ImmediateContext.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0); Device.ImmediateContext.InputAssembler.PrimitiveTopology = (PrimitiveTopology.TriangleList); //Get texture, what a mess this could be much better var blp = new BLPReader("Z:\\18566_full\\"); if (File.Exists(Path.Combine("Z:\\18566_full\\", reader.model.filename.Replace("M2", "blp")))) { blp.LoadBLP(reader.model.filename.Replace("M2", "blp")); } else { if (reader.model.textures.Count() > 0) { blp.LoadBLP(reader.model.textures[0]); } else { throw new Exception("No forking textures, mate."); } } MemoryStream s = new MemoryStream(); blp.bmp.Save(s, System.Drawing.Imaging.ImageFormat.Png); s.Seek(0, SeekOrigin.Begin); Texture2D texture = Texture2D.FromMemory <Texture2D>(Device, s.ToArray()); var textureView = new ShaderResourceView(Device, texture); var sampler = new SamplerState(Device, new SamplerStateDescription() { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, BorderColor = SharpDX.Color.Black, ComparisonFunction = Comparison.Never, MaximumAnisotropy = 16, MipLodBias = 0, MinimumLod = 0, MaximumLod = 16, }); Device.ImmediateContext.PixelShader.SetSampler(0, sampler); Device.ImmediateContext.PixelShader.SetShaderResource(0, textureView); //End of texture stuff, Set(ref m_pConstantBuffer, new ConstantBuffer <Projections>(Device)); Device.ImmediateContext.VertexShader.SetConstantBuffer(0, m_pConstantBuffer.Buffer); } //Make camera Camera = new FirstPersonCamera(); Camera.SetProjParams((float)Math.PI / 2, 1, 0.01f, 100.0f); Camera.SetViewParams(new Vector3(0.0f, 0.0f, -5.0f), new Vector3(0.0f, 1.0f, 0.0f)); }