D3D 直接操作后台缓存绘制

D3D设备Reset之前需要将所有 D3DPOOL_DEFAULT 内的资源释放,否则Reset会失败。 
改用D3DPOOL_MANAGED的可以不用手动释放。

//创建一个渲染表面 
IDirect3DSurface9* pSurface; 
pDevice->CreateOffscreenPlainSurface(width,height,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&pSurface,0); 
//写入表面void SetPixel(int x,int y,DWORD c)
{  
    D3DLOCKED_RECT rc;  
    memset(&rc,0,sizeof(rc));  
    pSurface->LockRect(&rc,NULL,D3DLOCK_DISCARD); 
  
    //Pitch表示一个表面上一行像素的长度(已经乘以DWORD宽度)  
    //除以4可以获得准确的一行像素个数  
    int linePixel = rc.Pitch/4;  
    DWORD* pBits = (DWORD*)rc.pBits;   
    int index = y*linePiexl + x;  
    //小心越界  
    pBits[index] = c;  
    pSurface->UnlockRect(); 
} 
//获取D3D默认交换链中的后台缓存 
IDirect3DSurface9* backBuffer = 0; 
pDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&backBuffer); 
//使用自定义渲染表面填充后台缓存 
pDevice->StretchRect(pSurface,0,backBuffer,0,D3DTEXF_NONE); 
//GetBackBuffer所得的缓存需要被释放 
backBuffer->Release(); 
//将交换链中的后台缓存显示 
m_pDevice->Present(0,0,0,0);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Qt 可以使用 Direct3D(D3D)来绘制图像到窗口的离屏表面。这个过程中,Qt 使用 D3D 渲染引擎来创建一个可渲染的表面,然后将这个表面绑定到一个纹理上,最后将这个纹理绘制到窗口上。 实现这个过程需要使用到 Qt 的 QQuickRenderControl 类,该类提供了一个接口可以在不使用 Qt Quick 的情况下使用 Qt 的渲染引擎。 具体实现方法如下: 1. 创建一个 QQuickRenderControl 对象,并将其设置为渲染目标。 2. 创建一个 QQuickWindow 对象,并将其设置为 QQuickRenderControl 的视口。 3. 创建一个 QSGRenderNode 对象,并将其设置为 QQuickWindow 的根节点。 4. 在 QSGRenderNode 的 render() 函数中实现绘制逻辑,将图像绘制到离屏表面上。 5. 将离屏表面绑定到一个纹理上,并将纹理绘制到窗口上。 以下是示例代码: ``` // 创建 QQuickRenderControl 对象 QQuickRenderControl* renderControl = new QQuickRenderControl(); // 创建 QQuickWindow 对象 QQuickWindow* window = new QQuickWindow(renderControl); // 设置视口 renderControl->initialize(window); // 创建 QSGRenderNode 对象 class RenderNode : public QSGRenderNode { public: RenderNode(QQuickWindow* window) : m_window(window) {} // 实现绘制逻辑 virtual void render(const QSGRenderContext& context) override { // 创建离屏表面 IDirect3DSurface9* surface = nullptr; m_window->d3dContext()->CreateOffscreenPlainSurface( m_window->width(), m_window->height(), D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, nullptr ); // 将图像绘制到离屏表面上 IDirect3DDevice9* device = nullptr; m_window->d3dContext()->GetDevice(&device); device->BeginScene(); // TODO: 绘制逻辑 device->EndScene(); device->Release(); // 将离屏表面绑定到纹理上 IDirect3DTexture9* texture = nullptr; m_window->d3dContext()->CreateTexture( m_window->width(), m_window->height(), 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, nullptr ); IDirect3DSurface9* textureSurface = nullptr; texture->GetSurfaceLevel(0, &textureSurface); m_window->d3dContext()->StretchRect(surface, nullptr, textureSurface, nullptr, D3DTEXF_NONE); textureSurface->Release(); surface->Release(); // 将纹理绘制到窗口上 QSGSimpleTextureNode* textureNode = static_cast<QSGSimpleTextureNode*>(context.renderState()->textureState()->get(texture, QSGTexture::RGBA8)); textureNode->setRect(0, 0, m_window->width(), m_window->height()); textureNode->setFiltering(QSGTexture::Linear); textureNode->setMipmapFiltering(QSGTexture::Linear); appendChildNode(textureNode); } private: QQuickWindow* m_window; }; RenderNode* rootNode = new RenderNode(window); window->setRenderTarget(rootNode); // 显示窗口 window->show(); ``` 需要注意的是,这个示例代码中使用的是 Direct3D 9,如果你的系统使用的是 Direct3D 11 或者 12,你需要将示例代码中的 IDirect3DDevice9、IDirect3DSurface9、IDirect3DTexture9 替换为对应的接口。同时,如果你使用的是 Qt 5.14 或者更早的版本,你需要使用 QQuickWindow::d3d12Context() 或 QQuickWindow::d3d11Context() 获取渲染上下文。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ww506772362

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值