请选择 进入手机版 | 继续访问电脑版

开源计算机图形学社区(Open Source Computer Graphics Community) |OpenGPU Forum (2007-2013)| OpenGPU Project

 找回密码
 注册
搜索
查看: 744|回复: 7

关于用DirectX11 显示纯像素数据的问题 [复制链接]

Rank: 4

注册时间
2017-8-3
积分
27
发表于 2017-8-3 13:40:09 |显示全部楼层
最近要做一个UWP,手头只有纯像素数据,指针指向一块按照BGRA32的格式存储的纯像素数据的连续内存,如何用DirectX11显示出来。ID3D11Texture2D如何填充数据,用Map 和 UnMap?Map之后直接把这块内存拷贝到texture2D的pData里面吗?除此之外,需要pixelshader吗?Direct2D有可以填充像素数据的方法或者类吗?本人是做嵌入式的,临时需要出一个显示的Demo,时间比较仓促,大概看了下msdn的实例,问的问题可能有点小白,请各位海涵。



Rank: 28Rank: 28Rank: 28Rank: 28Rank: 28Rank: 28Rank: 28

注册时间
2007-6-11
积分
29818
发表于 2017-8-3 14:59:59 |显示全部楼层
你已经自己回答了。直接画就行了。

使用道具 举报

Rank: 4

