1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.2 (2010/10/02)
#include "Wm5GraphicsPCH.h"
#include "Wm5Dx9RenderTarget.h"
#include "Wm5Dx9Mapping.h"
#include "Wm5Dx9RendererData.h"
#include "Wm5Dx9Texture2D.h"
#include "Wm5Renderer.h"
using namespace Wm5;
//----------------------------------------------------------------------------
PdrRenderTarget::PdrRenderTarget (Renderer* renderer,
const RenderTarget* renderTarget)
:
mSaveColorSurface(0),
mSaveDepthStencilSurface(0)
{
HRESULT hr;
WM5_UNUSED(hr);
mNumTargets = renderTarget->GetNumTargets();
assertion(mNumTargets >= 1,
"Number of render targets must be at least one.\n");
mWidth = renderTarget->GetWidth();
mHeight = renderTarget->GetHeight();
mFormat = renderTarget->GetFormat();
mHasDepthStencil = renderTarget->HasDepthStencil();
mColorTextures = new1<IDirect3DTexture9*>(mNumTargets);
mColorSurfaces = new1<IDirect3DSurface9*>(mNumTargets);
for (int i = 0; i < mNumTargets; ++i)
{
Texture2D* colorTexture = renderTarget->GetColorTexture(i);
assertion(!renderer->InTexture2DMap(colorTexture),
"Texture should not yet exist.\n");
PdrTexture2D* dxColorTexture = new0 PdrTexture2D(renderer, true,
colorTexture, renderTarget->HasMipmaps());
renderer->InsertInTexture2DMap(colorTexture, dxColorTexture);
mColorTextures[i] = dxColorTexture->mTexture;
mColorTextures[i]->AddRef();
hr = mColorTextures[i]->GetSurfaceLevel(0, &mColorSurfaces[i]);
assertion(hr == D3D_OK, "Failed to get rt %d color surface: %s\n",
i, DXGetErrorString(hr));
}
if (mHasDepthStencil)
{
Texture2D* depthStencilTexture;
PdrTexture2D* dxDepthStencilTexture;
depthStencilTexture = renderTarget->GetDepthStencilTexture();
dxDepthStencilTexture = new0 PdrTexture2D(renderer, false,
depthStencilTexture, false);
renderer->InsertInTexture2DMap(depthStencilTexture,
dxDepthStencilTexture);
mDepthStencilTexture = dxDepthStencilTexture->mTexture;
mDepthStencilTexture->AddRef();
hr = mDepthStencilTexture->GetSurfaceLevel(0, &mDepthStencilSurface);
assertion(hr == D3D_OK,
"Failed to get rt depthstencil surface: %s\n",
DXGetErrorString(hr));
}
else
{
mDepthStencilTexture = 0;
mDepthStencilSurface = 0;
}
}
//----------------------------------------------------------------------------
PdrRenderTarget::~PdrRenderTarget ()
{
int i;
for (i = 0; i < mNumTargets; ++i)
{
mColorSurfaces[i]->Release();
mColorTextures[i]->Release();
}
delete1(mColorTextures);
delete1(mColorSurfaces);
if (mHasDepthStencil)
{
if (mDepthStencilSurface)
{
mDepthStencilSurface->Release();
}
if (mDepthStencilTexture)
{
mDepthStencilTexture->Release();
}
}
}
//----------------------------------------------------------------------------
void PdrRenderTarget::Enable (Renderer* renderer)
{
IDirect3DDevice9* device = renderer->mData->mDevice;
HRESULT hr;
WM5_UNUSED(hr);
hr = device->GetRenderTarget(0, &mSaveColorSurface);
assertion(hr == D3D_OK, "Failed to get old rt 0 color surface: %s\n",
DXGetErrorString(hr));
// The viewport is automatically set by this call.
hr = device->SetRenderTarget(0, mColorSurfaces[0]);
assertion(hr == D3D_OK, "Failed to set new rt 0 color surface: %s\n",
DXGetErrorString(hr));
for (int i = 1; i < mNumTargets; ++i)
{
hr = device->SetRenderTarget((DWORD)i, mColorSurfaces[i]);
assertion(hr == D3D_OK, "Failed to set new rt %d color surface: %s\n",
i, DXGetErrorString(hr));
}
if (mHasDepthStencil)
{
hr = device->GetDepthStencilSurface(&mSaveDepthStencilSurface);
assertion(hr == D3D_OK,
"Failed to get old rt depthstencil surface: %s\n",
DXGetErrorString(hr));
hr = device->SetDepthStencilSurface(mDepthStencilSurface);
assertion(hr == D3D_OK,
"Failed to set new rt depthstencil surface: %s\n",
DXGetErrorString(hr));
}
}
//----------------------------------------------------------------------------
void PdrRenderTarget::Disable (Renderer* renderer)
{
IDirect3DDevice9* device = renderer->mData->mDevice;
HRESULT hr;
WM5_UNUSED(hr);
// The viewport is automatically restored by this call.
hr = device->SetRenderTarget(0, mSaveColorSurface);
assertion(hr == D3D_OK, "Failed to set old rt 0 color surface: %s\n",
DXGetErrorString(hr));
mSaveColorSurface->Release();
for (int i = 1; i < mNumTargets; ++i)
{
// The viewport is automatically restored by this call.
hr = device->SetRenderTarget((DWORD)i, 0);
assertion(hr == D3D_OK,
"Failed to set old rt %d color surface: %s\n", i,
DXGetErrorString(hr));
}
if (mHasDepthStencil)
{
hr = device->SetDepthStencilSurface(mSaveDepthStencilSurface);
assertion(hr == D3D_OK,
"Failed to set old rt 0 depthstencil surface: %s\n",
DXGetErrorString(hr));
mSaveDepthStencilSurface->Release();
}
}
//----------------------------------------------------------------------------
void PdrRenderTarget::ReadColor (int i, Renderer* renderer,
Texture2D*& texture)
{
if (i < 0 || i >= mNumTargets)
{
assertion(false, "Invalid target index.\n");
return;
}
IDirect3DDevice9* device = renderer->mData->mDevice;
HRESULT hr;
WM5_UNUSED(hr);
// Enable the input render target surface.
if (i == 0)
{
hr = device->GetRenderTarget(0, &mSaveColorSurface);
assertion(hr == D3D_OK, "Failed to get old rt 0 color surface: %s\n",
DXGetErrorString(hr));
}
hr = device->SetRenderTarget((DWORD)i, mColorSurfaces[i]);
assertion(hr == D3D_OK,
"Failed to set new rt %d color surface: %s\n", i,
DXGetErrorString(hr));
// Make a duplicate in system memory.
IDirect3DTexture9* copyTexture = 0;
hr = D3DXCreateTexture
(
device,
(UINT)mWidth,
(UINT)mHeight,
0,
0,
gDX9TextureFormat[mFormat],
D3DPOOL_SYSTEMMEM,
©Texture
);
assertion(hr == D3D_OK,
"Failed to create copy texture: %s\n",
DXGetErrorString(hr));
// Get the surface associated with the copy.
IDirect3DSurface9* copySurface = 0;
hr = copyTexture->GetSurfaceLevel(0, ©Surface);
assertion(hr == D3D_OK,
"Failed to get surface level for copy texture: %s\n",
DXGetErrorString(hr));
// Copy the render target surface.
hr = device->GetRenderTargetData(mColorSurfaces[i], copySurface);
assertion(hr == D3D_OK,
"Failed to copy the rt %d surface: %s\n", i,
DXGetErrorString(hr));
// Get the data to write to disk.
D3DLOCKED_RECT rect;
hr = copySurface->LockRect(&rect, 0, 0);
assertion(hr == D3D_OK,
"Failed to lock copy surface: %s\n",
DXGetErrorString(hr));
if (texture)
{
if (texture->GetFormat() != mFormat ||
texture->GetWidth() != (int)mWidth ||
texture->GetHeight() != (int)mHeight)
{
assertion(false, "Incompatible texture.\n");
delete0(texture);
texture = new0 Texture2D(mFormat, mWidth, mHeight, 1);
}
}
else
{
texture = new0 Texture2D(mFormat, mWidth, mHeight, 1);
}
memcpy(texture->GetData(0), rect.pBits, texture->GetNumLevelBytes(0));
hr = copySurface->UnlockRect();
assertion(hr == D3D_OK,
"Failed to unlock copy surface: %s\n",
DXGetErrorString(hr));
copySurface->Release();
copyTexture->Release();
// Restore the previous render target surface.
if (i == 0)
{
hr = device->SetRenderTarget(0, mSaveColorSurface);
assertion(hr == D3D_OK,
"Failed to set old rt 0 color surface: %s\n",
DXGetErrorString(hr));
mSaveColorSurface->Release();
}
}
//----------------------------------------------------------------------------
|