blob: 2faea58dc6151f20756798077a38b839e4981ef6 [file] [log] [blame]
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +00001//
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
9#include "Blit.h"
10
11#include <d3dx9.h>
12
13#include "main.h"
14#include "common/debug.h"
15
16namespace
17{
18// Standard Vertex Shader
19// Input 0 is the homogenous position.
20// Outputs the homogenous position as-is.
21// Outputs a tex coord with (0,0) in the upper-left corner of the screen and (1,1) in the bottom right.
22// C0.X must be negative half-pixel width, C0.Y must be half-pixel height. C0.ZW must be 0.
23const char standardvs[] =
24"struct VS_OUTPUT\n"
25"{\n"
26" float4 position : POSITION;\n"
27" float4 texcoord : TEXCOORD0;\n"
28"};\n"
29"\n"
30"uniform float4 halfPixelSize : c0;\n"
31"\n"
32"VS_OUTPUT main(in float4 position : POSITION)\n"
33"{\n"
34" VS_OUTPUT Out;\n"
35"\n"
36" Out.position = position + halfPixelSize;\n"
37" Out.texcoord = position * float4(0.5, -0.5, 1.0, 1.0) + float4(0.5, 0.5, 0, 0);\n"
38"\n"
39" return Out;\n"
40"}\n";
41
42// Flip Y Vertex Shader
43// Input 0 is the homogenous position.
44// Outputs the homogenous position as-is.
45// Outputs a tex coord with (0,1) in the upper-left corner of the screen and (1,0) in the bottom right.
46// C0.XY must be the half-pixel width and height. C0.ZW must be 0.
47const char flipyvs[] =
48"struct VS_OUTPUT\n"
49"{\n"
50" float4 position : POSITION;\n"
51" float4 texcoord : TEXCOORD0;\n"
52"};\n"
53"\n"
54"uniform float4 halfPixelSize : c0;\n"
55"\n"
56"VS_OUTPUT main(in float4 position : POSITION)\n"
57"{\n"
58" VS_OUTPUT Out;\n"
59"\n"
60" Out.position = position + halfPixelSize;\n"
61" Out.texcoord = position * float4(0.5, 0.5, 1.0, 1.0) + float4(0.5, 0.5, 0, 0);\n"
62"\n"
63" return Out;\n"
64"}\n";
65
66// Passthrough Pixel Shader
67// Outputs texture 0 sampled at texcoord 0.
68const char passthroughps[] =
69"sampler2D tex : s0;\n"
70"\n"
71"float4 main(float4 texcoord : TEXCOORD0) : COLOR\n"
72"{\n"
73" return tex2D(tex, texcoord.xy);\n"
74"}\n";
75
76// Luminance Conversion Pixel Shader
77// Outputs sample(tex0, tc0).rrra.
78// For LA output (pass A) set C0.X = 1, C0.Y = 0.
79// For L output (A = 1) set C0.X = 0, C0.Y = 1.
80const char luminanceps[] =
81"sampler2D tex : s0;\n"
82"\n"
83"uniform float4 mode : c0;\n"
84"\n"
85"float4 main(float4 texcoord : TEXCOORD0) : COLOR\n"
86"{\n"
87" float4 tmp = tex2D(tex, texcoord.xy);\n"
88" tmp.w = tmp.w * mode.x + mode.y;\n"
89" return tmp.xxxw;\n"
90"}\n";
91
92// RGB/A Component Mask Pixel Shader
93// Outputs sample(tex0, tc0) with options to force RGB = 0 and/or A = 1.
94// To force RGB = 0, set C0.X = 0, otherwise C0.X = 1.
95// To force A = 1, set C0.Z = 0, C0.W = 1, otherwise C0.Z = 1, C0.W = 0.
96const char componentmaskps[] =
97"sampler2D tex : s0;\n"
98"\n"
99"uniform float4 mode : c0;\n"
100"\n"
101"float4 main(float4 texcoord : TEXCOORD0) : COLOR\n"
102"{\n"
103" float4 tmp = tex2D(tex, texcoord.xy);\n"
104" tmp.xyz = tmp.xyz * mode.x;\n"
105" tmp.w = tmp.w * mode.z + mode.w;\n"
106" return tmp;\n"
107"}\n";
108
109}
110
111namespace gl
112{
113
114const char * const Blit::mShaderSource[] =
115{
116 standardvs,
117 flipyvs,
118 passthroughps,
119 luminanceps,
120 componentmaskps
121};
122
123Blit::Blit(Context *context)
124 : mContext(context), mQuadVertexBuffer(NULL), mQuadVertexDeclaration(NULL)
125{
126 initGeometry();
127 memset(mCompiledShaders, 0, sizeof(mCompiledShaders));
128}
129
130Blit::~Blit()
131{
132 if (mQuadVertexBuffer) mQuadVertexBuffer->Release();
133 if (mQuadVertexDeclaration) mQuadVertexDeclaration->Release();
134
135 for (int i = 0; i < SHADER_COUNT; i++)
136 {
137 if (mCompiledShaders[i])
138 {
139 mCompiledShaders[i]->Release();
140 }
141 }
142}
143
144void Blit::initGeometry()
145{
146 static const float quad[] =
147 {
148 -1, -1,
149 -1, 1,
150 1, -1,
151 1, 1
152 };
153
154 IDirect3DDevice9 *device = getDevice();
155
156 HRESULT hr = device->CreateVertexBuffer(sizeof(quad), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &mQuadVertexBuffer, NULL);
157
158 if (FAILED(hr))
159 {
160 ASSERT(hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
161 return error(GL_OUT_OF_MEMORY);
162 }
163
164 void *lockPtr;
165 mQuadVertexBuffer->Lock(0, 0, &lockPtr, 0);
166 memcpy(lockPtr, quad, sizeof(quad));
167 mQuadVertexBuffer->Unlock();
168
169 static const D3DVERTEXELEMENT9 elements[] =
170 {
171 { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
172 D3DDECL_END()
173 };
174
175 hr = device->CreateVertexDeclaration(elements, &mQuadVertexDeclaration);
176 if (FAILED(hr))
177 {
178 ASSERT(hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
179 return error(GL_OUT_OF_MEMORY);
180 }
181}
182
183template <class D3DShaderType>
184bool Blit::setShader(ShaderId source, const char *profile,
185 HRESULT (WINAPI IDirect3DDevice9::*createShader)(const DWORD *, D3DShaderType**),
186 HRESULT (WINAPI IDirect3DDevice9::*setShader)(D3DShaderType*))
187{
188 IDirect3DDevice9 *device = getDevice();
189
190 D3DShaderType *shader;
191
192 if (mCompiledShaders[source] != NULL)
193 {
194 shader = static_cast<D3DShaderType*>(mCompiledShaders[source]);
195 }
196 else
197 {
198 ID3DXBuffer *shaderCode;
199 HRESULT hr = D3DXCompileShader(mShaderSource[source], strlen(mShaderSource[source]), NULL, NULL, "main", profile, 0, &shaderCode, NULL, NULL);
200
201 if (FAILED(hr))
202 {
203 ERR("Failed to compile %s shader for blit operation %d, error 0x%08X.", profile, (int)source, hr);
204 return false;
205 }
206
207 hr = (device->*createShader)(static_cast<const DWORD*>(shaderCode->GetBufferPointer()), &shader);
208 if (FAILED(hr))
209 {
210 shaderCode->Release();
211 ERR("Failed to create %s shader for blit operation %d, error 0x%08X.", profile, (int)source, hr);
212 return false;
213 }
214
215 shaderCode->Release();
216
217 mCompiledShaders[source] = shader;
218 }
219
220 HRESULT hr = (device->*setShader)(shader);
221
222 if (FAILED(hr))
223 {
224 ERR("Failed to set %s shader for blit operation %d, error 0x%08X.", profile, (int)source, hr);
225 return false;
226 }
227
228 return true;
229}
230
231bool Blit::setVertexShader(ShaderId shader)
232{
233 return setShader<IDirect3DVertexShader9>(shader, mContext->getVertexShaderProfile(), &IDirect3DDevice9::CreateVertexShader, &IDirect3DDevice9::SetVertexShader);
234}
235
236bool Blit::setPixelShader(ShaderId shader)
237{
238 return setShader<IDirect3DPixelShader9>(shader, mContext->getPixelShaderProfile(), &IDirect3DDevice9::CreatePixelShader, &IDirect3DDevice9::SetPixelShader);
239}
240
241bool Blit::formatConvert(IDirect3DSurface9 *source, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest)
242{
243 IDirect3DTexture9 *texture = copySurfaceToTexture(source, sourceRect);
244 if (!texture)
245 {
246 return false;
247 }
248
249 IDirect3DDevice9 *device = getDevice();
250
251 device->SetTexture(0, texture);
252 device->SetRenderTarget(0, dest);
253
254 setViewport(sourceRect, xoffset, yoffset, dest);
255
256 setCommonBlitState();
257 if (setFormatConvertShaders(destFormat))
258 {
259 render();
260 }
261
262 texture->Release();
263
264 return true;
265}
266
267bool Blit::setFormatConvertShaders(GLenum destFormat)
268{
269 bool okay = setVertexShader(SHADER_VS_STANDARD);
270
271 switch (destFormat)
272 {
273 default: UNREACHABLE();
274 case GL_RGBA:
275 case GL_RGB:
276 case GL_ALPHA:
277 okay = okay && setPixelShader(SHADER_PS_COMPONENTMASK);
278 break;
279
280 case GL_LUMINANCE:
281 case GL_LUMINANCE_ALPHA:
282 okay = okay && setPixelShader(SHADER_PS_LUMINANCE);
283 break;
284 }
285
286 if (!okay)
287 {
288 return false;
289 }
290
291 enum { X = 0, Y = 1, Z = 2, W = 3 };
292
293 // The meaning of this constant depends on the shader that was selected.
294 // See the shader assembly code above for details.
295 float psConst0[4] = { 0, 0, 0, 0 };
296
297 switch (destFormat)
298 {
299 default: UNREACHABLE();
300 case GL_RGBA:
301 psConst0[X] = 1;
302 psConst0[Z] = 1;
303 break;
304
305 case GL_RGB:
306 psConst0[X] = 1;
307 psConst0[W] = 1;
308 break;
309
310 case GL_ALPHA:
311 psConst0[Z] = 1;
312 break;
313
314 case GL_LUMINANCE:
315 psConst0[Y] = 1;
316 break;
317
318 case GL_LUMINANCE_ALPHA:
319 psConst0[X] = 1;
320 break;
321 }
322
323 getDevice()->SetPixelShaderConstantF(0, psConst0, 1);
324
325 return true;
326}
327
328IDirect3DTexture9 *Blit::copySurfaceToTexture(IDirect3DSurface9 *surface, const RECT &sourceRect)
329{
330 IDirect3DDevice9 *device = getDevice();
331
332 D3DSURFACE_DESC sourceDesc;
333 surface->GetDesc(&sourceDesc);
334
335 // Copy the render target into a texture
336 IDirect3DTexture9 *texture;
337 HRESULT result = device->CreateTexture(sourceRect.right - sourceRect.left, sourceRect.top - sourceRect.bottom, 1, D3DUSAGE_RENDERTARGET, sourceDesc.Format, D3DPOOL_DEFAULT, &texture, NULL);
338
339 if (FAILED(result))
340 {
341 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
342 return error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL);
343 }
344
345 IDirect3DSurface9 *textureSurface;
346 result = texture->GetSurfaceLevel(0, &textureSurface);
347
348 if (FAILED(result))
349 {
350 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
351 texture->Release();
352 return error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL);
353 }
354
355 RECT d3dSourceRect;
356 d3dSourceRect.left = sourceRect.left;
357 d3dSourceRect.right = sourceRect.right;
358 d3dSourceRect.top = sourceRect.bottom;
359 d3dSourceRect.bottom = sourceRect.top;
360
361 result = device->StretchRect(surface, &d3dSourceRect, textureSurface, NULL, D3DTEXF_NONE);
362
363 textureSurface->Release();
364
365 if (FAILED(result))
366 {
367 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
368 texture->Release();
369 return error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL);
370 }
371
372 return texture;
373}
374
375void Blit::setViewport(const RECT &sourceRect, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest)
376{
377 D3DSURFACE_DESC desc;
378 dest->GetDesc(&desc);
379
380 IDirect3DDevice9 *device = getDevice();
381
382 D3DVIEWPORT9 vp;
383 vp.X = xoffset;
384 vp.Y = yoffset;
385 vp.Width = sourceRect.right - sourceRect.left;
386 vp.Height = sourceRect.top - sourceRect.bottom;
387 vp.MinZ = 0.0f;
388 vp.MaxZ = 1.0f;
389 device->SetViewport(&vp);
390
391 float halfPixelAdjust[4] = { -1.0f/vp.Width, 1.0f/vp.Height, 0, 0 };
392 device->SetVertexShaderConstantF(0, halfPixelAdjust, 1);
393}
394
395void Blit::setCommonBlitState()
396{
397 IDirect3DDevice9 *device = getDevice();
398
399 device->SetDepthStencilSurface(NULL);
400
401 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
402 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
403 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
404 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
405 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
406 device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
407 device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE);
408 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
409
410 device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
411 device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
412 device->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, FALSE);
413}
414
415void Blit::render()
416{
417 IDirect3DDevice9 *device = getDevice();
418
419 HRESULT hr = device->SetStreamSource(0, mQuadVertexBuffer, 0, 2 * sizeof(float));
420 hr = device->SetVertexDeclaration(mQuadVertexDeclaration);
421
422 device->BeginScene();
423 hr = device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
424 device->EndScene();
425}
426
427}