注册时间
2017-8-3
积分
27
发表于 2017-8-3 15:09:18 |显示全部楼层
  1.         m_window = Window::Current->CoreWindow;
  2.         Microsoft::WRL::ComPtr<ID3D11RenderTargetView>  m_d3dRenderTargetView;
  3.         Microsoft::WRL::ComPtr<ID3D11Device2>           m_d3dDevice;
  4.         Microsoft::WRL::ComPtr<ID3D11DeviceContext3>    m_d3dContext;
  5.         Microsoft::WRL::ComPtr<IDXGISwapChain1>         m_swapChain;


  6.         // This flag adds support for surfaces with a different color channel
  7.         // ordering than the API default. It is required for compatibility with
  8.         // Direct2D.
  9.         UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
  10. #if defined(_DEBUG)

  11.         // 如果项目处于调试生成过程中,请通过带有此标志的 SDK 层启用调试。
  12.         creationFlags |= D3D11_CREATE_DEVICE_DEBUG;

  13. #endif

  14.         // This example only uses feature level 9.1.
  15.         D3D_FEATURE_LEVEL featureLevels[] =
  16.         {
  17.                 D3D_FEATURE_LEVEL_11_1,
  18.                 D3D_FEATURE_LEVEL_11_0,
  19.                 D3D_FEATURE_LEVEL_10_1,
  20.                 D3D_FEATURE_LEVEL_10_0,
  21.                 D3D_FEATURE_LEVEL_9_3,
  22.                 D3D_FEATURE_LEVEL_9_1
  23.         };

  24.         // Create the Direct3D 11 API device object and a corresponding context.
  25.         ComPtr<ID3D11Device> device;
  26.         ComPtr<ID3D11DeviceContext> context;
  27.         HRESULT hr = D3D11CreateDevice(
  28.                 nullptr, // Specify nullptr to use the default adapter.
  29.                 D3D_DRIVER_TYPE_HARDWARE,
  30.                 nullptr,
  31.                 creationFlags,
  32.                 featureLevels,
  33.                 ARRAYSIZE(featureLevels),
  34.                 D3D11_SDK_VERSION, // Windows Store apps must set this to D3D11_SDK_VERSION.
  35.                 &device, // Returns the Direct3D device created.
  36.                 nullptr,
  37.                 &context // Returns the device immediate context.
  38.         );

  39.         // Store pointers to the Direct3D 11.2 API device and immediate context.
  40.         hr = device.As(&m_d3dDevice);

  41.         hr = context.As(&m_d3dContext);

  42.         ComPtr<IDXGIDevice2> dxgiDevice;
  43.         hr = m_d3dDevice.As(&dxgiDevice);
  44.         dxgiDevice->SetMaximumFrameLatency(1);

  45.         // Then, the adapter hosting the device;
  46.         ComPtr<IDXGIAdapter> dxgiAdapter;
  47.         hr = dxgiDevice->GetAdapter(&dxgiAdapter);

  48.         // Then, the factory that created the adapter interface:
  49.         ComPtr<IDXGIFactory2> dxgiFactory;
  50.         hr = dxgiAdapter->GetParent(
  51.                 __uuidof(IDXGIFactory2),
  52.                 &dxgiFactory
  53.         );
  54.         DXGI_SWAP_CHAIN_DESC1 swapChainDesc = { 0 };

  55.         swapChainDesc.Width = 0; // Match the size of the window.
  56.         swapChainDesc.Height = 0;
  57.         swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
  58.         swapChainDesc.Stereo = false;
  59.         swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
  60.         swapChainDesc.SampleDesc.Quality = 0;
  61.         swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  62.         swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
  63.         swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
  64.         swapChainDesc.Flags = 0;
  65.         swapChainDesc.Scaling = DXGI_SCALING_NONE;
  66.         swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;

  67.         // This sequence obtains the DXGI factory that was used to create the Direct3D device above.

  68.         hr = dxgiFactory->CreateSwapChainForCoreWindow(
  69.                 m_d3dDevice.Get(),
  70.                 reinterpret_cast<IUnknown*>(m_window.Get()),
  71.                 &swapChainDesc,
  72.                 nullptr,
  73.                 &m_swapChain
  74.         );

  75.         ComPtr<ID3D11Texture2D> backBuffer;
  76.         m_swapChain->GetBuffer(
  77.                 0,
  78.                 __uuidof(ID3D11Texture2D),
  79.                 &backBuffer
  80.         );

  81.         // Create a render target view on the back buffer.
  82.         hr = m_d3dDevice->CreateRenderTargetView(
  83.                 backBuffer.Get(),
  84.                 nullptr,
  85.                 &m_d3dRenderTargetView
  86.         );
  87.         D3D11_TEXTURE2D_DESC textureDesc;
  88.         ZeroMemory(&textureDesc, sizeof(textureDesc));
  89.         textureDesc.Width = 256;
  90.         textureDesc.Height = 256;
  91.         textureDesc.MipLevels = 1;
  92.         textureDesc.ArraySize = 1;
  93.         textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  94.         textureDesc.SampleDesc.Count = 1;
  95.         textureDesc.SampleDesc.Quality = 0;
  96.         textureDesc.Usage = D3D11_USAGE_DYNAMIC;
  97.         textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  98.         textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  99.         textureDesc.MiscFlags = 0;
  100.         char* data = (char *)malloc(256 * 256 * 4);
  101.         memset(data, 10, 256 * 256 * 4);
  102.         D3D11_SUBRESOURCE_DATA InitData;
  103.         ZeroMemory(&InitData, sizeof(D3D11_SUBRESOURCE_DATA));

  104.         InitData.pSysMem = data;
  105.         InitData.SysMemPitch = 4 * 256;
  106.         ComPtr<ID3D11Texture2D> textureBuf;
  107.         hr = m_d3dDevice->CreateTexture2D(&textureDesc, &InitData, &textureBuf);
  108.         /*        D3D11_SHADER_RESOURCE_VIEW_DESC viewdesc;
  109.         ZeroMemory(&viewdesc, sizeof(viewdesc));
  110.         viewdesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  111.         viewdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  112.         viewdesc.Texture2D.MipLevels = 1;
  113.         viewdesc.Texture2D.MostDetailedMip = 0;
  114.         ComPtr<ID3D11ShaderResourceView>  shader;
  115.         hr = m_d3dDevice->CreateShaderResourceView(textureBuf.Get(), &viewdesc, &shader);
  116.         */

  117.         D3D11_MAPPED_SUBRESOURCE  mapResource;

  118.         m_d3dContext->Map(textureBuf.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapResource);
  119.         mapResource.RowPitch = 256 * 4;
  120.         mapResource.DepthPitch = 0;
  121.         memcpy(mapResource.pData, data, 256 * 256 * 4);
  122.         m_d3dContext->Unmap(textureBuf.Get(), 0);

  123.        

  124.        
  125.         //        D3D11_TEXTURE2D_DESC backBufferDesc = { 0 };
  126.         //        backBuffer->GetDesc(&backBufferDesc);

  127.         D3D11_VIEWPORT viewport;
  128.         viewport.TopLeftX = 0.0f;
  129.         viewport.TopLeftY = 0.0f;
  130.         viewport.Width = static_cast<float>(textureDesc.Width);
  131.         viewport.Height = static_cast<float>(textureDesc.Height);
  132.         viewport.MinDepth = D3D11_MIN_DEPTH;
  133.         viewport.MaxDepth = D3D11_MAX_DEPTH;

  134.         m_d3dContext->RSSetViewports(1, &viewport);
  135.         m_window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  136.         m_d3dContext->OMSetRenderTargets(1, m_d3dRenderTargetView.GetAddressOf(), nullptr);
  137.         const float clearColor[4] = { 10.071f,20.04f,10.561f,1.0f };
  138.        
  139.         m_d3dContext->ClearRenderTargetView(m_d3dRenderTargetView.Get(), clearColor);

  140.         hr = m_swapChain->Present(1, 0);
