blob: 0f4217d30352a0e1dd57906dbcf8869c5119fc6e [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,
245 D3DFMT_R5G6B5,
246 D3DFMT_D24S8
247 };
248
249 int max = 0;
250 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
251 {
252 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
253 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
254 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
255
256 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
257 {
258 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
259 {
260 max = j;
261 }
262 }
263 }
264
265 mMaxSupportedSamples = max;
266
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000267 initExtensionString();
268
269 mState.viewportX = 0;
270 mState.viewportY = 0;
271 mState.viewportWidth = surface->getWidth();
272 mState.viewportHeight = surface->getHeight();
273
274 mState.scissorX = 0;
275 mState.scissorY = 0;
276 mState.scissorWidth = surface->getWidth();
277 mState.scissorHeight = surface->getHeight();
278
279 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000280 }
281
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
283 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000284 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000287 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000288 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289
290 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291
292 defaultRenderTarget->Release();
293
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000294 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000296 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000298
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000299 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000300
301 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302}
303
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000304// 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 +0000305void Context::markAllStateDirty()
306{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000307 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000308 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000309 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000310
311 mClearStateDirty = true;
312 mCullStateDirty = true;
313 mDepthStateDirty = true;
314 mMaskStateDirty = true;
315 mBlendStateDirty = true;
316 mStencilStateDirty = true;
317 mPolygonOffsetStateDirty = true;
318 mScissorStateDirty = true;
319 mSampleStateDirty = true;
320 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000321 mFrontFaceDirty = true;
322
323 if (mBufferBackEnd != NULL)
324 {
325 mBufferBackEnd->invalidate();
326 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000327}
328
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329void Context::setClearColor(float red, float green, float blue, float alpha)
330{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000331 mState.colorClearValue.red = red;
332 mState.colorClearValue.green = green;
333 mState.colorClearValue.blue = blue;
334 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335}
336
337void Context::setClearDepth(float depth)
338{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000339 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340}
341
342void Context::setClearStencil(int stencil)
343{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000344 mState.stencilClearValue = stencil;
345}
346
347void Context::setCullFace(bool enabled)
348{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000349 if (mState.cullFace != enabled)
350 {
351 mState.cullFace = enabled;
352 mCullStateDirty = true;
353 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000354}
355
356bool Context::isCullFaceEnabled() const
357{
358 return mState.cullFace;
359}
360
361void Context::setCullMode(GLenum mode)
362{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000363 if (mState.cullMode != mode)
364 {
365 mState.cullMode = mode;
366 mCullStateDirty = true;
367 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000368}
369
370void Context::setFrontFace(GLenum front)
371{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000372 if (mState.frontFace != front)
373 {
374 mState.frontFace = front;
375 mFrontFaceDirty = true;
376 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000377}
378
379void Context::setDepthTest(bool enabled)
380{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000381 if (mState.depthTest != enabled)
382 {
383 mState.depthTest = enabled;
384 mDepthStateDirty = true;
385 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000386}
387
388bool Context::isDepthTestEnabled() const
389{
390 return mState.depthTest;
391}
392
393void Context::setDepthFunc(GLenum depthFunc)
394{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000395 if (mState.depthFunc != depthFunc)
396 {
397 mState.depthFunc = depthFunc;
398 mDepthStateDirty = true;
399 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000400}
401
402void Context::setDepthRange(float zNear, float zFar)
403{
404 mState.zNear = zNear;
405 mState.zFar = zFar;
406}
407
408void Context::setBlend(bool enabled)
409{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000410 if (mState.blend != enabled)
411 {
412 mState.blend = enabled;
413 mBlendStateDirty = true;
414 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000415}
416
417bool Context::isBlendEnabled() const
418{
419 return mState.blend;
420}
421
422void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
423{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000424 if (mState.sourceBlendRGB != sourceRGB ||
425 mState.sourceBlendAlpha != sourceAlpha ||
426 mState.destBlendRGB != destRGB ||
427 mState.destBlendAlpha != destAlpha)
428 {
429 mState.sourceBlendRGB = sourceRGB;
430 mState.destBlendRGB = destRGB;
431 mState.sourceBlendAlpha = sourceAlpha;
432 mState.destBlendAlpha = destAlpha;
433 mBlendStateDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437void Context::setBlendColor(float red, float green, float blue, float alpha)
438{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000439 if (mState.blendColor.red != red ||
440 mState.blendColor.green != green ||
441 mState.blendColor.blue != blue ||
442 mState.blendColor.alpha != alpha)
443 {
444 mState.blendColor.red = red;
445 mState.blendColor.green = green;
446 mState.blendColor.blue = blue;
447 mState.blendColor.alpha = alpha;
448 mBlendStateDirty = true;
449 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000450}
451
452void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
453{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000454 if (mState.blendEquationRGB != rgbEquation ||
455 mState.blendEquationAlpha != alphaEquation)
456 {
457 mState.blendEquationRGB = rgbEquation;
458 mState.blendEquationAlpha = alphaEquation;
459 mBlendStateDirty = true;
460 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000461}
462
463void Context::setStencilTest(bool enabled)
464{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000465 if (mState.stencilTest != enabled)
466 {
467 mState.stencilTest = enabled;
468 mStencilStateDirty = true;
469 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000470}
471
472bool Context::isStencilTestEnabled() const
473{
474 return mState.stencilTest;
475}
476
477void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
478{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000479 if (mState.stencilFunc != stencilFunc ||
480 mState.stencilRef != stencilRef ||
481 mState.stencilMask != stencilMask)
482 {
483 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000484 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000485 mState.stencilMask = stencilMask;
486 mStencilStateDirty = true;
487 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000488}
489
490void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
491{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000492 if (mState.stencilBackFunc != stencilBackFunc ||
493 mState.stencilBackRef != stencilBackRef ||
494 mState.stencilBackMask != stencilBackMask)
495 {
496 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000497 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000498 mState.stencilBackMask = stencilBackMask;
499 mStencilStateDirty = true;
500 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000501}
502
503void Context::setStencilWritemask(GLuint stencilWritemask)
504{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000505 if (mState.stencilWritemask != stencilWritemask)
506 {
507 mState.stencilWritemask = stencilWritemask;
508 mStencilStateDirty = true;
509 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000510}
511
512void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
513{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000514 if (mState.stencilBackWritemask != stencilBackWritemask)
515 {
516 mState.stencilBackWritemask = stencilBackWritemask;
517 mStencilStateDirty = true;
518 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000519}
520
521void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
522{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000523 if (mState.stencilFail != stencilFail ||
524 mState.stencilPassDepthFail != stencilPassDepthFail ||
525 mState.stencilPassDepthPass != stencilPassDepthPass)
526 {
527 mState.stencilFail = stencilFail;
528 mState.stencilPassDepthFail = stencilPassDepthFail;
529 mState.stencilPassDepthPass = stencilPassDepthPass;
530 mStencilStateDirty = true;
531 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000532}
533
534void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.stencilBackFail != stencilBackFail ||
537 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
538 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
539 {
540 mState.stencilBackFail = stencilBackFail;
541 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
542 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
543 mStencilStateDirty = true;
544 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000545}
546
547void Context::setPolygonOffsetFill(bool enabled)
548{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000549 if (mState.polygonOffsetFill != enabled)
550 {
551 mState.polygonOffsetFill = enabled;
552 mPolygonOffsetStateDirty = true;
553 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000554}
555
556bool Context::isPolygonOffsetFillEnabled() const
557{
558 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000559
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000560}
561
562void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
563{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000564 if (mState.polygonOffsetFactor != factor ||
565 mState.polygonOffsetUnits != units)
566 {
567 mState.polygonOffsetFactor = factor;
568 mState.polygonOffsetUnits = units;
569 mPolygonOffsetStateDirty = true;
570 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000571}
572
573void Context::setSampleAlphaToCoverage(bool enabled)
574{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000575 if (mState.sampleAlphaToCoverage != enabled)
576 {
577 mState.sampleAlphaToCoverage = enabled;
578 mSampleStateDirty = true;
579 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000580}
581
582bool Context::isSampleAlphaToCoverageEnabled() const
583{
584 return mState.sampleAlphaToCoverage;
585}
586
587void Context::setSampleCoverage(bool enabled)
588{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000589 if (mState.sampleCoverage != enabled)
590 {
591 mState.sampleCoverage = enabled;
592 mSampleStateDirty = true;
593 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000594}
595
596bool Context::isSampleCoverageEnabled() const
597{
598 return mState.sampleCoverage;
599}
600
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000601void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000602{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000603 if (mState.sampleCoverageValue != value ||
604 mState.sampleCoverageInvert != invert)
605 {
606 mState.sampleCoverageValue = value;
607 mState.sampleCoverageInvert = invert;
608 mSampleStateDirty = true;
609 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000610}
611
612void Context::setScissorTest(bool enabled)
613{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000614 if (mState.scissorTest != enabled)
615 {
616 mState.scissorTest = enabled;
617 mScissorStateDirty = true;
618 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000619}
620
621bool Context::isScissorTestEnabled() const
622{
623 return mState.scissorTest;
624}
625
626void Context::setDither(bool enabled)
627{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000628 if (mState.dither != enabled)
629 {
630 mState.dither = enabled;
631 mDitherStateDirty = true;
632 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000633}
634
635bool Context::isDitherEnabled() const
636{
637 return mState.dither;
638}
639
640void Context::setLineWidth(GLfloat width)
641{
642 mState.lineWidth = width;
643}
644
645void Context::setGenerateMipmapHint(GLenum hint)
646{
647 mState.generateMipmapHint = hint;
648}
649
650void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
651{
652 mState.viewportX = x;
653 mState.viewportY = y;
654 mState.viewportWidth = width;
655 mState.viewportHeight = height;
656}
657
658void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
659{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000660 if (mState.scissorX != x || mState.scissorY != y ||
661 mState.scissorWidth != width || mState.scissorHeight != height)
662 {
663 mState.scissorX = x;
664 mState.scissorY = y;
665 mState.scissorWidth = width;
666 mState.scissorHeight = height;
667 mScissorStateDirty = true;
668 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000669}
670
671void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
672{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000673 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
674 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
675 {
676 mState.colorMaskRed = red;
677 mState.colorMaskGreen = green;
678 mState.colorMaskBlue = blue;
679 mState.colorMaskAlpha = alpha;
680 mMaskStateDirty = true;
681 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000682}
683
684void Context::setDepthMask(bool mask)
685{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000686 if (mState.depthMask != mask)
687 {
688 mState.depthMask = mask;
689 mMaskStateDirty = true;
690 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000691}
692
693void Context::setActiveSampler(int active)
694{
695 mState.activeSampler = active;
696}
697
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000698GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000699{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000700 return mState.readFramebuffer;
701}
702
703GLuint Context::getDrawFramebufferHandle() const
704{
705 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000706}
707
708GLuint Context::getRenderbufferHandle() const
709{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000710 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000711}
712
713GLuint Context::getArrayBufferHandle() const
714{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000715 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716}
717
718void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
719{
720 mState.vertexAttribute[attribNum].mEnabled = enabled;
721}
722
723const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
724{
725 return mState.vertexAttribute[attribNum];
726}
727
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000728void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000729 GLsizei stride, const void *pointer)
730{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000731 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000732 mState.vertexAttribute[attribNum].mSize = size;
733 mState.vertexAttribute[attribNum].mType = type;
734 mState.vertexAttribute[attribNum].mNormalized = normalized;
735 mState.vertexAttribute[attribNum].mStride = stride;
736 mState.vertexAttribute[attribNum].mPointer = pointer;
737}
738
739const void *Context::getVertexAttribPointer(unsigned int attribNum) const
740{
741 return mState.vertexAttribute[attribNum].mPointer;
742}
743
744// returns entire set of attributes as a block
745const AttributeState *Context::getVertexAttribBlock()
746{
747 return mState.vertexAttribute;
748}
749
750void Context::setPackAlignment(GLint alignment)
751{
752 mState.packAlignment = alignment;
753}
754
755GLint Context::getPackAlignment() const
756{
757 return mState.packAlignment;
758}
759
760void Context::setUnpackAlignment(GLint alignment)
761{
762 mState.unpackAlignment = alignment;
763}
764
765GLint Context::getUnpackAlignment() const
766{
767 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768}
769
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770GLuint Context::createBuffer()
771{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000772 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773}
774
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775GLuint Context::createProgram()
776{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000777 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778}
779
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000780GLuint Context::createShader(GLenum type)
781{
782 return mResourceManager->createShader(type);
783}
784
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785GLuint Context::createTexture()
786{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000787 return mResourceManager->createTexture();
788}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000790GLuint Context::createRenderbuffer()
791{
792 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793}
794
795// Returns an unused framebuffer name
796GLuint Context::createFramebuffer()
797{
798 unsigned int handle = 1;
799
800 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
801 {
802 handle++;
803 }
804
805 mFramebufferMap[handle] = NULL;
806
807 return handle;
808}
809
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810void Context::deleteBuffer(GLuint buffer)
811{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000812 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813 {
814 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000816
817 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818}
819
820void Context::deleteShader(GLuint shader)
821{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000822 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823}
824
825void Context::deleteProgram(GLuint program)
826{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000827 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828}
829
830void Context::deleteTexture(GLuint texture)
831{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000832 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833 {
834 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000836
837 mResourceManager->deleteTexture(texture);
838}
839
840void Context::deleteRenderbuffer(GLuint renderbuffer)
841{
842 if (mResourceManager->getRenderbuffer(renderbuffer))
843 {
844 detachRenderbuffer(renderbuffer);
845 }
846
847 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
850void Context::deleteFramebuffer(GLuint framebuffer)
851{
852 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
853
854 if (framebufferObject != mFramebufferMap.end())
855 {
856 detachFramebuffer(framebuffer);
857
858 delete framebufferObject->second;
859 mFramebufferMap.erase(framebufferObject);
860 }
861}
862
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000863Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 return mResourceManager->getBuffer(handle);
866}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868Shader *Context::getShader(GLuint handle)
869{
870 return mResourceManager->getShader(handle);
871}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000873Program *Context::getProgram(GLuint handle)
874{
875 return mResourceManager->getProgram(handle);
876}
877
878Texture *Context::getTexture(GLuint handle)
879{
880 return mResourceManager->getTexture(handle);
881}
882
883Renderbuffer *Context::getRenderbuffer(GLuint handle)
884{
885 return mResourceManager->getRenderbuffer(handle);
886}
887
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000888Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000889{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000890 return getFramebuffer(mState.readFramebuffer);
891}
892
893Framebuffer *Context::getDrawFramebuffer()
894{
895 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896}
897
898void Context::bindArrayBuffer(unsigned int buffer)
899{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000900 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000902 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
904
905void Context::bindElementArrayBuffer(unsigned int buffer)
906{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000909 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
912void Context::bindTexture2D(GLuint texture)
913{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000914 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000916 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000918 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
921void Context::bindTextureCubeMap(GLuint texture)
922{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000923 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000925 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000927 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928}
929
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000930void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931{
932 if (!getFramebuffer(framebuffer))
933 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000934 mFramebufferMap[framebuffer] = new Framebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935 }
936
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000937 mState.readFramebuffer = framebuffer;
938}
939
940void Context::bindDrawFramebuffer(GLuint framebuffer)
941{
942 if (!getFramebuffer(framebuffer))
943 {
944 mFramebufferMap[framebuffer] = new Framebuffer();
945 }
946
947 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
950void Context::bindRenderbuffer(GLuint renderbuffer)
951{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000952 mResourceManager->checkRenderbufferAllocation(renderbuffer);
953
954 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955}
956
957void Context::useProgram(GLuint program)
958{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000959 GLuint priorProgram = mState.currentProgram;
960 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000961
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000962 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000964 Program *newProgram = mResourceManager->getProgram(program);
965 Program *oldProgram = mResourceManager->getProgram(priorProgram);
966
967 if (newProgram)
968 {
969 newProgram->addRef();
970 }
971
972 if (oldProgram)
973 {
974 oldProgram->release();
975 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
979void Context::setFramebufferZero(Framebuffer *buffer)
980{
981 delete mFramebufferMap[0];
982 mFramebufferMap[0] = buffer;
983}
984
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000985void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000987 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
988 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989}
990
991Framebuffer *Context::getFramebuffer(unsigned int handle)
992{
993 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000994
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995 if (framebuffer == mFramebufferMap.end())
996 {
997 return NULL;
998 }
999 else
1000 {
1001 return framebuffer->second;
1002 }
1003}
1004
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005Buffer *Context::getArrayBuffer()
1006{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001007 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008}
1009
1010Buffer *Context::getElementArrayBuffer()
1011{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001012 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013}
1014
1015Program *Context::getCurrentProgram()
1016{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001017 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018}
1019
1020Texture2D *Context::getTexture2D()
1021{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001022 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 +00001023 {
1024 return mTexture2DZero;
1025 }
1026
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001027 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
1030TextureCubeMap *Context::getTextureCubeMap()
1031{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001032 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 +00001033 {
1034 return mTextureCubeMapZero;
1035 }
1036
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001037 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001040Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001042 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001043
1044 if (texid == 0)
1045 {
1046 switch (type)
1047 {
1048 default: UNREACHABLE();
1049 case SAMPLER_2D: return mTexture2DZero;
1050 case SAMPLER_CUBE: return mTextureCubeMapZero;
1051 }
1052 }
1053
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001054 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001055}
1056
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001057bool Context::getBooleanv(GLenum pname, GLboolean *params)
1058{
1059 switch (pname)
1060 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001061 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1062 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1063 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001064 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001065 params[0] = mState.colorMaskRed;
1066 params[1] = mState.colorMaskGreen;
1067 params[2] = mState.colorMaskBlue;
1068 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001069 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001070 case GL_CULL_FACE: *params = mState.cullFace;
1071 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1072 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1073 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1074 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1075 case GL_STENCIL_TEST: *params = mState.stencilTest;
1076 case GL_DEPTH_TEST: *params = mState.depthTest;
1077 case GL_BLEND: *params = mState.blend;
1078 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001079 default:
1080 return false;
1081 }
1082
1083 return true;
1084}
1085
1086bool Context::getFloatv(GLenum pname, GLfloat *params)
1087{
1088 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1089 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1090 // GetIntegerv as its native query function. As it would require conversion in any
1091 // case, this should make no difference to the calling application.
1092 switch (pname)
1093 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001094 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1095 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1096 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1097 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1098 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001099 case GL_ALIASED_LINE_WIDTH_RANGE:
1100 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1101 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1102 break;
1103 case GL_ALIASED_POINT_SIZE_RANGE:
1104 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001105 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 +00001106 break;
1107 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001108 params[0] = mState.zNear;
1109 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001110 break;
1111 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001112 params[0] = mState.colorClearValue.red;
1113 params[1] = mState.colorClearValue.green;
1114 params[2] = mState.colorClearValue.blue;
1115 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001116 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001117 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001118 params[0] = mState.blendColor.red;
1119 params[1] = mState.blendColor.green;
1120 params[2] = mState.blendColor.blue;
1121 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001122 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001123 default:
1124 return false;
1125 }
1126
1127 return true;
1128}
1129
1130bool Context::getIntegerv(GLenum pname, GLint *params)
1131{
1132 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1133 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1134 // GetIntegerv as its native query function. As it would require conversion in any
1135 // case, this should make no difference to the calling application. You may find it in
1136 // Context::getFloatv.
1137 switch (pname)
1138 {
1139 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1140 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1141 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1142 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1143 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1144 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1145 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1146 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001147 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1148 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
1149 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
1150 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001151 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1152 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001153 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1154 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1155 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001156 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001157 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1158 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1159 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1160 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1161 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1162 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1163 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1164 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1165 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1166 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1167 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1168 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1169 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1170 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1171 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1172 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1173 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1174 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1175 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1176 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1177 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1178 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1179 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1180 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1181 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1182 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1183 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1184 case GL_SUBPIXEL_BITS: *params = 4; break;
1185 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1186 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001187 case GL_MAX_SAMPLES_ANGLE:
1188 {
1189 GLsizei maxSamples = getMaxSupportedSamples();
1190 if (maxSamples != 0)
1191 {
1192 *params = maxSamples;
1193 }
1194 else
1195 {
1196 return false;
1197 }
1198
1199 break;
1200 }
1201 case GL_SAMPLE_BUFFERS:
1202 case GL_SAMPLES:
1203 {
1204 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1205 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1206 {
1207 switch (pname)
1208 {
1209 case GL_SAMPLE_BUFFERS:
1210 if (framebuffer->getSamples() != 0)
1211 {
1212 *params = 1;
1213 }
1214 else
1215 {
1216 *params = 0;
1217 }
1218 break;
1219 case GL_SAMPLES:
1220 *params = framebuffer->getSamples();
1221 break;
1222 }
1223 }
1224 else
1225 {
1226 *params = 0;
1227 }
1228 }
1229 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001230 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1231 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1232 case GL_MAX_VIEWPORT_DIMS:
1233 {
1234 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1235 params[0] = maxDimension;
1236 params[1] = maxDimension;
1237 }
1238 break;
1239 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001240 params[0] = mState.viewportX;
1241 params[1] = mState.viewportY;
1242 params[2] = mState.viewportWidth;
1243 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001244 break;
1245 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001246 params[0] = mState.scissorX;
1247 params[1] = mState.scissorY;
1248 params[2] = mState.scissorWidth;
1249 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001250 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001251 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1252 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001253 case GL_RED_BITS:
1254 case GL_GREEN_BITS:
1255 case GL_BLUE_BITS:
1256 case GL_ALPHA_BITS:
1257 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001258 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001259 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1260
1261 if (colorbuffer)
1262 {
1263 switch (pname)
1264 {
1265 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1266 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1267 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1268 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1269 }
1270 }
1271 else
1272 {
1273 *params = 0;
1274 }
1275 }
1276 break;
1277 case GL_DEPTH_BITS:
1278 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001279 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001280 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001281
1282 if (depthbuffer)
1283 {
1284 *params = depthbuffer->getDepthSize();
1285 }
1286 else
1287 {
1288 *params = 0;
1289 }
1290 }
1291 break;
1292 case GL_STENCIL_BITS:
1293 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001294 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001295 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001296
1297 if (stencilbuffer)
1298 {
1299 *params = stencilbuffer->getStencilSize();
1300 }
1301 else
1302 {
1303 *params = 0;
1304 }
1305 }
1306 break;
1307 case GL_TEXTURE_BINDING_2D:
1308 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001309 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001310 {
1311 error(GL_INVALID_OPERATION);
1312 return false;
1313 }
1314
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001315 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001316 }
1317 break;
1318 case GL_TEXTURE_BINDING_CUBE_MAP:
1319 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001320 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001321 {
1322 error(GL_INVALID_OPERATION);
1323 return false;
1324 }
1325
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001326 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001327 }
1328 break;
1329 default:
1330 return false;
1331 }
1332
1333 return true;
1334}
1335
1336bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1337{
1338 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1339 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1340 // to the fact that it is stored internally as a float, and so would require conversion
1341 // if returned from Context::getIntegerv. Since this conversion is already implemented
1342 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1343 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1344 // application.
1345 switch (pname)
1346 {
1347 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1348 case GL_SHADER_BINARY_FORMATS:
1349 {
1350 *type = GL_INT;
1351 *numParams = 0;
1352 }
1353 break;
1354 case GL_MAX_VERTEX_ATTRIBS:
1355 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1356 case GL_MAX_VARYING_VECTORS:
1357 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1358 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1359 case GL_MAX_TEXTURE_IMAGE_UNITS:
1360 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1361 case GL_MAX_RENDERBUFFER_SIZE:
1362 case GL_NUM_SHADER_BINARY_FORMATS:
1363 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1364 case GL_ARRAY_BUFFER_BINDING:
1365 case GL_FRAMEBUFFER_BINDING:
1366 case GL_RENDERBUFFER_BINDING:
1367 case GL_CURRENT_PROGRAM:
1368 case GL_PACK_ALIGNMENT:
1369 case GL_UNPACK_ALIGNMENT:
1370 case GL_GENERATE_MIPMAP_HINT:
1371 case GL_RED_BITS:
1372 case GL_GREEN_BITS:
1373 case GL_BLUE_BITS:
1374 case GL_ALPHA_BITS:
1375 case GL_DEPTH_BITS:
1376 case GL_STENCIL_BITS:
1377 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1378 case GL_CULL_FACE_MODE:
1379 case GL_FRONT_FACE:
1380 case GL_ACTIVE_TEXTURE:
1381 case GL_STENCIL_FUNC:
1382 case GL_STENCIL_VALUE_MASK:
1383 case GL_STENCIL_REF:
1384 case GL_STENCIL_FAIL:
1385 case GL_STENCIL_PASS_DEPTH_FAIL:
1386 case GL_STENCIL_PASS_DEPTH_PASS:
1387 case GL_STENCIL_BACK_FUNC:
1388 case GL_STENCIL_BACK_VALUE_MASK:
1389 case GL_STENCIL_BACK_REF:
1390 case GL_STENCIL_BACK_FAIL:
1391 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1392 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1393 case GL_DEPTH_FUNC:
1394 case GL_BLEND_SRC_RGB:
1395 case GL_BLEND_SRC_ALPHA:
1396 case GL_BLEND_DST_RGB:
1397 case GL_BLEND_DST_ALPHA:
1398 case GL_BLEND_EQUATION_RGB:
1399 case GL_BLEND_EQUATION_ALPHA:
1400 case GL_STENCIL_WRITEMASK:
1401 case GL_STENCIL_BACK_WRITEMASK:
1402 case GL_STENCIL_CLEAR_VALUE:
1403 case GL_SUBPIXEL_BITS:
1404 case GL_MAX_TEXTURE_SIZE:
1405 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1406 case GL_SAMPLE_BUFFERS:
1407 case GL_SAMPLES:
1408 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1409 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1410 case GL_TEXTURE_BINDING_2D:
1411 case GL_TEXTURE_BINDING_CUBE_MAP:
1412 {
1413 *type = GL_INT;
1414 *numParams = 1;
1415 }
1416 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001417 case GL_MAX_SAMPLES_ANGLE:
1418 {
1419 if (getMaxSupportedSamples() != 0)
1420 {
1421 *type = GL_INT;
1422 *numParams = 1;
1423 }
1424 else
1425 {
1426 return false;
1427 }
1428 }
1429 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001430 case GL_MAX_VIEWPORT_DIMS:
1431 {
1432 *type = GL_INT;
1433 *numParams = 2;
1434 }
1435 break;
1436 case GL_VIEWPORT:
1437 case GL_SCISSOR_BOX:
1438 {
1439 *type = GL_INT;
1440 *numParams = 4;
1441 }
1442 break;
1443 case GL_SHADER_COMPILER:
1444 case GL_SAMPLE_COVERAGE_INVERT:
1445 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001446 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1447 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1448 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1449 case GL_SAMPLE_COVERAGE:
1450 case GL_SCISSOR_TEST:
1451 case GL_STENCIL_TEST:
1452 case GL_DEPTH_TEST:
1453 case GL_BLEND:
1454 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001455 {
1456 *type = GL_BOOL;
1457 *numParams = 1;
1458 }
1459 break;
1460 case GL_COLOR_WRITEMASK:
1461 {
1462 *type = GL_BOOL;
1463 *numParams = 4;
1464 }
1465 break;
1466 case GL_POLYGON_OFFSET_FACTOR:
1467 case GL_POLYGON_OFFSET_UNITS:
1468 case GL_SAMPLE_COVERAGE_VALUE:
1469 case GL_DEPTH_CLEAR_VALUE:
1470 case GL_LINE_WIDTH:
1471 {
1472 *type = GL_FLOAT;
1473 *numParams = 1;
1474 }
1475 break;
1476 case GL_ALIASED_LINE_WIDTH_RANGE:
1477 case GL_ALIASED_POINT_SIZE_RANGE:
1478 case GL_DEPTH_RANGE:
1479 {
1480 *type = GL_FLOAT;
1481 *numParams = 2;
1482 }
1483 break;
1484 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001485 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001486 {
1487 *type = GL_FLOAT;
1488 *numParams = 4;
1489 }
1490 break;
1491 default:
1492 return false;
1493 }
1494
1495 return true;
1496}
1497
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498// Applies the render target surface, depth stencil surface, viewport rectangle and
1499// scissor rectangle to the Direct3D 9 device
1500bool Context::applyRenderTarget(bool ignoreViewport)
1501{
1502 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001503
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001504 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505
1506 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1507 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001508 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1509
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510 return false;
1511 }
1512
1513 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001514 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001516 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1517 if (renderTargetSerial != mAppliedRenderTargetSerial)
1518 {
1519 device->SetRenderTarget(0, renderTarget);
1520 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001521 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 +00001522 }
1523
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001524 unsigned int depthbufferSerial = 0;
1525 unsigned int stencilbufferSerial = 0;
1526 if (framebufferObject->getDepthbufferType() != GL_NONE)
1527 {
1528 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1529 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1530 }
1531 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1532 {
1533 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1534 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1535 }
1536
1537 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1538 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001539 {
1540 device->SetDepthStencilSurface(depthStencil);
1541 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001542 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001544
1545 D3DVIEWPORT9 viewport;
1546 D3DSURFACE_DESC desc;
1547 renderTarget->GetDesc(&desc);
1548
1549 if (ignoreViewport)
1550 {
1551 viewport.X = 0;
1552 viewport.Y = 0;
1553 viewport.Width = desc.Width;
1554 viewport.Height = desc.Height;
1555 viewport.MinZ = 0.0f;
1556 viewport.MaxZ = 1.0f;
1557 }
1558 else
1559 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001560 viewport.X = std::max(mState.viewportX, 0);
1561 viewport.Y = std::max(mState.viewportY, 0);
1562 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1563 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1564 viewport.MinZ = clamp01(mState.zNear);
1565 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001566 }
1567
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001568 if (viewport.Width <= 0 || viewport.Height <= 0)
1569 {
1570 return false; // Nothing to render
1571 }
1572
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001573 device->SetViewport(&viewport);
1574
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001575 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001577 if (mState.scissorTest)
1578 {
1579 RECT rect = {mState.scissorX,
1580 mState.scissorY,
1581 mState.scissorX + mState.scissorWidth,
1582 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001583 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1584 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001585 device->SetScissorRect(&rect);
1586 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1587 }
1588 else
1589 {
1590 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1591 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001592
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001593 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594 }
1595
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001596 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598 Program *programObject = getCurrentProgram();
1599
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001600 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001601 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001603
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001604 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001605 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1606 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1607 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001608 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1609
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001610 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001611 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001612 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1613
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001614 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001615 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001616
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001617 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001618 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001619
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001620 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001621 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001622 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623 }
1624
1625 return true;
1626}
1627
1628// 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 +00001629void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630{
1631 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001632 Program *programObject = getCurrentProgram();
1633
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001634 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001635 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001636 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001637
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001638 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001639 GLint alwaysFront = !isTriangleMode(drawMode);
1640 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1641
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001642 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001643
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001644 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001645 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001646 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001648 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649 }
1650 else
1651 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001652 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653 }
1654
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001655 mCullStateDirty = false;
1656 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001658 if (mDepthStateDirty)
1659 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001660 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001662 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1663 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 }
1665 else
1666 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001667 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001669
1670 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 }
1672
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001673 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001675 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001676 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001677 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1678
1679 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1680 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1681 {
1682 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1683 }
1684 else
1685 {
1686 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1687 unorm<8>(mState.blendColor.alpha),
1688 unorm<8>(mState.blendColor.alpha),
1689 unorm<8>(mState.blendColor.alpha)));
1690 }
1691
1692 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1693 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1694 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1695
1696 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1697 mState.destBlendRGB != mState.destBlendAlpha ||
1698 mState.blendEquationRGB != mState.blendEquationAlpha)
1699 {
1700 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1701
1702 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1703 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1704 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1705
1706 }
1707 else
1708 {
1709 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1710 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001711 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001712 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001713 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001714 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001715 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001716
1717 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001718 }
1719
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001720 if (mStencilStateDirty || mFrontFaceDirty)
1721 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001722 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001723 {
1724 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1725 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1726
1727 // FIXME: Unsupported by D3D9
1728 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1729 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1730 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1731 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1732 mState.stencilRef != mState.stencilBackRef ||
1733 mState.stencilMask != mState.stencilBackMask)
1734 {
1735 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1736 return error(GL_INVALID_OPERATION);
1737 }
1738
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001739 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001740 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001741 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1742
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001743 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1744 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1745 es2dx::ConvertComparison(mState.stencilFunc));
1746
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001747 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 +00001748 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1749
1750 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1751 es2dx::ConvertStencilOp(mState.stencilFail));
1752 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1753 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1754 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1755 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1756
1757 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1758 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1759 es2dx::ConvertComparison(mState.stencilBackFunc));
1760
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001761 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 +00001762 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1763
1764 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1765 es2dx::ConvertStencilOp(mState.stencilBackFail));
1766 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1767 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1768 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1769 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1770 }
1771 else
1772 {
1773 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1774 }
1775
1776 mStencilStateDirty = false;
1777 }
1778
1779 if (mMaskStateDirty)
1780 {
1781 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1782 mState.colorMaskBlue, mState.colorMaskAlpha));
1783 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1784
1785 mMaskStateDirty = false;
1786 }
1787
1788 if (mPolygonOffsetStateDirty)
1789 {
1790 if (mState.polygonOffsetFill)
1791 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001792 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001793 if (depthbuffer)
1794 {
1795 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1796 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1797 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1798 }
1799 }
1800 else
1801 {
1802 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1803 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1804 }
1805
1806 mPolygonOffsetStateDirty = false;
1807 }
1808
1809 if (mConfig->mMultiSample != 0 && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001811 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001812 {
1813 FIXME("Sample alpha to coverage is unimplemented.");
1814 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001816 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001817 {
1818 FIXME("Sample coverage is unimplemented.");
1819 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001820
1821 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822 }
1823
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 if (mDitherStateDirty)
1825 {
1826 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1827
1828 mDitherStateDirty = false;
1829 }
1830
1831 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832}
1833
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001834// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001835void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001837 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001839 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001841 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 }
1843 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001844}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001846GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001847{
1848 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1849
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001850 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001851 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001852 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001853 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001854 }
1855
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001856 lookupAttributeMapping(translated);
1857
1858 mBufferBackEnd->setupAttributesPreDraw(translated);
1859
1860 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1861 {
1862 if (translated[i].enabled && translated[i].nonArray)
1863 {
1864 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1865 if (err != GL_NO_ERROR)
1866 {
1867 return err;
1868 }
1869
1870 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1871
1872 *useIndexing = true;
1873 return GL_NO_ERROR;
1874 }
1875 }
1876
1877 *useIndexing = false;
1878 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001879}
1880
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001881GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001882{
1883 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1884
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001885 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001886
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001887 if (err == GL_NO_ERROR)
1888 {
1889 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001890
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001891 mBufferBackEnd->setupAttributesPreDraw(translated);
1892 }
1893
1894 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001895}
1896
1897// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001898GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001899{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001900 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001901
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001902 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001903 {
1904 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1905 }
1906
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001907 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908}
1909
1910// Applies the shaders and shader constants to the Direct3D 9 device
1911void Context::applyShaders()
1912{
1913 IDirect3DDevice9 *device = getDevice();
1914 Program *programObject = getCurrentProgram();
1915 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1916 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1917
1918 device->SetVertexShader(vertexShader);
1919 device->SetPixelShader(pixelShader);
1920
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001921 if (programObject->getSerial() != mAppliedProgram)
1922 {
1923 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001924 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001925 mAppliedProgram = programObject->getSerial();
1926 }
1927
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 programObject->applyUniforms();
1929}
1930
1931// Applies the textures and sampler states to the Direct3D 9 device
1932void Context::applyTextures()
1933{
1934 IDirect3DDevice9 *device = getDevice();
1935 Program *programObject = getCurrentProgram();
1936
1937 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1938 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001939 int textureUnit = programObject->getSamplerMapping(sampler);
1940 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001942 SamplerType textureType = programObject->getSamplerType(sampler);
1943
1944 Texture *texture = getSamplerTexture(textureUnit, textureType);
1945
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001946 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001947 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001948 if (texture->isComplete())
1949 {
1950 GLenum wrapS = texture->getWrapS();
1951 GLenum wrapT = texture->getWrapT();
1952 GLenum minFilter = texture->getMinFilter();
1953 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001955 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1956 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001958 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1959 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1960 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1961 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1962 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001964 device->SetTexture(sampler, texture->getTexture());
1965 }
1966 else
1967 {
1968 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1969 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001970 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001971
1972 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001974 else
1975 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001976 if (programObject->isSamplerDirty(sampler))
1977 {
1978 device->SetTexture(sampler, NULL);
1979 programObject->setSamplerDirty(sampler, false);
1980 }
1981 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982 }
1983}
1984
1985void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1986{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001987 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00001988
1989 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
1990 {
1991 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1992 }
1993
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001994 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
1995 {
1996 return error(GL_INVALID_OPERATION);
1997 }
1998
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2000 IDirect3DDevice9 *device = getDevice();
2001
2002 D3DSURFACE_DESC desc;
2003 renderTarget->GetDesc(&desc);
2004
2005 IDirect3DSurface9 *systemSurface;
2006 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2007
2008 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2009 {
2010 return error(GL_OUT_OF_MEMORY);
2011 }
2012
2013 ASSERT(SUCCEEDED(result));
2014
2015 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2016 {
2017 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2018 }
2019
2020 result = device->GetRenderTargetData(renderTarget, systemSurface);
2021
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 if (FAILED(result))
2023 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024 systemSurface->Release();
2025
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002026 switch (result)
2027 {
2028 case D3DERR_DRIVERINTERNALERROR:
2029 case D3DERR_DEVICELOST:
2030 return error(GL_OUT_OF_MEMORY);
2031 default:
2032 UNREACHABLE();
2033 return; // No sensible error to generate
2034 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 }
2036
2037 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002038 RECT rect = {std::max(x, 0),
2039 std::max(y, 0),
2040 std::min(x + width, (int)desc.Width),
2041 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002042
2043 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2044
2045 if (FAILED(result))
2046 {
2047 UNREACHABLE();
2048 systemSurface->Release();
2049
2050 return; // No sensible error to generate
2051 }
2052
2053 unsigned char *source = (unsigned char*)lock.pBits;
2054 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002055 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002057 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002058
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 for (int j = 0; j < rect.bottom - rect.top; j++)
2060 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002061 if (desc.Format == D3DFMT_A8R8G8B8 &&
2062 format == GL_BGRA_EXT &&
2063 type == GL_UNSIGNED_BYTE)
2064 {
2065 // Fast path for EXT_read_format_bgra, given
2066 // an RGBA source buffer. Note that buffers with no
2067 // alpha go through the slow path below.
2068 memcpy(dest + j * outputPitch,
2069 source + j * lock.Pitch,
2070 (rect.right - rect.left) * 4);
2071 continue;
2072 }
2073
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 for (int i = 0; i < rect.right - rect.left; i++)
2075 {
2076 float r;
2077 float g;
2078 float b;
2079 float a;
2080
2081 switch (desc.Format)
2082 {
2083 case D3DFMT_R5G6B5:
2084 {
2085 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2086
2087 a = 1.0f;
2088 b = (rgb & 0x001F) * (1.0f / 0x001F);
2089 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2090 r = (rgb & 0xF800) * (1.0f / 0xF800);
2091 }
2092 break;
2093 case D3DFMT_X1R5G5B5:
2094 {
2095 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2096
2097 a = 1.0f;
2098 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2099 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2100 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2101 }
2102 break;
2103 case D3DFMT_A1R5G5B5:
2104 {
2105 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2106
2107 a = (argb & 0x8000) ? 1.0f : 0.0f;
2108 b = (argb & 0x001F) * (1.0f / 0x001F);
2109 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2110 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2111 }
2112 break;
2113 case D3DFMT_A8R8G8B8:
2114 {
2115 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2116
2117 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2118 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2119 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2120 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2121 }
2122 break;
2123 case D3DFMT_X8R8G8B8:
2124 {
2125 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2126
2127 a = 1.0f;
2128 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2129 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2130 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2131 }
2132 break;
2133 case D3DFMT_A2R10G10B10:
2134 {
2135 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2136
2137 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2138 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2139 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2140 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2141 }
2142 break;
2143 default:
2144 UNIMPLEMENTED(); // FIXME
2145 UNREACHABLE();
2146 }
2147
2148 switch (format)
2149 {
2150 case GL_RGBA:
2151 switch (type)
2152 {
2153 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002154 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2155 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2156 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2157 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 break;
2159 default: UNREACHABLE();
2160 }
2161 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002162 case GL_BGRA_EXT:
2163 switch (type)
2164 {
2165 case GL_UNSIGNED_BYTE:
2166 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2167 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2168 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2169 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2170 break;
2171 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2172 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2173 // this type is packed as follows:
2174 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2175 // --------------------------------------------------------------------------------
2176 // | 4th | 3rd | 2nd | 1st component |
2177 // --------------------------------------------------------------------------------
2178 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2179 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2180 ((unsigned short)(15 * a + 0.5f) << 12)|
2181 ((unsigned short)(15 * r + 0.5f) << 8) |
2182 ((unsigned short)(15 * g + 0.5f) << 4) |
2183 ((unsigned short)(15 * b + 0.5f) << 0);
2184 break;
2185 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2186 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2187 // this type is packed as follows:
2188 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2189 // --------------------------------------------------------------------------------
2190 // | 4th | 3rd | 2nd | 1st component |
2191 // --------------------------------------------------------------------------------
2192 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2193 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2194 ((unsigned short)( a + 0.5f) << 15) |
2195 ((unsigned short)(31 * r + 0.5f) << 10) |
2196 ((unsigned short)(31 * g + 0.5f) << 5) |
2197 ((unsigned short)(31 * b + 0.5f) << 0);
2198 break;
2199 default: UNREACHABLE();
2200 }
2201 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002202 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203 switch (type)
2204 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002205 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002206 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2207 ((unsigned short)(31 * b + 0.5f) << 0) |
2208 ((unsigned short)(63 * g + 0.5f) << 5) |
2209 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002210 break;
2211 default: UNREACHABLE();
2212 }
2213 break;
2214 default: UNREACHABLE();
2215 }
2216 }
2217 }
2218
2219 systemSurface->UnlockRect();
2220
2221 systemSurface->Release();
2222}
2223
2224void Context::clear(GLbitfield mask)
2225{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002226 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002227
2228 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2229 {
2230 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2231
2232 return;
2233 }
2234
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002235 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002236 IDirect3DDevice9 *device = getDevice();
2237 DWORD flags = 0;
2238
2239 if (mask & GL_COLOR_BUFFER_BIT)
2240 {
2241 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002242
2243 if (framebufferObject->getColorbufferType() != GL_NONE)
2244 {
2245 flags |= D3DCLEAR_TARGET;
2246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247 }
2248
2249 if (mask & GL_DEPTH_BUFFER_BIT)
2250 {
2251 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002252 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 {
2254 flags |= D3DCLEAR_ZBUFFER;
2255 }
2256 }
2257
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258 GLuint stencilUnmasked = 0x0;
2259
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002260 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002263 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002265 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2266 D3DSURFACE_DESC desc;
2267 depthStencil->GetDesc(&desc);
2268
2269 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2270 stencilUnmasked = (0x1 << stencilSize) - 1;
2271
2272 if (stencilUnmasked != 0x0)
2273 {
2274 flags |= D3DCLEAR_STENCIL;
2275 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 }
2277 }
2278
2279 if (mask != 0)
2280 {
2281 return error(GL_INVALID_VALUE);
2282 }
2283
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002284 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2285 {
2286 return;
2287 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002289 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2290 unorm<8>(mState.colorClearValue.red),
2291 unorm<8>(mState.colorClearValue.green),
2292 unorm<8>(mState.colorClearValue.blue));
2293 float depth = clamp01(mState.depthClearValue);
2294 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295
2296 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2297
2298 D3DSURFACE_DESC desc;
2299 renderTarget->GetDesc(&desc);
2300
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002301 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302
2303 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002304 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002306 !(mState.colorMaskRed && mState.colorMaskGreen &&
2307 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308
2309 if (needMaskedColorClear || needMaskedStencilClear)
2310 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002311 // State which is altered in all paths from this point to the clear call is saved.
2312 // State which is altered in only some paths will be flagged dirty in the case that
2313 // that path is taken.
2314 HRESULT hr;
2315 if (mMaskedClearSavedState == NULL)
2316 {
2317 hr = device->BeginStateBlock();
2318 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2319
2320 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2321 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2322 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2323 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2324 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2325 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2326 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2327 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2328 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2329 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2330 device->SetPixelShader(NULL);
2331 device->SetVertexShader(NULL);
2332 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2333 device->SetStreamSourceFreq(0, 1);
2334
2335 hr = device->EndStateBlock(&mMaskedClearSavedState);
2336 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2337 }
2338
2339 ASSERT(mMaskedClearSavedState != NULL);
2340
2341 if (mMaskedClearSavedState != NULL)
2342 {
2343 hr = mMaskedClearSavedState->Capture();
2344 ASSERT(SUCCEEDED(hr));
2345 }
2346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2348 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2349 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2350 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2351 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2352 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2353 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2354 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2355
2356 if (flags & D3DCLEAR_TARGET)
2357 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002358 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2359 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2360 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2361 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362 }
2363 else
2364 {
2365 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2366 }
2367
2368 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2369 {
2370 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2371 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2372 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2373 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002374 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002375 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2377 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002378 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379 }
2380 else
2381 {
2382 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2383 }
2384
2385 device->SetPixelShader(NULL);
2386 device->SetVertexShader(NULL);
2387 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002388 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002390 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391 {
2392 float x, y, z, w;
2393 D3DCOLOR diffuse;
2394 };
2395
2396 Vertex quad[4];
2397 quad[0].x = 0.0f;
2398 quad[0].y = (float)desc.Height;
2399 quad[0].z = 0.0f;
2400 quad[0].w = 1.0f;
2401 quad[0].diffuse = color;
2402
2403 quad[1].x = (float)desc.Width;
2404 quad[1].y = (float)desc.Height;
2405 quad[1].z = 0.0f;
2406 quad[1].w = 1.0f;
2407 quad[1].diffuse = color;
2408
2409 quad[2].x = 0.0f;
2410 quad[2].y = 0.0f;
2411 quad[2].z = 0.0f;
2412 quad[2].w = 1.0f;
2413 quad[2].diffuse = color;
2414
2415 quad[3].x = (float)desc.Width;
2416 quad[3].y = 0.0f;
2417 quad[3].z = 0.0f;
2418 quad[3].w = 1.0f;
2419 quad[3].diffuse = color;
2420
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002421 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423
2424 if (flags & D3DCLEAR_ZBUFFER)
2425 {
2426 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2427 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2428 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2429 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002430
2431 if (mMaskedClearSavedState != NULL)
2432 {
2433 mMaskedClearSavedState->Apply();
2434 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002436 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437 {
2438 device->Clear(0, NULL, flags, color, depth, stencil);
2439 }
2440}
2441
2442void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2443{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002444 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 {
2446 return error(GL_INVALID_OPERATION);
2447 }
2448
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002449 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450 IDirect3DDevice9 *device = getDevice();
2451 D3DPRIMITIVETYPE primitiveType;
2452 int primitiveCount;
2453
2454 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2455 return error(GL_INVALID_ENUM);
2456
2457 if (primitiveCount <= 0)
2458 {
2459 return;
2460 }
2461
2462 if (!applyRenderTarget(false))
2463 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002464 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 }
2466
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002467 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002468
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002469 TranslatedIndexData indexInfo;
2470 bool useIndexing;
2471 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002472 if (err != GL_NO_ERROR)
2473 {
2474 return error(err);
2475 }
2476
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 applyShaders();
2478 applyTextures();
2479
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002480 if (!getCurrentProgram()->validateSamplers())
2481 {
2482 return error(GL_INVALID_OPERATION);
2483 }
2484
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002485 if (!cullSkipsDraw(mode))
2486 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002487 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002488 if (useIndexing)
2489 {
2490 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2491 }
2492 else
2493 {
2494 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2495 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002496 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497}
2498
2499void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2500{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002501 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502 {
2503 return error(GL_INVALID_OPERATION);
2504 }
2505
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002506 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 {
2508 return error(GL_INVALID_OPERATION);
2509 }
2510
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002511 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 IDirect3DDevice9 *device = getDevice();
2513 D3DPRIMITIVETYPE primitiveType;
2514 int primitiveCount;
2515
2516 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2517 return error(GL_INVALID_ENUM);
2518
2519 if (primitiveCount <= 0)
2520 {
2521 return;
2522 }
2523
2524 if (!applyRenderTarget(false))
2525 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002526 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527 }
2528
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002529 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002530
2531 TranslatedIndexData indexInfo;
2532 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2533 if (err != GL_NO_ERROR)
2534 {
2535 return error(err);
2536 }
2537
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002538 err = applyVertexBuffer(indexInfo);
2539 if (err != GL_NO_ERROR)
2540 {
2541 return error(err);
2542 }
2543
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 applyShaders();
2545 applyTextures();
2546
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002547 if (!getCurrentProgram()->validateSamplers())
2548 {
2549 return error(GL_INVALID_OPERATION);
2550 }
2551
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002552 if (!cullSkipsDraw(mode))
2553 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002554 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002555 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 +00002556 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557}
2558
2559void Context::finish()
2560{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002561 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 IDirect3DDevice9 *device = getDevice();
2563 IDirect3DQuery9 *occlusionQuery = NULL;
2564
2565 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2566
2567 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2568 {
2569 return error(GL_OUT_OF_MEMORY);
2570 }
2571
2572 ASSERT(SUCCEEDED(result));
2573
2574 if (occlusionQuery)
2575 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002576 IDirect3DStateBlock9 *savedState = NULL;
2577 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2578
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579 occlusionQuery->Issue(D3DISSUE_BEGIN);
2580
2581 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002582 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583 device->SetPixelShader(NULL);
2584 device->SetVertexShader(NULL);
2585 device->SetFVF(D3DFVF_XYZRHW);
2586 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002587 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002588 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589
2590 occlusionQuery->Issue(D3DISSUE_END);
2591
2592 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2593 {
2594 // Keep polling, but allow other threads to do something useful first
2595 Sleep(0);
2596 }
2597
2598 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002599
2600 if (savedState)
2601 {
2602 savedState->Apply();
2603 savedState->Release();
2604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605 }
2606}
2607
2608void Context::flush()
2609{
2610 IDirect3DDevice9 *device = getDevice();
2611 IDirect3DQuery9 *eventQuery = NULL;
2612
2613 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2614
2615 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2616 {
2617 return error(GL_OUT_OF_MEMORY);
2618 }
2619
2620 ASSERT(SUCCEEDED(result));
2621
2622 if (eventQuery)
2623 {
2624 eventQuery->Issue(D3DISSUE_END);
2625
2626 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2627 {
2628 // Keep polling, but allow other threads to do something useful first
2629 Sleep(0);
2630 }
2631
2632 eventQuery->Release();
2633 }
2634}
2635
2636void Context::recordInvalidEnum()
2637{
2638 mInvalidEnum = true;
2639}
2640
2641void Context::recordInvalidValue()
2642{
2643 mInvalidValue = true;
2644}
2645
2646void Context::recordInvalidOperation()
2647{
2648 mInvalidOperation = true;
2649}
2650
2651void Context::recordOutOfMemory()
2652{
2653 mOutOfMemory = true;
2654}
2655
2656void Context::recordInvalidFramebufferOperation()
2657{
2658 mInvalidFramebufferOperation = true;
2659}
2660
2661// Get one of the recorded errors and clear its flag, if any.
2662// [OpenGL ES 2.0.24] section 2.5 page 13.
2663GLenum Context::getError()
2664{
2665 if (mInvalidEnum)
2666 {
2667 mInvalidEnum = false;
2668
2669 return GL_INVALID_ENUM;
2670 }
2671
2672 if (mInvalidValue)
2673 {
2674 mInvalidValue = false;
2675
2676 return GL_INVALID_VALUE;
2677 }
2678
2679 if (mInvalidOperation)
2680 {
2681 mInvalidOperation = false;
2682
2683 return GL_INVALID_OPERATION;
2684 }
2685
2686 if (mOutOfMemory)
2687 {
2688 mOutOfMemory = false;
2689
2690 return GL_OUT_OF_MEMORY;
2691 }
2692
2693 if (mInvalidFramebufferOperation)
2694 {
2695 mInvalidFramebufferOperation = false;
2696
2697 return GL_INVALID_FRAMEBUFFER_OPERATION;
2698 }
2699
2700 return GL_NO_ERROR;
2701}
2702
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002703bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002704{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002705 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002706}
2707
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002708int Context::getMaxSupportedSamples() const
2709{
2710 return mMaxSupportedSamples;
2711}
2712
2713int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2714{
2715 if (requested == 0)
2716 {
2717 return requested;
2718 }
2719
2720 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2721 if (itr == mMultiSampleSupport.end())
2722 {
2723 return -1;
2724 }
2725
2726 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2727 {
2728 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2729 {
2730 return i;
2731 }
2732 }
2733
2734 return -1;
2735}
2736
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737void Context::detachBuffer(GLuint buffer)
2738{
2739 // [OpenGL ES 2.0.24] section 2.9 page 22:
2740 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2741 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2742
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002743 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002745 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746 }
2747
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002748 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002750 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751 }
2752
2753 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2754 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002755 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002756 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002757 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758 }
2759 }
2760}
2761
2762void Context::detachTexture(GLuint texture)
2763{
2764 // [OpenGL ES 2.0.24] section 3.8 page 84:
2765 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2766 // rebound to texture object zero
2767
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002768 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002770 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002772 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002773 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002774 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002775 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776 }
2777 }
2778
2779 // [OpenGL ES 2.0.24] section 4.4 page 112:
2780 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2781 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2782 // image was attached in the currently bound framebuffer.
2783
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002784 Framebuffer *readFramebuffer = getReadFramebuffer();
2785 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002787 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002789 readFramebuffer->detachTexture(texture);
2790 }
2791
2792 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2793 {
2794 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795 }
2796}
2797
2798void Context::detachFramebuffer(GLuint framebuffer)
2799{
2800 // [OpenGL ES 2.0.24] section 4.4 page 107:
2801 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2802 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2803
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002804 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002806 bindReadFramebuffer(0);
2807 }
2808
2809 if (mState.drawFramebuffer == framebuffer)
2810 {
2811 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812 }
2813}
2814
2815void Context::detachRenderbuffer(GLuint renderbuffer)
2816{
2817 // [OpenGL ES 2.0.24] section 4.4 page 109:
2818 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2819 // had been executed with the target RENDERBUFFER and name of zero.
2820
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002821 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822 {
2823 bindRenderbuffer(0);
2824 }
2825
2826 // [OpenGL ES 2.0.24] section 4.4 page 111:
2827 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2828 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2829 // point to which this image was attached in the currently bound framebuffer.
2830
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002831 Framebuffer *readFramebuffer = getReadFramebuffer();
2832 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002833
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002834 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002836 readFramebuffer->detachRenderbuffer(renderbuffer);
2837 }
2838
2839 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2840 {
2841 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002842 }
2843}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002844
2845Texture *Context::getIncompleteTexture(SamplerType type)
2846{
2847 Texture *t = mIncompleteTextures[type];
2848
2849 if (t == NULL)
2850 {
2851 static const GLubyte color[] = { 0, 0, 0, 255 };
2852
2853 switch (type)
2854 {
2855 default:
2856 UNREACHABLE();
2857 // default falls through to SAMPLER_2D
2858
2859 case SAMPLER_2D:
2860 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002861 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002862 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002863 t = incomplete2d;
2864 }
2865 break;
2866
2867 case SAMPLER_CUBE:
2868 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002869 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002870
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002871 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2872 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2873 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2874 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2875 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2876 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002877
2878 t = incompleteCube;
2879 }
2880 break;
2881 }
2882
2883 mIncompleteTextures[type] = t;
2884 }
2885
2886 return t;
2887}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002888
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002889bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002890{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002891 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002892}
2893
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002894bool Context::isTriangleMode(GLenum drawMode)
2895{
2896 switch (drawMode)
2897 {
2898 case GL_TRIANGLES:
2899 case GL_TRIANGLE_FAN:
2900 case GL_TRIANGLE_STRIP:
2901 return true;
2902 case GL_POINTS:
2903 case GL_LINES:
2904 case GL_LINE_LOOP:
2905 case GL_LINE_STRIP:
2906 return false;
2907 default: UNREACHABLE();
2908 }
2909
2910 return false;
2911}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002912
2913void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2914{
2915 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2916
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002917 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2918 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2919 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2920 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002921
2922 mVertexDataManager->dirtyCurrentValues();
2923}
2924
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002925void Context::initExtensionString()
2926{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002927 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002928 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
2929 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00002930 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002931
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002932 if (getMaxSupportedSamples() == 0)
2933 {
2934 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
2935 }
2936
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002937 if (mBufferBackEnd->supportIntIndices())
2938 {
2939 mExtensionString += "GL_OES_element_index_uint ";
2940 }
2941
2942 std::string::size_type end = mExtensionString.find_last_not_of(' ');
2943 if (end != std::string::npos)
2944 {
2945 mExtensionString.resize(end+1);
2946 }
2947}
2948
2949const char *Context::getExtensionString() const
2950{
2951 return mExtensionString.c_str();
2952}
2953
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00002954void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2955 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2956 GLbitfield mask)
2957{
2958 IDirect3DDevice9 *device = getDevice();
2959
2960 Framebuffer *readFramebuffer = getReadFramebuffer();
2961 Framebuffer *drawFramebuffer = getDrawFramebuffer();
2962
2963 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
2964 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2965 {
2966 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2967 }
2968
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002969 if (drawFramebuffer->getSamples() != 0)
2970 {
2971 return error(GL_INVALID_OPERATION);
2972 }
2973
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00002974 RECT sourceRect;
2975 RECT destRect;
2976
2977 if (srcX0 < srcX1)
2978 {
2979 sourceRect.left = srcX0;
2980 sourceRect.right = srcX1;
2981 destRect.left = dstX0;
2982 destRect.right = dstX1;
2983 }
2984 else
2985 {
2986 sourceRect.left = srcX1;
2987 destRect.left = dstX1;
2988 sourceRect.right = srcX0;
2989 destRect.right = dstX0;
2990 }
2991
2992 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
2993 // flip our Y-values here
2994 if (srcY0 < srcY1)
2995 {
2996 sourceRect.bottom = srcY1;
2997 destRect.bottom = dstY1;
2998 sourceRect.top = srcY0;
2999 destRect.top = dstY0;
3000 }
3001 else
3002 {
3003 sourceRect.bottom = srcY0;
3004 destRect.bottom = dstY0;
3005 sourceRect.top = srcY1;
3006 destRect.top = dstY1;
3007 }
3008
3009 RECT sourceScissoredRect = sourceRect;
3010 RECT destScissoredRect = destRect;
3011
3012 if (mState.scissorTest)
3013 {
3014 // Only write to parts of the destination framebuffer which pass the scissor test
3015 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3016 // rect will be checked against scissorY, rather than the bottom.
3017 if (destRect.left < mState.scissorX)
3018 {
3019 int xDiff = mState.scissorX - destRect.left;
3020 destScissoredRect.left = mState.scissorX;
3021 sourceScissoredRect.left += xDiff;
3022 }
3023
3024 if (destRect.right > mState.scissorX + mState.scissorWidth)
3025 {
3026 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3027 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3028 sourceScissoredRect.right -= xDiff;
3029 }
3030
3031 if (destRect.top < mState.scissorY)
3032 {
3033 int yDiff = mState.scissorY - destRect.top;
3034 destScissoredRect.top = mState.scissorY;
3035 sourceScissoredRect.top += yDiff;
3036 }
3037
3038 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3039 {
3040 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3041 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3042 sourceScissoredRect.bottom -= yDiff;
3043 }
3044 }
3045
3046 bool blitRenderTarget = false;
3047 bool blitDepthStencil = false;
3048
3049 RECT sourceTrimmedRect = sourceScissoredRect;
3050 RECT destTrimmedRect = destScissoredRect;
3051
3052 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3053 // the actual draw and read surfaces.
3054 if (sourceTrimmedRect.left < 0)
3055 {
3056 int xDiff = 0 - sourceTrimmedRect.left;
3057 sourceTrimmedRect.left = 0;
3058 destTrimmedRect.left += xDiff;
3059 }
3060
3061 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3062 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3063 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3064 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3065
3066 if (sourceTrimmedRect.right > readBufferWidth)
3067 {
3068 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3069 sourceTrimmedRect.right = readBufferWidth;
3070 destTrimmedRect.right -= xDiff;
3071 }
3072
3073 if (sourceTrimmedRect.top < 0)
3074 {
3075 int yDiff = 0 - sourceTrimmedRect.top;
3076 sourceTrimmedRect.top = 0;
3077 destTrimmedRect.top += yDiff;
3078 }
3079
3080 if (sourceTrimmedRect.bottom > readBufferHeight)
3081 {
3082 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3083 sourceTrimmedRect.bottom = readBufferHeight;
3084 destTrimmedRect.bottom -= yDiff;
3085 }
3086
3087 if (destTrimmedRect.left < 0)
3088 {
3089 int xDiff = 0 - destTrimmedRect.left;
3090 destTrimmedRect.left = 0;
3091 sourceTrimmedRect.left += xDiff;
3092 }
3093
3094 if (destTrimmedRect.right > drawBufferWidth)
3095 {
3096 int xDiff = destTrimmedRect.right - drawBufferWidth;
3097 destTrimmedRect.right = drawBufferWidth;
3098 sourceTrimmedRect.right -= xDiff;
3099 }
3100
3101 if (destTrimmedRect.top < 0)
3102 {
3103 int yDiff = 0 - destTrimmedRect.top;
3104 destTrimmedRect.top = 0;
3105 sourceTrimmedRect.top += yDiff;
3106 }
3107
3108 if (destTrimmedRect.bottom > drawBufferHeight)
3109 {
3110 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3111 destTrimmedRect.bottom = drawBufferHeight;
3112 sourceTrimmedRect.bottom -= yDiff;
3113 }
3114
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003115 bool partialBufferCopy = false;
3116 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3117 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3118 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3119 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3120 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3121 {
3122 partialBufferCopy = true;
3123 }
3124
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003125 if (mask & GL_COLOR_BUFFER_BIT)
3126 {
3127 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3128 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3129 {
3130 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3131 return error(GL_INVALID_OPERATION);
3132 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003133
3134 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3135 {
3136 return error(GL_INVALID_OPERATION);
3137 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003138
3139 blitRenderTarget = true;
3140
3141 }
3142
3143 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3144 {
3145 DepthStencilbuffer *readDSBuffer = NULL;
3146 DepthStencilbuffer *drawDSBuffer = NULL;
3147
3148 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3149 // both a depth and stencil buffer, it will be the same buffer.
3150
3151 if (mask & GL_DEPTH_BUFFER_BIT)
3152 {
3153 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3154 {
3155 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3156 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3157 {
3158 return error(GL_INVALID_OPERATION);
3159 }
3160
3161 blitDepthStencil = true;
3162 readDSBuffer = readFramebuffer->getDepthbuffer();
3163 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3164 }
3165 }
3166
3167 if (mask & GL_STENCIL_BUFFER_BIT)
3168 {
3169 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3170 {
3171 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3172 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3173 {
3174 return error(GL_INVALID_OPERATION);
3175 }
3176
3177 blitDepthStencil = true;
3178 readDSBuffer = readFramebuffer->getStencilbuffer();
3179 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3180 }
3181 }
3182
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003183 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003184 {
3185 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3186 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3187 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003188
3189 if (drawDSBuffer->getSamples() != 0)
3190 {
3191 return error(GL_INVALID_OPERATION);
3192 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003193 }
3194
3195 if (blitRenderTarget || blitDepthStencil)
3196 {
3197 egl::Display *display = getDisplay();
3198 display->endScene();
3199
3200 if (blitRenderTarget)
3201 {
3202 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3203 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3204
3205 if (FAILED(result))
3206 {
3207 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3208 return;
3209 }
3210 }
3211
3212 if (blitDepthStencil)
3213 {
3214 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3215
3216 if (FAILED(result))
3217 {
3218 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3219 return;
3220 }
3221 }
3222 }
3223}
3224
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003225}
3226
3227extern "C"
3228{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003229gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003230{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003231 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003232}
3233
3234void glDestroyContext(gl::Context *context)
3235{
3236 delete context;
3237
3238 if (context == gl::getContext())
3239 {
3240 gl::makeCurrent(NULL, NULL, NULL);
3241 }
3242}
3243
3244void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3245{
3246 gl::makeCurrent(context, display, surface);
3247}
3248
3249gl::Context *glGetCurrentContext()
3250{
3251 return gl::getContext();
3252}
3253}