blob: f07f3a92c6dbe94f8c1fc51233d5573376abeffe [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +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// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
22#include "libGLESv2/FrameBuffer.h"
23#include "libGLESv2/Program.h"
24#include "libGLESv2/RenderBuffer.h"
25#include "libGLESv2/Shader.h"
26#include "libGLESv2/Texture.h"
27#include "libGLESv2/geometry/backend.h"
28#include "libGLESv2/geometry/VertexDataManager.h"
29#include "libGLESv2/geometry/IndexDataManager.h"
30#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.com86487c22010-03-11 19:41:43 +000032#undef near
33#undef far
34
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035namespace gl
36{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000037Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000038 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039{
40 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000041
daniel@transgaming.com428d1582010-05-04 03:35:25 +000042 mState.depthClearValue = 1.0f;
43 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
daniel@transgaming.com428d1582010-05-04 03:35:25 +000045 mState.cullFace = false;
46 mState.cullMode = GL_BACK;
47 mState.frontFace = GL_CCW;
48 mState.depthTest = false;
49 mState.depthFunc = GL_LESS;
50 mState.blend = false;
51 mState.sourceBlendRGB = GL_ONE;
52 mState.sourceBlendAlpha = GL_ONE;
53 mState.destBlendRGB = GL_ZERO;
54 mState.destBlendAlpha = GL_ZERO;
55 mState.blendEquationRGB = GL_FUNC_ADD;
56 mState.blendEquationAlpha = GL_FUNC_ADD;
57 mState.blendColor.red = 0;
58 mState.blendColor.green = 0;
59 mState.blendColor.blue = 0;
60 mState.blendColor.alpha = 0;
61 mState.stencilTest = false;
62 mState.stencilFunc = GL_ALWAYS;
63 mState.stencilRef = 0;
64 mState.stencilMask = -1;
65 mState.stencilWritemask = -1;
66 mState.stencilBackFunc = GL_ALWAYS;
67 mState.stencilBackRef = 0;
68 mState.stencilBackMask = - 1;
69 mState.stencilBackWritemask = -1;
70 mState.stencilFail = GL_KEEP;
71 mState.stencilPassDepthFail = GL_KEEP;
72 mState.stencilPassDepthPass = GL_KEEP;
73 mState.stencilBackFail = GL_KEEP;
74 mState.stencilBackPassDepthFail = GL_KEEP;
75 mState.stencilBackPassDepthPass = GL_KEEP;
76 mState.polygonOffsetFill = false;
77 mState.polygonOffsetFactor = 0.0f;
78 mState.polygonOffsetUnits = 0.0f;
79 mState.sampleAlphaToCoverage = false;
80 mState.sampleCoverage = false;
81 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000082 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000083 mState.scissorTest = false;
84 mState.dither = true;
85 mState.generateMipmapHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086
daniel@transgaming.com428d1582010-05-04 03:35:25 +000087 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.viewportX = 0;
90 mState.viewportY = 0;
91 mState.viewportWidth = config->mDisplayMode.Width;
92 mState.viewportHeight = config->mDisplayMode.Height;
93 mState.zNear = 0.0f;
94 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.scissorX = 0;
97 mState.scissorY = 0;
98 mState.scissorWidth = config->mDisplayMode.Width;
99 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000101 mState.colorMaskRed = true;
102 mState.colorMaskGreen = true;
103 mState.colorMaskBlue = true;
104 mState.colorMaskAlpha = true;
105 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000107 if (shareContext != NULL)
108 {
109 mResourceManager = shareContext->mResourceManager;
110 mResourceManager->addRef();
111 }
112 else
113 {
114 mResourceManager = new ResourceManager();
115 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000116
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117 // [OpenGL ES 2.0.24] section 3.7 page 83:
118 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
119 // and cube map texture state vectors respectively associated with them.
120 // In order that access to these initial textures not be lost, they are treated as texture
121 // objects all of whose names are 0.
122
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000123 mTexture2DZero = new Texture2D(0);
124 mTextureCubeMapZero = new TextureCubeMap(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
126 mColorbufferZero = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000127 mDepthStencilbufferZero = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000129 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000130 bindArrayBuffer(0);
131 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132 bindTextureCubeMap(0);
133 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000134 bindReadFramebuffer(0);
135 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 bindRenderbuffer(0);
137
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000138 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 {
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000140 mIncompleteTextures[type] = NULL;
141 }
142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000145 mState.packAlignment = 4;
146 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000147
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000148 mBufferBackEnd = NULL;
149 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000150 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000151 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 mInvalidEnum = false;
154 mInvalidValue = false;
155 mInvalidOperation = false;
156 mOutOfMemory = false;
157 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000158
159 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000160
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000161 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000162 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000163 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166Context::~Context()
167{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000168 if (mState.currentProgram != 0)
169 {
170 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
171 if (programObject)
172 {
173 programObject->release();
174 }
175 mState.currentProgram = 0;
176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000178 while (!mFramebufferMap.empty())
179 {
180 deleteFramebuffer(mFramebufferMap.begin()->first);
181 }
182
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000183 while (!mMultiSampleSupport.empty())
184 {
185 delete [] mMultiSampleSupport.begin()->second;
186 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
187 }
188
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000189 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
190 {
191 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
192 {
193 mState.samplerTexture[type][sampler].set(NULL);
194 }
195 }
196
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000197 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
198 {
199 delete mIncompleteTextures[type];
200 }
201
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000202 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
203 {
204 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
205 }
206
207 mState.arrayBuffer.set(NULL);
208 mState.elementArrayBuffer.set(NULL);
209 mState.texture2D.set(NULL);
210 mState.textureCubeMap.set(NULL);
211 mState.renderbuffer.set(NULL);
212
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000213 delete mTexture2DZero;
214 delete mTextureCubeMapZero;
215
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000216 delete mBufferBackEnd;
217 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000218 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000219 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000220
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000221 if (mMaskedClearSavedState)
222 {
223 mMaskedClearSavedState->Release();
224 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000225
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000226 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227}
228
229void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
230{
231 IDirect3DDevice9 *device = display->getDevice();
232
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000233 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000234 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000235 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000236
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000237 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000238 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000239 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000240 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000241
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000242 const D3DFORMAT renderBufferFormats[] =
243 {
244 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000245 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000246 D3DFMT_R5G6B5,
247 D3DFMT_D24S8
248 };
249
250 int max = 0;
251 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
252 {
253 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
254 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
255 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
256
257 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
258 {
259 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
260 {
261 max = j;
262 }
263 }
264 }
265
266 mMaxSupportedSamples = max;
267
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000268 initExtensionString();
269
270 mState.viewportX = 0;
271 mState.viewportY = 0;
272 mState.viewportWidth = surface->getWidth();
273 mState.viewportHeight = surface->getHeight();
274
275 mState.scissorX = 0;
276 mState.scissorY = 0;
277 mState.scissorWidth = surface->getWidth();
278 mState.scissorHeight = surface->getHeight();
279
280 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000281 }
282
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
284 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000285 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000288 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000289 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
291 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
293 defaultRenderTarget->Release();
294
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000295 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000297 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000299
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000300 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000301
302 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303}
304
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000305// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000306void Context::markAllStateDirty()
307{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000308 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000309 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000310 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000311
312 mClearStateDirty = true;
313 mCullStateDirty = true;
314 mDepthStateDirty = true;
315 mMaskStateDirty = true;
316 mBlendStateDirty = true;
317 mStencilStateDirty = true;
318 mPolygonOffsetStateDirty = true;
319 mScissorStateDirty = true;
320 mSampleStateDirty = true;
321 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000322 mFrontFaceDirty = true;
323
324 if (mBufferBackEnd != NULL)
325 {
326 mBufferBackEnd->invalidate();
327 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000328}
329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330void Context::setClearColor(float red, float green, float blue, float alpha)
331{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000332 mState.colorClearValue.red = red;
333 mState.colorClearValue.green = green;
334 mState.colorClearValue.blue = blue;
335 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336}
337
338void Context::setClearDepth(float depth)
339{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000340 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341}
342
343void Context::setClearStencil(int stencil)
344{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000345 mState.stencilClearValue = stencil;
346}
347
348void Context::setCullFace(bool enabled)
349{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000350 if (mState.cullFace != enabled)
351 {
352 mState.cullFace = enabled;
353 mCullStateDirty = true;
354 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000355}
356
357bool Context::isCullFaceEnabled() const
358{
359 return mState.cullFace;
360}
361
362void Context::setCullMode(GLenum mode)
363{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000364 if (mState.cullMode != mode)
365 {
366 mState.cullMode = mode;
367 mCullStateDirty = true;
368 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000369}
370
371void Context::setFrontFace(GLenum front)
372{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000373 if (mState.frontFace != front)
374 {
375 mState.frontFace = front;
376 mFrontFaceDirty = true;
377 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000378}
379
380void Context::setDepthTest(bool enabled)
381{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000382 if (mState.depthTest != enabled)
383 {
384 mState.depthTest = enabled;
385 mDepthStateDirty = true;
386 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000387}
388
389bool Context::isDepthTestEnabled() const
390{
391 return mState.depthTest;
392}
393
394void Context::setDepthFunc(GLenum depthFunc)
395{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000396 if (mState.depthFunc != depthFunc)
397 {
398 mState.depthFunc = depthFunc;
399 mDepthStateDirty = true;
400 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000401}
402
403void Context::setDepthRange(float zNear, float zFar)
404{
405 mState.zNear = zNear;
406 mState.zFar = zFar;
407}
408
409void Context::setBlend(bool enabled)
410{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000411 if (mState.blend != enabled)
412 {
413 mState.blend = enabled;
414 mBlendStateDirty = true;
415 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000416}
417
418bool Context::isBlendEnabled() const
419{
420 return mState.blend;
421}
422
423void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
424{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000425 if (mState.sourceBlendRGB != sourceRGB ||
426 mState.sourceBlendAlpha != sourceAlpha ||
427 mState.destBlendRGB != destRGB ||
428 mState.destBlendAlpha != destAlpha)
429 {
430 mState.sourceBlendRGB = sourceRGB;
431 mState.destBlendRGB = destRGB;
432 mState.sourceBlendAlpha = sourceAlpha;
433 mState.destBlendAlpha = destAlpha;
434 mBlendStateDirty = true;
435 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000436}
437
438void Context::setBlendColor(float red, float green, float blue, float alpha)
439{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000440 if (mState.blendColor.red != red ||
441 mState.blendColor.green != green ||
442 mState.blendColor.blue != blue ||
443 mState.blendColor.alpha != alpha)
444 {
445 mState.blendColor.red = red;
446 mState.blendColor.green = green;
447 mState.blendColor.blue = blue;
448 mState.blendColor.alpha = alpha;
449 mBlendStateDirty = true;
450 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000451}
452
453void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
454{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000455 if (mState.blendEquationRGB != rgbEquation ||
456 mState.blendEquationAlpha != alphaEquation)
457 {
458 mState.blendEquationRGB = rgbEquation;
459 mState.blendEquationAlpha = alphaEquation;
460 mBlendStateDirty = true;
461 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000462}
463
464void Context::setStencilTest(bool enabled)
465{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000466 if (mState.stencilTest != enabled)
467 {
468 mState.stencilTest = enabled;
469 mStencilStateDirty = true;
470 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000471}
472
473bool Context::isStencilTestEnabled() const
474{
475 return mState.stencilTest;
476}
477
478void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
479{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000480 if (mState.stencilFunc != stencilFunc ||
481 mState.stencilRef != stencilRef ||
482 mState.stencilMask != stencilMask)
483 {
484 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000485 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000486 mState.stencilMask = stencilMask;
487 mStencilStateDirty = true;
488 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000489}
490
491void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
492{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000493 if (mState.stencilBackFunc != stencilBackFunc ||
494 mState.stencilBackRef != stencilBackRef ||
495 mState.stencilBackMask != stencilBackMask)
496 {
497 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000498 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000499 mState.stencilBackMask = stencilBackMask;
500 mStencilStateDirty = true;
501 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000502}
503
504void Context::setStencilWritemask(GLuint stencilWritemask)
505{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000506 if (mState.stencilWritemask != stencilWritemask)
507 {
508 mState.stencilWritemask = stencilWritemask;
509 mStencilStateDirty = true;
510 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000511}
512
513void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
514{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000515 if (mState.stencilBackWritemask != stencilBackWritemask)
516 {
517 mState.stencilBackWritemask = stencilBackWritemask;
518 mStencilStateDirty = true;
519 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000520}
521
522void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
523{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000524 if (mState.stencilFail != stencilFail ||
525 mState.stencilPassDepthFail != stencilPassDepthFail ||
526 mState.stencilPassDepthPass != stencilPassDepthPass)
527 {
528 mState.stencilFail = stencilFail;
529 mState.stencilPassDepthFail = stencilPassDepthFail;
530 mState.stencilPassDepthPass = stencilPassDepthPass;
531 mStencilStateDirty = true;
532 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000533}
534
535void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
536{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 if (mState.stencilBackFail != stencilBackFail ||
538 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
539 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
540 {
541 mState.stencilBackFail = stencilBackFail;
542 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
543 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
544 mStencilStateDirty = true;
545 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000546}
547
548void Context::setPolygonOffsetFill(bool enabled)
549{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000550 if (mState.polygonOffsetFill != enabled)
551 {
552 mState.polygonOffsetFill = enabled;
553 mPolygonOffsetStateDirty = true;
554 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000555}
556
557bool Context::isPolygonOffsetFillEnabled() const
558{
559 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000560
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000561}
562
563void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
564{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000565 if (mState.polygonOffsetFactor != factor ||
566 mState.polygonOffsetUnits != units)
567 {
568 mState.polygonOffsetFactor = factor;
569 mState.polygonOffsetUnits = units;
570 mPolygonOffsetStateDirty = true;
571 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000572}
573
574void Context::setSampleAlphaToCoverage(bool enabled)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.sampleAlphaToCoverage != enabled)
577 {
578 mState.sampleAlphaToCoverage = enabled;
579 mSampleStateDirty = true;
580 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000581}
582
583bool Context::isSampleAlphaToCoverageEnabled() const
584{
585 return mState.sampleAlphaToCoverage;
586}
587
588void Context::setSampleCoverage(bool enabled)
589{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000590 if (mState.sampleCoverage != enabled)
591 {
592 mState.sampleCoverage = enabled;
593 mSampleStateDirty = true;
594 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000595}
596
597bool Context::isSampleCoverageEnabled() const
598{
599 return mState.sampleCoverage;
600}
601
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000602void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000603{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000604 if (mState.sampleCoverageValue != value ||
605 mState.sampleCoverageInvert != invert)
606 {
607 mState.sampleCoverageValue = value;
608 mState.sampleCoverageInvert = invert;
609 mSampleStateDirty = true;
610 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000611}
612
613void Context::setScissorTest(bool enabled)
614{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000615 if (mState.scissorTest != enabled)
616 {
617 mState.scissorTest = enabled;
618 mScissorStateDirty = true;
619 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000620}
621
622bool Context::isScissorTestEnabled() const
623{
624 return mState.scissorTest;
625}
626
627void Context::setDither(bool enabled)
628{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000629 if (mState.dither != enabled)
630 {
631 mState.dither = enabled;
632 mDitherStateDirty = true;
633 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000634}
635
636bool Context::isDitherEnabled() const
637{
638 return mState.dither;
639}
640
641void Context::setLineWidth(GLfloat width)
642{
643 mState.lineWidth = width;
644}
645
646void Context::setGenerateMipmapHint(GLenum hint)
647{
648 mState.generateMipmapHint = hint;
649}
650
651void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
652{
653 mState.viewportX = x;
654 mState.viewportY = y;
655 mState.viewportWidth = width;
656 mState.viewportHeight = height;
657}
658
659void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
660{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000661 if (mState.scissorX != x || mState.scissorY != y ||
662 mState.scissorWidth != width || mState.scissorHeight != height)
663 {
664 mState.scissorX = x;
665 mState.scissorY = y;
666 mState.scissorWidth = width;
667 mState.scissorHeight = height;
668 mScissorStateDirty = true;
669 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670}
671
672void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
673{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000674 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
675 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
676 {
677 mState.colorMaskRed = red;
678 mState.colorMaskGreen = green;
679 mState.colorMaskBlue = blue;
680 mState.colorMaskAlpha = alpha;
681 mMaskStateDirty = true;
682 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000683}
684
685void Context::setDepthMask(bool mask)
686{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000687 if (mState.depthMask != mask)
688 {
689 mState.depthMask = mask;
690 mMaskStateDirty = true;
691 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000692}
693
694void Context::setActiveSampler(int active)
695{
696 mState.activeSampler = active;
697}
698
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000699GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000700{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000701 return mState.readFramebuffer;
702}
703
704GLuint Context::getDrawFramebufferHandle() const
705{
706 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000707}
708
709GLuint Context::getRenderbufferHandle() const
710{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000711 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000712}
713
714GLuint Context::getArrayBufferHandle() const
715{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000716 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000717}
718
719void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
720{
721 mState.vertexAttribute[attribNum].mEnabled = enabled;
722}
723
724const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
725{
726 return mState.vertexAttribute[attribNum];
727}
728
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000729void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000730 GLsizei stride, const void *pointer)
731{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000732 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000733 mState.vertexAttribute[attribNum].mSize = size;
734 mState.vertexAttribute[attribNum].mType = type;
735 mState.vertexAttribute[attribNum].mNormalized = normalized;
736 mState.vertexAttribute[attribNum].mStride = stride;
737 mState.vertexAttribute[attribNum].mPointer = pointer;
738}
739
740const void *Context::getVertexAttribPointer(unsigned int attribNum) const
741{
742 return mState.vertexAttribute[attribNum].mPointer;
743}
744
745// returns entire set of attributes as a block
746const AttributeState *Context::getVertexAttribBlock()
747{
748 return mState.vertexAttribute;
749}
750
751void Context::setPackAlignment(GLint alignment)
752{
753 mState.packAlignment = alignment;
754}
755
756GLint Context::getPackAlignment() const
757{
758 return mState.packAlignment;
759}
760
761void Context::setUnpackAlignment(GLint alignment)
762{
763 mState.unpackAlignment = alignment;
764}
765
766GLint Context::getUnpackAlignment() const
767{
768 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769}
770
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771GLuint Context::createBuffer()
772{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000773 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000774}
775
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776GLuint Context::createProgram()
777{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000778 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779}
780
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000781GLuint Context::createShader(GLenum type)
782{
783 return mResourceManager->createShader(type);
784}
785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786GLuint Context::createTexture()
787{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000788 return mResourceManager->createTexture();
789}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000791GLuint Context::createRenderbuffer()
792{
793 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794}
795
796// Returns an unused framebuffer name
797GLuint Context::createFramebuffer()
798{
799 unsigned int handle = 1;
800
801 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
802 {
803 handle++;
804 }
805
806 mFramebufferMap[handle] = NULL;
807
808 return handle;
809}
810
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811void Context::deleteBuffer(GLuint buffer)
812{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000813 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814 {
815 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000817
818 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819}
820
821void Context::deleteShader(GLuint shader)
822{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000823 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824}
825
826void Context::deleteProgram(GLuint program)
827{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000828 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829}
830
831void Context::deleteTexture(GLuint texture)
832{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000833 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834 {
835 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000837
838 mResourceManager->deleteTexture(texture);
839}
840
841void Context::deleteRenderbuffer(GLuint renderbuffer)
842{
843 if (mResourceManager->getRenderbuffer(renderbuffer))
844 {
845 detachRenderbuffer(renderbuffer);
846 }
847
848 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849}
850
851void Context::deleteFramebuffer(GLuint framebuffer)
852{
853 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
854
855 if (framebufferObject != mFramebufferMap.end())
856 {
857 detachFramebuffer(framebuffer);
858
859 delete framebufferObject->second;
860 mFramebufferMap.erase(framebufferObject);
861 }
862}
863
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000866 return mResourceManager->getBuffer(handle);
867}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869Shader *Context::getShader(GLuint handle)
870{
871 return mResourceManager->getShader(handle);
872}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874Program *Context::getProgram(GLuint handle)
875{
876 return mResourceManager->getProgram(handle);
877}
878
879Texture *Context::getTexture(GLuint handle)
880{
881 return mResourceManager->getTexture(handle);
882}
883
884Renderbuffer *Context::getRenderbuffer(GLuint handle)
885{
886 return mResourceManager->getRenderbuffer(handle);
887}
888
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000889Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000890{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000891 return getFramebuffer(mState.readFramebuffer);
892}
893
894Framebuffer *Context::getDrawFramebuffer()
895{
896 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
899void Context::bindArrayBuffer(unsigned int buffer)
900{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000901 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000903 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904}
905
906void Context::bindElementArrayBuffer(unsigned int buffer)
907{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000908 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000910 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
913void Context::bindTexture2D(GLuint texture)
914{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000915 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000917 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000919 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920}
921
922void Context::bindTextureCubeMap(GLuint texture)
923{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000924 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000926 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000928 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929}
930
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000931void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932{
933 if (!getFramebuffer(framebuffer))
934 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000935 mFramebufferMap[framebuffer] = new Framebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936 }
937
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000938 mState.readFramebuffer = framebuffer;
939}
940
941void Context::bindDrawFramebuffer(GLuint framebuffer)
942{
943 if (!getFramebuffer(framebuffer))
944 {
945 mFramebufferMap[framebuffer] = new Framebuffer();
946 }
947
948 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
951void Context::bindRenderbuffer(GLuint renderbuffer)
952{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000953 mResourceManager->checkRenderbufferAllocation(renderbuffer);
954
955 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
958void Context::useProgram(GLuint program)
959{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000960 GLuint priorProgram = mState.currentProgram;
961 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000962
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965 Program *newProgram = mResourceManager->getProgram(program);
966 Program *oldProgram = mResourceManager->getProgram(priorProgram);
967
968 if (newProgram)
969 {
970 newProgram->addRef();
971 }
972
973 if (oldProgram)
974 {
975 oldProgram->release();
976 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978}
979
980void Context::setFramebufferZero(Framebuffer *buffer)
981{
982 delete mFramebufferMap[0];
983 mFramebufferMap[0] = buffer;
984}
985
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000986void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000988 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
989 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990}
991
992Framebuffer *Context::getFramebuffer(unsigned int handle)
993{
994 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000995
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996 if (framebuffer == mFramebufferMap.end())
997 {
998 return NULL;
999 }
1000 else
1001 {
1002 return framebuffer->second;
1003 }
1004}
1005
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006Buffer *Context::getArrayBuffer()
1007{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001008 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009}
1010
1011Buffer *Context::getElementArrayBuffer()
1012{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001013 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014}
1015
1016Program *Context::getCurrentProgram()
1017{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001018 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001019}
1020
1021Texture2D *Context::getTexture2D()
1022{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001023 if (mState.texture2D.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024 {
1025 return mTexture2DZero;
1026 }
1027
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001028 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
1031TextureCubeMap *Context::getTextureCubeMap()
1032{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001033 if (mState.textureCubeMap.id() == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034 {
1035 return mTextureCubeMapZero;
1036 }
1037
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001038 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039}
1040
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001041Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001043 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001044
1045 if (texid == 0)
1046 {
1047 switch (type)
1048 {
1049 default: UNREACHABLE();
1050 case SAMPLER_2D: return mTexture2DZero;
1051 case SAMPLER_CUBE: return mTextureCubeMapZero;
1052 }
1053 }
1054
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001055 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056}
1057
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001058bool Context::getBooleanv(GLenum pname, GLboolean *params)
1059{
1060 switch (pname)
1061 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001062 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1063 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1064 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001065 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001066 params[0] = mState.colorMaskRed;
1067 params[1] = mState.colorMaskGreen;
1068 params[2] = mState.colorMaskBlue;
1069 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001070 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001071 case GL_CULL_FACE: *params = mState.cullFace;
1072 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1073 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1074 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1075 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1076 case GL_STENCIL_TEST: *params = mState.stencilTest;
1077 case GL_DEPTH_TEST: *params = mState.depthTest;
1078 case GL_BLEND: *params = mState.blend;
1079 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001080 default:
1081 return false;
1082 }
1083
1084 return true;
1085}
1086
1087bool Context::getFloatv(GLenum pname, GLfloat *params)
1088{
1089 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1090 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1091 // GetIntegerv as its native query function. As it would require conversion in any
1092 // case, this should make no difference to the calling application.
1093 switch (pname)
1094 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001095 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1096 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1097 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1098 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1099 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001100 case GL_ALIASED_LINE_WIDTH_RANGE:
1101 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1102 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1103 break;
1104 case GL_ALIASED_POINT_SIZE_RANGE:
1105 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001106 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001107 break;
1108 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001109 params[0] = mState.zNear;
1110 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001111 break;
1112 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001113 params[0] = mState.colorClearValue.red;
1114 params[1] = mState.colorClearValue.green;
1115 params[2] = mState.colorClearValue.blue;
1116 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001117 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001118 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001119 params[0] = mState.blendColor.red;
1120 params[1] = mState.blendColor.green;
1121 params[2] = mState.blendColor.blue;
1122 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001123 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001124 default:
1125 return false;
1126 }
1127
1128 return true;
1129}
1130
1131bool Context::getIntegerv(GLenum pname, GLint *params)
1132{
1133 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1134 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1135 // GetIntegerv as its native query function. As it would require conversion in any
1136 // case, this should make no difference to the calling application. You may find it in
1137 // Context::getFloatv.
1138 switch (pname)
1139 {
1140 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1141 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1142 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1143 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1144 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1145 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1146 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1147 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001148 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1149 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
1150 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
1151 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001152 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1153 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001154 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1155 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1156 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001157 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001158 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1159 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1160 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1161 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1162 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1163 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1164 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1165 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1166 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1167 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1168 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1169 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1170 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1171 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1172 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1173 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1174 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1175 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1176 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1177 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1178 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1179 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1180 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1181 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1182 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1183 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1184 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1185 case GL_SUBPIXEL_BITS: *params = 4; break;
1186 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1187 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001188 case GL_MAX_SAMPLES_ANGLE:
1189 {
1190 GLsizei maxSamples = getMaxSupportedSamples();
1191 if (maxSamples != 0)
1192 {
1193 *params = maxSamples;
1194 }
1195 else
1196 {
1197 return false;
1198 }
1199
1200 break;
1201 }
1202 case GL_SAMPLE_BUFFERS:
1203 case GL_SAMPLES:
1204 {
1205 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1206 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1207 {
1208 switch (pname)
1209 {
1210 case GL_SAMPLE_BUFFERS:
1211 if (framebuffer->getSamples() != 0)
1212 {
1213 *params = 1;
1214 }
1215 else
1216 {
1217 *params = 0;
1218 }
1219 break;
1220 case GL_SAMPLES:
1221 *params = framebuffer->getSamples();
1222 break;
1223 }
1224 }
1225 else
1226 {
1227 *params = 0;
1228 }
1229 }
1230 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001231 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1232 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1233 case GL_MAX_VIEWPORT_DIMS:
1234 {
1235 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1236 params[0] = maxDimension;
1237 params[1] = maxDimension;
1238 }
1239 break;
1240 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001241 params[0] = mState.viewportX;
1242 params[1] = mState.viewportY;
1243 params[2] = mState.viewportWidth;
1244 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001245 break;
1246 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001247 params[0] = mState.scissorX;
1248 params[1] = mState.scissorY;
1249 params[2] = mState.scissorWidth;
1250 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001251 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001252 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1253 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001254 case GL_RED_BITS:
1255 case GL_GREEN_BITS:
1256 case GL_BLUE_BITS:
1257 case GL_ALPHA_BITS:
1258 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001259 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001260 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1261
1262 if (colorbuffer)
1263 {
1264 switch (pname)
1265 {
1266 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1267 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1268 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1269 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1270 }
1271 }
1272 else
1273 {
1274 *params = 0;
1275 }
1276 }
1277 break;
1278 case GL_DEPTH_BITS:
1279 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001280 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001281 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001282
1283 if (depthbuffer)
1284 {
1285 *params = depthbuffer->getDepthSize();
1286 }
1287 else
1288 {
1289 *params = 0;
1290 }
1291 }
1292 break;
1293 case GL_STENCIL_BITS:
1294 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001295 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001296 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001297
1298 if (stencilbuffer)
1299 {
1300 *params = stencilbuffer->getStencilSize();
1301 }
1302 else
1303 {
1304 *params = 0;
1305 }
1306 }
1307 break;
1308 case GL_TEXTURE_BINDING_2D:
1309 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001310 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001311 {
1312 error(GL_INVALID_OPERATION);
1313 return false;
1314 }
1315
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001316 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001317 }
1318 break;
1319 case GL_TEXTURE_BINDING_CUBE_MAP:
1320 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001321 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001322 {
1323 error(GL_INVALID_OPERATION);
1324 return false;
1325 }
1326
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001327 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 }
1329 break;
1330 default:
1331 return false;
1332 }
1333
1334 return true;
1335}
1336
1337bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1338{
1339 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1340 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1341 // to the fact that it is stored internally as a float, and so would require conversion
1342 // if returned from Context::getIntegerv. Since this conversion is already implemented
1343 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1344 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1345 // application.
1346 switch (pname)
1347 {
1348 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1349 case GL_SHADER_BINARY_FORMATS:
1350 {
1351 *type = GL_INT;
1352 *numParams = 0;
1353 }
1354 break;
1355 case GL_MAX_VERTEX_ATTRIBS:
1356 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1357 case GL_MAX_VARYING_VECTORS:
1358 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1359 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1360 case GL_MAX_TEXTURE_IMAGE_UNITS:
1361 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1362 case GL_MAX_RENDERBUFFER_SIZE:
1363 case GL_NUM_SHADER_BINARY_FORMATS:
1364 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1365 case GL_ARRAY_BUFFER_BINDING:
1366 case GL_FRAMEBUFFER_BINDING:
1367 case GL_RENDERBUFFER_BINDING:
1368 case GL_CURRENT_PROGRAM:
1369 case GL_PACK_ALIGNMENT:
1370 case GL_UNPACK_ALIGNMENT:
1371 case GL_GENERATE_MIPMAP_HINT:
1372 case GL_RED_BITS:
1373 case GL_GREEN_BITS:
1374 case GL_BLUE_BITS:
1375 case GL_ALPHA_BITS:
1376 case GL_DEPTH_BITS:
1377 case GL_STENCIL_BITS:
1378 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1379 case GL_CULL_FACE_MODE:
1380 case GL_FRONT_FACE:
1381 case GL_ACTIVE_TEXTURE:
1382 case GL_STENCIL_FUNC:
1383 case GL_STENCIL_VALUE_MASK:
1384 case GL_STENCIL_REF:
1385 case GL_STENCIL_FAIL:
1386 case GL_STENCIL_PASS_DEPTH_FAIL:
1387 case GL_STENCIL_PASS_DEPTH_PASS:
1388 case GL_STENCIL_BACK_FUNC:
1389 case GL_STENCIL_BACK_VALUE_MASK:
1390 case GL_STENCIL_BACK_REF:
1391 case GL_STENCIL_BACK_FAIL:
1392 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1393 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1394 case GL_DEPTH_FUNC:
1395 case GL_BLEND_SRC_RGB:
1396 case GL_BLEND_SRC_ALPHA:
1397 case GL_BLEND_DST_RGB:
1398 case GL_BLEND_DST_ALPHA:
1399 case GL_BLEND_EQUATION_RGB:
1400 case GL_BLEND_EQUATION_ALPHA:
1401 case GL_STENCIL_WRITEMASK:
1402 case GL_STENCIL_BACK_WRITEMASK:
1403 case GL_STENCIL_CLEAR_VALUE:
1404 case GL_SUBPIXEL_BITS:
1405 case GL_MAX_TEXTURE_SIZE:
1406 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1407 case GL_SAMPLE_BUFFERS:
1408 case GL_SAMPLES:
1409 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1410 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1411 case GL_TEXTURE_BINDING_2D:
1412 case GL_TEXTURE_BINDING_CUBE_MAP:
1413 {
1414 *type = GL_INT;
1415 *numParams = 1;
1416 }
1417 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001418 case GL_MAX_SAMPLES_ANGLE:
1419 {
1420 if (getMaxSupportedSamples() != 0)
1421 {
1422 *type = GL_INT;
1423 *numParams = 1;
1424 }
1425 else
1426 {
1427 return false;
1428 }
1429 }
1430 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001431 case GL_MAX_VIEWPORT_DIMS:
1432 {
1433 *type = GL_INT;
1434 *numParams = 2;
1435 }
1436 break;
1437 case GL_VIEWPORT:
1438 case GL_SCISSOR_BOX:
1439 {
1440 *type = GL_INT;
1441 *numParams = 4;
1442 }
1443 break;
1444 case GL_SHADER_COMPILER:
1445 case GL_SAMPLE_COVERAGE_INVERT:
1446 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001447 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1448 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1449 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1450 case GL_SAMPLE_COVERAGE:
1451 case GL_SCISSOR_TEST:
1452 case GL_STENCIL_TEST:
1453 case GL_DEPTH_TEST:
1454 case GL_BLEND:
1455 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001456 {
1457 *type = GL_BOOL;
1458 *numParams = 1;
1459 }
1460 break;
1461 case GL_COLOR_WRITEMASK:
1462 {
1463 *type = GL_BOOL;
1464 *numParams = 4;
1465 }
1466 break;
1467 case GL_POLYGON_OFFSET_FACTOR:
1468 case GL_POLYGON_OFFSET_UNITS:
1469 case GL_SAMPLE_COVERAGE_VALUE:
1470 case GL_DEPTH_CLEAR_VALUE:
1471 case GL_LINE_WIDTH:
1472 {
1473 *type = GL_FLOAT;
1474 *numParams = 1;
1475 }
1476 break;
1477 case GL_ALIASED_LINE_WIDTH_RANGE:
1478 case GL_ALIASED_POINT_SIZE_RANGE:
1479 case GL_DEPTH_RANGE:
1480 {
1481 *type = GL_FLOAT;
1482 *numParams = 2;
1483 }
1484 break;
1485 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001486 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001487 {
1488 *type = GL_FLOAT;
1489 *numParams = 4;
1490 }
1491 break;
1492 default:
1493 return false;
1494 }
1495
1496 return true;
1497}
1498
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499// Applies the render target surface, depth stencil surface, viewport rectangle and
1500// scissor rectangle to the Direct3D 9 device
1501bool Context::applyRenderTarget(bool ignoreViewport)
1502{
1503 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001504
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001505 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001506
1507 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1508 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001509 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1510
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001511 return false;
1512 }
1513
1514 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001515 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001516
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001517 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1518 if (renderTargetSerial != mAppliedRenderTargetSerial)
1519 {
1520 device->SetRenderTarget(0, renderTarget);
1521 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001522 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001523 }
1524
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001525 unsigned int depthbufferSerial = 0;
1526 unsigned int stencilbufferSerial = 0;
1527 if (framebufferObject->getDepthbufferType() != GL_NONE)
1528 {
1529 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1530 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1531 }
1532 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1533 {
1534 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1535 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1536 }
1537
1538 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1539 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001540 {
1541 device->SetDepthStencilSurface(depthStencil);
1542 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001543 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545
1546 D3DVIEWPORT9 viewport;
1547 D3DSURFACE_DESC desc;
1548 renderTarget->GetDesc(&desc);
1549
1550 if (ignoreViewport)
1551 {
1552 viewport.X = 0;
1553 viewport.Y = 0;
1554 viewport.Width = desc.Width;
1555 viewport.Height = desc.Height;
1556 viewport.MinZ = 0.0f;
1557 viewport.MaxZ = 1.0f;
1558 }
1559 else
1560 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001561 viewport.X = std::max(mState.viewportX, 0);
1562 viewport.Y = std::max(mState.viewportY, 0);
1563 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1564 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1565 viewport.MinZ = clamp01(mState.zNear);
1566 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001567 }
1568
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001569 if (viewport.Width <= 0 || viewport.Height <= 0)
1570 {
1571 return false; // Nothing to render
1572 }
1573
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574 device->SetViewport(&viewport);
1575
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001576 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001578 if (mState.scissorTest)
1579 {
1580 RECT rect = {mState.scissorX,
1581 mState.scissorY,
1582 mState.scissorX + mState.scissorWidth,
1583 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001584 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1585 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001586 device->SetScissorRect(&rect);
1587 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1588 }
1589 else
1590 {
1591 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1592 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001593
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001594 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595 }
1596
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001597 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599 Program *programObject = getCurrentProgram();
1600
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001601 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001602 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001604
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001605 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001606 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1607 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1608 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001609 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1610
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001611 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001612 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001613 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1614
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001615 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001616 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001617
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001618 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001619 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001620
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001621 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001622 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001623 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624 }
1625
1626 return true;
1627}
1628
1629// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001630void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631{
1632 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001633 Program *programObject = getCurrentProgram();
1634
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001635 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001636 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001637 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001639 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001640 GLint alwaysFront = !isTriangleMode(drawMode);
1641 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1642
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001643 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001644
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001645 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001647 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001649 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650 }
1651 else
1652 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001653 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654 }
1655
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001656 mCullStateDirty = false;
1657 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001658
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001659 if (mDepthStateDirty)
1660 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001661 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001662 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001663 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1664 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665 }
1666 else
1667 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001668 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001670
1671 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 }
1673
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001674 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001676 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001677 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001678 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1679
1680 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1681 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1682 {
1683 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1684 }
1685 else
1686 {
1687 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1688 unorm<8>(mState.blendColor.alpha),
1689 unorm<8>(mState.blendColor.alpha),
1690 unorm<8>(mState.blendColor.alpha)));
1691 }
1692
1693 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1694 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1695 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1696
1697 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1698 mState.destBlendRGB != mState.destBlendAlpha ||
1699 mState.blendEquationRGB != mState.blendEquationAlpha)
1700 {
1701 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1702
1703 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1704 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1705 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1706
1707 }
1708 else
1709 {
1710 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1711 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001712 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001713 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001714 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001715 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001716 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001717
1718 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719 }
1720
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001721 if (mStencilStateDirty || mFrontFaceDirty)
1722 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001723 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001724 {
1725 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1726 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1727
1728 // FIXME: Unsupported by D3D9
1729 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1730 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1731 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1732 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1733 mState.stencilRef != mState.stencilBackRef ||
1734 mState.stencilMask != mState.stencilBackMask)
1735 {
1736 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1737 return error(GL_INVALID_OPERATION);
1738 }
1739
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001740 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001741 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001742 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1743
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001744 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1745 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1746 es2dx::ConvertComparison(mState.stencilFunc));
1747
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001748 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1750
1751 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1752 es2dx::ConvertStencilOp(mState.stencilFail));
1753 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1754 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1755 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1756 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1757
1758 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1759 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1760 es2dx::ConvertComparison(mState.stencilBackFunc));
1761
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001762 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1764
1765 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1766 es2dx::ConvertStencilOp(mState.stencilBackFail));
1767 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1768 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1769 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1770 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1771 }
1772 else
1773 {
1774 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1775 }
1776
1777 mStencilStateDirty = false;
1778 }
1779
1780 if (mMaskStateDirty)
1781 {
1782 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1783 mState.colorMaskBlue, mState.colorMaskAlpha));
1784 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1785
1786 mMaskStateDirty = false;
1787 }
1788
1789 if (mPolygonOffsetStateDirty)
1790 {
1791 if (mState.polygonOffsetFill)
1792 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001793 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001794 if (depthbuffer)
1795 {
1796 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1797 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1798 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1799 }
1800 }
1801 else
1802 {
1803 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1804 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1805 }
1806
1807 mPolygonOffsetStateDirty = false;
1808 }
1809
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001810 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001812 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001813 {
1814 FIXME("Sample alpha to coverage is unimplemented.");
1815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001817 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001818 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001819 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1820 unsigned int mask = 0;
1821 if (mState.sampleCoverageValue != 0)
1822 {
1823 float threshold = 0.5f;
1824
1825 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1826 {
1827 mask <<= 1;
1828
1829 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1830 {
1831 threshold += 1.0f;
1832 mask |= 1;
1833 }
1834 }
1835 }
1836
1837 if (mState.sampleCoverageInvert)
1838 {
1839 mask = ~mask;
1840 }
1841
1842 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1843 }
1844 else
1845 {
1846 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001847 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848
1849 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850 }
1851
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001852 if (mDitherStateDirty)
1853 {
1854 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1855
1856 mDitherStateDirty = false;
1857 }
1858
1859 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860}
1861
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001862// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001863void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001865 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001867 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001869 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 }
1871 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001872}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001874GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001875{
1876 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1877
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001878 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001879 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001880 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001881 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001882 }
1883
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001884 lookupAttributeMapping(translated);
1885
1886 mBufferBackEnd->setupAttributesPreDraw(translated);
1887
1888 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1889 {
1890 if (translated[i].enabled && translated[i].nonArray)
1891 {
1892 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1893 if (err != GL_NO_ERROR)
1894 {
1895 return err;
1896 }
1897
1898 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1899
1900 *useIndexing = true;
1901 return GL_NO_ERROR;
1902 }
1903 }
1904
1905 *useIndexing = false;
1906 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001907}
1908
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001909GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001910{
1911 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1912
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001913 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001914
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001915 if (err == GL_NO_ERROR)
1916 {
1917 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001918
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001919 mBufferBackEnd->setupAttributesPreDraw(translated);
1920 }
1921
1922 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001923}
1924
1925// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001926GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001927{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001928 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001929
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001930 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001931 {
1932 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1933 }
1934
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001935 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936}
1937
1938// Applies the shaders and shader constants to the Direct3D 9 device
1939void Context::applyShaders()
1940{
1941 IDirect3DDevice9 *device = getDevice();
1942 Program *programObject = getCurrentProgram();
1943 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1944 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1945
1946 device->SetVertexShader(vertexShader);
1947 device->SetPixelShader(pixelShader);
1948
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001949 if (programObject->getSerial() != mAppliedProgram)
1950 {
1951 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001952 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001953 mAppliedProgram = programObject->getSerial();
1954 }
1955
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956 programObject->applyUniforms();
1957}
1958
1959// Applies the textures and sampler states to the Direct3D 9 device
1960void Context::applyTextures()
1961{
1962 IDirect3DDevice9 *device = getDevice();
1963 Program *programObject = getCurrentProgram();
1964
1965 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1966 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001967 int textureUnit = programObject->getSamplerMapping(sampler);
1968 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001970 SamplerType textureType = programObject->getSamplerType(sampler);
1971
1972 Texture *texture = getSamplerTexture(textureUnit, textureType);
1973
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001974 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001975 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001976 if (texture->isComplete())
1977 {
1978 GLenum wrapS = texture->getWrapS();
1979 GLenum wrapT = texture->getWrapT();
1980 GLenum minFilter = texture->getMinFilter();
1981 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001983 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1984 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001986 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1987 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1988 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1989 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1990 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001992 device->SetTexture(sampler, texture->getTexture());
1993 }
1994 else
1995 {
1996 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1997 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001998 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001999
2000 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002002 else
2003 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002004 if (programObject->isSamplerDirty(sampler))
2005 {
2006 device->SetTexture(sampler, NULL);
2007 programObject->setSamplerDirty(sampler, false);
2008 }
2009 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010 }
2011}
2012
2013void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2014{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002015 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002016
2017 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2018 {
2019 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2020 }
2021
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002022 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2023 {
2024 return error(GL_INVALID_OPERATION);
2025 }
2026
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2028 IDirect3DDevice9 *device = getDevice();
2029
2030 D3DSURFACE_DESC desc;
2031 renderTarget->GetDesc(&desc);
2032
2033 IDirect3DSurface9 *systemSurface;
2034 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2035
2036 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2037 {
2038 return error(GL_OUT_OF_MEMORY);
2039 }
2040
2041 ASSERT(SUCCEEDED(result));
2042
2043 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2044 {
2045 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2046 }
2047
2048 result = device->GetRenderTargetData(renderTarget, systemSurface);
2049
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 if (FAILED(result))
2051 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 systemSurface->Release();
2053
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002054 switch (result)
2055 {
2056 case D3DERR_DRIVERINTERNALERROR:
2057 case D3DERR_DEVICELOST:
2058 return error(GL_OUT_OF_MEMORY);
2059 default:
2060 UNREACHABLE();
2061 return; // No sensible error to generate
2062 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063 }
2064
2065 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002066 RECT rect = {std::max(x, 0),
2067 std::max(y, 0),
2068 std::min(x + width, (int)desc.Width),
2069 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070
2071 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2072
2073 if (FAILED(result))
2074 {
2075 UNREACHABLE();
2076 systemSurface->Release();
2077
2078 return; // No sensible error to generate
2079 }
2080
2081 unsigned char *source = (unsigned char*)lock.pBits;
2082 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002083 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002085 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002086
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 for (int j = 0; j < rect.bottom - rect.top; j++)
2088 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002089 if (desc.Format == D3DFMT_A8R8G8B8 &&
2090 format == GL_BGRA_EXT &&
2091 type == GL_UNSIGNED_BYTE)
2092 {
2093 // Fast path for EXT_read_format_bgra, given
2094 // an RGBA source buffer. Note that buffers with no
2095 // alpha go through the slow path below.
2096 memcpy(dest + j * outputPitch,
2097 source + j * lock.Pitch,
2098 (rect.right - rect.left) * 4);
2099 continue;
2100 }
2101
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 for (int i = 0; i < rect.right - rect.left; i++)
2103 {
2104 float r;
2105 float g;
2106 float b;
2107 float a;
2108
2109 switch (desc.Format)
2110 {
2111 case D3DFMT_R5G6B5:
2112 {
2113 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2114
2115 a = 1.0f;
2116 b = (rgb & 0x001F) * (1.0f / 0x001F);
2117 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2118 r = (rgb & 0xF800) * (1.0f / 0xF800);
2119 }
2120 break;
2121 case D3DFMT_X1R5G5B5:
2122 {
2123 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2124
2125 a = 1.0f;
2126 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2127 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2128 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2129 }
2130 break;
2131 case D3DFMT_A1R5G5B5:
2132 {
2133 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2134
2135 a = (argb & 0x8000) ? 1.0f : 0.0f;
2136 b = (argb & 0x001F) * (1.0f / 0x001F);
2137 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2138 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2139 }
2140 break;
2141 case D3DFMT_A8R8G8B8:
2142 {
2143 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2144
2145 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2146 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2147 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2148 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2149 }
2150 break;
2151 case D3DFMT_X8R8G8B8:
2152 {
2153 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2154
2155 a = 1.0f;
2156 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2157 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2158 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2159 }
2160 break;
2161 case D3DFMT_A2R10G10B10:
2162 {
2163 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2164
2165 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2166 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2167 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2168 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2169 }
2170 break;
2171 default:
2172 UNIMPLEMENTED(); // FIXME
2173 UNREACHABLE();
2174 }
2175
2176 switch (format)
2177 {
2178 case GL_RGBA:
2179 switch (type)
2180 {
2181 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002182 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2183 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2184 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2185 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186 break;
2187 default: UNREACHABLE();
2188 }
2189 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002190 case GL_BGRA_EXT:
2191 switch (type)
2192 {
2193 case GL_UNSIGNED_BYTE:
2194 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2195 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2196 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2197 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2198 break;
2199 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2200 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2201 // this type is packed as follows:
2202 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2203 // --------------------------------------------------------------------------------
2204 // | 4th | 3rd | 2nd | 1st component |
2205 // --------------------------------------------------------------------------------
2206 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2207 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2208 ((unsigned short)(15 * a + 0.5f) << 12)|
2209 ((unsigned short)(15 * r + 0.5f) << 8) |
2210 ((unsigned short)(15 * g + 0.5f) << 4) |
2211 ((unsigned short)(15 * b + 0.5f) << 0);
2212 break;
2213 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2214 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2215 // this type is packed as follows:
2216 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2217 // --------------------------------------------------------------------------------
2218 // | 4th | 3rd | 2nd | 1st component |
2219 // --------------------------------------------------------------------------------
2220 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2221 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2222 ((unsigned short)( a + 0.5f) << 15) |
2223 ((unsigned short)(31 * r + 0.5f) << 10) |
2224 ((unsigned short)(31 * g + 0.5f) << 5) |
2225 ((unsigned short)(31 * b + 0.5f) << 0);
2226 break;
2227 default: UNREACHABLE();
2228 }
2229 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002230 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231 switch (type)
2232 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002233 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002234 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2235 ((unsigned short)(31 * b + 0.5f) << 0) |
2236 ((unsigned short)(63 * g + 0.5f) << 5) |
2237 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 break;
2239 default: UNREACHABLE();
2240 }
2241 break;
2242 default: UNREACHABLE();
2243 }
2244 }
2245 }
2246
2247 systemSurface->UnlockRect();
2248
2249 systemSurface->Release();
2250}
2251
2252void Context::clear(GLbitfield mask)
2253{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002254 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002255
2256 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2257 {
2258 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2259
2260 return;
2261 }
2262
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002263 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 IDirect3DDevice9 *device = getDevice();
2265 DWORD flags = 0;
2266
2267 if (mask & GL_COLOR_BUFFER_BIT)
2268 {
2269 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002270
2271 if (framebufferObject->getColorbufferType() != GL_NONE)
2272 {
2273 flags |= D3DCLEAR_TARGET;
2274 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275 }
2276
2277 if (mask & GL_DEPTH_BUFFER_BIT)
2278 {
2279 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002280 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281 {
2282 flags |= D3DCLEAR_ZBUFFER;
2283 }
2284 }
2285
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 GLuint stencilUnmasked = 0x0;
2287
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002288 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002291 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002293 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2294 D3DSURFACE_DESC desc;
2295 depthStencil->GetDesc(&desc);
2296
2297 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2298 stencilUnmasked = (0x1 << stencilSize) - 1;
2299
2300 if (stencilUnmasked != 0x0)
2301 {
2302 flags |= D3DCLEAR_STENCIL;
2303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 }
2305 }
2306
2307 if (mask != 0)
2308 {
2309 return error(GL_INVALID_VALUE);
2310 }
2311
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002312 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2313 {
2314 return;
2315 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002317 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2318 unorm<8>(mState.colorClearValue.red),
2319 unorm<8>(mState.colorClearValue.green),
2320 unorm<8>(mState.colorClearValue.blue));
2321 float depth = clamp01(mState.depthClearValue);
2322 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323
2324 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2325
2326 D3DSURFACE_DESC desc;
2327 renderTarget->GetDesc(&desc);
2328
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002329 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330
2331 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002332 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002334 !(mState.colorMaskRed && mState.colorMaskGreen &&
2335 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
2337 if (needMaskedColorClear || needMaskedStencilClear)
2338 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002339 // State which is altered in all paths from this point to the clear call is saved.
2340 // State which is altered in only some paths will be flagged dirty in the case that
2341 // that path is taken.
2342 HRESULT hr;
2343 if (mMaskedClearSavedState == NULL)
2344 {
2345 hr = device->BeginStateBlock();
2346 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2347
2348 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2349 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2350 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2351 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2352 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2353 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2354 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2355 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2356 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2357 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2358 device->SetPixelShader(NULL);
2359 device->SetVertexShader(NULL);
2360 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2361 device->SetStreamSourceFreq(0, 1);
2362
2363 hr = device->EndStateBlock(&mMaskedClearSavedState);
2364 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2365 }
2366
2367 ASSERT(mMaskedClearSavedState != NULL);
2368
2369 if (mMaskedClearSavedState != NULL)
2370 {
2371 hr = mMaskedClearSavedState->Capture();
2372 ASSERT(SUCCEEDED(hr));
2373 }
2374
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2376 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2377 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2378 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2379 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2380 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2381 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2382 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2383
2384 if (flags & D3DCLEAR_TARGET)
2385 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002386 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2387 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2388 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2389 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002390 }
2391 else
2392 {
2393 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2394 }
2395
2396 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2397 {
2398 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2399 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2400 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2401 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002402 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002403 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2405 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002406 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 }
2408 else
2409 {
2410 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2411 }
2412
2413 device->SetPixelShader(NULL);
2414 device->SetVertexShader(NULL);
2415 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002416 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002418 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 {
2420 float x, y, z, w;
2421 D3DCOLOR diffuse;
2422 };
2423
2424 Vertex quad[4];
2425 quad[0].x = 0.0f;
2426 quad[0].y = (float)desc.Height;
2427 quad[0].z = 0.0f;
2428 quad[0].w = 1.0f;
2429 quad[0].diffuse = color;
2430
2431 quad[1].x = (float)desc.Width;
2432 quad[1].y = (float)desc.Height;
2433 quad[1].z = 0.0f;
2434 quad[1].w = 1.0f;
2435 quad[1].diffuse = color;
2436
2437 quad[2].x = 0.0f;
2438 quad[2].y = 0.0f;
2439 quad[2].z = 0.0f;
2440 quad[2].w = 1.0f;
2441 quad[2].diffuse = color;
2442
2443 quad[3].x = (float)desc.Width;
2444 quad[3].y = 0.0f;
2445 quad[3].z = 0.0f;
2446 quad[3].w = 1.0f;
2447 quad[3].diffuse = color;
2448
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002449 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451
2452 if (flags & D3DCLEAR_ZBUFFER)
2453 {
2454 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2455 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2456 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2457 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002458
2459 if (mMaskedClearSavedState != NULL)
2460 {
2461 mMaskedClearSavedState->Apply();
2462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002464 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 {
2466 device->Clear(0, NULL, flags, color, depth, stencil);
2467 }
2468}
2469
2470void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2471{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002472 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 {
2474 return error(GL_INVALID_OPERATION);
2475 }
2476
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002477 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 IDirect3DDevice9 *device = getDevice();
2479 D3DPRIMITIVETYPE primitiveType;
2480 int primitiveCount;
2481
2482 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2483 return error(GL_INVALID_ENUM);
2484
2485 if (primitiveCount <= 0)
2486 {
2487 return;
2488 }
2489
2490 if (!applyRenderTarget(false))
2491 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002492 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493 }
2494
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002495 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002496
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002497 TranslatedIndexData indexInfo;
2498 bool useIndexing;
2499 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002500 if (err != GL_NO_ERROR)
2501 {
2502 return error(err);
2503 }
2504
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 applyShaders();
2506 applyTextures();
2507
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002508 if (!getCurrentProgram()->validateSamplers())
2509 {
2510 return error(GL_INVALID_OPERATION);
2511 }
2512
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002513 if (!cullSkipsDraw(mode))
2514 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002515 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002516 if (useIndexing)
2517 {
2518 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2519 }
2520 else
2521 {
2522 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2523 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525}
2526
2527void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2528{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002529 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530 {
2531 return error(GL_INVALID_OPERATION);
2532 }
2533
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002534 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535 {
2536 return error(GL_INVALID_OPERATION);
2537 }
2538
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002539 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540 IDirect3DDevice9 *device = getDevice();
2541 D3DPRIMITIVETYPE primitiveType;
2542 int primitiveCount;
2543
2544 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2545 return error(GL_INVALID_ENUM);
2546
2547 if (primitiveCount <= 0)
2548 {
2549 return;
2550 }
2551
2552 if (!applyRenderTarget(false))
2553 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002554 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 }
2556
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002557 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002558
2559 TranslatedIndexData indexInfo;
2560 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2561 if (err != GL_NO_ERROR)
2562 {
2563 return error(err);
2564 }
2565
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002566 err = applyVertexBuffer(indexInfo);
2567 if (err != GL_NO_ERROR)
2568 {
2569 return error(err);
2570 }
2571
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002572 applyShaders();
2573 applyTextures();
2574
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002575 if (!getCurrentProgram()->validateSamplers())
2576 {
2577 return error(GL_INVALID_OPERATION);
2578 }
2579
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002580 if (!cullSkipsDraw(mode))
2581 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002582 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002583 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002584 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002585}
2586
2587void Context::finish()
2588{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002589 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590 IDirect3DDevice9 *device = getDevice();
2591 IDirect3DQuery9 *occlusionQuery = NULL;
2592
2593 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2594
2595 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2596 {
2597 return error(GL_OUT_OF_MEMORY);
2598 }
2599
2600 ASSERT(SUCCEEDED(result));
2601
2602 if (occlusionQuery)
2603 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002604 IDirect3DStateBlock9 *savedState = NULL;
2605 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2606
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607 occlusionQuery->Issue(D3DISSUE_BEGIN);
2608
2609 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002610 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611 device->SetPixelShader(NULL);
2612 device->SetVertexShader(NULL);
2613 device->SetFVF(D3DFVF_XYZRHW);
2614 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002615 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617
2618 occlusionQuery->Issue(D3DISSUE_END);
2619
2620 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2621 {
2622 // Keep polling, but allow other threads to do something useful first
2623 Sleep(0);
2624 }
2625
2626 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002627
2628 if (savedState)
2629 {
2630 savedState->Apply();
2631 savedState->Release();
2632 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633 }
2634}
2635
2636void Context::flush()
2637{
2638 IDirect3DDevice9 *device = getDevice();
2639 IDirect3DQuery9 *eventQuery = NULL;
2640
2641 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2642
2643 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2644 {
2645 return error(GL_OUT_OF_MEMORY);
2646 }
2647
2648 ASSERT(SUCCEEDED(result));
2649
2650 if (eventQuery)
2651 {
2652 eventQuery->Issue(D3DISSUE_END);
2653
2654 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2655 {
2656 // Keep polling, but allow other threads to do something useful first
2657 Sleep(0);
2658 }
2659
2660 eventQuery->Release();
2661 }
2662}
2663
2664void Context::recordInvalidEnum()
2665{
2666 mInvalidEnum = true;
2667}
2668
2669void Context::recordInvalidValue()
2670{
2671 mInvalidValue = true;
2672}
2673
2674void Context::recordInvalidOperation()
2675{
2676 mInvalidOperation = true;
2677}
2678
2679void Context::recordOutOfMemory()
2680{
2681 mOutOfMemory = true;
2682}
2683
2684void Context::recordInvalidFramebufferOperation()
2685{
2686 mInvalidFramebufferOperation = true;
2687}
2688
2689// Get one of the recorded errors and clear its flag, if any.
2690// [OpenGL ES 2.0.24] section 2.5 page 13.
2691GLenum Context::getError()
2692{
2693 if (mInvalidEnum)
2694 {
2695 mInvalidEnum = false;
2696
2697 return GL_INVALID_ENUM;
2698 }
2699
2700 if (mInvalidValue)
2701 {
2702 mInvalidValue = false;
2703
2704 return GL_INVALID_VALUE;
2705 }
2706
2707 if (mInvalidOperation)
2708 {
2709 mInvalidOperation = false;
2710
2711 return GL_INVALID_OPERATION;
2712 }
2713
2714 if (mOutOfMemory)
2715 {
2716 mOutOfMemory = false;
2717
2718 return GL_OUT_OF_MEMORY;
2719 }
2720
2721 if (mInvalidFramebufferOperation)
2722 {
2723 mInvalidFramebufferOperation = false;
2724
2725 return GL_INVALID_FRAMEBUFFER_OPERATION;
2726 }
2727
2728 return GL_NO_ERROR;
2729}
2730
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002731bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002732{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002733 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002734}
2735
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002736int Context::getMaxSupportedSamples() const
2737{
2738 return mMaxSupportedSamples;
2739}
2740
2741int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2742{
2743 if (requested == 0)
2744 {
2745 return requested;
2746 }
2747
2748 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2749 if (itr == mMultiSampleSupport.end())
2750 {
2751 return -1;
2752 }
2753
2754 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2755 {
2756 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2757 {
2758 return i;
2759 }
2760 }
2761
2762 return -1;
2763}
2764
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002765void Context::detachBuffer(GLuint buffer)
2766{
2767 // [OpenGL ES 2.0.24] section 2.9 page 22:
2768 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2769 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2770
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002771 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002773 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774 }
2775
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002776 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002778 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002779 }
2780
2781 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2782 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002783 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002785 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786 }
2787 }
2788}
2789
2790void Context::detachTexture(GLuint texture)
2791{
2792 // [OpenGL ES 2.0.24] section 3.8 page 84:
2793 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2794 // rebound to texture object zero
2795
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002796 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002797 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002798 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002799 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002800 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002801 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002802 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002803 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804 }
2805 }
2806
2807 // [OpenGL ES 2.0.24] section 4.4 page 112:
2808 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2809 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2810 // image was attached in the currently bound framebuffer.
2811
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002812 Framebuffer *readFramebuffer = getReadFramebuffer();
2813 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002814
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002815 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002817 readFramebuffer->detachTexture(texture);
2818 }
2819
2820 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2821 {
2822 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002823 }
2824}
2825
2826void Context::detachFramebuffer(GLuint framebuffer)
2827{
2828 // [OpenGL ES 2.0.24] section 4.4 page 107:
2829 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2830 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2831
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002832 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002833 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002834 bindReadFramebuffer(0);
2835 }
2836
2837 if (mState.drawFramebuffer == framebuffer)
2838 {
2839 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002840 }
2841}
2842
2843void Context::detachRenderbuffer(GLuint renderbuffer)
2844{
2845 // [OpenGL ES 2.0.24] section 4.4 page 109:
2846 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2847 // had been executed with the target RENDERBUFFER and name of zero.
2848
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002849 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850 {
2851 bindRenderbuffer(0);
2852 }
2853
2854 // [OpenGL ES 2.0.24] section 4.4 page 111:
2855 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2856 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2857 // point to which this image was attached in the currently bound framebuffer.
2858
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002859 Framebuffer *readFramebuffer = getReadFramebuffer();
2860 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002862 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002863 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002864 readFramebuffer->detachRenderbuffer(renderbuffer);
2865 }
2866
2867 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2868 {
2869 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870 }
2871}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002872
2873Texture *Context::getIncompleteTexture(SamplerType type)
2874{
2875 Texture *t = mIncompleteTextures[type];
2876
2877 if (t == NULL)
2878 {
2879 static const GLubyte color[] = { 0, 0, 0, 255 };
2880
2881 switch (type)
2882 {
2883 default:
2884 UNREACHABLE();
2885 // default falls through to SAMPLER_2D
2886
2887 case SAMPLER_2D:
2888 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002889 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002890 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002891 t = incomplete2d;
2892 }
2893 break;
2894
2895 case SAMPLER_CUBE:
2896 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002897 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002898
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002899 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2900 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2901 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2902 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2903 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2904 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002905
2906 t = incompleteCube;
2907 }
2908 break;
2909 }
2910
2911 mIncompleteTextures[type] = t;
2912 }
2913
2914 return t;
2915}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002916
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002917bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002918{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002919 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002920}
2921
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002922bool Context::isTriangleMode(GLenum drawMode)
2923{
2924 switch (drawMode)
2925 {
2926 case GL_TRIANGLES:
2927 case GL_TRIANGLE_FAN:
2928 case GL_TRIANGLE_STRIP:
2929 return true;
2930 case GL_POINTS:
2931 case GL_LINES:
2932 case GL_LINE_LOOP:
2933 case GL_LINE_STRIP:
2934 return false;
2935 default: UNREACHABLE();
2936 }
2937
2938 return false;
2939}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002940
2941void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2942{
2943 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2944
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002945 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2946 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2947 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2948 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002949
2950 mVertexDataManager->dirtyCurrentValues();
2951}
2952
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002953void Context::initExtensionString()
2954{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002955 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002956 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
2957 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00002958 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002959
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002960
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00002961 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002962 {
2963 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
2964 }
2965
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002966 if (mBufferBackEnd->supportIntIndices())
2967 {
2968 mExtensionString += "GL_OES_element_index_uint ";
2969 }
2970
2971 std::string::size_type end = mExtensionString.find_last_not_of(' ');
2972 if (end != std::string::npos)
2973 {
2974 mExtensionString.resize(end+1);
2975 }
2976}
2977
2978const char *Context::getExtensionString() const
2979{
2980 return mExtensionString.c_str();
2981}
2982
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00002983void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2984 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2985 GLbitfield mask)
2986{
2987 IDirect3DDevice9 *device = getDevice();
2988
2989 Framebuffer *readFramebuffer = getReadFramebuffer();
2990 Framebuffer *drawFramebuffer = getDrawFramebuffer();
2991
2992 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
2993 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2994 {
2995 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2996 }
2997
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002998 if (drawFramebuffer->getSamples() != 0)
2999 {
3000 return error(GL_INVALID_OPERATION);
3001 }
3002
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003003 RECT sourceRect;
3004 RECT destRect;
3005
3006 if (srcX0 < srcX1)
3007 {
3008 sourceRect.left = srcX0;
3009 sourceRect.right = srcX1;
3010 destRect.left = dstX0;
3011 destRect.right = dstX1;
3012 }
3013 else
3014 {
3015 sourceRect.left = srcX1;
3016 destRect.left = dstX1;
3017 sourceRect.right = srcX0;
3018 destRect.right = dstX0;
3019 }
3020
3021 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3022 // flip our Y-values here
3023 if (srcY0 < srcY1)
3024 {
3025 sourceRect.bottom = srcY1;
3026 destRect.bottom = dstY1;
3027 sourceRect.top = srcY0;
3028 destRect.top = dstY0;
3029 }
3030 else
3031 {
3032 sourceRect.bottom = srcY0;
3033 destRect.bottom = dstY0;
3034 sourceRect.top = srcY1;
3035 destRect.top = dstY1;
3036 }
3037
3038 RECT sourceScissoredRect = sourceRect;
3039 RECT destScissoredRect = destRect;
3040
3041 if (mState.scissorTest)
3042 {
3043 // Only write to parts of the destination framebuffer which pass the scissor test
3044 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3045 // rect will be checked against scissorY, rather than the bottom.
3046 if (destRect.left < mState.scissorX)
3047 {
3048 int xDiff = mState.scissorX - destRect.left;
3049 destScissoredRect.left = mState.scissorX;
3050 sourceScissoredRect.left += xDiff;
3051 }
3052
3053 if (destRect.right > mState.scissorX + mState.scissorWidth)
3054 {
3055 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3056 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3057 sourceScissoredRect.right -= xDiff;
3058 }
3059
3060 if (destRect.top < mState.scissorY)
3061 {
3062 int yDiff = mState.scissorY - destRect.top;
3063 destScissoredRect.top = mState.scissorY;
3064 sourceScissoredRect.top += yDiff;
3065 }
3066
3067 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3068 {
3069 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3070 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3071 sourceScissoredRect.bottom -= yDiff;
3072 }
3073 }
3074
3075 bool blitRenderTarget = false;
3076 bool blitDepthStencil = false;
3077
3078 RECT sourceTrimmedRect = sourceScissoredRect;
3079 RECT destTrimmedRect = destScissoredRect;
3080
3081 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3082 // the actual draw and read surfaces.
3083 if (sourceTrimmedRect.left < 0)
3084 {
3085 int xDiff = 0 - sourceTrimmedRect.left;
3086 sourceTrimmedRect.left = 0;
3087 destTrimmedRect.left += xDiff;
3088 }
3089
3090 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3091 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3092 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3093 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3094
3095 if (sourceTrimmedRect.right > readBufferWidth)
3096 {
3097 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3098 sourceTrimmedRect.right = readBufferWidth;
3099 destTrimmedRect.right -= xDiff;
3100 }
3101
3102 if (sourceTrimmedRect.top < 0)
3103 {
3104 int yDiff = 0 - sourceTrimmedRect.top;
3105 sourceTrimmedRect.top = 0;
3106 destTrimmedRect.top += yDiff;
3107 }
3108
3109 if (sourceTrimmedRect.bottom > readBufferHeight)
3110 {
3111 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3112 sourceTrimmedRect.bottom = readBufferHeight;
3113 destTrimmedRect.bottom -= yDiff;
3114 }
3115
3116 if (destTrimmedRect.left < 0)
3117 {
3118 int xDiff = 0 - destTrimmedRect.left;
3119 destTrimmedRect.left = 0;
3120 sourceTrimmedRect.left += xDiff;
3121 }
3122
3123 if (destTrimmedRect.right > drawBufferWidth)
3124 {
3125 int xDiff = destTrimmedRect.right - drawBufferWidth;
3126 destTrimmedRect.right = drawBufferWidth;
3127 sourceTrimmedRect.right -= xDiff;
3128 }
3129
3130 if (destTrimmedRect.top < 0)
3131 {
3132 int yDiff = 0 - destTrimmedRect.top;
3133 destTrimmedRect.top = 0;
3134 sourceTrimmedRect.top += yDiff;
3135 }
3136
3137 if (destTrimmedRect.bottom > drawBufferHeight)
3138 {
3139 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3140 destTrimmedRect.bottom = drawBufferHeight;
3141 sourceTrimmedRect.bottom -= yDiff;
3142 }
3143
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003144 bool partialBufferCopy = false;
3145 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3146 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3147 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3148 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3149 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3150 {
3151 partialBufferCopy = true;
3152 }
3153
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003154 if (mask & GL_COLOR_BUFFER_BIT)
3155 {
3156 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3157 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3158 {
3159 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3160 return error(GL_INVALID_OPERATION);
3161 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003162
3163 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3164 {
3165 return error(GL_INVALID_OPERATION);
3166 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003167
3168 blitRenderTarget = true;
3169
3170 }
3171
3172 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3173 {
3174 DepthStencilbuffer *readDSBuffer = NULL;
3175 DepthStencilbuffer *drawDSBuffer = NULL;
3176
3177 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3178 // both a depth and stencil buffer, it will be the same buffer.
3179
3180 if (mask & GL_DEPTH_BUFFER_BIT)
3181 {
3182 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3183 {
3184 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3185 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3186 {
3187 return error(GL_INVALID_OPERATION);
3188 }
3189
3190 blitDepthStencil = true;
3191 readDSBuffer = readFramebuffer->getDepthbuffer();
3192 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3193 }
3194 }
3195
3196 if (mask & GL_STENCIL_BUFFER_BIT)
3197 {
3198 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3199 {
3200 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3201 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3202 {
3203 return error(GL_INVALID_OPERATION);
3204 }
3205
3206 blitDepthStencil = true;
3207 readDSBuffer = readFramebuffer->getStencilbuffer();
3208 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3209 }
3210 }
3211
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003212 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003213 {
3214 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3215 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3216 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003217
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003218 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3219 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003220 {
3221 return error(GL_INVALID_OPERATION);
3222 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003223 }
3224
3225 if (blitRenderTarget || blitDepthStencil)
3226 {
3227 egl::Display *display = getDisplay();
3228 display->endScene();
3229
3230 if (blitRenderTarget)
3231 {
3232 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3233 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3234
3235 if (FAILED(result))
3236 {
3237 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3238 return;
3239 }
3240 }
3241
3242 if (blitDepthStencil)
3243 {
3244 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3245
3246 if (FAILED(result))
3247 {
3248 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3249 return;
3250 }
3251 }
3252 }
3253}
3254
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003255}
3256
3257extern "C"
3258{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003259gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003260{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003261 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003262}
3263
3264void glDestroyContext(gl::Context *context)
3265{
3266 delete context;
3267
3268 if (context == gl::getContext())
3269 {
3270 gl::makeCurrent(NULL, NULL, NULL);
3271 }
3272}
3273
3274void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3275{
3276 gl::makeCurrent(context, display, surface);
3277}
3278
3279gl::Context *glGetCurrentContext()
3280{
3281 return gl::getContext();
3282}
3283}