daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Blit.cpp: Surface copy utility class. |
| 8 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 9 | #include "libGLESv2/Blit.h" |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 10 | |
| 11 | #include <d3dx9.h> |
| 12 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 13 | #include "common/debug.h" |
| 14 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 15 | #include "libGLESv2/main.h" |
| 16 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 17 | namespace |
| 18 | { |
| 19 | // Standard Vertex Shader |
| 20 | // Input 0 is the homogenous position. |
| 21 | // Outputs the homogenous position as-is. |
| 22 | // Outputs a tex coord with (0,0) in the upper-left corner of the screen and (1,1) in the bottom right. |
| 23 | // C0.X must be negative half-pixel width, C0.Y must be half-pixel height. C0.ZW must be 0. |
| 24 | const char standardvs[] = |
| 25 | "struct VS_OUTPUT\n" |
| 26 | "{\n" |
| 27 | " float4 position : POSITION;\n" |
| 28 | " float4 texcoord : TEXCOORD0;\n" |
| 29 | "};\n" |
| 30 | "\n" |
| 31 | "uniform float4 halfPixelSize : c0;\n" |
| 32 | "\n" |
| 33 | "VS_OUTPUT main(in float4 position : POSITION)\n" |
| 34 | "{\n" |
| 35 | " VS_OUTPUT Out;\n" |
| 36 | "\n" |
| 37 | " Out.position = position + halfPixelSize;\n" |
| 38 | " Out.texcoord = position * float4(0.5, -0.5, 1.0, 1.0) + float4(0.5, 0.5, 0, 0);\n" |
| 39 | "\n" |
| 40 | " return Out;\n" |
| 41 | "}\n"; |
| 42 | |
| 43 | // Flip Y Vertex Shader |
| 44 | // Input 0 is the homogenous position. |
| 45 | // Outputs the homogenous position as-is. |
| 46 | // Outputs a tex coord with (0,1) in the upper-left corner of the screen and (1,0) in the bottom right. |
| 47 | // C0.XY must be the half-pixel width and height. C0.ZW must be 0. |
| 48 | const char flipyvs[] = |
| 49 | "struct VS_OUTPUT\n" |
| 50 | "{\n" |
| 51 | " float4 position : POSITION;\n" |
| 52 | " float4 texcoord : TEXCOORD0;\n" |
| 53 | "};\n" |
| 54 | "\n" |
| 55 | "uniform float4 halfPixelSize : c0;\n" |
| 56 | "\n" |
| 57 | "VS_OUTPUT main(in float4 position : POSITION)\n" |
| 58 | "{\n" |
| 59 | " VS_OUTPUT Out;\n" |
| 60 | "\n" |
| 61 | " Out.position = position + halfPixelSize;\n" |
| 62 | " Out.texcoord = position * float4(0.5, 0.5, 1.0, 1.0) + float4(0.5, 0.5, 0, 0);\n" |
| 63 | "\n" |
| 64 | " return Out;\n" |
| 65 | "}\n"; |
| 66 | |
| 67 | // Passthrough Pixel Shader |
| 68 | // Outputs texture 0 sampled at texcoord 0. |
| 69 | const char passthroughps[] = |
| 70 | "sampler2D tex : s0;\n" |
| 71 | "\n" |
| 72 | "float4 main(float4 texcoord : TEXCOORD0) : COLOR\n" |
| 73 | "{\n" |
| 74 | " return tex2D(tex, texcoord.xy);\n" |
| 75 | "}\n"; |
| 76 | |
| 77 | // Luminance Conversion Pixel Shader |
| 78 | // Outputs sample(tex0, tc0).rrra. |
| 79 | // For LA output (pass A) set C0.X = 1, C0.Y = 0. |
| 80 | // For L output (A = 1) set C0.X = 0, C0.Y = 1. |
| 81 | const char luminanceps[] = |
| 82 | "sampler2D tex : s0;\n" |
| 83 | "\n" |
| 84 | "uniform float4 mode : c0;\n" |
| 85 | "\n" |
| 86 | "float4 main(float4 texcoord : TEXCOORD0) : COLOR\n" |
| 87 | "{\n" |
| 88 | " float4 tmp = tex2D(tex, texcoord.xy);\n" |
| 89 | " tmp.w = tmp.w * mode.x + mode.y;\n" |
| 90 | " return tmp.xxxw;\n" |
| 91 | "}\n"; |
| 92 | |
| 93 | // RGB/A Component Mask Pixel Shader |
| 94 | // Outputs sample(tex0, tc0) with options to force RGB = 0 and/or A = 1. |
| 95 | // To force RGB = 0, set C0.X = 0, otherwise C0.X = 1. |
| 96 | // To force A = 1, set C0.Z = 0, C0.W = 1, otherwise C0.Z = 1, C0.W = 0. |
| 97 | const char componentmaskps[] = |
| 98 | "sampler2D tex : s0;\n" |
| 99 | "\n" |
| 100 | "uniform float4 mode : c0;\n" |
| 101 | "\n" |
| 102 | "float4 main(float4 texcoord : TEXCOORD0) : COLOR\n" |
| 103 | "{\n" |
| 104 | " float4 tmp = tex2D(tex, texcoord.xy);\n" |
| 105 | " tmp.xyz = tmp.xyz * mode.x;\n" |
| 106 | " tmp.w = tmp.w * mode.z + mode.w;\n" |
| 107 | " return tmp;\n" |
| 108 | "}\n"; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | namespace gl |
| 113 | { |
| 114 | |
| 115 | const char * const Blit::mShaderSource[] = |
| 116 | { |
| 117 | standardvs, |
| 118 | flipyvs, |
| 119 | passthroughps, |
| 120 | luminanceps, |
| 121 | componentmaskps |
| 122 | }; |
| 123 | |
| 124 | Blit::Blit(Context *context) |
daniel@transgaming.com | 1ddd1dd | 2010-05-11 02:29:34 +0000 | [diff] [blame] | 125 | : mContext(context), mQuadVertexBuffer(NULL), mQuadVertexDeclaration(NULL), mSavedRenderTarget(NULL), mSavedDepthStencil(NULL), mSavedStateBlock(NULL) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 126 | { |
| 127 | initGeometry(); |
| 128 | memset(mCompiledShaders, 0, sizeof(mCompiledShaders)); |
| 129 | } |
| 130 | |
| 131 | Blit::~Blit() |
| 132 | { |
daniel@transgaming.com | 1ddd1dd | 2010-05-11 02:29:34 +0000 | [diff] [blame] | 133 | if (mSavedStateBlock) mSavedStateBlock->Release(); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 134 | if (mQuadVertexBuffer) mQuadVertexBuffer->Release(); |
| 135 | if (mQuadVertexDeclaration) mQuadVertexDeclaration->Release(); |
| 136 | |
| 137 | for (int i = 0; i < SHADER_COUNT; i++) |
| 138 | { |
| 139 | if (mCompiledShaders[i]) |
| 140 | { |
| 141 | mCompiledShaders[i]->Release(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void Blit::initGeometry() |
| 147 | { |
| 148 | static const float quad[] = |
| 149 | { |
| 150 | -1, -1, |
| 151 | -1, 1, |
| 152 | 1, -1, |
| 153 | 1, 1 |
| 154 | }; |
| 155 | |
| 156 | IDirect3DDevice9 *device = getDevice(); |
| 157 | |
daniel@transgaming.com | aa61460 | 2011-04-28 16:20:58 +0000 | [diff] [blame] | 158 | HRESULT result = device->CreateVertexBuffer(sizeof(quad), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &mQuadVertexBuffer, NULL); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 159 | |
daniel@transgaming.com | aa61460 | 2011-04-28 16:20:58 +0000 | [diff] [blame] | 160 | if (FAILED(result)) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 161 | { |
daniel@transgaming.com | aa61460 | 2011-04-28 16:20:58 +0000 | [diff] [blame] | 162 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 163 | return error(GL_OUT_OF_MEMORY); |
| 164 | } |
| 165 | |
daniel@transgaming.com | aa61460 | 2011-04-28 16:20:58 +0000 | [diff] [blame] | 166 | void *lockPtr = NULL; |
| 167 | result = mQuadVertexBuffer->Lock(0, 0, &lockPtr, 0); |
| 168 | |
| 169 | if (FAILED(result) || lockPtr == NULL) |
| 170 | { |
| 171 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 172 | return error(GL_OUT_OF_MEMORY); |
| 173 | } |
| 174 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 175 | memcpy(lockPtr, quad, sizeof(quad)); |
| 176 | mQuadVertexBuffer->Unlock(); |
| 177 | |
| 178 | static const D3DVERTEXELEMENT9 elements[] = |
| 179 | { |
| 180 | { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, |
| 181 | D3DDECL_END() |
| 182 | }; |
| 183 | |
daniel@transgaming.com | aa61460 | 2011-04-28 16:20:58 +0000 | [diff] [blame] | 184 | result = device->CreateVertexDeclaration(elements, &mQuadVertexDeclaration); |
| 185 | |
| 186 | if (FAILED(result)) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 187 | { |
daniel@transgaming.com | aa61460 | 2011-04-28 16:20:58 +0000 | [diff] [blame] | 188 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 189 | return error(GL_OUT_OF_MEMORY); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | template <class D3DShaderType> |
| 194 | bool Blit::setShader(ShaderId source, const char *profile, |
| 195 | HRESULT (WINAPI IDirect3DDevice9::*createShader)(const DWORD *, D3DShaderType**), |
| 196 | HRESULT (WINAPI IDirect3DDevice9::*setShader)(D3DShaderType*)) |
| 197 | { |
| 198 | IDirect3DDevice9 *device = getDevice(); |
| 199 | |
| 200 | D3DShaderType *shader; |
| 201 | |
| 202 | if (mCompiledShaders[source] != NULL) |
| 203 | { |
| 204 | shader = static_cast<D3DShaderType*>(mCompiledShaders[source]); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | ID3DXBuffer *shaderCode; |
| 209 | HRESULT hr = D3DXCompileShader(mShaderSource[source], strlen(mShaderSource[source]), NULL, NULL, "main", profile, 0, &shaderCode, NULL, NULL); |
| 210 | |
| 211 | if (FAILED(hr)) |
| 212 | { |
| 213 | ERR("Failed to compile %s shader for blit operation %d, error 0x%08X.", profile, (int)source, hr); |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | hr = (device->*createShader)(static_cast<const DWORD*>(shaderCode->GetBufferPointer()), &shader); |
| 218 | if (FAILED(hr)) |
| 219 | { |
| 220 | shaderCode->Release(); |
| 221 | ERR("Failed to create %s shader for blit operation %d, error 0x%08X.", profile, (int)source, hr); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | shaderCode->Release(); |
| 226 | |
| 227 | mCompiledShaders[source] = shader; |
| 228 | } |
| 229 | |
| 230 | HRESULT hr = (device->*setShader)(shader); |
| 231 | |
| 232 | if (FAILED(hr)) |
| 233 | { |
| 234 | ERR("Failed to set %s shader for blit operation %d, error 0x%08X.", profile, (int)source, hr); |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | bool Blit::setVertexShader(ShaderId shader) |
| 242 | { |
daniel@transgaming.com | be5a086 | 2010-07-28 19:20:37 +0000 | [diff] [blame] | 243 | return setShader<IDirect3DVertexShader9>(shader, mContext->supportsShaderModel3() ? "vs_3_0" : "vs_2_0", &IDirect3DDevice9::CreateVertexShader, &IDirect3DDevice9::SetVertexShader); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | bool Blit::setPixelShader(ShaderId shader) |
| 247 | { |
daniel@transgaming.com | be5a086 | 2010-07-28 19:20:37 +0000 | [diff] [blame] | 248 | return setShader<IDirect3DPixelShader9>(shader, mContext->supportsShaderModel3() ? "ps_3_0" : "ps_2_0", &IDirect3DDevice9::CreatePixelShader, &IDirect3DDevice9::SetPixelShader); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 249 | } |
| 250 | |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 251 | RECT Blit::getSurfaceRect(IDirect3DSurface9 *surface) const |
| 252 | { |
| 253 | D3DSURFACE_DESC desc; |
| 254 | surface->GetDesc(&desc); |
| 255 | |
| 256 | RECT rect; |
| 257 | rect.left = 0; |
| 258 | rect.top = 0; |
| 259 | rect.right = desc.Width; |
| 260 | rect.bottom = desc.Height; |
| 261 | |
| 262 | return rect; |
| 263 | } |
| 264 | |
| 265 | bool Blit::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) |
| 266 | { |
| 267 | IDirect3DTexture9 *texture = copySurfaceToTexture(source, getSurfaceRect(source)); |
| 268 | if (!texture) |
| 269 | { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | IDirect3DDevice9 *device = getDevice(); |
daniel@transgaming.com | 1ddd1dd | 2010-05-11 02:29:34 +0000 | [diff] [blame] | 274 | |
| 275 | saveState(); |
| 276 | |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 277 | device->SetTexture(0, texture); |
| 278 | device->SetRenderTarget(0, dest); |
| 279 | |
| 280 | setVertexShader(SHADER_VS_STANDARD); |
| 281 | setPixelShader(SHADER_PS_PASSTHROUGH); |
| 282 | |
| 283 | setCommonBlitState(); |
| 284 | device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); |
| 285 | device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); |
| 286 | |
| 287 | setViewport(getSurfaceRect(dest), 0, 0); |
| 288 | |
| 289 | render(); |
| 290 | |
| 291 | texture->Release(); |
| 292 | |
daniel@transgaming.com | 1ddd1dd | 2010-05-11 02:29:34 +0000 | [diff] [blame] | 293 | restoreState(); |
| 294 | |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | |
daniel@transgaming.com | eef864a | 2011-04-22 11:33:27 +0000 | [diff] [blame] | 298 | bool Blit::copy(IDirect3DSurface9 *source, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest) |
| 299 | { |
| 300 | IDirect3DDevice9 *device = getDevice(); |
| 301 | |
| 302 | D3DSURFACE_DESC sourceDesc; |
| 303 | D3DSURFACE_DESC destDesc; |
| 304 | source->GetDesc(&sourceDesc); |
| 305 | dest->GetDesc(&destDesc); |
| 306 | |
| 307 | if (sourceDesc.Format == destDesc.Format && destDesc.Usage & D3DUSAGE_RENDERTARGET) // Can use StretchRect |
| 308 | { |
| 309 | RECT destRect = {xoffset, yoffset, xoffset + (sourceRect.right - sourceRect.left), yoffset + (sourceRect.bottom - sourceRect.top)}; |
| 310 | HRESULT result = device->StretchRect(source, &sourceRect, dest, &destRect, D3DTEXF_POINT); |
| 311 | |
| 312 | if (FAILED(result)) |
| 313 | { |
| 314 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 315 | return error(GL_OUT_OF_MEMORY, false); |
| 316 | } |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | return formatConvert(source, sourceRect, destFormat, xoffset, yoffset, dest); |
| 321 | } |
| 322 | |
| 323 | return true; |
| 324 | } |
| 325 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 326 | bool Blit::formatConvert(IDirect3DSurface9 *source, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest) |
| 327 | { |
| 328 | IDirect3DTexture9 *texture = copySurfaceToTexture(source, sourceRect); |
| 329 | if (!texture) |
| 330 | { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | IDirect3DDevice9 *device = getDevice(); |
| 335 | |
daniel@transgaming.com | 1ddd1dd | 2010-05-11 02:29:34 +0000 | [diff] [blame] | 336 | saveState(); |
| 337 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 338 | device->SetTexture(0, texture); |
| 339 | device->SetRenderTarget(0, dest); |
| 340 | |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 341 | setViewport(sourceRect, xoffset, yoffset); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 342 | |
| 343 | setCommonBlitState(); |
| 344 | if (setFormatConvertShaders(destFormat)) |
| 345 | { |
| 346 | render(); |
| 347 | } |
| 348 | |
| 349 | texture->Release(); |
| 350 | |
daniel@transgaming.com | 1ddd1dd | 2010-05-11 02:29:34 +0000 | [diff] [blame] | 351 | restoreState(); |
| 352 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
| 356 | bool Blit::setFormatConvertShaders(GLenum destFormat) |
| 357 | { |
| 358 | bool okay = setVertexShader(SHADER_VS_STANDARD); |
| 359 | |
| 360 | switch (destFormat) |
| 361 | { |
| 362 | default: UNREACHABLE(); |
| 363 | case GL_RGBA: |
daniel@transgaming.com | a9198d9 | 2010-08-08 04:49:56 +0000 | [diff] [blame] | 364 | case GL_BGRA_EXT: |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 365 | case GL_RGB: |
| 366 | case GL_ALPHA: |
| 367 | okay = okay && setPixelShader(SHADER_PS_COMPONENTMASK); |
| 368 | break; |
| 369 | |
| 370 | case GL_LUMINANCE: |
| 371 | case GL_LUMINANCE_ALPHA: |
| 372 | okay = okay && setPixelShader(SHADER_PS_LUMINANCE); |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | if (!okay) |
| 377 | { |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | enum { X = 0, Y = 1, Z = 2, W = 3 }; |
| 382 | |
| 383 | // The meaning of this constant depends on the shader that was selected. |
| 384 | // See the shader assembly code above for details. |
| 385 | float psConst0[4] = { 0, 0, 0, 0 }; |
| 386 | |
| 387 | switch (destFormat) |
| 388 | { |
| 389 | default: UNREACHABLE(); |
| 390 | case GL_RGBA: |
daniel@transgaming.com | a9198d9 | 2010-08-08 04:49:56 +0000 | [diff] [blame] | 391 | case GL_BGRA_EXT: |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 392 | psConst0[X] = 1; |
| 393 | psConst0[Z] = 1; |
| 394 | break; |
| 395 | |
| 396 | case GL_RGB: |
| 397 | psConst0[X] = 1; |
| 398 | psConst0[W] = 1; |
| 399 | break; |
| 400 | |
| 401 | case GL_ALPHA: |
| 402 | psConst0[Z] = 1; |
| 403 | break; |
| 404 | |
| 405 | case GL_LUMINANCE: |
| 406 | psConst0[Y] = 1; |
| 407 | break; |
| 408 | |
| 409 | case GL_LUMINANCE_ALPHA: |
| 410 | psConst0[X] = 1; |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | getDevice()->SetPixelShaderConstantF(0, psConst0, 1); |
| 415 | |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | IDirect3DTexture9 *Blit::copySurfaceToTexture(IDirect3DSurface9 *surface, const RECT &sourceRect) |
| 420 | { |
daniel@transgaming.com | d36c6a0 | 2010-08-31 12:15:09 +0000 | [diff] [blame] | 421 | if (!surface) |
| 422 | { |
| 423 | return NULL; |
| 424 | } |
| 425 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 426 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 427 | IDirect3DDevice9 *device = getDevice(); |
| 428 | |
| 429 | D3DSURFACE_DESC sourceDesc; |
| 430 | surface->GetDesc(&sourceDesc); |
| 431 | |
| 432 | // Copy the render target into a texture |
| 433 | IDirect3DTexture9 *texture; |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 434 | HRESULT result = device->CreateTexture(sourceRect.right - sourceRect.left, sourceRect.bottom - sourceRect.top, 1, D3DUSAGE_RENDERTARGET, sourceDesc.Format, D3DPOOL_DEFAULT, &texture, NULL); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 435 | |
| 436 | if (FAILED(result)) |
| 437 | { |
| 438 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 439 | return error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL); |
| 440 | } |
| 441 | |
| 442 | IDirect3DSurface9 *textureSurface; |
| 443 | result = texture->GetSurfaceLevel(0, &textureSurface); |
| 444 | |
| 445 | if (FAILED(result)) |
| 446 | { |
| 447 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 448 | texture->Release(); |
| 449 | return error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL); |
| 450 | } |
| 451 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 452 | display->endScene(); |
daniel@transgaming.com | 4c5142c | 2010-10-15 17:58:27 +0000 | [diff] [blame] | 453 | result = device->StretchRect(surface, &sourceRect, textureSurface, NULL, D3DTEXF_NONE); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 454 | |
| 455 | textureSurface->Release(); |
| 456 | |
| 457 | if (FAILED(result)) |
| 458 | { |
| 459 | ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); |
| 460 | texture->Release(); |
| 461 | return error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL); |
| 462 | } |
| 463 | |
| 464 | return texture; |
| 465 | } |
| 466 | |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 467 | void Blit::setViewport(const RECT &sourceRect, GLint xoffset, GLint yoffset) |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 468 | { |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 469 | IDirect3DDevice9 *device = getDevice(); |
| 470 | |
| 471 | D3DVIEWPORT9 vp; |
| 472 | vp.X = xoffset; |
| 473 | vp.Y = yoffset; |
| 474 | vp.Width = sourceRect.right - sourceRect.left; |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 475 | vp.Height = sourceRect.bottom - sourceRect.top; |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 476 | vp.MinZ = 0.0f; |
| 477 | vp.MaxZ = 1.0f; |
| 478 | device->SetViewport(&vp); |
| 479 | |
| 480 | float halfPixelAdjust[4] = { -1.0f/vp.Width, 1.0f/vp.Height, 0, 0 }; |
| 481 | device->SetVertexShaderConstantF(0, halfPixelAdjust, 1); |
| 482 | } |
| 483 | |
| 484 | void Blit::setCommonBlitState() |
| 485 | { |
| 486 | IDirect3DDevice9 *device = getDevice(); |
| 487 | |
| 488 | device->SetDepthStencilSurface(NULL); |
| 489 | |
| 490 | device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 491 | device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 492 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); |
| 493 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 494 | device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); |
| 495 | device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); |
| 496 | device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); |
| 497 | device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); |
| 498 | |
| 499 | device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); |
| 500 | device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); |
| 501 | device->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, FALSE); |
daniel@transgaming.com | 8fd99e2 | 2010-04-20 18:52:00 +0000 | [diff] [blame] | 502 | device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); |
| 503 | device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 504 | |
daniel@transgaming.com | 74d760b | 2010-11-03 12:27:18 +0000 | [diff] [blame] | 505 | RECT scissorRect = {0}; // Scissoring is disabled for flipping, but we need this to capture and restore the old rectangle |
| 506 | device->SetScissorRect(&scissorRect); |
daniel@transgaming.com | 8ca9c6e | 2012-01-27 15:38:54 +0000 | [diff] [blame^] | 507 | |
| 508 | for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 509 | { |
| 510 | device->SetStreamSourceFreq(i, 1); |
| 511 | } |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | void Blit::render() |
| 515 | { |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 516 | egl::Display *display = getDisplay(); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 517 | IDirect3DDevice9 *device = getDevice(); |
| 518 | |
| 519 | HRESULT hr = device->SetStreamSource(0, mQuadVertexBuffer, 0, 2 * sizeof(float)); |
| 520 | hr = device->SetVertexDeclaration(mQuadVertexDeclaration); |
| 521 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 522 | display->startScene(); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 523 | hr = device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 524 | } |
| 525 | |
daniel@transgaming.com | 1ddd1dd | 2010-05-11 02:29:34 +0000 | [diff] [blame] | 526 | void Blit::saveState() |
| 527 | { |
| 528 | IDirect3DDevice9 *device = getDevice(); |
| 529 | |
| 530 | HRESULT hr; |
| 531 | |
| 532 | device->GetDepthStencilSurface(&mSavedDepthStencil); |
| 533 | device->GetRenderTarget(0, &mSavedRenderTarget); |
| 534 | |
| 535 | if (mSavedStateBlock == NULL) |
| 536 | { |
| 537 | hr = device->BeginStateBlock(); |
| 538 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 539 | |
| 540 | setCommonBlitState(); |
| 541 | |
| 542 | static const float dummyConst[4] = { 0, 0, 0, 0 }; |
| 543 | |
| 544 | device->SetVertexShader(NULL); |
| 545 | device->SetVertexShaderConstantF(0, dummyConst, 1); |
| 546 | device->SetPixelShader(NULL); |
| 547 | device->SetPixelShaderConstantF(0, dummyConst, 1); |
| 548 | |
| 549 | D3DVIEWPORT9 dummyVp; |
| 550 | dummyVp.X = 0; |
| 551 | dummyVp.Y = 0; |
| 552 | dummyVp.Width = 1; |
| 553 | dummyVp.Height = 1; |
| 554 | dummyVp.MinZ = 0; |
| 555 | dummyVp.MaxZ = 1; |
| 556 | |
| 557 | device->SetViewport(&dummyVp); |
| 558 | |
| 559 | device->SetTexture(0, NULL); |
| 560 | |
| 561 | device->SetStreamSource(0, mQuadVertexBuffer, 0, 0); |
| 562 | |
| 563 | device->SetVertexDeclaration(mQuadVertexDeclaration); |
| 564 | |
| 565 | hr = device->EndStateBlock(&mSavedStateBlock); |
| 566 | ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); |
| 567 | } |
| 568 | |
| 569 | ASSERT(mSavedStateBlock != NULL); |
| 570 | |
| 571 | if (mSavedStateBlock != NULL) |
| 572 | { |
| 573 | hr = mSavedStateBlock->Capture(); |
| 574 | ASSERT(SUCCEEDED(hr)); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | void Blit::restoreState() |
| 579 | { |
| 580 | IDirect3DDevice9 *device = getDevice(); |
| 581 | |
| 582 | device->SetDepthStencilSurface(mSavedDepthStencil); |
| 583 | if (mSavedDepthStencil != NULL) |
| 584 | { |
| 585 | mSavedDepthStencil->Release(); |
| 586 | mSavedDepthStencil = NULL; |
| 587 | } |
| 588 | |
| 589 | device->SetRenderTarget(0, mSavedRenderTarget); |
| 590 | if (mSavedRenderTarget != NULL) |
| 591 | { |
| 592 | mSavedRenderTarget->Release(); |
| 593 | mSavedRenderTarget = NULL; |
| 594 | } |
| 595 | |
| 596 | ASSERT(mSavedStateBlock != NULL); |
| 597 | |
| 598 | if (mSavedStateBlock != NULL) |
| 599 | { |
| 600 | mSavedStateBlock->Apply(); |
| 601 | } |
| 602 | } |
| 603 | |
daniel@transgaming.com | b8c28ed | 2010-04-13 03:26:32 +0000 | [diff] [blame] | 604 | } |