public void init(IntPtr a_sharedHandler) { if (a_sharedHandler == IntPtr.Zero) { return; } SharedHadler = a_sharedHandler; m_shared_texture = Direct3D11Device.Instance.Device.CreateTexture2D(a_sharedHandler); }
public void init(uint a_Width, uint a_Height) { NativeStructs.D3D11_TEXTURE2D_DESC lTextureDesc = new NativeStructs.D3D11_TEXTURE2D_DESC(); lTextureDesc.Width = a_Width; lTextureDesc.Height = a_Height; lTextureDesc.Format = NativeStructs.DXGI_FORMAT_B8G8R8X8_UNORM; lTextureDesc.ArraySize = 1; lTextureDesc.BindFlags = NativeStructs.D3D11_BIND_RENDER_TARGET | NativeStructs.D3D11_BIND_SHADER_RESOURCE; lTextureDesc.MiscFlags = NativeStructs.D3D11_RESOURCE_MISC_SHARED; lTextureDesc.SampleDesc = new NativeStructs.DXGI_SAMPLE_DESC(); lTextureDesc.SampleDesc.Count = 1; lTextureDesc.SampleDesc.Quality = 0; lTextureDesc.MipLevels = 1; lTextureDesc.CPUAccessFlags = 0; lTextureDesc.Usage = NativeStructs.D3D11_USAGE_DEFAULT; if (m_target_texture != null) { m_target_texture.Dispose(); } m_target_texture = null; m_target_texture = Direct3D11Device.Instance.Device.CreateTexture2D(lTextureDesc); m_resource = m_target_texture.getDXGIResource(); m_shared_handler = m_resource.GetSharedHandle(); }
private void CreateTexture(byte[] data, uint width, uint height, out D3D11Texture2D texture, out D3D11ShaderResourceView textureView) { D3D11Texture2DDesc textureDesc = new D3D11Texture2DDesc(DxgiFormat.R8G8B8A8UNorm, width, height, 1, 1); D3D11SubResourceData[] textureSubResData = new[] { new D3D11SubResourceData(data, width * 4) }; D3D11Texture2D texture2D = this.d3dDevice.CreateTexture2D(textureDesc, textureSubResData); D3D11ShaderResourceView shaderResourceView; try { D3D11ShaderResourceViewDesc textureViewDesc = new D3D11ShaderResourceViewDesc { Format = textureDesc.Format, ViewDimension = D3D11SrvDimension.Texture2D, Texture2D = new D3D11Texture2DSrv { MipLevels = textureDesc.MipLevels, MostDetailedMip = 0 } }; shaderResourceView = this.d3dDevice.CreateShaderResourceView(texture2D, textureViewDesc); } catch { D3D11Utils.DisposeAndNull(ref texture2D); throw; } texture = texture2D; textureView = shaderResourceView; }
public void CreateWindowSizeDependentResources() { uint width = this.deviceResources.BackBufferWidth; uint height = this.deviceResources.BackBufferHeight; // Create fragment count buffer this.fragmentCountBuffer = this.deviceResources.D3DDevice.CreateTexture2D( new D3D11Texture2DDesc( DxgiFormat.R32UInt, width, height, 1, 1, D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource)); // Create prefix sum buffer this.prefixSum = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc( width * height * 4, D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource, D3D11Usage.Default, D3D11CpuAccessOptions.None, D3D11ResourceMiscOptions.None, 4)); // Create the deep frame buffer. // This simple allocation scheme for the deep frame buffer allocates space for 8 times the size of the // frame buffer, which means that it can hold an average of 8 fragments per pixel. This will usually waste some // space, and in some cases of high overdraw the buffer could run into problems with overflow. It // may be useful to make the buffer size more intelligent to avoid these problems. this.deepBuffer = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc( width * height * 8 * 4, D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource, D3D11Usage.Default, D3D11CpuAccessOptions.None, D3D11ResourceMiscOptions.None, 4)); // Create deep frame buffer for color this.deepBufferColor = this.deviceResources.D3DDevice.CreateBuffer(new D3D11BufferDesc( width * height * 8 * 4 * 1, D3D11BindOptions.UnorderedAccess | D3D11BindOptions.ShaderResource, D3D11Usage.Default, D3D11CpuAccessOptions.None, D3D11ResourceMiscOptions.None, 4 * 1)); this.fragmentCountRV = this.deviceResources.D3DDevice.CreateShaderResourceView( this.fragmentCountBuffer, new D3D11ShaderResourceViewDesc( D3D11SrvDimension.Texture2D, DxgiFormat.R32UInt, 0, 1)); this.fragmentCountUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView( this.fragmentCountBuffer, new D3D11UnorderedAccessViewDesc( D3D11UavDimension.Texture2D, DxgiFormat.R32UInt, 0)); this.prefixSumUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView( this.prefixSum, new D3D11UnorderedAccessViewDesc( D3D11UavDimension.Buffer, DxgiFormat.R32UInt, 0, width * height)); this.deepBufferUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView( this.deepBuffer, new D3D11UnorderedAccessViewDesc( D3D11UavDimension.Buffer, DxgiFormat.R32Float, 0, width * height * 8)); this.deepBufferColorUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView( this.deepBufferColor, new D3D11UnorderedAccessViewDesc( D3D11UavDimension.Buffer, DxgiFormat.B8G8R8A8UNorm, 0, width * height * 8)); this.screenTexture = this.deviceResources.D3DDevice.CreateTexture2D( new D3D11Texture2DDesc( DxgiFormat.B8G8R8A8UNorm, width, height, 1, 1, D3D11BindOptions.RenderTarget | D3D11BindOptions.UnorderedAccess)); this.screenTextureRTV = this.deviceResources.D3DDevice.CreateRenderTargetView(this.screenTexture, null); this.screenTextureUAV = this.deviceResources.D3DDevice.CreateUnorderedAccessView(this.screenTexture, new D3D11UnorderedAccessViewDesc(D3D11UavDimension.Texture2D, DxgiFormat.B8G8R8A8UNorm, 0)); }
private void CreateWindowSizeDependentResources() { this.d3dContext.OutputMergerSetRenderTargets(new D3D11RenderTargetView[] { null }, null); D3D11Utils.DisposeAndNull(ref this.backBuffer); D3D11Utils.DisposeAndNull(ref this.offscreenBuffer); D3D11Utils.DisposeAndNull(ref this.d3dRenderTargetView); D3D11Utils.DisposeAndNull(ref this.d3dDepthStencilView); D2D1Utils.DisposeAndNull(ref this.d2dRenderTarget); this.d3dContext.Flush(); var createdBackBuffer = this.OnCreateBackBuffer(); if (createdBackBuffer == null) { return; } this.backBuffer = createdBackBuffer; var backBufferDesc = this.backBuffer.Description; this.backBufferWidth = backBufferDesc.Width; this.backBufferHeight = backBufferDesc.Height; if (this.d3dSampleDesc.Count > 1) { D3D11Texture2DDesc desc = new D3D11Texture2DDesc( DxgiFormat.B8G8R8A8UNorm, this.backBufferWidth, this.backBufferHeight, 1, 1, D3D11BindOptions.RenderTarget, D3D11Usage.Default, D3D11CpuAccessOptions.None, this.d3dSampleDesc.Count, this.d3dSampleDesc.Quality, D3D11ResourceMiscOptions.None); this.offscreenBuffer = this.D3DDevice.CreateTexture2D(desc); } if (this.d3dSampleDesc.Count > 1) { D3D11RenderTargetViewDesc renderTargetViewDesc = new D3D11RenderTargetViewDesc(D3D11RtvDimension.Texture2DMs); this.d3dRenderTargetView = this.d3dDevice.CreateRenderTargetView(this.offscreenBuffer, renderTargetViewDesc); } else { D3D11RenderTargetViewDesc renderTargetViewDesc = new D3D11RenderTargetViewDesc(D3D11RtvDimension.Texture2D); this.d3dRenderTargetView = this.d3dDevice.CreateRenderTargetView(this.backBuffer, renderTargetViewDesc); } D3D11Texture2DDesc depthStencilDesc = new D3D11Texture2DDesc { Width = this.backBufferWidth, Height = this.backBufferHeight, MipLevels = 1, ArraySize = 1, Format = DxgiFormat.D24UNormS8UInt, SampleDesc = this.d3dSampleDesc, Usage = D3D11Usage.Default, BindOptions = D3D11BindOptions.DepthStencil, CpuAccessOptions = D3D11CpuAccessOptions.None, MiscOptions = D3D11ResourceMiscOptions.None }; using (var depthStencil = this.d3dDevice.CreateTexture2D(depthStencilDesc)) { D3D11DepthStencilViewDesc depthStencilViewDesc = new D3D11DepthStencilViewDesc(this.d3dSampleDesc.Count > 1 ? D3D11DsvDimension.Texture2DMs : D3D11DsvDimension.Texture2D); this.d3dDepthStencilView = this.d3dDevice.CreateDepthStencilView(depthStencil, depthStencilViewDesc); } this.screenViewport = new D3D11Viewport { TopLeftX = 0, TopLeftY = 0, Width = this.backBufferWidth, Height = this.backBufferHeight, MinDepth = 0.0f, MaxDepth = 1.0f }; this.d3dContext.RasterizerStageSetViewports(new[] { this.screenViewport }); using (var surface = new DxgiSurface2(this.d3dSampleDesc.Count > 1 ? this.offscreenBuffer.Handle : this.backBuffer.Handle)) { float dpiX; float dpiY; this.d2dFactory.GetDesktopDpi(out dpiX, out dpiY); var properties = new D2D1RenderTargetProperties( D2D1RenderTargetType.Default, new D2D1PixelFormat(DxgiFormat.B8G8R8A8UNorm, D2D1AlphaMode.Premultiplied), dpiX, dpiY, D2D1RenderTargetUsages.None, D2D1FeatureLevel.Default); this.d2dRenderTarget = this.d2dFactory.CreateDxgiSurfaceRenderTarget(surface, properties); } this.d2dRenderTarget.AntialiasMode = D2D1AntialiasMode.PerPrimitive; this.d2dRenderTarget.TextAntialiasMode = D2D1TextAntialiasMode.Grayscale; D3D11RasterizerDesc rasterizerStateDesc = new D3D11RasterizerDesc(D3D11FillMode.Solid, D3D11CullMode.Back, false, 0, 0.0f, 0.0f, true, false, true, false); using (var rasterizerState = this.d3dDevice.CreateRasterizerState(rasterizerStateDesc)) { this.d3dContext.RasterizerStageSetState(rasterizerState); } }
// Use this for initialization void Start() { NewTextureForRender(); if (Application.isPlaying) { if (MicroLightManager.Instance) { switch (MicroLightManager.Instance.mRenderType) { case RendererType.MicroLightRender: { //Create Shared Texture2D LeftRT2D = MicroLightPlugin.D3D11Helper.CreateSharedView(MicroLightManager.Instance.m_UnityDevice, DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, (int)TextureSize.x, (int)TextureSize.y); if (LeftRT2D == IntPtr.Zero) { Debug.Log(this + " LeftRT2D is null"); } HANDLE LeftRTHandleSend = MicroLightPlugin.D3D11Helper.GetSharedHandle(MicroLightManager.Instance.m_UnityDevice, LeftRT2D); if (LeftRTHandleSend == IntPtr.Zero) { Debug.Log(this + " LeftRTHandleSend is null"); } D3D11Resource LeftRTHandleReceive = MicroLightPlugin.D3D11Helper.OpenSharedResource(MicroLightManager.Instance.m_Device, LeftRTHandleSend); if (LeftRTHandleReceive == IntPtr.Zero) { Debug.Log(this + " LeftRTHandleReceive is null"); } RightRT2D = MicroLightPlugin.D3D11Helper.CreateSharedView(MicroLightManager.Instance.m_UnityDevice, DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, (int)TextureSize.x, (int)TextureSize.y); if (RightRT2D == IntPtr.Zero) { Debug.Log(this + " RightRT2D is null"); } HANDLE RightRTHandleSend = MicroLightPlugin.D3D11Helper.GetSharedHandle(MicroLightManager.Instance.m_UnityDevice, RightRT2D); if (RightRTHandleSend == IntPtr.Zero) { Debug.Log(this + " RightRTHandleSend is null"); } D3D11Resource RightRTHandleReceive = MicroLightPlugin.D3D11Helper.OpenSharedResource(MicroLightManager.Instance.m_Device, RightRTHandleSend); if (RightRTHandleSend == IntPtr.Zero) { Debug.Log(this + " RightRTHandleReceive is null"); } leftView = MicroLightPlugin.D3D11Helper.CreateShaderResourceView(MicroLightManager.Instance.m_Device, LeftRTHandleReceive); rightView = MicroLightPlugin.D3D11Helper.CreateShaderResourceView(MicroLightManager.Instance.m_Device, RightRTHandleReceive); if (leftView == IntPtr.Zero) { UnityEngine.Debug.Log("leftView is null"); } if (rightView == IntPtr.Zero) { UnityEngine.Debug.Log("rightView is null"); } switch (MicroLightManager.Instance.mRenderMode) { case RenderMode.LR3D: { MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, ((int)id * 10 + (int)LeftEye.mEyeTye), leftView, rightView, 0.5f, 1.0f, -0.25f, 0); MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, ((int)id * 10 + (int)RightEye.mEyeTye), rightView, leftView, 0.5f, 1.0f, 0.25f, 0); } break; case RenderMode.Stereo: { switch (id) { case TrackingSpaceId.Surface: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 1, 1, 0, 0); break; case TrackingSpaceId.LTypeRoomFront: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, -0.25f, 0); break; case TrackingSpaceId.LTypeRoomFloor: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, 0.25f, 0); break; case TrackingSpaceId.TrapezoidRoomLeft: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.375f, 0); break; case TrackingSpaceId.TrapezoidRoomFront: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.125f, 0); break; case TrackingSpaceId.TrapezoidRoomRight: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.125f, 0); break; #if MicroLightGridView case TrackingSpaceId.TrapezoidRoomFloor: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id + 1, leftView, rightView, 0.25f, 1.0f, 0.375f, 0); break; #else case TrackingSpaceId.TrapezoidRoomFloor: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_RenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.375f, 0); break; #endif } } break; } } break; case RendererType.UnityRender: { leftView = MicroLightPlugin.D3D11Helper.CreateShaderResourceViewFromRenderTexture(MicroLightManager.Instance.m_UnityDevice, LeftRT.GetNativeTexturePtr(), DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM); rightView = MicroLightPlugin.D3D11Helper.CreateShaderResourceViewFromRenderTexture(MicroLightManager.Instance.m_UnityDevice, RightRT.GetNativeTexturePtr(), DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM); if (leftView == IntPtr.Zero) { UnityEngine.Debug.Log("leftView is null"); } if (rightView == IntPtr.Zero) { UnityEngine.Debug.Log("rightView is null"); } switch (MicroLightManager.Instance.mRenderMode) { case RenderMode.LR3D: MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, ((int)id * 10 + (int)LeftEye.mEyeTye), leftView, rightView, 0.5f, 1.0f, -0.25f, 0); MicroLightPlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, ((int)id * 10 + (int)RightEye.mEyeTye), rightView, leftView, 0.5f, 1.0f, 0.25f, 0); break; case RenderMode.Stereo: switch (id) { case TrackingSpaceId.Surface: UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 1, 1, 0, 0); break; case TrackingSpaceId.LTypeRoomFront: UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, -0.25f, 0); break; case TrackingSpaceId.LTypeRoomFloor: UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, 0.25f, 0); break; case TrackingSpaceId.TrapezoidRoomLeft: UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.375f, 0); break; case TrackingSpaceId.TrapezoidRoomFront: UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.125f, 0); break; case TrackingSpaceId.TrapezoidRoomRight: UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.125f, 0); break; #if MicroLightGridView case TrackingSpaceId.TrapezoidRoomFloor: UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id + 1, leftView, rightView, 0.25f, 1.0f, 0.375f, 0); break; #else case TrackingSpaceId.TrapezoidRoomFloor: UnityNativePlugin.Render.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.375f, 0); break; #endif } break; } } break; case RendererType.UnityCoverRender: { leftView = MicroLightPlugin.D3D11Helper.CreateShaderResourceViewFromRenderTexture(MicroLightManager.Instance.m_UnityDevice, LeftRT.GetNativeTexturePtr(), DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM); rightView = MicroLightPlugin.D3D11Helper.CreateShaderResourceViewFromRenderTexture(MicroLightManager.Instance.m_UnityDevice, RightRT.GetNativeTexturePtr(), DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM); if (leftView == IntPtr.Zero) { UnityEngine.Debug.Log("leftView is null"); } if (rightView == IntPtr.Zero) { UnityEngine.Debug.Log("rightView is null"); } switch (MicroLightManager.Instance.mRenderMode) { case RenderMode.LR3D: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_CoverRenderAPI, ((int)id * 10 + (int)LeftEye.mEyeTye), leftView, rightView, 0.5f, 1.0f, -0.25f, 0); UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_CoverRenderAPI, ((int)id * 10 + (int)RightEye.mEyeTye), rightView, leftView, 0.5f, 1.0f, 0.25f, 0); break; case RenderMode.Stereo: switch (id) { case TrackingSpaceId.Surface: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 1, 1, 0, 0); break; case TrackingSpaceId.LTypeRoomFront: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, -0.25f, 0); break; case TrackingSpaceId.LTypeRoomFloor: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.5f, 1.0f, 0.25f, 0); break; case TrackingSpaceId.TrapezoidRoomLeft: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.375f, 0); break; case TrackingSpaceId.TrapezoidRoomFront: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, -0.125f, 0); break; case TrackingSpaceId.TrapezoidRoomRight: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.125f, 0); break; #if MicroLightGridView case TrackingSpaceId.TrapezoidRoomFloor: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id + 1, leftView, rightView, 0.25f, 1.0f, 0.375f, 0); break; #else case TrackingSpaceId.TrapezoidRoomFloor: UnityNativePlugin.CoverRender.AddView(MicroLightManager.Instance.m_UnityRenderAPI, (int)id, leftView, rightView, 0.25f, 1.0f, 0.375f, 0); break; #endif } break; } } break; } } } }
public static extern HANDLE GetSharedHandle(D3D11Device device, D3D11Texture2D texture);
public void LoadTexture(string filename, uint width, uint height, out D3D11Texture2D texture, out D3D11ShaderResourceView textureView) { byte[] textureData = File.ReadAllBytes(filename); this.CreateTexture(textureData, width, height, out texture, out textureView); }
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; }