public void OnWindowCreated(WindowCreatedMessage msg) { // Add event handlers msg.Form.KeyDown += Form_KeyDown; msg.Form.KeyUp += Form_KeyUp; msg.Form.MouseDown += Form_MouseDown; msg.Form.MouseUp += Form_MouseUp; msg.Form.MouseMove += Form_MouseMove; // TODO: Optimise by caching message objects (reduces mem allocations) }
public void OnInitialise(InitialiseMessage msg) { // Initialise renderer Console.WriteLine("Initialising renderer..."); rendermsg = new RenderMessage(); updatemsg = new UpdateMessage(); // Create the window window = new RenderForm("Castle Renderer - 11030062"); window.Width = 1280; window.Height = 720; // Add form events window.FormClosed += window_FormClosed; // Defaults ClearColour = new Color4(1.0f, 0.0f, 0.0f, 1.0f); // Setup the device var description = new SwapChainDescription() { BufferCount = 1, Usage = Usage.RenderTargetOutput, OutputHandle = window.Handle, IsWindowed = true, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), SampleDescription = new SampleDescription(1, 0), Flags = SwapChainFlags.AllowModeSwitch, SwapEffect = SwapEffect.Discard }; Device tmp; var result = Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out tmp, out swapchain); if (result.IsFailure) { Console.WriteLine("Failed to create Direct3D11 device (" + result.Code.ToString() + ":" + result.Description + ")"); return; } Device = tmp; context = Device.ImmediateContext; using (var factory = swapchain.GetParent<Factory>()) factory.SetWindowAssociation(window.Handle, WindowAssociationFlags.IgnoreAltEnter); // Check AA stuff int q = Device.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, 8); // Setup the viewport viewport = new Viewport(0.0f, 0.0f, window.ClientSize.Width, window.ClientSize.Height); viewport.MinZ = 0.0f; viewport.MaxZ = 1.0f; context.Rasterizer.SetViewports(viewport); // Setup the backbuffer using (var resource = Resource.FromSwapChain<Texture2D>(swapchain, 0)) rtBackbuffer = new RenderTargetView(Device, resource); // Setup depth for backbuffer { Texture2DDescription texdesc = new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, Format = Format.D32_Float, Width = (int)viewport.Width, Height = (int)viewport.Height, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; texDepthBuffer = new Texture2D(Device, texdesc); DepthStencilViewDescription viewdesc = new DepthStencilViewDescription() { ArraySize = 0, Format = Format.D32_Float, Dimension = DepthStencilViewDimension.Texture2D, MipSlice = 0, Flags = 0, FirstArraySlice = 0 }; vwDepthBuffer = new DepthStencilView(Device, texDepthBuffer, viewdesc); } // Setup states #region Depth States // Setup depth states { DepthStencilStateDescription desc = new DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Less }; Depth_Enabled = DepthStencilState.FromDescription(Device, desc); } { DepthStencilStateDescription desc = new DepthStencilStateDescription() { IsDepthEnabled = false, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.Zero, DepthComparison = Comparison.Less }; Depth_Disabled = DepthStencilState.FromDescription(Device, desc); } { DepthStencilStateDescription desc = new DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.Zero, DepthComparison = Comparison.Less }; Depth_ReadOnly = DepthStencilState.FromDescription(Device, desc); } #endregion #region Sampler States Sampler_Clamp = SamplerState.FromDescription(Device, new SamplerDescription() { AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, Filter = Filter.Anisotropic, MinimumLod = 0.0f, MaximumLod = float.MaxValue, MaximumAnisotropy = 16 }); Sampler_Clamp_Point = SamplerState.FromDescription(Device, new SamplerDescription() { AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, Filter = Filter.MinMagMipPoint }); Sampler_Clamp_Linear = SamplerState.FromDescription(Device, new SamplerDescription() { AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, Filter = Filter.MinMagMipLinear }); Sampler_Wrap = SamplerState.FromDescription(Device, new SamplerDescription() { AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, Filter = Filter.Anisotropic, MinimumLod = 0.0f, MaximumLod = float.MaxValue, MaximumAnisotropy = 16 }); #endregion #region Rasterizer States Culling_Backface = RasterizerState.FromDescription(Device, new RasterizerStateDescription() { CullMode = CullMode.Back, DepthBias = 0, DepthBiasClamp = 0.0f, IsDepthClipEnabled = true, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsFrontCounterclockwise = false, IsMultisampleEnabled = true, IsScissorEnabled = false, SlopeScaledDepthBias = 0.0f }); Culling_Frontface = RasterizerState.FromDescription(Device, new RasterizerStateDescription() { CullMode = CullMode.Front, DepthBias = 0, DepthBiasClamp = 0.0f, IsDepthClipEnabled = true, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsFrontCounterclockwise = false, IsMultisampleEnabled = true, IsScissorEnabled = false, SlopeScaledDepthBias = 0.0f }); Culling_None = RasterizerState.FromDescription(Device, new RasterizerStateDescription() { CullMode = CullMode.None, DepthBias = 0, DepthBiasClamp = 0.0f, IsDepthClipEnabled = true, FillMode = FillMode.Solid, IsAntialiasedLineEnabled = false, IsFrontCounterclockwise = false, IsMultisampleEnabled = true, IsScissorEnabled = false, SlopeScaledDepthBias = 0.0f }); #endregion #region Blend States { BlendStateDescription desc = new BlendStateDescription(); desc.RenderTargets[0].BlendEnable = true; desc.RenderTargets[0].BlendOperation = BlendOperation.Add; desc.RenderTargets[0].SourceBlend = BlendOption.One; desc.RenderTargets[0].DestinationBlend = BlendOption.Zero; desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; desc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; Blend_Opaque = BlendState.FromDescription(Device, desc); } { BlendStateDescription desc = new BlendStateDescription(); desc.RenderTargets[0].BlendEnable = true; desc.RenderTargets[0].BlendOperation = BlendOperation.Add; desc.RenderTargets[0].SourceBlend = BlendOption.SourceAlpha; desc.RenderTargets[0].DestinationBlend = BlendOption.InverseSourceAlpha; desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; desc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; Blend_Alpha = BlendState.FromDescription(Device, desc); } { BlendStateDescription desc = new BlendStateDescription(); desc.RenderTargets[0].BlendEnable = true; desc.RenderTargets[0].BlendOperation = BlendOperation.Add; desc.RenderTargets[0].SourceBlend = BlendOption.One; desc.RenderTargets[0].DestinationBlend = BlendOption.One; desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; desc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; Blend_Add = BlendState.FromDescription(Device, desc); } { BlendStateDescription desc = new BlendStateDescription(); desc.RenderTargets[0].BlendEnable = true; desc.RenderTargets[0].BlendOperation = BlendOperation.Add; desc.RenderTargets[0].SourceBlend = BlendOption.DestinationColor; desc.RenderTargets[0].DestinationBlend = BlendOption.Zero; desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; desc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; Blend_Multiply = BlendState.FromDescription(Device, desc); } #endregion // Setup default states Depth = Depth_Enabled; Culling = Culling_Backface; Blend = Blend_Opaque; // Setup other objects shaderresourcemap = new Dictionary<Resource, ShaderResourceView>(); resourceviewslots = new ShaderResourceViewData[MaxPixelShaderResourceViewSlots]; // Send the window created message WindowCreatedMessage windowcreatedmsg = new WindowCreatedMessage(); windowcreatedmsg.Form = window; Owner.MessagePool.SendMessage(windowcreatedmsg); // Show the form window.Show(); }