public Resource Load(ResourceInformation info) { if (info.GetBool("IsCubemap")) { return(LoadCubemap(info)); } SharpDX.Toolkit.Graphics.Texture2D texture = SharpDX.Toolkit.Graphics.Texture2D.Load(device, info.Filepath); SharpDX.Direct3D11.ShaderResourceView textureView; bool isLinearData = info.GetBool("IsLinear"); if (isLinearData) { textureView = new SharpDX.Direct3D11.ShaderResourceView(device, texture); } else { SharpDX.Direct3D11.ImageLoadInformation imageInfo = new SharpDX.Direct3D11.ImageLoadInformation(); imageInfo.BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource; imageInfo.Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm_SRgb; imageInfo.Filter = SharpDX.Direct3D11.FilterFlags.SRgb | SharpDX.Direct3D11.FilterFlags.None; SharpDX.Direct3D11.Resource sRGBTexture = SharpDX.Direct3D11.Texture2D.FromFile(device, info.Filepath, imageInfo); textureView = new SharpDX.Direct3D11.ShaderResourceView(device, sRGBTexture); } Texture fearTexture = new Texture(texture, textureView); return(fearTexture); }
protected override void Dispose(bool disposing) { if (!IsDisposed) { #if DIRECTX if (disposing) { if (_resourceView != null) { _resourceView.Dispose(); _resourceView = null; } if (_texture != null) { _texture.Dispose(); _texture = null; } } #elif OPENGL GraphicsDevice.AddDisposeAction(() => { GL.DeleteTextures(1, ref glTexture); GraphicsExtensions.CheckGLError(); glTexture = -1; }); glLastSamplerState = null; #endif } base.Dispose(disposing); }
internal SharpDX.Direct3D11.ShaderResourceView GetShaderResourceView() { if (_resourceView == null) _resourceView = new SharpDX.Direct3D11.ShaderResourceView(GraphicsDevice._d3dDevice, GetTexture()); return _resourceView; }
private Resource LoadCubemap(ResourceInformation cubeInfo) { SharpDX.Toolkit.Graphics.TextureCube cube = SharpDX.Toolkit.Graphics.TextureCube.Load(device, cubeInfo.Filepath); SharpDX.Direct3D11.ShaderResourceView cubeView = new SharpDX.Direct3D11.ShaderResourceView(device, cube); return(new TextureCube(cube, cubeView)); }
internal SharpDX.Direct3D11.ShaderResourceView GetShaderResourceView() { if (_resourceView == null) { _resourceView = CreateShaderResourceView(); } return(_resourceView); }
public static SharpDX.Direct3D11.ShaderResourceView FromBitmapFile(Common.DeviceResources deviceResources, string bitmapFile) { using (var bitmap = TextureLoader.LoadBitmap(deviceResources.WicImagingFactory, bitmapFile)) using (var texture2D = TextureLoader.CreateTexture2DFromBitmap(deviceResources.D3DDevice, bitmap)) { SharpDX.Direct3D11.ShaderResourceView textureView = new SharpDX.Direct3D11.ShaderResourceView(deviceResources.D3DDevice, texture2D); return(textureView); } }
internal SharpDX.Direct3D11.ShaderResourceView GetShaderResourceView() { if (_resourceView == null) { _resourceView = new SharpDX.Direct3D11.ShaderResourceView(GraphicsDevice._d3dDevice, _texture); } return(_resourceView); }
public void Render() { if (!loadingFinished || !physicalCamera.Ready) { return; } var device = deviceResources.D3DDevice; var context = deviceResources.D3DDeviceContext; int stride = SharpDX.Utilities.SizeOf <VertexPositionUV>(); int offset = 0; var bufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(vertexBuffer, stride, offset); context.InputAssembler.SetVertexBuffers(0, bufferBinding); context.InputAssembler.SetIndexBuffer(indexBuffer, SharpDX.DXGI.Format.R16_UInt, 0); context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; context.InputAssembler.InputLayout = inputLayout; context.VertexShader.SetShader(vertexShader, null, 0); context.VertexShader.SetConstantBuffers(0, modelConstantBuffer); context.PixelShader.SetShader(pixelShader, null, 0); var cameraTexture = physicalCamera.AcquireTexture(); if (cameraTexture == null) { return; } var luminanceView = new SharpDX.Direct3D11.ShaderResourceView(device, cameraTexture, new SharpDX.Direct3D11.ShaderResourceViewDescription() { Format = SharpDX.DXGI.Format.R8_UInt, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource() { MipLevels = 1 } }); var chrominanceView = new SharpDX.Direct3D11.ShaderResourceView(device, cameraTexture, new SharpDX.Direct3D11.ShaderResourceViewDescription() { Format = SharpDX.DXGI.Format.R8G8_UInt, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource() { MipLevels = 1 } }); context.PixelShader.SetShaderResource(0, luminanceView); context.PixelShader.SetShaderResource(1, chrominanceView); context.DrawIndexedInstanced(6, 2, 0, 0, 0); luminanceView.Dispose(); chrominanceView.Dispose(); physicalCamera.ReleaseTexture(); }
/// <summary> /// Creates device-based resources to store a constant buffer, cube /// geometry, and vertex and pixel shaders. In some cases this will also /// store a geometry shader. /// </summary> public void CreateDeviceDependentResourcesAsync() { ReleaseDeviceDependentResources(); // Create a constant buffer to store the model matrix. modelConstantBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.ConstantBuffer, ref modelConstantBufferData)); // Create the Texture, ShaderResource and Sampler state lock (thisLock) { m_texture = this.ToDispose(new SharpDX.Direct3D11.Texture2D(deviceResources.D3DDevice, new SharpDX.Direct3D11.Texture2DDescription() { Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, Width = (int)m_mediaPlayer.PlaybackSession.NaturalVideoWidth, //Width Height = (int)m_mediaPlayer.PlaybackSession.NaturalVideoHeight, //Height MipLevels = 1, ArraySize = 1, BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget, Usage = SharpDX.Direct3D11.ResourceUsage.Default, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None })); m_textureView = this.ToDispose(new SharpDX.Direct3D11.ShaderResourceView(deviceResources.D3DDevice, m_texture)); } m_quadTextureSamplerState = this.ToDispose(new SharpDX.Direct3D11.SamplerState(deviceResources.D3DDevice, new SharpDX.Direct3D11.SamplerStateDescription() { Filter = SharpDX.Direct3D11.Filter.Anisotropic, AddressU = SharpDX.Direct3D11.TextureAddressMode.Clamp, AddressV = SharpDX.Direct3D11.TextureAddressMode.Clamp, AddressW = SharpDX.Direct3D11.TextureAddressMode.Clamp, MaximumAnisotropy = 3, MinimumLod = 0, MaximumLod = 3, MipLodBias = 0, BorderColor = new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 0), ComparisonFunction = SharpDX.Direct3D11.Comparison.Never })); CreateD3D11Surface(); LoadShaders(); }
/// <summary> /// Draws a quad with a texture. This Draw method is using a simple pixel shader that is sampling the texture. /// </summary> /// <param name="texture">The texture to draw.</param> /// <param name="samplerState">State of the sampler. If null, default sampler is <see cref="SamplerStateCollection.LinearClamp" />.</param> /// <param name="fullScreenTriangle">if set to <c>true</c> to draw an optimized full screen triangle as a full screen quad.</param> public void Draw(SharpDX.Direct3D11.ShaderResourceView texture, SharpDX.Direct3D11.SamplerState samplerState = null, bool fullScreenTriangle = false) { GraphicsDevice.SetVertexBuffer(fullScreenTriangle ? sharedData.VertexBufferFullQuad : sharedData.VertexBuffer); GraphicsDevice.SetVertexInputLayout(sharedData.VertexInputLayout); ResetShaderStages(); // Make sure that we are using our vertex shader textureParameter.SetResource(texture); textureSamplerParameter.SetResource(samplerState ?? GraphicsDevice.SamplerStates.LinearClamp); textureCopyPass.Apply(); GraphicsDevice.Draw(PrimitiveType.TriangleStrip, fullScreenTriangle ? 3 : 4); // Reset the vertex buffer GraphicsDevice.SetVertexBuffer(0, null, 0); GraphicsDevice.InputAssemblerStage.InputLayout = null; // NOTE SmartK8: Verify functionality GraphicsDevice.Context.PixelShader.SetShaderResource(0, null); }
/// <summary> /// Draws a quad with a texture. This Draw method is using a simple pixel shader that is sampling the texture. /// </summary> /// <param name="texture">The texture to draw.</param> /// <param name="samplerState">State of the sampler. If null, default sampler is <see cref="SamplerStateCollection.LinearClamp" />.</param> public void Draw(SharpDX.Direct3D11.ShaderResourceView texture, SharpDX.Direct3D11.SamplerState samplerState = null) { GraphicsDevice.SetVertexBuffer(sharedData.VertexBuffer); GraphicsDevice.SetVertexInputLayout(sharedData.VertexInputLayout); ResetShaderStages(); // Make sure that we are using our vertex shader textureParameter.SetResource(texture); textureSamplerParameter.SetResource(samplerState ?? GraphicsDevice.SamplerStates.LinearClamp); textureCopyPass.Apply(); GraphicsDevice.Draw(PrimitiveType.TriangleStrip, 4); // Reset the vertex buffer GraphicsDevice.SetVertexBuffer(0, null, 0); GraphicsDevice.InputAssemblerStage.SetInputLayout(null); GraphicsDevice.Context.PixelShader.SetShaderResource(0, null); }
private static void Render(SharpDX.Direct3D11.ShaderResourceView textureArraySRV, MyBindableResource depthRead) { RC.SetVS(m_vs); if (MyRender11.DebugOverrides.OIT) { RC.SetPS(m_psOIT); } else { RC.SetPS(m_ps); } RC.SetVB(0, null, 0); RC.SetIB(m_ib.Buffer, SharpDX.DXGI.Format.R32_UInt); RC.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; RC.SetCB(1, m_activeListConstantBuffer); RC.BindSRV(0, depthRead); RC.DeviceContext.PixelShader.SetShaderResources(1, textureArraySRV); RC.VSBindRawSRV(0, m_particleBuffer); RC.VSBindRawSRV(1, m_aliveIndexBuffer); RC.VSBindRawSRV(2, m_emitterStructuredBuffer); RC.DeviceContext.VertexShader.SetShaderResource(MyCommon.SKYBOX_IBL_SLOT, MyRender11.IsIntelBrokenCubemapsWorkaround ? Resources.MyTextures.GetView(Resources.MyTextures.IntelFallbackCubeTexId) : MyEnvironmentProbe.Instance.cubemapPrefiltered.SRV); RC.DeviceContext.VertexShader.SetShaderResource(MyCommon.SKYBOX2_IBL_SLOT, Resources.MyTextures.GetView(Resources.MyTextures.GetTexture(MyRender11.Environment.NightSkyboxPrefiltered, Resources.MyTextureEnum.CUBEMAP, true))); // bind render target? if (!MyStereoRender.Enable) { RC.DeviceContext.DrawIndexedInstancedIndirect(m_indirectDrawArgsBuffer.Buffer, 0); } else { MyStereoRender.DrawIndexedInstancedIndirectGPUParticles(RC, m_indirectDrawArgsBuffer.Buffer, 0); } MyRender11.ProcessDebugOutput(); RC.DeviceContext.PixelShader.SetShaderResource(MyCommon.SKYBOX_IBL_SLOT, null); RC.DeviceContext.PixelShader.SetShaderResource(MyCommon.SKYBOX2_IBL_SLOT, null); }
public override void Dispose() { #if DIRECTX if (_resourceView != null) { _resourceView.Dispose(); _resourceView = null; } if (_texture != null) { _texture.Dispose(); _texture = null; } #elif OPENGL GL.DeleteTextures(1, ref glTexture); GraphicsExtensions.CheckGLError(); #endif base.Dispose(); }
public RenderBuffer( IGraphicsDevice graphicsDevice, int width, int height ) : base(graphicsDevice, width, height, 1, true) { targetView = new SharpDX.Direct3D11.RenderTargetView ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, Handle as SharpDX.Direct3D11.Texture2D, new SharpDX.Direct3D11.RenderTargetViewDescription () { Dimension = SharpDX.Direct3D11.RenderTargetViewDimension.Texture2D, Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, Texture2D = new SharpDX.Direct3D11.RenderTargetViewDescription.Texture2DResource () { MipSlice = 0 } } ); depthStencilBuffer = new SharpDX.Direct3D11.Texture2D ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, new SharpDX.Direct3D11.Texture2DDescription () { ArraySize = 1, Width = width, Height = height, Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, MipLevels = 0, BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil, } ); depthStencil = new SharpDX.Direct3D11.DepthStencilView ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, depthStencilBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription () { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D, Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt, Texture2D = new SharpDX.Direct3D11.DepthStencilViewDescription.Texture2DResource () { MipSlice = 0 } } ); shaderView = new SharpDX.Direct3D11.ShaderResourceView ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, Handle as SharpDX.Direct3D11.Texture2D, new SharpDX.Direct3D11.ShaderResourceViewDescription () { Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource () { MipLevels = 1, MostDetailedMip = 0 } } ); }
public void OnResize(int width, int height) { if (mRealTexture != null) mRealTexture.Dispose(); if (mTmpTexture != null) mTmpTexture.Dispose(); mRealTexture = new SharpDX.Direct3D11.Texture2D(mDevice.Device, new SharpDX.Direct3D11.Texture2DDescription { ArraySize = 1, BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, Format = Format.B8G8R8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.SharedKeyedmutex, SampleDescription = new SampleDescription(1, 0), Usage = SharpDX.Direct3D11.ResourceUsage.Default }); using (var resource = mRealTexture.QueryInterface<SharpDX.DXGI.Resource>()) mTmpTexture = D2DDevice.OpenSharedResource<Texture2D>(resource.SharedHandle); if (NativeView != null) NativeView.Dispose(); NativeView = new SharpDX.Direct3D11.ShaderResourceView(mDevice.Device, mRealTexture, new SharpDX.Direct3D11.ShaderResourceViewDescription { Format = Format.B8G8R8A8_UNorm, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource { MipLevels = 1, MostDetailedMip = 0 } }); if (RenderTarget != null) RenderTarget.Dispose(); using (var surface = mTmpTexture.QueryInterface<Surface>()) RenderTarget = new RenderTarget(Direct2DFactory, surface, new RenderTargetProperties() { DpiX = 0.0f, DpiY = 0.0f, MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT, PixelFormat = new PixelFormat() { AlphaMode = AlphaMode.Premultiplied, Format = Format.Unknown }, Type = RenderTargetType.Hardware, Usage = RenderTargetUsage.None }); if (mMutex10 != null) mMutex10.Dispose(); if (mMutex11 != null) mMutex11.Dispose(); mMutex10 = mTmpTexture.QueryInterface<KeyedMutex>(); mMutex11 = mRealTexture.QueryInterface<KeyedMutex>(); Brushes.Initialize(RenderTarget); Fonts.Initialize(DirectWriteFactory); Button.Initialize(); Frame.Initialize(); // right now the texture is unowned and only a key of 0 will succeed. // after releasing it with a specific key said key then can be used for // further locking. mMutex10.Acquire(0, -1); mMutex10.Release(Key11); }
/// <summary> /// Creates device-based resources to store a constant buffer, cube /// geometry, and vertex and pixel shaders. In some cases this will also /// store a geometry shader. /// </summary> public async void CreateDeviceDependentResourcesAsync() { ReleaseDeviceDependentResources(); usingVprtShaders = deviceResources.D3DDeviceSupportsVprt; var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; // On devices that do support the D3D11_FEATURE_D3D11_OPTIONS3:: // VPAndRTArrayIndexFromAnyShaderFeedingRasterizer optional feature // we can avoid using a pass-through geometry shader to set the render // target array index, thus avoiding any overhead that would be // incurred by setting the geometry shader stage. var vertexShaderFileName = usingVprtShaders ? "Content\\Shaders\\VPRTVertexShader.cso" : "Content\\Shaders\\VertexShader.cso"; // Load the compiled vertex shader. var vertexShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync(vertexShaderFileName)); // After the vertex shader file is loaded, create the shader and input layout. vertexShader = this.ToDispose(new SharpDX.Direct3D11.VertexShader( deviceResources.D3DDevice, vertexShaderByteCode)); //TODO: Change VertexDesc to use new structure: VertexPositionTextureCoordinates SharpDX.Direct3D11.InputElement[] vertexDesc = { new SharpDX.Direct3D11.InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0, SharpDX.Direct3D11.InputClassification.PerVertexData, 0), new SharpDX.Direct3D11.InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32B32_Float, 12, 0, SharpDX.Direct3D11.InputClassification.PerVertexData, 0), }; inputLayout = this.ToDispose(new SharpDX.Direct3D11.InputLayout( deviceResources.D3DDevice, vertexShaderByteCode, vertexDesc)); if (!usingVprtShaders) { // Load the compiled pass-through geometry shader. var geometryShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync("Content\\Shaders\\GeometryShader.cso")); // After the pass-through geometry shader file is loaded, create the shader. geometryShader = this.ToDispose(new SharpDX.Direct3D11.GeometryShader( deviceResources.D3DDevice, geometryShaderByteCode)); } // Load the compiled pixel shader. var pixelShaderByteCode = await DirectXHelper.ReadDataAsync(await folder.GetFileAsync("Content\\Shaders\\PixelShader.cso")); // After the pixel shader file is loaded, create the shader. pixelShader = this.ToDispose(new SharpDX.Direct3D11.PixelShader( deviceResources.D3DDevice, pixelShaderByteCode)); //TODO: Use a photo editing app to create a texture, like GIMP2 with DDS Plugin, make sure photo width & height divisible by 4, for a cube must be an exact square //used Gimp 2 with DDS Plugin to create a 2000x2000 image and export at .dds file. //in the export options I used BC3_UNorm format, along with automatic MIP levels string textureName2 = "Content\\Textures\\nuwaupian_holding_fire3.dds"; //TODO: Create the SamplerState var samplerStateDescription = new SharpDX.Direct3D11.SamplerStateDescription(); samplerStateDescription.Filter = SharpDX.Direct3D11.Filter.MinMagMipLinear; samplerStateDescription.AddressU = SharpDX.Direct3D11.TextureAddressMode.Wrap; samplerStateDescription.AddressV = SharpDX.Direct3D11.TextureAddressMode.Wrap; samplerStateDescription.AddressW = SharpDX.Direct3D11.TextureAddressMode.Wrap; samplerStateDescription.BorderColor = new SharpDX.Mathematics.Interop.RawColor4(0f, 0f, 0f, 1f); samplerState = new SharpDX.Direct3D11.SamplerState(deviceResources.D3DDevice, samplerStateDescription); //TODO: Use the TextureLoader class to create a bitmap source from a .DDS texture file //Retreived TextureLoader from DirectXTK or DirectText Tool var bitmapSource2 = TextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), textureName2); //TODO: Create a Texture Description to describe the texture you'll be using or converting to var textDesc = new SharpDX.Direct3D11.Texture2DDescription() { Width = bitmapSource2.Size.Width, Height = bitmapSource2.Size.Height, ArraySize = 6, BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource, Usage = SharpDX.Direct3D11.ResourceUsage.Default, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm, MipLevels = 1, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.TextureCube, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), }; //TODO: Create Shader Resource View var shaderResourceDesc = new SharpDX.Direct3D11.ShaderResourceViewDescription() { Format = textDesc.Format, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCube, TextureCube = new SharpDX.Direct3D11.ShaderResourceViewDescription.TextureCubeResource() { MipLevels = textDesc.MipLevels, MostDetailedMip = 0 } }; //TODO: Create 6 pointers for the 6 sides of the cube, each pointing to an 2000x2000 image you want to display IntPtr[] ptrImages = new IntPtr[6]; //TODO: Get the Stride of each image - stride is the size of 1 row pixels int stride = bitmapSource2.Size.Width * 4; //TODO: for each of the 6 pointers, create a buffer to hold the pixels using the DataStream object, using (var buffer = new SharpDX.DataStream(bitmapSource2.Size.Height * stride, true, true)) { //TODO: for each of the 6 data streams, copy the pixels into a buffer // Copy the content of the WIC to the buffer bitmapSource2.CopyPixels(stride, buffer); //TODO: for each of the 6 pointers get the IntPtr to the buffers, taking care not get rid of the buffers, pointers, or datastreams before we can create the texture cube ptrImages[0] = buffer.DataPointer; using (var buffer1 = new SharpDX.DataStream(bitmapSource2.Size.Height * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource2.CopyPixels(stride, buffer1); ptrImages[1] = buffer1.DataPointer; using (var buffer2 = new SharpDX.DataStream(bitmapSource2.Size.Height * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource2.CopyPixels(stride, buffer2); ptrImages[2] = buffer2.DataPointer; using (var buffer3 = new SharpDX.DataStream(bitmapSource2.Size.Height * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource2.CopyPixels(stride, buffer3); ptrImages[3] = buffer3.DataPointer; using (var buffer4 = new SharpDX.DataStream(bitmapSource2.Size.Height * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource2.CopyPixels(stride, buffer4); ptrImages[4] = buffer4.DataPointer; using (var buffer5 = new SharpDX.DataStream(bitmapSource2.Size.Height * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource2.CopyPixels(stride, buffer5); ptrImages[5] = buffer5.DataPointer; //TODO: create a DataBox of the 6 pixel buffers. The DataBox is the typed array which we described in the TextureDescription and ShaderResource Description to hold the 6 sides var dataRects = new SharpDX.DataBox[] { new SharpDX.DataBox(ptrImages[0], stride, 0), new SharpDX.DataBox(ptrImages[1], stride, 0), new SharpDX.DataBox(ptrImages[2], stride, 0), new SharpDX.DataBox(ptrImages[3], stride, 0), new SharpDX.DataBox(ptrImages[4], stride, 0), new SharpDX.DataBox(ptrImages[5], stride, 0) }; //TODO: Now create the TextureCube var texture2D = new SharpDX.Direct3D11.Texture2D(deviceResources.D3DDevice, textDesc, dataRects); //TODO: Now create the TextureShaderResourceView - which will be used in the PixelShader, after this point we can release all the buffers textureResource = new SharpDX.Direct3D11.ShaderResourceView(deviceResources.D3DDevice, texture2D, shaderResourceDesc); } } } } } } //TODO: Change to VertexPositionCoordinate // Load mesh vertices. Each vertex has a position and a color. // Note that the cube size has changed from the default DirectX app // template. Windows Holographic is scaled in meters, so to draw the // cube at a comfortable size we made the cube width 0.2 m (20 cm). VertexPositionCoordinate[] cubeVertices = { new VertexPositionCoordinate(new Vector3(-0.1f, -0.1f, -0.1f), new Vector3(0.0f, 0.0f, 0.0f)), new VertexPositionCoordinate(new Vector3(-0.1f, -0.1f, 0.1f), new Vector3(0.0f, 0.0f, 1.0f)), new VertexPositionCoordinate(new Vector3(-0.1f, 0.1f, -0.1f), new Vector3(0.0f, 1.0f, 0.0f)), new VertexPositionCoordinate(new Vector3(-0.1f, 0.1f, 0.1f), new Vector3(0.0f, 1.0f, 1.0f)), new VertexPositionCoordinate(new Vector3(0.1f, -0.1f, -0.1f), new Vector3(1.0f, 0.0f, 0.0f)), new VertexPositionCoordinate(new Vector3(0.1f, -0.1f, 0.1f), new Vector3(1.0f, 0.0f, 1.0f)), new VertexPositionCoordinate(new Vector3(0.1f, 0.1f, -0.1f), new Vector3(1.0f, 1.0f, 0.0f)), new VertexPositionCoordinate(new Vector3(0.1f, 0.1f, 0.1f), new Vector3(1.0f, 1.0f, 1.0f)), }; vertexBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.VertexBuffer, cubeVertices)); // Load mesh indices. Each trio of indices represents // a triangle to be rendered on the screen. // For example: 0,2,1 means that the vertices with indexes // 0, 2 and 1 from the vertex buffer compose the // first triangle of this mesh. ushort[] cubeIndices = { 2, 1, 0, // -x 2, 3, 1, 6, 4, 5, // +x 6, 5, 7, 0, 1, 5, // -y 0, 5, 4, 2, 6, 7, // +y 2, 7, 3, 0, 4, 6, // -z 0, 6, 2, 1, 3, 7, // +z 1, 7, 5, }; indexCount = cubeIndices.Length; indexBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.IndexBuffer, cubeIndices)); // Create a constant buffer to store the model matrix. modelConstantBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.ConstantBuffer, ref modelConstantBufferData)); // Once the cube is loaded, the object is ready to be rendered. loadingComplete = true; }
internal static int Gather(MyGPUEmitterData[] data, out SharpDX.Direct3D11.ShaderResourceView textureArraySRV) { MyRenderStats.Generic.WriteFormat("GPU particles allocated: {0}", m_totalParticles, VRage.Stats.MyStatTypeEnum.CurrentValue, 300, 0); MyRenderStats.Generic.WriteFormat("GPU particles overload: {0}", m_overloaded ? 1.0f : 0, VRage.Stats.MyStatTypeEnum.CurrentValue, 300, 0); for (int i = 0; i < MAX_LIVE_EMITTERS; i++) { data[i].NumParticlesToEmitThisFrame = 0; } int maxEmitterIndex = -1; uint textureIndex = 0; foreach (var id in m_idIndex.Values) { if (MyCommon.TimerMs > m_emitters.Data[id.Index].DieAt) { m_emitters.Data[id.Index].GPUEmitter.Data.Flags |= GPUEmitterFlags.Dead; } float toEmit = MyCommon.LastFrameDelta() * m_emitters.Data[id.Index].GPUEmitter.ParticlesPerSecond + m_emitters.Data[id.Index].ParticlesEmittedFraction; m_emitters.Data[id.Index].GPUEmitter.Data.NumParticlesToEmitThisFrame = (int)toEmit; m_emitters.Data[id.Index].ParticlesEmittedFraction = toEmit - m_emitters.Data[id.Index].GPUEmitter.Data.NumParticlesToEmitThisFrame; if (m_emitters.Data[id.Index].TextureId != Resources.TexId.NULL) { MyRenderProxy.Assert(m_textureArrayIndices.ContainsKey(m_emitters.Data[id.Index].TextureId)); textureIndex = m_textureArrayIndices[m_emitters.Data[id.Index].TextureId].Index; } else { textureIndex = 0; } int bufferIndex = m_emitters.Data[id.Index].BufferIndex; data[bufferIndex] = m_emitters.Data[id.Index].GPUEmitter.Data; data[bufferIndex].Position = m_emitters.Data[id.Index].GPUEmitter.WorldPosition - MyEnvironment.CameraPosition; data[bufferIndex].TextureIndex1 |= textureIndex << (ATLAS_INDEX_BITS + ATLAS_DIMENSION_BITS * 2); if (bufferIndex > maxEmitterIndex) { maxEmitterIndex = bufferIndex; } } UpdateTextureArray(); if (m_textureArray != null) { textureArraySRV = m_textureArray.SRV; } else { textureArraySRV = null; } foreach (var id in m_idIndex.Values.ToArray()) { if ((m_emitters.Data[id.Index].GPUEmitter.Data.Flags & GPUEmitterFlags.Dead) > 0) { m_totalParticles -= m_emitters.Data[id.Index].GPUEmitter.MaxParticles(); Remove(id); } } return(maxEmitterIndex + 1); }
public void OnResize(int width, int height) { if (mRealTexture != null) { mRealTexture.Dispose(); } if (mTmpTexture != null) { mTmpTexture.Dispose(); } mRealTexture = new SharpDX.Direct3D11.Texture2D(mDevice.Device, new SharpDX.Direct3D11.Texture2DDescription { ArraySize = 1, BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, Format = Format.B8G8R8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.SharedKeyedmutex, SampleDescription = new SampleDescription(1, 0), Usage = SharpDX.Direct3D11.ResourceUsage.Default }); using (var resource = mRealTexture.QueryInterface <SharpDX.DXGI.Resource>()) mTmpTexture = D2DDevice.OpenSharedResource <Texture2D>(resource.SharedHandle); if (NativeView != null) { NativeView.Dispose(); } NativeView = new SharpDX.Direct3D11.ShaderResourceView(mDevice.Device, mRealTexture, new SharpDX.Direct3D11.ShaderResourceViewDescription { Format = Format.B8G8R8A8_UNorm, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource { MipLevels = 1, MostDetailedMip = 0 } }); if (RenderTarget != null) { RenderTarget.Dispose(); } using (var surface = mTmpTexture.QueryInterface <Surface>()) RenderTarget = new RenderTarget(Direct2DFactory, surface, new RenderTargetProperties() { DpiX = 0.0f, DpiY = 0.0f, MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT, PixelFormat = new PixelFormat() { AlphaMode = AlphaMode.Premultiplied, Format = Format.Unknown }, Type = RenderTargetType.Hardware, Usage = RenderTargetUsage.None }); if (mMutex10 != null) { mMutex10.Dispose(); } if (mMutex11 != null) { mMutex11.Dispose(); } mMutex10 = mTmpTexture.QueryInterface <KeyedMutex>(); mMutex11 = mRealTexture.QueryInterface <KeyedMutex>(); Brushes.Initialize(RenderTarget); Fonts.Initialize(DirectWriteFactory); Button.Initialize(); Frame.Initialize(); // right now the texture is unowned and only a key of 0 will succeed. // after releasing it with a specific key said key then can be used for // further locking. mMutex10.Acquire(0, -1); mMutex10.Release(Key11); }
protected override void Dispose(bool disposing) { if (!IsDisposed) { #if DIRECTX if (disposing) { if (_resourceView != null) { _resourceView.Dispose(); _resourceView = null; } if (_texture != null) { _texture.Dispose(); _texture = null; } } #elif OPENGL GraphicsDevice.AddDisposeAction(() => { GL.DeleteTextures(1, ref glTexture); GraphicsExtensions.CheckGLError(); glTexture = -1; }); glLastSamplerState = null; #endif } base.Dispose(disposing); }
public Texture(Texture2D tex, SharpDX.Direct3D11.ShaderResourceView texView) { texture = tex; textureView = texView; }
public TextureCube(SharpDX.Toolkit.Graphics.TextureCube cube, SharpDX.Direct3D11.ShaderResourceView texView) { texCube = cube; textureView = texView; }
internal static int Gather(MyGPUEmitterData[] data, out SharpDX.Direct3D11.ShaderResourceView textureArraySRV) { for (int i = 0; i < MAX_LIVE_EMITTERS; i++) { data[i].NumParticlesToEmitThisFrame = 0; } // sort emitters! List <MyLiveData> emitters = m_emitters.Values.ToList(); //if (emitters.Count > MAX_LIVE_EMITTERS) emitters.Sort(); int maxEmitterIndex = -1; uint textureIndex = 0; int unassociatedCount = 0; int skipCount = 0; int unsortedCount = 0; int firstUnassociatedIndex = -1; int lastAssociatedIndex = -1; for (int i = 0; i < emitters.Count; i++) { var emitter = emitters[i]; // assiociate buffer index to new emitters & track overload to free space for unassociated near emitters later if (emitter.BufferIndex == -1) { if (m_freeBufferIndices.Count > 0) { emitter.BufferIndex = m_freeBufferIndices.Pop(); } else { unassociatedCount++; if (firstUnassociatedIndex == -1) { firstUnassociatedIndex = i; } } } else { skipCount = unassociatedCount; lastAssociatedIndex = i; if (unassociatedCount > 0) { unsortedCount++; } } if (MyCommon.TimerMs > emitter.DieAt) { emitter.GPUEmitter.Data.Flags |= GPUEmitterFlags.Dead; } if (emitter.BufferIndex != -1) { if ((emitter.GPUEmitter.Data.Flags & GPUEmitterFlags.FreezeEmit) == 0) { float toEmit = MyCommon.LastFrameDelta() * emitter.GPUEmitter.ParticlesPerSecond + emitter.ParticlesEmittedFraction; emitter.GPUEmitter.Data.NumParticlesToEmitThisFrame = (int)toEmit; emitter.ParticlesEmittedFraction = toEmit - emitter.GPUEmitter.Data.NumParticlesToEmitThisFrame; } if (emitter.TextureId != Resources.TexId.NULL) { MyRenderProxy.Assert(m_textureArrayIndices.ContainsKey(emitter.TextureId)); textureIndex = m_textureArrayIndices[emitter.TextureId].Index; } else { textureIndex = 0; } int bufferIndex = emitter.BufferIndex; data[bufferIndex] = emitter.GPUEmitter.Data; Vector3 pos = emitter.GPUEmitter.WorldPosition - MyRender11.Environment.CameraPosition; data[bufferIndex].RotationMatrix.M14 = pos.X; data[bufferIndex].RotationMatrix.M24 = pos.Y; data[bufferIndex].RotationMatrix.M34 = pos.Z; data[bufferIndex].TextureIndex1 |= textureIndex << (ATLAS_INDEX_BITS + ATLAS_DIMENSION_BITS * 2); if (bufferIndex > maxEmitterIndex) { maxEmitterIndex = bufferIndex; } } } /*MyRenderStats.Generic.WriteFormat("GPU particles allocated: {0}", totalParticles, VRage.Stats.MyStatTypeEnum.CurrentValue, 300, 0); * MyRenderStats.Generic.WriteFormat("GPU particles overload: {0}", overloadedParticles ? 1.0f : 0, VRage.Stats.MyStatTypeEnum.CurrentValue, 300, 0); * MyRenderStats.Generic.WriteFormat("GPU emitters overload: {0}", overloadedEmitters ? 1.0f : 0, VRage.Stats.MyStatTypeEnum.CurrentValue, 300, 0);*/ UpdateTextureArray(); if (m_textureArray != null) { textureArraySRV = m_textureArray.SRV; } else { textureArraySRV = null; } // stop emitters far away to make room for emitters nearby if (skipCount > 0 && unsortedCount > 0) { // iterate until buffer-unassociated index is larger then unassocited one for (int i = firstUnassociatedIndex, j = lastAssociatedIndex; i < j;) { var emitter = emitters[j]; // free last buffer-associated emitter data[emitter.BufferIndex].Flags |= GPUEmitterFlags.Dead; data[emitter.BufferIndex].NumParticlesToEmitThisFrame = 0; m_freeBufferIndices.Push(emitter.BufferIndex); emitter.BufferIndex = -1; // find new last buffer-associated emitter do { j--; }while (j > 0 && emitters[j].BufferIndex == -1); // find next buffer-unassociated emitter do { i++; } while (i < emitters.Count && emitters[i].BufferIndex != -1); } } foreach (var emitter in emitters) { if ((emitter.GPUEmitter.Data.Flags & GPUEmitterFlags.Dead) > 0) { Remove(emitter); } } return(maxEmitterIndex + 1); }
public VRRenderer(SharpDX.Direct3D11.Device d3d11Device, SharpDX.Direct3D11.Device unityDevice, VRRig rig) { instance = this; this.rig = rig; device = d3d11Device; unityRenderer = unityDevice; bounds = new VRTextureBounds_t(); bounds.vMin = bounds.uMin = 0; bounds.vMax = bounds.uMax = 1; var w = rig.leftTexture.Description.Width; var h = rig.leftTexture.Description.Height; var description = new SharpDX.Direct3D11.Texture2DDescription() { Width = (int)w, Height = (int)h, MipLevels = 1, ArraySize = 1, SampleDescription = new SampleDescription() { Count = 1 }, BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource | SharpDX.Direct3D11.BindFlags.RenderTarget, CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.Shared, Usage = SharpDX.Direct3D11.ResourceUsage.Default }; System.Diagnostics.Trace.WriteLine("Creating D3D11 Textures"); leftVRTexture = new SharpDX.Direct3D11.Texture2D(d3d11Device, description); rightVRTexture = new SharpDX.Direct3D11.Texture2D(d3d11Device, description); leftEye = new Texture_t() { eColorSpace = EColorSpace.Gamma, eType = ETextureType.DirectX, handle = leftVRTexture.NativePointer }; rightEye = new Texture_t() { eColorSpace = EColorSpace.Gamma, eType = ETextureType.DirectX, handle = rightVRTexture.NativePointer }; var sharedHandleLeft = leftVRTexture.QueryInterface <Resource>().SharedHandle; var sharedHandleRight = rightVRTexture.QueryInterface <Resource>().SharedHandle; leftHandle = unityRenderer.OpenSharedResource <SharpDX.Direct3D11.Texture2D>(sharedHandleLeft); rightHandle = unityRenderer.OpenSharedResource <SharpDX.Direct3D11.Texture2D>(sharedHandleRight); var viewDesc = new SharpDX.Direct3D11.ShaderResourceViewDescription() { Format = Format.R8G8B8A8_UNorm, Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D, Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource() { MipLevels = 1, MostDetailedMip = 0 } }; var leftView = new SharpDX.Direct3D11.ShaderResourceView(unityDevice, leftHandle, viewDesc); var rightView = new SharpDX.Direct3D11.ShaderResourceView(unityDevice, rightHandle, viewDesc); leftUnityTexture = Texture2D.CreateExternalTexture(w, h, TextureFormat.ARGB32, false, false, leftView.NativePointer); rightUnityTexture = Texture2D.CreateExternalTexture(w, h, TextureFormat.ARGB32, false, false, rightView.NativePointer); }