blob: 53ac0032a1a8340d0774137ceedd4f615fa1ae11 [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
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001809 if (framebufferObject->isMultisample() && 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 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001818 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1819 unsigned int mask = 0;
1820 if (mState.sampleCoverageValue != 0)
1821 {
1822 float threshold = 0.5f;
1823
1824 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1825 {
1826 mask <<= 1;
1827
1828 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1829 {
1830 threshold += 1.0f;
1831 mask |= 1;
1832 }
1833 }
1834 }
1835
1836 if (mState.sampleCoverageInvert)
1837 {
1838 mask = ~mask;
1839 }
1840
1841 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1842 }
1843 else
1844 {
1845 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001846 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001847
1848 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849 }
1850
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851 if (mDitherStateDirty)
1852 {
1853 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1854
1855 mDitherStateDirty = false;
1856 }
1857
1858 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001859}
1860
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001861// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001862void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001863{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001864 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001866 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001868 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869 }
1870 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001871}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001873GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001874{
1875 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1876
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001877 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001878 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001879 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001880 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001881 }
1882
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001883 lookupAttributeMapping(translated);
1884
1885 mBufferBackEnd->setupAttributesPreDraw(translated);
1886
1887 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1888 {
1889 if (translated[i].enabled && translated[i].nonArray)
1890 {
1891 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1892 if (err != GL_NO_ERROR)
1893 {
1894 return err;
1895 }
1896
1897 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1898
1899 *useIndexing = true;
1900 return GL_NO_ERROR;
1901 }
1902 }
1903
1904 *useIndexing = false;
1905 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001906}
1907
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001908GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001909{
1910 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1911
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001912 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001913
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001914 if (err == GL_NO_ERROR)
1915 {
1916 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001917
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001918 mBufferBackEnd->setupAttributesPreDraw(translated);
1919 }
1920
1921 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001922}
1923
1924// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001925GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001926{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001927 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001928
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001929 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001930 {
1931 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1932 }
1933
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001934 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935}
1936
1937// Applies the shaders and shader constants to the Direct3D 9 device
1938void Context::applyShaders()
1939{
1940 IDirect3DDevice9 *device = getDevice();
1941 Program *programObject = getCurrentProgram();
1942 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1943 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1944
1945 device->SetVertexShader(vertexShader);
1946 device->SetPixelShader(pixelShader);
1947
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001948 if (programObject->getSerial() != mAppliedProgram)
1949 {
1950 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001951 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001952 mAppliedProgram = programObject->getSerial();
1953 }
1954
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955 programObject->applyUniforms();
1956}
1957
1958// Applies the textures and sampler states to the Direct3D 9 device
1959void Context::applyTextures()
1960{
1961 IDirect3DDevice9 *device = getDevice();
1962 Program *programObject = getCurrentProgram();
1963
1964 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1965 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001966 int textureUnit = programObject->getSamplerMapping(sampler);
1967 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001969 SamplerType textureType = programObject->getSamplerType(sampler);
1970
1971 Texture *texture = getSamplerTexture(textureUnit, textureType);
1972
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001973 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001974 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001975 if (texture->isComplete())
1976 {
1977 GLenum wrapS = texture->getWrapS();
1978 GLenum wrapT = texture->getWrapT();
1979 GLenum minFilter = texture->getMinFilter();
1980 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001982 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1983 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001985 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1986 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1987 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1988 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1989 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001991 device->SetTexture(sampler, texture->getTexture());
1992 }
1993 else
1994 {
1995 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1996 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001997 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001998
1999 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002001 else
2002 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002003 if (programObject->isSamplerDirty(sampler))
2004 {
2005 device->SetTexture(sampler, NULL);
2006 programObject->setSamplerDirty(sampler, false);
2007 }
2008 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009 }
2010}
2011
2012void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2013{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002014 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002015
2016 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2017 {
2018 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2019 }
2020
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002021 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2022 {
2023 return error(GL_INVALID_OPERATION);
2024 }
2025
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2027 IDirect3DDevice9 *device = getDevice();
2028
2029 D3DSURFACE_DESC desc;
2030 renderTarget->GetDesc(&desc);
2031
2032 IDirect3DSurface9 *systemSurface;
2033 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2034
2035 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2036 {
2037 return error(GL_OUT_OF_MEMORY);
2038 }
2039
2040 ASSERT(SUCCEEDED(result));
2041
2042 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2043 {
2044 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2045 }
2046
2047 result = device->GetRenderTargetData(renderTarget, systemSurface);
2048
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 if (FAILED(result))
2050 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051 systemSurface->Release();
2052
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002053 switch (result)
2054 {
2055 case D3DERR_DRIVERINTERNALERROR:
2056 case D3DERR_DEVICELOST:
2057 return error(GL_OUT_OF_MEMORY);
2058 default:
2059 UNREACHABLE();
2060 return; // No sensible error to generate
2061 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 }
2063
2064 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002065 RECT rect = {std::max(x, 0),
2066 std::max(y, 0),
2067 std::min(x + width, (int)desc.Width),
2068 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069
2070 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2071
2072 if (FAILED(result))
2073 {
2074 UNREACHABLE();
2075 systemSurface->Release();
2076
2077 return; // No sensible error to generate
2078 }
2079
2080 unsigned char *source = (unsigned char*)lock.pBits;
2081 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002082 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002084 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 for (int j = 0; j < rect.bottom - rect.top; j++)
2087 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002088 if (desc.Format == D3DFMT_A8R8G8B8 &&
2089 format == GL_BGRA_EXT &&
2090 type == GL_UNSIGNED_BYTE)
2091 {
2092 // Fast path for EXT_read_format_bgra, given
2093 // an RGBA source buffer. Note that buffers with no
2094 // alpha go through the slow path below.
2095 memcpy(dest + j * outputPitch,
2096 source + j * lock.Pitch,
2097 (rect.right - rect.left) * 4);
2098 continue;
2099 }
2100
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101 for (int i = 0; i < rect.right - rect.left; i++)
2102 {
2103 float r;
2104 float g;
2105 float b;
2106 float a;
2107
2108 switch (desc.Format)
2109 {
2110 case D3DFMT_R5G6B5:
2111 {
2112 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2113
2114 a = 1.0f;
2115 b = (rgb & 0x001F) * (1.0f / 0x001F);
2116 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2117 r = (rgb & 0xF800) * (1.0f / 0xF800);
2118 }
2119 break;
2120 case D3DFMT_X1R5G5B5:
2121 {
2122 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2123
2124 a = 1.0f;
2125 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2126 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2127 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2128 }
2129 break;
2130 case D3DFMT_A1R5G5B5:
2131 {
2132 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2133
2134 a = (argb & 0x8000) ? 1.0f : 0.0f;
2135 b = (argb & 0x001F) * (1.0f / 0x001F);
2136 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2137 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2138 }
2139 break;
2140 case D3DFMT_A8R8G8B8:
2141 {
2142 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2143
2144 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2145 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2146 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2147 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2148 }
2149 break;
2150 case D3DFMT_X8R8G8B8:
2151 {
2152 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2153
2154 a = 1.0f;
2155 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2156 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2157 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2158 }
2159 break;
2160 case D3DFMT_A2R10G10B10:
2161 {
2162 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2163
2164 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2165 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2166 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2167 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2168 }
2169 break;
2170 default:
2171 UNIMPLEMENTED(); // FIXME
2172 UNREACHABLE();
2173 }
2174
2175 switch (format)
2176 {
2177 case GL_RGBA:
2178 switch (type)
2179 {
2180 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002181 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2182 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2183 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2184 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 break;
2186 default: UNREACHABLE();
2187 }
2188 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002189 case GL_BGRA_EXT:
2190 switch (type)
2191 {
2192 case GL_UNSIGNED_BYTE:
2193 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2194 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2195 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2196 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2197 break;
2198 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2199 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2200 // this type is packed as follows:
2201 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2202 // --------------------------------------------------------------------------------
2203 // | 4th | 3rd | 2nd | 1st component |
2204 // --------------------------------------------------------------------------------
2205 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2206 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2207 ((unsigned short)(15 * a + 0.5f) << 12)|
2208 ((unsigned short)(15 * r + 0.5f) << 8) |
2209 ((unsigned short)(15 * g + 0.5f) << 4) |
2210 ((unsigned short)(15 * b + 0.5f) << 0);
2211 break;
2212 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2213 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2214 // this type is packed as follows:
2215 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2216 // --------------------------------------------------------------------------------
2217 // | 4th | 3rd | 2nd | 1st component |
2218 // --------------------------------------------------------------------------------
2219 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2220 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2221 ((unsigned short)( a + 0.5f) << 15) |
2222 ((unsigned short)(31 * r + 0.5f) << 10) |
2223 ((unsigned short)(31 * g + 0.5f) << 5) |
2224 ((unsigned short)(31 * b + 0.5f) << 0);
2225 break;
2226 default: UNREACHABLE();
2227 }
2228 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002229 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230 switch (type)
2231 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002232 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002233 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2234 ((unsigned short)(31 * b + 0.5f) << 0) |
2235 ((unsigned short)(63 * g + 0.5f) << 5) |
2236 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237 break;
2238 default: UNREACHABLE();
2239 }
2240 break;
2241 default: UNREACHABLE();
2242 }
2243 }
2244 }
2245
2246 systemSurface->UnlockRect();
2247
2248 systemSurface->Release();
2249}
2250
2251void Context::clear(GLbitfield mask)
2252{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002253 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002254
2255 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2256 {
2257 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2258
2259 return;
2260 }
2261
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002262 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 IDirect3DDevice9 *device = getDevice();
2264 DWORD flags = 0;
2265
2266 if (mask & GL_COLOR_BUFFER_BIT)
2267 {
2268 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002269
2270 if (framebufferObject->getColorbufferType() != GL_NONE)
2271 {
2272 flags |= D3DCLEAR_TARGET;
2273 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 }
2275
2276 if (mask & GL_DEPTH_BUFFER_BIT)
2277 {
2278 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002279 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 {
2281 flags |= D3DCLEAR_ZBUFFER;
2282 }
2283 }
2284
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 GLuint stencilUnmasked = 0x0;
2286
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002287 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002290 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002292 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2293 D3DSURFACE_DESC desc;
2294 depthStencil->GetDesc(&desc);
2295
2296 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2297 stencilUnmasked = (0x1 << stencilSize) - 1;
2298
2299 if (stencilUnmasked != 0x0)
2300 {
2301 flags |= D3DCLEAR_STENCIL;
2302 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 }
2304 }
2305
2306 if (mask != 0)
2307 {
2308 return error(GL_INVALID_VALUE);
2309 }
2310
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002311 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2312 {
2313 return;
2314 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002316 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2317 unorm<8>(mState.colorClearValue.red),
2318 unorm<8>(mState.colorClearValue.green),
2319 unorm<8>(mState.colorClearValue.blue));
2320 float depth = clamp01(mState.depthClearValue);
2321 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322
2323 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2324
2325 D3DSURFACE_DESC desc;
2326 renderTarget->GetDesc(&desc);
2327
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002328 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329
2330 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002331 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002333 !(mState.colorMaskRed && mState.colorMaskGreen &&
2334 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335
2336 if (needMaskedColorClear || needMaskedStencilClear)
2337 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002338 // State which is altered in all paths from this point to the clear call is saved.
2339 // State which is altered in only some paths will be flagged dirty in the case that
2340 // that path is taken.
2341 HRESULT hr;
2342 if (mMaskedClearSavedState == NULL)
2343 {
2344 hr = device->BeginStateBlock();
2345 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2346
2347 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 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2356 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2357 device->SetPixelShader(NULL);
2358 device->SetVertexShader(NULL);
2359 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2360 device->SetStreamSourceFreq(0, 1);
2361
2362 hr = device->EndStateBlock(&mMaskedClearSavedState);
2363 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2364 }
2365
2366 ASSERT(mMaskedClearSavedState != NULL);
2367
2368 if (mMaskedClearSavedState != NULL)
2369 {
2370 hr = mMaskedClearSavedState->Capture();
2371 ASSERT(SUCCEEDED(hr));
2372 }
2373
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2375 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2376 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2377 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2378 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2379 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2380 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2381 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2382
2383 if (flags & D3DCLEAR_TARGET)
2384 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002385 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2386 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2387 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2388 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 }
2390 else
2391 {
2392 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2393 }
2394
2395 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2396 {
2397 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2398 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2399 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2400 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002401 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002402 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2404 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002405 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 }
2407 else
2408 {
2409 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2410 }
2411
2412 device->SetPixelShader(NULL);
2413 device->SetVertexShader(NULL);
2414 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002415 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002417 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418 {
2419 float x, y, z, w;
2420 D3DCOLOR diffuse;
2421 };
2422
2423 Vertex quad[4];
2424 quad[0].x = 0.0f;
2425 quad[0].y = (float)desc.Height;
2426 quad[0].z = 0.0f;
2427 quad[0].w = 1.0f;
2428 quad[0].diffuse = color;
2429
2430 quad[1].x = (float)desc.Width;
2431 quad[1].y = (float)desc.Height;
2432 quad[1].z = 0.0f;
2433 quad[1].w = 1.0f;
2434 quad[1].diffuse = color;
2435
2436 quad[2].x = 0.0f;
2437 quad[2].y = 0.0f;
2438 quad[2].z = 0.0f;
2439 quad[2].w = 1.0f;
2440 quad[2].diffuse = color;
2441
2442 quad[3].x = (float)desc.Width;
2443 quad[3].y = 0.0f;
2444 quad[3].z = 0.0f;
2445 quad[3].w = 1.0f;
2446 quad[3].diffuse = color;
2447
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002448 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450
2451 if (flags & D3DCLEAR_ZBUFFER)
2452 {
2453 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2454 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2455 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2456 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002457
2458 if (mMaskedClearSavedState != NULL)
2459 {
2460 mMaskedClearSavedState->Apply();
2461 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002463 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464 {
2465 device->Clear(0, NULL, flags, color, depth, stencil);
2466 }
2467}
2468
2469void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2470{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002471 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472 {
2473 return error(GL_INVALID_OPERATION);
2474 }
2475
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002476 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 IDirect3DDevice9 *device = getDevice();
2478 D3DPRIMITIVETYPE primitiveType;
2479 int primitiveCount;
2480
2481 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2482 return error(GL_INVALID_ENUM);
2483
2484 if (primitiveCount <= 0)
2485 {
2486 return;
2487 }
2488
2489 if (!applyRenderTarget(false))
2490 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002491 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492 }
2493
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002494 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002495
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002496 TranslatedIndexData indexInfo;
2497 bool useIndexing;
2498 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002499 if (err != GL_NO_ERROR)
2500 {
2501 return error(err);
2502 }
2503
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 applyShaders();
2505 applyTextures();
2506
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002507 if (!getCurrentProgram()->validateSamplers())
2508 {
2509 return error(GL_INVALID_OPERATION);
2510 }
2511
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002512 if (!cullSkipsDraw(mode))
2513 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002514 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002515 if (useIndexing)
2516 {
2517 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2518 }
2519 else
2520 {
2521 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2522 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002523 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524}
2525
2526void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2527{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002528 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529 {
2530 return error(GL_INVALID_OPERATION);
2531 }
2532
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002533 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 {
2535 return error(GL_INVALID_OPERATION);
2536 }
2537
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002538 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 IDirect3DDevice9 *device = getDevice();
2540 D3DPRIMITIVETYPE primitiveType;
2541 int primitiveCount;
2542
2543 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2544 return error(GL_INVALID_ENUM);
2545
2546 if (primitiveCount <= 0)
2547 {
2548 return;
2549 }
2550
2551 if (!applyRenderTarget(false))
2552 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002553 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554 }
2555
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002556 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002557
2558 TranslatedIndexData indexInfo;
2559 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2560 if (err != GL_NO_ERROR)
2561 {
2562 return error(err);
2563 }
2564
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002565 err = applyVertexBuffer(indexInfo);
2566 if (err != GL_NO_ERROR)
2567 {
2568 return error(err);
2569 }
2570
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571 applyShaders();
2572 applyTextures();
2573
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002574 if (!getCurrentProgram()->validateSamplers())
2575 {
2576 return error(GL_INVALID_OPERATION);
2577 }
2578
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002579 if (!cullSkipsDraw(mode))
2580 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002581 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002582 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 +00002583 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584}
2585
2586void Context::finish()
2587{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002588 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589 IDirect3DDevice9 *device = getDevice();
2590 IDirect3DQuery9 *occlusionQuery = NULL;
2591
2592 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2593
2594 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2595 {
2596 return error(GL_OUT_OF_MEMORY);
2597 }
2598
2599 ASSERT(SUCCEEDED(result));
2600
2601 if (occlusionQuery)
2602 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002603 IDirect3DStateBlock9 *savedState = NULL;
2604 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2605
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002606 occlusionQuery->Issue(D3DISSUE_BEGIN);
2607
2608 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002609 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610 device->SetPixelShader(NULL);
2611 device->SetVertexShader(NULL);
2612 device->SetFVF(D3DFVF_XYZRHW);
2613 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002614 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616
2617 occlusionQuery->Issue(D3DISSUE_END);
2618
2619 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2620 {
2621 // Keep polling, but allow other threads to do something useful first
2622 Sleep(0);
2623 }
2624
2625 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002626
2627 if (savedState)
2628 {
2629 savedState->Apply();
2630 savedState->Release();
2631 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632 }
2633}
2634
2635void Context::flush()
2636{
2637 IDirect3DDevice9 *device = getDevice();
2638 IDirect3DQuery9 *eventQuery = NULL;
2639
2640 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2641
2642 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2643 {
2644 return error(GL_OUT_OF_MEMORY);
2645 }
2646
2647 ASSERT(SUCCEEDED(result));
2648
2649 if (eventQuery)
2650 {
2651 eventQuery->Issue(D3DISSUE_END);
2652
2653 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2654 {
2655 // Keep polling, but allow other threads to do something useful first
2656 Sleep(0);
2657 }
2658
2659 eventQuery->Release();
2660 }
2661}
2662
2663void Context::recordInvalidEnum()
2664{
2665 mInvalidEnum = true;
2666}
2667
2668void Context::recordInvalidValue()
2669{
2670 mInvalidValue = true;
2671}
2672
2673void Context::recordInvalidOperation()
2674{
2675 mInvalidOperation = true;
2676}
2677
2678void Context::recordOutOfMemory()
2679{
2680 mOutOfMemory = true;
2681}
2682
2683void Context::recordInvalidFramebufferOperation()
2684{
2685 mInvalidFramebufferOperation = true;
2686}
2687
2688// Get one of the recorded errors and clear its flag, if any.
2689// [OpenGL ES 2.0.24] section 2.5 page 13.
2690GLenum Context::getError()
2691{
2692 if (mInvalidEnum)
2693 {
2694 mInvalidEnum = false;
2695
2696 return GL_INVALID_ENUM;
2697 }
2698
2699 if (mInvalidValue)
2700 {
2701 mInvalidValue = false;
2702
2703 return GL_INVALID_VALUE;
2704 }
2705
2706 if (mInvalidOperation)
2707 {
2708 mInvalidOperation = false;
2709
2710 return GL_INVALID_OPERATION;
2711 }
2712
2713 if (mOutOfMemory)
2714 {
2715 mOutOfMemory = false;
2716
2717 return GL_OUT_OF_MEMORY;
2718 }
2719
2720 if (mInvalidFramebufferOperation)
2721 {
2722 mInvalidFramebufferOperation = false;
2723
2724 return GL_INVALID_FRAMEBUFFER_OPERATION;
2725 }
2726
2727 return GL_NO_ERROR;
2728}
2729
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002730bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002731{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002732 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002733}
2734
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002735int Context::getMaxSupportedSamples() const
2736{
2737 return mMaxSupportedSamples;
2738}
2739
2740int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2741{
2742 if (requested == 0)
2743 {
2744 return requested;
2745 }
2746
2747 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2748 if (itr == mMultiSampleSupport.end())
2749 {
2750 return -1;
2751 }
2752
2753 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2754 {
2755 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2756 {
2757 return i;
2758 }
2759 }
2760
2761 return -1;
2762}
2763
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764void Context::detachBuffer(GLuint buffer)
2765{
2766 // [OpenGL ES 2.0.24] section 2.9 page 22:
2767 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2768 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2769
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002770 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002772 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773 }
2774
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002775 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002777 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778 }
2779
2780 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2781 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002782 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002783 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002784 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002785 }
2786 }
2787}
2788
2789void Context::detachTexture(GLuint texture)
2790{
2791 // [OpenGL ES 2.0.24] section 3.8 page 84:
2792 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2793 // rebound to texture object zero
2794
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002795 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002797 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002798 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002799 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002800 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002801 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002802 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803 }
2804 }
2805
2806 // [OpenGL ES 2.0.24] section 4.4 page 112:
2807 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2808 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2809 // image was attached in the currently bound framebuffer.
2810
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002811 Framebuffer *readFramebuffer = getReadFramebuffer();
2812 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002814 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002816 readFramebuffer->detachTexture(texture);
2817 }
2818
2819 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2820 {
2821 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822 }
2823}
2824
2825void Context::detachFramebuffer(GLuint framebuffer)
2826{
2827 // [OpenGL ES 2.0.24] section 4.4 page 107:
2828 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2829 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2830
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002831 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002832 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002833 bindReadFramebuffer(0);
2834 }
2835
2836 if (mState.drawFramebuffer == framebuffer)
2837 {
2838 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839 }
2840}
2841
2842void Context::detachRenderbuffer(GLuint renderbuffer)
2843{
2844 // [OpenGL ES 2.0.24] section 4.4 page 109:
2845 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2846 // had been executed with the target RENDERBUFFER and name of zero.
2847
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002848 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849 {
2850 bindRenderbuffer(0);
2851 }
2852
2853 // [OpenGL ES 2.0.24] section 4.4 page 111:
2854 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2855 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2856 // point to which this image was attached in the currently bound framebuffer.
2857
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002858 Framebuffer *readFramebuffer = getReadFramebuffer();
2859 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002861 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002863 readFramebuffer->detachRenderbuffer(renderbuffer);
2864 }
2865
2866 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2867 {
2868 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869 }
2870}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002871
2872Texture *Context::getIncompleteTexture(SamplerType type)
2873{
2874 Texture *t = mIncompleteTextures[type];
2875
2876 if (t == NULL)
2877 {
2878 static const GLubyte color[] = { 0, 0, 0, 255 };
2879
2880 switch (type)
2881 {
2882 default:
2883 UNREACHABLE();
2884 // default falls through to SAMPLER_2D
2885
2886 case SAMPLER_2D:
2887 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002888 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002889 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002890 t = incomplete2d;
2891 }
2892 break;
2893
2894 case SAMPLER_CUBE:
2895 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002896 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002897
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002898 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2899 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2900 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2901 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2902 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2903 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002904
2905 t = incompleteCube;
2906 }
2907 break;
2908 }
2909
2910 mIncompleteTextures[type] = t;
2911 }
2912
2913 return t;
2914}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002915
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002916bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002917{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002918 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002919}
2920
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002921bool Context::isTriangleMode(GLenum drawMode)
2922{
2923 switch (drawMode)
2924 {
2925 case GL_TRIANGLES:
2926 case GL_TRIANGLE_FAN:
2927 case GL_TRIANGLE_STRIP:
2928 return true;
2929 case GL_POINTS:
2930 case GL_LINES:
2931 case GL_LINE_LOOP:
2932 case GL_LINE_STRIP:
2933 return false;
2934 default: UNREACHABLE();
2935 }
2936
2937 return false;
2938}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002939
2940void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2941{
2942 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2943
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002944 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2945 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2946 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2947 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002948
2949 mVertexDataManager->dirtyCurrentValues();
2950}
2951
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002952void Context::initExtensionString()
2953{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002954 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002955 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
2956 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00002957 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002958
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002959
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002960 if (getMaxSupportedSamples() == 0)
2961 {
2962 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
2963 }
2964
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002965 if (mBufferBackEnd->supportIntIndices())
2966 {
2967 mExtensionString += "GL_OES_element_index_uint ";
2968 }
2969
2970 std::string::size_type end = mExtensionString.find_last_not_of(' ');
2971 if (end != std::string::npos)
2972 {
2973 mExtensionString.resize(end+1);
2974 }
2975}
2976
2977const char *Context::getExtensionString() const
2978{
2979 return mExtensionString.c_str();
2980}
2981
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00002982void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2983 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2984 GLbitfield mask)
2985{
2986 IDirect3DDevice9 *device = getDevice();
2987
2988 Framebuffer *readFramebuffer = getReadFramebuffer();
2989 Framebuffer *drawFramebuffer = getDrawFramebuffer();
2990
2991 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
2992 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2993 {
2994 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2995 }
2996
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002997 if (drawFramebuffer->getSamples() != 0)
2998 {
2999 return error(GL_INVALID_OPERATION);
3000 }
3001
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003002 RECT sourceRect;
3003 RECT destRect;
3004
3005 if (srcX0 < srcX1)
3006 {
3007 sourceRect.left = srcX0;
3008 sourceRect.right = srcX1;
3009 destRect.left = dstX0;
3010 destRect.right = dstX1;
3011 }
3012 else
3013 {
3014 sourceRect.left = srcX1;
3015 destRect.left = dstX1;
3016 sourceRect.right = srcX0;
3017 destRect.right = dstX0;
3018 }
3019
3020 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3021 // flip our Y-values here
3022 if (srcY0 < srcY1)
3023 {
3024 sourceRect.bottom = srcY1;
3025 destRect.bottom = dstY1;
3026 sourceRect.top = srcY0;
3027 destRect.top = dstY0;
3028 }
3029 else
3030 {
3031 sourceRect.bottom = srcY0;
3032 destRect.bottom = dstY0;
3033 sourceRect.top = srcY1;
3034 destRect.top = dstY1;
3035 }
3036
3037 RECT sourceScissoredRect = sourceRect;
3038 RECT destScissoredRect = destRect;
3039
3040 if (mState.scissorTest)
3041 {
3042 // Only write to parts of the destination framebuffer which pass the scissor test
3043 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3044 // rect will be checked against scissorY, rather than the bottom.
3045 if (destRect.left < mState.scissorX)
3046 {
3047 int xDiff = mState.scissorX - destRect.left;
3048 destScissoredRect.left = mState.scissorX;
3049 sourceScissoredRect.left += xDiff;
3050 }
3051
3052 if (destRect.right > mState.scissorX + mState.scissorWidth)
3053 {
3054 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3055 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3056 sourceScissoredRect.right -= xDiff;
3057 }
3058
3059 if (destRect.top < mState.scissorY)
3060 {
3061 int yDiff = mState.scissorY - destRect.top;
3062 destScissoredRect.top = mState.scissorY;
3063 sourceScissoredRect.top += yDiff;
3064 }
3065
3066 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3067 {
3068 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3069 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3070 sourceScissoredRect.bottom -= yDiff;
3071 }
3072 }
3073
3074 bool blitRenderTarget = false;
3075 bool blitDepthStencil = false;
3076
3077 RECT sourceTrimmedRect = sourceScissoredRect;
3078 RECT destTrimmedRect = destScissoredRect;
3079
3080 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3081 // the actual draw and read surfaces.
3082 if (sourceTrimmedRect.left < 0)
3083 {
3084 int xDiff = 0 - sourceTrimmedRect.left;
3085 sourceTrimmedRect.left = 0;
3086 destTrimmedRect.left += xDiff;
3087 }
3088
3089 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3090 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3091 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3092 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3093
3094 if (sourceTrimmedRect.right > readBufferWidth)
3095 {
3096 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3097 sourceTrimmedRect.right = readBufferWidth;
3098 destTrimmedRect.right -= xDiff;
3099 }
3100
3101 if (sourceTrimmedRect.top < 0)
3102 {
3103 int yDiff = 0 - sourceTrimmedRect.top;
3104 sourceTrimmedRect.top = 0;
3105 destTrimmedRect.top += yDiff;
3106 }
3107
3108 if (sourceTrimmedRect.bottom > readBufferHeight)
3109 {
3110 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3111 sourceTrimmedRect.bottom = readBufferHeight;
3112 destTrimmedRect.bottom -= yDiff;
3113 }
3114
3115 if (destTrimmedRect.left < 0)
3116 {
3117 int xDiff = 0 - destTrimmedRect.left;
3118 destTrimmedRect.left = 0;
3119 sourceTrimmedRect.left += xDiff;
3120 }
3121
3122 if (destTrimmedRect.right > drawBufferWidth)
3123 {
3124 int xDiff = destTrimmedRect.right - drawBufferWidth;
3125 destTrimmedRect.right = drawBufferWidth;
3126 sourceTrimmedRect.right -= xDiff;
3127 }
3128
3129 if (destTrimmedRect.top < 0)
3130 {
3131 int yDiff = 0 - destTrimmedRect.top;
3132 destTrimmedRect.top = 0;
3133 sourceTrimmedRect.top += yDiff;
3134 }
3135
3136 if (destTrimmedRect.bottom > drawBufferHeight)
3137 {
3138 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3139 destTrimmedRect.bottom = drawBufferHeight;
3140 sourceTrimmedRect.bottom -= yDiff;
3141 }
3142
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003143 bool partialBufferCopy = false;
3144 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3145 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3146 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3147 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3148 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3149 {
3150 partialBufferCopy = true;
3151 }
3152
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003153 if (mask & GL_COLOR_BUFFER_BIT)
3154 {
3155 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3156 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3157 {
3158 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3159 return error(GL_INVALID_OPERATION);
3160 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003161
3162 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3163 {
3164 return error(GL_INVALID_OPERATION);
3165 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003166
3167 blitRenderTarget = true;
3168
3169 }
3170
3171 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3172 {
3173 DepthStencilbuffer *readDSBuffer = NULL;
3174 DepthStencilbuffer *drawDSBuffer = NULL;
3175
3176 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3177 // both a depth and stencil buffer, it will be the same buffer.
3178
3179 if (mask & GL_DEPTH_BUFFER_BIT)
3180 {
3181 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3182 {
3183 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3184 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3185 {
3186 return error(GL_INVALID_OPERATION);
3187 }
3188
3189 blitDepthStencil = true;
3190 readDSBuffer = readFramebuffer->getDepthbuffer();
3191 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3192 }
3193 }
3194
3195 if (mask & GL_STENCIL_BUFFER_BIT)
3196 {
3197 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3198 {
3199 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3200 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3201 {
3202 return error(GL_INVALID_OPERATION);
3203 }
3204
3205 blitDepthStencil = true;
3206 readDSBuffer = readFramebuffer->getStencilbuffer();
3207 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3208 }
3209 }
3210
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003211 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003212 {
3213 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3214 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3215 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003216
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003217 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3218 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003219 {
3220 return error(GL_INVALID_OPERATION);
3221 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003222 }
3223
3224 if (blitRenderTarget || blitDepthStencil)
3225 {
3226 egl::Display *display = getDisplay();
3227 display->endScene();
3228
3229 if (blitRenderTarget)
3230 {
3231 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3232 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3233
3234 if (FAILED(result))
3235 {
3236 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3237 return;
3238 }
3239 }
3240
3241 if (blitDepthStencil)
3242 {
3243 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3244
3245 if (FAILED(result))
3246 {
3247 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3248 return;
3249 }
3250 }
3251 }
3252}
3253
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003254}
3255
3256extern "C"
3257{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003258gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003259{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003260 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003261}
3262
3263void glDestroyContext(gl::Context *context)
3264{
3265 delete context;
3266
3267 if (context == gl::getContext())
3268 {
3269 gl::makeCurrent(NULL, NULL, NULL);
3270 }
3271}
3272
3273void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3274{
3275 gl::makeCurrent(context, display, surface);
3276}
3277
3278gl::Context *glGetCurrentContext()
3279{
3280 return gl::getContext();
3281}
3282}