复制代码
为了证明我是个菜鸟,我贴上一段代码

使用道具 举报

Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17

注册时间
2009-3-27
积分
4244
发表于 2017-8-5 06:30:06 |显示全部楼层
做一个2个三角的片,把数据Map到Texture上去然后贴到片上,最后用片画全屏

使用道具 举报

Rank: 4

注册时间
2017-8-3
积分
27
发表于 2017-8-7 10:33:07 |显示全部楼层
Phantom 发表于 2017-8-5 06:30
做一个2个三角的片,把数据Map到Texture上去然后贴到片上,最后用片画全屏

老哥,片的接口名是什么,map只要把像素数据memcpy上去吗?

使用道具 举报

Rank: 4

注册时间
2017-8-3
积分
27
发表于 2017-8-7 10:37:40 |显示全部楼层
Phantom 发表于 2017-8-5 06:30
做一个2个三角的片,把数据Map到Texture上去然后贴到片上,最后用片画全屏

片就是创建RenderTarget?

使用道具 举报

Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17

注册时间
2009-3-27
积分
4244
发表于 2017-8-8 00:22:14 |显示全部楼层
null 发表于 2017-8-7 10:33
老哥,片的接口名是什么,map只要把像素数据memcpy上去吗?

不是,你需要建连个三角形组成一个方形,然后把图帖到这个片上,然后在进行片的绘制上片的4个点重合到屏幕的4个角就完成显示了

使用道具 举报

Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17

注册时间
2009-3-27
积分
4244
发表于 2017-8-8 00:44:52 |显示全部楼层
重点资源包括:
1.组成一个方片的4个顶点,属性至少包括坐标和一套UV,CreateBuffer
2.一张Texture2D,更新图片数据,贴在片上,CreateTexture
3.一套用于显示的Shader,包括一个VS一个PS,这两个Shader由于需求只是显示出来所以很简单

使用道具 举报

最近看过此主题的会员

您需要登录后才可以回帖 登录 | 注册

‹‹
我的工具栏

关于我们|手机版|Archiver|开源计算机图形学社区(Open Source Computer Graphics Community) | OpenGPU Project | OpenGPU Forum (2007-2013)

GMT+8, 2017-10-22 09:10 , Processed in 0.072433 second(s), 11 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部