/// <summary> /// Maps the data contained in a subresource to a memory pointer, and denies the GPU access to that subresource. /// </summary> /// <param name="resource">The resource.</param> /// <param name="mipSlice">The mip slice.</param> /// <param name="arraySlice">The array slice.</param> /// <param name="mode">The mode.</param> /// <param name="flags">The flags.</param> /// <param name="stream">The output stream containing the pointer.</param> /// <returns> /// The locked <see cref="SharpDX.DataBox"/> /// </returns> /// <unmanaged>HRESULT ID3D11DeviceContext::Map([In] ID3D11Resource* pResource,[In] unsigned int Subresource,[In] D3D11_MAP MapType,[In] D3D11_MAP_FLAG MapFlags,[Out] D3D11_MAPPED_SUBRESOURCE* pMappedResource)</unmanaged> public DataBox MapSubresource(Texture1D resource, int mipSlice, int arraySlice, MapMode mode, MapFlags flags, out DataStream stream) { int mipSize; var box = MapSubresource(resource, mipSlice, arraySlice, mode, flags, out mipSize); stream = new DataStream(box.DataPointer, mipSize * (int)DXGI.FormatHelper.SizeOfInBytes(resource.Description.Format), true, true); return(box); }
/// <summary> /// Initializes a new instance of the <see cref="Texture1DBase" /> class. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="description1D">The description.</param> protected internal Texture1D(GraphicsDevice device, TextureDescription description1D, DataBox[] dataBox = null) : base(device, description1D, ViewType.Full, 0, 0) { NativeDescription = ConvertToNativeDescription(description1D); Resource = new SharpDX.Direct3D11.Texture1D(device.NativeDevice, NativeDescription, ConvertDataBoxes(dataBox)); NativeDeviceChild = Resource; NativeShaderResourceView = GetShaderResourceView(ViewType, ArraySlice, MipLevel); NativeUnorderedAccessView = GetUnorderedAccessView(ArraySlice, MipLevel); }
/// <summary> /// Initializes a new instance of the <see cref="Texture1DBase" /> class. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="description1D">The description.</param> protected internal Texture1D(GraphicsDevice device, Texture1D texture, ViewType viewType, int arraySlice, int mipMapSlice, PixelFormat viewFormat = PixelFormat.None) : base(device, texture, viewType, arraySlice, mipMapSlice, viewFormat) { // Copy the device child, but don't use NativeDeviceChild, as it is registering it for disposing. _nativeDeviceChild = texture._nativeDeviceChild; Resource = texture.Resource; NativeDescription = texture.NativeDescription; dxgiSurface = texture.dxgiSurface; NativeShaderResourceView = GetShaderResourceView(ViewType, ArraySlice, MipLevel); NativeUnorderedAccessView = GetUnorderedAccessView(ArraySlice, MipLevel); }
/// <summary> /// Function to create an image with initial data. /// </summary> /// <param name="initialData">Data to use when creating the image.</param> /// <remarks> /// The <paramref name="initialData" /> can be NULL (Nothing in VB.Net) IF the texture is not created with an Immutable usage flag. /// <para>To initialize the texture, create a new <see cref="GorgonLibrary.Graphics.GorgonImageData">GorgonImageData</see> object and fill it with image information.</para> /// </remarks> protected override void OnInitialize(GorgonImageData initialData) { var desc = new D3D.Texture1DDescription { ArraySize = Settings.ArrayCount, BindFlags = GetBindFlags(false, false), Format = (GI.Format)Settings.Format, Width = Settings.Width, MipLevels = Settings.MipCount, OptionFlags = D3D.ResourceOptionFlags.None, Usage = (D3D.ResourceUsage)Settings.Usage }; switch (Settings.Usage) { case BufferUsage.Staging: desc.CpuAccessFlags = D3D.CpuAccessFlags.Read | D3D.CpuAccessFlags.Write; break; case BufferUsage.Dynamic: desc.CpuAccessFlags = D3D.CpuAccessFlags.Write; break; default: desc.CpuAccessFlags = D3D.CpuAccessFlags.None; break; } if ((initialData != null) && (initialData.Buffers.Count > 0)) { D3DResource = new D3D.Texture1D(Graphics.D3DDevice, desc, initialData.Buffers.DataBoxes); } else { D3DResource = new D3D.Texture1D(Graphics.D3DDevice, desc); } }
// Simple DDS loader ported from http://msdn.microsoft.com/en-us/library/windows/apps/jj651550.aspx static void CreateD3DResources( SharpDX.Direct3D11.Device d3dDevice, TextureDimension resDim, int width, int height, int depth, int mipCount, int arraySize, Format format, bool isCubeMap, DataBox[] initData, //_In_reads_(mipCount*arraySize) D3D11_SUBRESOURCE_DATA* initData, out SharpDX.Direct3D11.Resource texture, out SharpDX.Direct3D11.ShaderResourceView textureView //_Out_opt_ ID3D11Resource** texture, //_Out_opt_ ID3D11ShaderResourceView** textureView ) { texture = null; textureView = null; if (d3dDevice == null || initData == null) { return; } switch (resDim) { case TextureDimension.Texture1D:// D3D11_RESOURCE_DIMENSION_TEXTURE1D: { Texture1DDescription desc = new Texture1DDescription(); //D3D11_TEXTURE1D_DESC desc; desc.Width = width; desc.MipLevels = mipCount; desc.ArraySize = arraySize; desc.Format = format; desc.Usage = ResourceUsage.Default; desc.BindFlags = BindFlags.ShaderResource;// D3D11_BIND_SHADER_RESOURCE; desc.CpuAccessFlags = CpuAccessFlags.None; desc.OptionFlags = ResourceOptionFlags.None; Texture1D tex = null; //ID3D11Texture1D* tex = nullptr; tex = new Texture1D(d3dDevice, desc, initData); //hr = d3dDevice->CreateTexture1D(&desc, initData, &tex); if (tex != null) { ShaderResourceViewDescription SRVDesc = new ShaderResourceViewDescription(); //D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc; //memset(&SRVDesc, 0, sizeof(SRVDesc)); SRVDesc.Format = format; if (arraySize > 1) { SRVDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture1DArray;// D3D_SRV_DIMENSION_TEXTURE1DARRAY; SRVDesc.Texture1DArray.MipLevels = desc.MipLevels; SRVDesc.Texture1DArray.ArraySize = arraySize; } else { SRVDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture1D;// D3D_SRV_DIMENSION_TEXTURE1D; SRVDesc.Texture1D.MipLevels = desc.MipLevels; } textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc); //hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView); if (textureView == null) { tex.Dispose(); return; } texture = tex; } } break; case TextureDimension.TextureCube: case TextureDimension.Texture2D:// D3D11_RESOURCE_DIMENSION_TEXTURE2D: { Texture2DDescription desc = new Texture2DDescription(); desc.Width = width; desc.Height = height; desc.MipLevels = mipCount; desc.ArraySize = arraySize; desc.Format = format; desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; desc.Usage = ResourceUsage.Default; desc.BindFlags = BindFlags.ShaderResource; desc.CpuAccessFlags = CpuAccessFlags.None; desc.OptionFlags = (isCubeMap) ? ResourceOptionFlags.TextureCube : ResourceOptionFlags.None; Texture2D tex = null; tex = new Texture2D(d3dDevice, desc, initData); tex.DebugName = "Test"; //hr = d3dDevice->CreateTexture2D(&desc, initData, &tex); if (tex != null) { ShaderResourceViewDescription SRVDesc = new ShaderResourceViewDescription(); SRVDesc.Format = format; if (isCubeMap) { if (arraySize > 6) { SRVDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCubeArray; SRVDesc.TextureCubeArray.MipLevels = desc.MipLevels; // Earlier we set arraySize to (NumCubes * 6) SRVDesc.TextureCubeArray.CubeCount = arraySize / 6; } else { SRVDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.TextureCube; SRVDesc.TextureCube.MipLevels = desc.MipLevels; } } else if (arraySize > 1) { SRVDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2DArray; SRVDesc.Texture2DArray.MipLevels = desc.MipLevels; SRVDesc.Texture2DArray.ArraySize = arraySize; } else { SRVDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D; SRVDesc.Texture2D.MipLevels = desc.MipLevels; } textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc); //hr = d3dDevice->CreateShaderResourceView(tex, &SRVDesc, textureView); texture = tex; } } break; case TextureDimension.Texture3D: { Texture3DDescription desc = new Texture3DDescription(); desc.Width = width; desc.Height = height; desc.Depth = depth; desc.MipLevels = mipCount; desc.Format = format; desc.Usage = ResourceUsage.Default; desc.BindFlags = BindFlags.ShaderResource; desc.CpuAccessFlags = CpuAccessFlags.None; desc.OptionFlags = ResourceOptionFlags.None; Texture3D tex = null; tex = new Texture3D(d3dDevice, desc, initData); //hr = d3dDevice->CreateTexture3D(&desc, initData, &tex); if (tex != null) { ShaderResourceViewDescription SRVDesc = new ShaderResourceViewDescription(); SRVDesc.Format = format; SRVDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture3D; SRVDesc.Texture3D.MipLevels = desc.MipLevels; textureView = new ShaderResourceView(d3dDevice, tex, SRVDesc); texture = tex; } } break; } }
/// <summary> /// Specialised constructor for use only by derived classes. /// </summary> /// <param name="device">The device.</param> /// <param name="texture">The texture.</param> /// <msdn-id>ff476520</msdn-id> /// <unmanaged>HRESULT ID3D11Device::CreateTexture1D([In] const D3D11_TEXTURE1D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture1D** ppTexture1D)</unmanaged> /// <unmanaged-short>ID3D11Device::CreateTexture1D</unmanaged-short> protected internal Texture1DBase(DirectXDevice device, SharpDX.Direct3D11.Texture1D texture) : base(device, texture.Description) { Resource = texture; }
/// <summary> /// Initializes a new instance of the <see cref="Texture1DBase" /> class. /// </summary> /// <param name="device">The <see cref="DirectXDevice"/>.</param> /// <param name="description1D">The description.</param> /// <param name="dataBox">A variable-length parameters list containing data rectangles.</param> /// <msdn-id>ff476520</msdn-id> /// <unmanaged>HRESULT ID3D11Device::CreateTexture1D([In] const D3D11_TEXTURE1D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture1D** ppTexture1D)</unmanaged> /// <unmanaged-short>ID3D11Device::CreateTexture1D</unmanaged-short> protected internal Texture1DBase(DirectXDevice device, Texture1DDescription description1D, DataBox[] dataBox) : base(device, description1D) { Resource = new SharpDX.Direct3D11.Texture1D(device, description1D, dataBox); }
private void SetRandomTex(int Seed) { // set the seed _Seed = Seed; // set the random size int RandNum_Size = 256; // set the description of the texture Texture1DDescription RandomTex_Desc = new Texture1DDescription(); RandomTex_Desc.Format = Format.R8_UInt; RandomTex_Desc.CpuAccessFlags = CpuAccessFlags.None; RandomTex_Desc.Width = RandNum_Size * 2; RandomTex_Desc.Usage = ResourceUsage.Default; RandomTex_Desc.OptionFlags = ResourceOptionFlags.None; RandomTex_Desc.MipLevels = 1; RandomTex_Desc.BindFlags = BindFlags.ShaderResource; RandomTex_Desc.ArraySize = 1; // start the stream DataStream stream = new DataStream(RandNum_Size * 2, true, true); // Initialize the random generator Random generator = new Random(Seed); // allocate the random values byte[] RandNum_Host = new byte[RandNum_Size * 2]; // Copy the source data twice to the generator array for (int i = 0; i < RandNum_Size; i++) { RandNum_Host[i] = (byte)generator.Next(256); RandNum_Host[i + RandNum_Size] = RandNum_Host[i]; } // pass the randoms to the stream stream.WriteRange<byte>(RandNum_Host); stream.Position = 0; // create the texture and pass the data Texture1D RandomTex = new Texture1D(Engine.g_device, RandomTex_Desc, stream); // close the stream we don't need it any more stream.Close(); // create the resource view to be able to pass it to the shader ShaderResourceView RandomResourceView = new ShaderResourceView(Engine.g_device, RandomTex); // set the Resource to the shader RandomTex_Variable.SetResource(RandomResourceView); }
/// <summary> /// Creates a new texture from a <see cref="SharpDX.Direct3D11.Texture1D"/>. /// </summary> /// <param name="device">The <see cref="DirectXDevice"/>.</param> /// <param name="texture">The native texture <see cref="SharpDX.Direct3D11.Texture1D"/>.</param> /// <returns> /// A new instance of <see cref="Texture1D"/> class. /// </returns> /// <msdn-id>ff476520</msdn-id> /// <unmanaged>HRESULT ID3D11Device::CreateTexture1D([In] const D3D11_TEXTURE1D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture1D** ppTexture1D)</unmanaged> /// <unmanaged-short>ID3D11Device::CreateTexture1D</unmanaged-short> public static Texture1D New(DirectXDevice device, SharpDX.Direct3D11.Texture1D texture) { return(new Texture1D(device, texture)); }
internal Texture1D(DirectXDevice directX, SharpDX.Direct3D11.Texture1D texture) : base(directX, texture) { }
public static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // Create main disposer var mainDisposer = new Disposer(); // var random = new Random(1415926535); #region demo setup // show setup dialog and get setup values var title = "Mash Room by Jolt"; var setupView = new StartupView(title); var setupModel = new SetupModel(); var presenter = new SetupPresenter(setupView, setupModel); setupModel.TryLoadSettings(); setupModel.SelectAdapter(0); var dialogResult = setupView.ShowDialog(); if (dialogResult != DialogResult.OK) return; #if DEBUG setupModel.SaveSettings(); // todo: remove save in release mode #endif #endregion // Create Device and SwapChain var demo = new Demo().Init(setupModel, title); var inputHandler = new InputHandler().Bind(demo); #region audio // var audioDeviceType = (setupModel.UseAudio) ? AudioDeviceType.Bass : AudioDeviceType.Silent; var audioDeviceManager = new AudioDeviceManager(); var audioDevice = mainDisposer.Add(audioDeviceManager.CreateDevice(audioDeviceType)); // if (setupModel.UseAudio) { // BassNet.Registration( setupModel.BassRegistrationEmail ?? "", setupModel.BassRegistrationKey ?? ""); // 140 bpm // delay 3.428 (dvs 2 takter i 140 bpm) var bpm = 140; var audioName = "Mashup_v1.00.mp3"; var audioAsset = mainDisposer.Add(demo.AudioManager[audioName]); // init and load audio audioDevice.Init(); audioDevice.Load(audioAsset.Value); audioDevice.PlayPosition = setupModel.StartTime; audioDevice.Bpm = bpm; // use the audio device as timer demo.Timer = audioDevice; } #endregion #region sync demo.SyncManager = new SyncManager().Init(setupModel.SyncRecordMode, demo.Timer.Bpm, 4); demo.SyncManager.TimerDevice = demo.Timer; #endregion #region shaders var planeVertexShader = demo.ShaderManager["plane.vs.cso"].VertexShader; var planePixelShader = demo.ShaderManager["plane.ps.cso"].PixelShader; var postPixelShader = demo.ShaderManager["post.ps.cso"].PixelShader; var postVertexShader = demo.ShaderManager["post.vs.cso"].VertexShader; var vanillaEffect = mainDisposer.Add(new VanillaEffect(demo).Init()); var vanillaLayout = mainDisposer.Add(vanillaEffect.InputLayout.InputLayout); #endregion #region models // load all models demo.ModelManager.LoadAll(); // make all models white (todo: why) foreach (var model in demo.ModelManager) { model.Color = new Color(255, 255, 255, 255); model.ReCreateBuffer(demo.Device); } #endregion #region textures Texture2D backBuffer = null; Texture2D depthBuffer = null; DepthStencilView depthView = null; // 1D texture for scanline distortion var distortionTexture = new Texture1D(demo.Device, new Texture1DDescription { CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Usage = ResourceUsage.Default, Width = 1080, ArraySize = 1, MipLevels = 1, BindFlags = BindFlags.ShaderResource, OptionFlags = ResourceOptionFlags.None }); var distortionData = new byte[4 * distortionTexture.Description.Width]; var distortionSRV = new ShaderResourceView(demo.Device, distortionTexture); // foreground texture var foregroundTexture = new Texture2D(demo.Device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8_UNorm, Usage = ResourceUsage.Default, Width = 1920, Height = 1080, ArraySize = 1, MipLevels = 1, BindFlags = BindFlags.ShaderResource, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), }); var foregroundSRV = new ShaderResourceView(demo.Device, foregroundTexture); var foregroundData = new byte[foregroundTexture.Description.Width * foregroundTexture.Description.Height]; // noise texture var noiseTexture = new Texture2D(demo.Device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8_UNorm, Usage = ResourceUsage.Default, Width = 1920 / 5, Height = 1080 / 5, ArraySize = 1, MipLevels = 1, BindFlags = BindFlags.ShaderResource, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), }); var noiseSRV = new ShaderResourceView(demo.Device, noiseTexture); var noiseData = new byte[noiseTexture.Description.Width * noiseTexture.Description.Height]; #endregion #region acts var act0 = mainDisposer.Add(new Act0(demo).Init()); #endregion #region effects //var fractalCityEffect = mainDisposer.Add(new FractalCityEffect(demo).Init()); //var oceanEffect = mainDisposer.Add(new OceanEffect(demo).Init()); //var dustEffect = mainDisposer.Add(new DustEffect(demo).Init()); //var cloudEffect = mainDisposer.Add(new CloudEffect(demo).Init()); //var mandelbulbEffect = mainDisposer.Add(new MandelbulbEffect(demo).Init()); //var redPlanetEffect = mainDisposer.Add(new Effect(demo).Init(new EffectDescription //{ // PixelShaderName = "redPlanet.ps.cso", // TextureNames = "redPlanet0.png,redPlanet1.jpg".Split(',') //})); //var pseudoKleinianEffect = mainDisposer.Add(new Effect(demo).Init(new EffectDescription //{ // PixelShaderName = "pseudoKleinian.ps.cso", //})); //var nonameEffect00 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname00.ps.cso", TextureNames = "tex09.jpg".Split(',') })); //var nonameEffect01 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname01.ps.cso" })); //var nonameEffect02 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname02.ps.cso", TextureNames = "tex09.jpg,tex07.jpg".Split(',') })); //var nonameEffect03 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname03.ps.cso" })); //var nonameEffect04 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname04.ps.cso", TextureNames = "noise256.png".Split(',') })); //var nonameEffect05 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname05.ps.cso" })); //var nonameEffect06 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname06.ps.cso" })); //var nonameEffect07 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname07.ps.cso" })); //var nonameEffect08 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname08.ps.cso" })); //var nonameEffect09 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname09.ps.cso" })); //var nonameEffect10 = mainDisposer.Add(new Effect(demo).Init(new EffectDescription { PixelShaderName = "noname10.ps.cso" })); #endregion var fillStateMsaadesc = RasterizerStateDescription.Default(); fillStateMsaadesc.IsMultisampleEnabled = true; var msaaFillState = new RasterizerState(demo.Device, fillStateMsaadesc); var fillState = new RasterizerState(demo.Device, RasterizerStateDescription.Default()); var vanillaRenderContext = null as RenderContext; var postRenderContext = null as RenderContext; var planeRenderContext = null as RenderContext; // Main loop using (var renderDisposer = new Disposer()) { RenderLoop.Run(demo.Form, () => { if (demo.SyncManager.RowIndex == 0x000006d0) { demo.Form.Close(); return; } // TODO refactor: move the resize code #region resize if (demo.OutputWasResized) { demo.OutputWasResized = false; renderDisposer.DisposeAll(); // dispose old swapchain if (demo.SwapChain != null) { demo.SwapChain.Dispose(); } // create new swapchain demo.SwapChain = renderDisposer.Add(new SwapChain(setupModel.Factory, demo.Device, new SwapChainDescription { BufferCount = 1, ModeDescription = setupModel.Mode, IsWindowed = !setupModel.FullScreen, OutputHandle = demo.Form.Handle, //SampleDescription = new SampleDescription(setupModel.MultiSampleCount, setupModel.MultiSampleQuality), SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput })); if (setupModel.FullScreen) { demo.SwapChain.SetFullscreenState(true, setupModel.Output); } // var postOutputRenderTarget = new RenderTarget(demo.Device, demo.SwapChain); // Get the backbuffer from the swapchain backBuffer = renderDisposer.Add(demo.SwapChain.GetBackBuffer<Texture2D>(0)); // Setup targets and viewport for rendering var postInputRenderTarget = new RenderTarget( device: demo.Device, width: setupModel.Mode.Width / 1, // todo: introduce a scalefactor height: setupModel.Mode.Height / 1, // todo: introduce a scalefactor sampleCount: 1, // todo: use setupModel.MultiSampleCount, sampleQuality: 0, // todo: use setupModel.MultiSampleQuality, format: Format.R8G8B8A8_UNorm // todo: use setupModel.Format ); // Create the depth buffer depthBuffer = renderDisposer.Add(new Texture2D(demo.Device, new Texture2DDescription { Format = Format.D32_Float_S8X24_UInt, ArraySize = 1, MipLevels = 1, Width = postInputRenderTarget.Width, Height = postInputRenderTarget.Height, SampleDescription = postInputRenderTarget.Texture.Description.SampleDescription, Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None })); // Create the depth buffer view depthView = renderDisposer.Add(new DepthStencilView(demo.Device, depthBuffer)); vanillaRenderContext = renderDisposer.Add(new RenderContext { PrimitiveTopology = PrimitiveTopology.TriangleList, InputLayout = vanillaLayout, VertexShader = null, PixelShader = null, RasterizerState = msaaFillState, DepthStencilView = depthView, RenderTarget = postInputRenderTarget, }); postRenderContext = renderDisposer.Add(new RenderContext { PrimitiveTopology = PrimitiveTopology.TriangleList, InputLayout = vanillaLayout, VertexShader = postVertexShader, PixelShader = postPixelShader, RasterizerState = fillState, DepthStencilView = null, RenderTarget = postOutputRenderTarget, }); planeRenderContext = new RenderContext { PrimitiveTopology = PrimitiveTopology.TriangleList, InputLayout = vanillaLayout, VertexShader = planeVertexShader, PixelShader = planePixelShader, RasterizerState = fillState, DepthStencilView = null, RenderTarget = postOutputRenderTarget, }; // todo: remove this hack if (!setupModel.SyncRecordMode) { demo.Timer.StartPlaying(); } if (setupModel.FullScreen) { Cursor.Hide(); } } #endregion // todo: remove demo.SyncManager.Update(demo.Timer.Time); var syncRow = new { demo.SyncManager.Data.Part, demo.SyncManager.Data.Lead, }; // Present! // a crash here is quite common when VertexShader or PixelShader is null demo.SwapChain.Present(setupModel.UseVerticalSync ? 1 : 0, PresentFlags.None); // horizontal distortion and color separation random.NextBytes(distortionData); demo.DeviceContext.UpdateSubresource(distortionData, distortionTexture); // static noise random.NextBytes(noiseData); demo.DeviceContext.UpdateSubresource(noiseData, noiseTexture, 0, noiseTexture.Description.Width); // setup for normal rendering { // use normal rendering demo.DeviceContext.PixelShader.SetShaderResource(0, null); demo.RenderContext = vanillaRenderContext; // clear demo.DeviceContext.ClearDepthStencilView(demo.RenderContext.DepthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0); demo.DeviceContext.ClearRenderTargetView(demo.RenderContext.RenderTarget.RenderTargetView, Color.Black); } // render act act0.Render(); // demo.DeviceContext.ClearRenderTargetView(postRenderContext.RenderTarget.RenderTargetView, Color.Black); // render plane to screen if (true) { // TODO is this really needed ? demo.DeviceContext.OutputMerger.ResetTargets(); demo.DeviceContext.VertexShader.SetConstantBuffer(0, null); // use post rendering demo.RenderContext = postRenderContext; // set textures demo.DeviceContext.PixelShader.SetShaderResource(0, vanillaRenderContext.RenderTarget.ShaderResourceView); demo.DeviceContext.PixelShader.SetShaderResource(1, distortionSRV); demo.DeviceContext.PixelShader.SetShaderResource(2, noiseSRV); // use plane model var model = demo.ModelManager["plane2"]; // todo: make sure we have no z-buffer conflicts (might be hw dependent) var camera = new NoCamera(); demo.Draw(model, camera); } }); } // dispose Disposer.Dispose(ref mainDisposer); }
private ShaderResourceView GenerateTexture(IContext context, int sliceCount) { Texture1D provinceTexture = new Texture1D(context.DirectX.Device, new Texture1DDescription { Width = sliceCount, Format = Format.R32G32B32A32_Float, BindFlags = BindFlags.ShaderResource, ArraySize = 1, MipLevels = 1 }); int rowPitch = 16 * sliceCount; var byteArray = new byte[rowPitch]; for (int i = 0; i < sliceCount; i++) { CustomColor color = ColorInSlice(i); Array.Copy(BitConverter.GetBytes(color.Red), 0, byteArray, i * 16, 4); Array.Copy(BitConverter.GetBytes(color.Green), 0, byteArray, i * 16 + 4, 4); Array.Copy(BitConverter.GetBytes(color.Blue), 0, byteArray, i * 16 + 8, 4); Array.Copy(BitConverter.GetBytes(1.0f), 0, byteArray, i * 16 + 12, 4); } DataStream dataStream = new DataStream(rowPitch, true, true); dataStream.Write(byteArray, 0, rowPitch); DataBox data = new DataBox(dataStream.DataPointer, rowPitch, rowPitch); context.DirectX.DeviceContext.UpdateSubresource(data, provinceTexture); return new ShaderResourceView(context.DirectX.Device, provinceTexture); }
public static ShaderResourceView CreateRandomTexture1D(Device device) { var randomValues = new List<Vector4>(); for (var i = 0; i < 1024; i++) { randomValues.Add(new Vector4(MathF.Rand.NextFloat(-1.0f, 1.0f), MathF.Rand.NextFloat(-1.0f, 1.0f), MathF.Rand.NextFloat(-1.0f, 1.0f), MathF.Rand.NextFloat(-1.0f, 1.0f))); } var texDesc = new Texture1DDescription() { ArraySize = 1, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R32G32B32A32_Float, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Immutable, Width = 1024 }; var randTex = new Texture1D(device, texDesc, DataStream.Create(randomValues.ToArray(), false, false)); var viewDesc = new ShaderResourceViewDescription() { Format = texDesc.Format, Dimension = ShaderResourceViewDimension.Texture1D, Texture1D = new ShaderResourceViewDescription.Texture1DResource() { MipLevels = texDesc.MipLevels, MostDetailedMip = 0 } }; var randTexSRV = new ShaderResourceView(device, randTex, viewDesc); Utilities.Dispose(ref randTex); return randTexSRV; }
internal RenderTarget1D(DirectXDevice device, SharpDX.Direct3D11.Texture1D texture) : base(device, texture) { }
/// <summary> /// Creates a new <see cref="RenderTarget1D"/> from a <see cref="SharpDX.Direct3D11.Texture1D"/>. /// </summary> /// <param name="device">The <see cref="DirectXDevice"/>.</param> /// <param name="texture">The native texture <see cref="SharpDX.Direct3D11.Texture1D"/>.</param> /// <returns> /// A new instance of <see cref="RenderTarget1D"/> class. /// </returns> /// <msdn-id>ff476520</msdn-id> /// <unmanaged>HRESULT ID3D11Device::CreateTexture1D([In] const D3D11_TEXTURE1D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture1D** ppTexture1D)</unmanaged> /// <unmanaged-short>ID3D11Device::CreateTexture1D</unmanaged-short> public static RenderTarget1D New(DirectXDevice device, SharpDX.Direct3D11.Texture1D texture) { return(new RenderTarget1D(device, texture)); }
private ShaderResourceView GenerateProvinceTexture(IContext context, IList<LandProvince> provinces, Func<LandProvince, CustomColor> colorGenerator) { int maxId = provinces.Max(p => p.NumericId); Texture1D provinceTexture = new Texture1D(context.DirectX.Device, new Texture1DDescription { Width = maxId + 1, Format = Format.R32G32B32A32_Float, BindFlags = BindFlags.ShaderResource, ArraySize = 1, MipLevels = 1 }); int rowPitch = 16 * provinceTexture.Description.Width; var byteArray = new byte[rowPitch]; foreach (LandProvince province in provinces) { CustomColor color = colorGenerator(province); Array.Copy(BitConverter.GetBytes(color.Red), 0, byteArray, province.NumericId * 16, 4); Array.Copy(BitConverter.GetBytes(color.Green), 0, byteArray, province.NumericId * 16 + 4, 4); Array.Copy(BitConverter.GetBytes(color.Blue), 0, byteArray, province.NumericId * 16 + 8, 4); Array.Copy(BitConverter.GetBytes(1.0f), 0, byteArray, province.NumericId * 16 + 12, 4); } DataStream dataStream = new DataStream(rowPitch,true, true); dataStream.Write(byteArray,0,rowPitch); DataBox data = new DataBox(dataStream.DataPointer, rowPitch, rowPitch); //ResourceRegion region = new ResourceRegion(); context.DirectX.DeviceContext.UpdateSubresource(data, provinceTexture); return new ShaderResourceView(context.DirectX.Device, provinceTexture); }