blob: 48ef8fc25e7b8a386ecd120d30e4a181cd2b9930 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
22#include "libGLESv2/FrameBuffer.h"
23#include "libGLESv2/Program.h"
24#include "libGLESv2/RenderBuffer.h"
25#include "libGLESv2/Shader.h"
26#include "libGLESv2/Texture.h"
27#include "libGLESv2/geometry/backend.h"
28#include "libGLESv2/geometry/VertexDataManager.h"
29#include "libGLESv2/geometry/IndexDataManager.h"
30#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.com86487c22010-03-11 19:41:43 +000032#undef near
33#undef far
34
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035namespace gl
36{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000037Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000038 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039{
40 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000041
daniel@transgaming.com428d1582010-05-04 03:35:25 +000042 mState.depthClearValue = 1.0f;
43 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
daniel@transgaming.com428d1582010-05-04 03:35:25 +000045 mState.cullFace = false;
46 mState.cullMode = GL_BACK;
47 mState.frontFace = GL_CCW;
48 mState.depthTest = false;
49 mState.depthFunc = GL_LESS;
50 mState.blend = false;
51 mState.sourceBlendRGB = GL_ONE;
52 mState.sourceBlendAlpha = GL_ONE;
53 mState.destBlendRGB = GL_ZERO;
54 mState.destBlendAlpha = GL_ZERO;
55 mState.blendEquationRGB = GL_FUNC_ADD;
56 mState.blendEquationAlpha = GL_FUNC_ADD;
57 mState.blendColor.red = 0;
58 mState.blendColor.green = 0;
59 mState.blendColor.blue = 0;
60 mState.blendColor.alpha = 0;
61 mState.stencilTest = false;
62 mState.stencilFunc = GL_ALWAYS;
63 mState.stencilRef = 0;
64 mState.stencilMask = -1;
65 mState.stencilWritemask = -1;
66 mState.stencilBackFunc = GL_ALWAYS;
67 mState.stencilBackRef = 0;
68 mState.stencilBackMask = - 1;
69 mState.stencilBackWritemask = -1;
70 mState.stencilFail = GL_KEEP;
71 mState.stencilPassDepthFail = GL_KEEP;
72 mState.stencilPassDepthPass = GL_KEEP;
73 mState.stencilBackFail = GL_KEEP;
74 mState.stencilBackPassDepthFail = GL_KEEP;
75 mState.stencilBackPassDepthPass = GL_KEEP;
76 mState.polygonOffsetFill = false;
77 mState.polygonOffsetFactor = 0.0f;
78 mState.polygonOffsetUnits = 0.0f;
79 mState.sampleAlphaToCoverage = false;
80 mState.sampleCoverage = false;
81 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000082 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000083 mState.scissorTest = false;
84 mState.dither = true;
85 mState.generateMipmapHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086
daniel@transgaming.com428d1582010-05-04 03:35:25 +000087 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.viewportX = 0;
90 mState.viewportY = 0;
91 mState.viewportWidth = config->mDisplayMode.Width;
92 mState.viewportHeight = config->mDisplayMode.Height;
93 mState.zNear = 0.0f;
94 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.scissorX = 0;
97 mState.scissorY = 0;
98 mState.scissorWidth = config->mDisplayMode.Width;
99 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000101 mState.colorMaskRed = true;
102 mState.colorMaskGreen = true;
103 mState.colorMaskBlue = true;
104 mState.colorMaskAlpha = true;
105 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000107 if (shareContext != NULL)
108 {
109 mResourceManager = shareContext->mResourceManager;
110 mResourceManager->addRef();
111 }
112 else
113 {
114 mResourceManager = new ResourceManager();
115 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000116
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117 // [OpenGL ES 2.0.24] section 3.7 page 83:
118 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
119 // and cube map texture state vectors respectively associated with them.
120 // In order that access to these initial textures not be lost, they are treated as texture
121 // objects all of whose names are 0.
122
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000123 mTexture2DZero = new Texture2D(0);
124 mTextureCubeMapZero = new TextureCubeMap(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
126 mColorbufferZero = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000127 mDepthStencilbufferZero = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000129 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000130 bindArrayBuffer(0);
131 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132 bindTextureCubeMap(0);
133 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000134 bindReadFramebuffer(0);
135 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 bindRenderbuffer(0);
137
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000138 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 {
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000140 mIncompleteTextures[type] = NULL;
141 }
142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000145 mState.packAlignment = 4;
146 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000147
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000148 mBufferBackEnd = NULL;
149 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000150 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000151 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 mInvalidEnum = false;
154 mInvalidValue = false;
155 mInvalidOperation = false;
156 mOutOfMemory = false;
157 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000158
159 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000160
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000161 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000162 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000163 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164}
165
166Context::~Context()
167{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000168 if (mState.currentProgram != 0)
169 {
170 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
171 if (programObject)
172 {
173 programObject->release();
174 }
175 mState.currentProgram = 0;
176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000178 while (!mFramebufferMap.empty())
179 {
180 deleteFramebuffer(mFramebufferMap.begin()->first);
181 }
182
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000183 while (!mMultiSampleSupport.empty())
184 {
185 delete [] mMultiSampleSupport.begin()->second;
186 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
187 }
188
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000189 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
190 {
191 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
192 {
193 mState.samplerTexture[type][sampler].set(NULL);
194 }
195 }
196
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000197 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
198 {
199 delete mIncompleteTextures[type];
200 }
201
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000202 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
203 {
204 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
205 }
206
207 mState.arrayBuffer.set(NULL);
208 mState.elementArrayBuffer.set(NULL);
209 mState.texture2D.set(NULL);
210 mState.textureCubeMap.set(NULL);
211 mState.renderbuffer.set(NULL);
212
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000213 delete mTexture2DZero;
214 delete mTextureCubeMapZero;
215
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000216 delete mBufferBackEnd;
217 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000218 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000219 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000220
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000221 if (mMaskedClearSavedState)
222 {
223 mMaskedClearSavedState->Release();
224 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000225
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000226 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227}
228
229void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
230{
231 IDirect3DDevice9 *device = display->getDevice();
232
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000233 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000234 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000235 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000236
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000237 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000238 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000239 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000240 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000241
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000242 const D3DFORMAT renderBufferFormats[] =
243 {
244 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000245 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000246 D3DFMT_R5G6B5,
247 D3DFMT_D24S8
248 };
249
250 int max = 0;
251 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
252 {
253 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
254 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
255 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
256
257 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
258 {
259 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
260 {
261 max = j;
262 }
263 }
264 }
265
266 mMaxSupportedSamples = max;
267
daniel@transgaming.com01868132010-08-24 19:21:17 +0000268 mSupportsCompressedTextures = display->getCompressedTextureSupport();
269
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000270 initExtensionString();
271
272 mState.viewportX = 0;
273 mState.viewportY = 0;
274 mState.viewportWidth = surface->getWidth();
275 mState.viewportHeight = surface->getHeight();
276
277 mState.scissorX = 0;
278 mState.scissorY = 0;
279 mState.scissorWidth = surface->getWidth();
280 mState.scissorHeight = surface->getHeight();
281
282 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000283 }
284
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
286 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000287 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000290 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000291 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
293 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
295 defaultRenderTarget->Release();
296
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000297 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000299 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000301
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000302 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000303
304 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305}
306
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000307// 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 +0000308void Context::markAllStateDirty()
309{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000310 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000311 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000312 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000313
314 mClearStateDirty = true;
315 mCullStateDirty = true;
316 mDepthStateDirty = true;
317 mMaskStateDirty = true;
318 mBlendStateDirty = true;
319 mStencilStateDirty = true;
320 mPolygonOffsetStateDirty = true;
321 mScissorStateDirty = true;
322 mSampleStateDirty = true;
323 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000324 mFrontFaceDirty = true;
325
326 if (mBufferBackEnd != NULL)
327 {
328 mBufferBackEnd->invalidate();
329 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000330}
331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332void Context::setClearColor(float red, float green, float blue, float alpha)
333{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000334 mState.colorClearValue.red = red;
335 mState.colorClearValue.green = green;
336 mState.colorClearValue.blue = blue;
337 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338}
339
340void Context::setClearDepth(float depth)
341{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000342 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343}
344
345void Context::setClearStencil(int stencil)
346{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000347 mState.stencilClearValue = stencil;
348}
349
350void Context::setCullFace(bool enabled)
351{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000352 if (mState.cullFace != enabled)
353 {
354 mState.cullFace = enabled;
355 mCullStateDirty = true;
356 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000357}
358
359bool Context::isCullFaceEnabled() const
360{
361 return mState.cullFace;
362}
363
364void Context::setCullMode(GLenum mode)
365{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000366 if (mState.cullMode != mode)
367 {
368 mState.cullMode = mode;
369 mCullStateDirty = true;
370 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000371}
372
373void Context::setFrontFace(GLenum front)
374{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000375 if (mState.frontFace != front)
376 {
377 mState.frontFace = front;
378 mFrontFaceDirty = true;
379 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000380}
381
382void Context::setDepthTest(bool enabled)
383{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000384 if (mState.depthTest != enabled)
385 {
386 mState.depthTest = enabled;
387 mDepthStateDirty = true;
388 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000389}
390
391bool Context::isDepthTestEnabled() const
392{
393 return mState.depthTest;
394}
395
396void Context::setDepthFunc(GLenum depthFunc)
397{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000398 if (mState.depthFunc != depthFunc)
399 {
400 mState.depthFunc = depthFunc;
401 mDepthStateDirty = true;
402 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000403}
404
405void Context::setDepthRange(float zNear, float zFar)
406{
407 mState.zNear = zNear;
408 mState.zFar = zFar;
409}
410
411void Context::setBlend(bool enabled)
412{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000413 if (mState.blend != enabled)
414 {
415 mState.blend = enabled;
416 mBlendStateDirty = true;
417 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000418}
419
420bool Context::isBlendEnabled() const
421{
422 return mState.blend;
423}
424
425void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
426{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000427 if (mState.sourceBlendRGB != sourceRGB ||
428 mState.sourceBlendAlpha != sourceAlpha ||
429 mState.destBlendRGB != destRGB ||
430 mState.destBlendAlpha != destAlpha)
431 {
432 mState.sourceBlendRGB = sourceRGB;
433 mState.destBlendRGB = destRGB;
434 mState.sourceBlendAlpha = sourceAlpha;
435 mState.destBlendAlpha = destAlpha;
436 mBlendStateDirty = true;
437 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000438}
439
440void Context::setBlendColor(float red, float green, float blue, float alpha)
441{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000442 if (mState.blendColor.red != red ||
443 mState.blendColor.green != green ||
444 mState.blendColor.blue != blue ||
445 mState.blendColor.alpha != alpha)
446 {
447 mState.blendColor.red = red;
448 mState.blendColor.green = green;
449 mState.blendColor.blue = blue;
450 mState.blendColor.alpha = alpha;
451 mBlendStateDirty = true;
452 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000453}
454
455void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
456{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000457 if (mState.blendEquationRGB != rgbEquation ||
458 mState.blendEquationAlpha != alphaEquation)
459 {
460 mState.blendEquationRGB = rgbEquation;
461 mState.blendEquationAlpha = alphaEquation;
462 mBlendStateDirty = true;
463 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000464}
465
466void Context::setStencilTest(bool enabled)
467{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000468 if (mState.stencilTest != enabled)
469 {
470 mState.stencilTest = enabled;
471 mStencilStateDirty = true;
472 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000473}
474
475bool Context::isStencilTestEnabled() const
476{
477 return mState.stencilTest;
478}
479
480void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
481{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000482 if (mState.stencilFunc != stencilFunc ||
483 mState.stencilRef != stencilRef ||
484 mState.stencilMask != stencilMask)
485 {
486 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000487 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000488 mState.stencilMask = stencilMask;
489 mStencilStateDirty = true;
490 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000491}
492
493void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
494{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000495 if (mState.stencilBackFunc != stencilBackFunc ||
496 mState.stencilBackRef != stencilBackRef ||
497 mState.stencilBackMask != stencilBackMask)
498 {
499 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000500 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000501 mState.stencilBackMask = stencilBackMask;
502 mStencilStateDirty = true;
503 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000504}
505
506void Context::setStencilWritemask(GLuint stencilWritemask)
507{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000508 if (mState.stencilWritemask != stencilWritemask)
509 {
510 mState.stencilWritemask = stencilWritemask;
511 mStencilStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
516{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000517 if (mState.stencilBackWritemask != stencilBackWritemask)
518 {
519 mState.stencilBackWritemask = stencilBackWritemask;
520 mStencilStateDirty = true;
521 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000522}
523
524void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
525{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000526 if (mState.stencilFail != stencilFail ||
527 mState.stencilPassDepthFail != stencilPassDepthFail ||
528 mState.stencilPassDepthPass != stencilPassDepthPass)
529 {
530 mState.stencilFail = stencilFail;
531 mState.stencilPassDepthFail = stencilPassDepthFail;
532 mState.stencilPassDepthPass = stencilPassDepthPass;
533 mStencilStateDirty = true;
534 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000535}
536
537void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
538{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000539 if (mState.stencilBackFail != stencilBackFail ||
540 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
541 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
542 {
543 mState.stencilBackFail = stencilBackFail;
544 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
545 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
546 mStencilStateDirty = true;
547 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000548}
549
550void Context::setPolygonOffsetFill(bool enabled)
551{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000552 if (mState.polygonOffsetFill != enabled)
553 {
554 mState.polygonOffsetFill = enabled;
555 mPolygonOffsetStateDirty = true;
556 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000557}
558
559bool Context::isPolygonOffsetFillEnabled() const
560{
561 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000562
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000563}
564
565void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
566{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000567 if (mState.polygonOffsetFactor != factor ||
568 mState.polygonOffsetUnits != units)
569 {
570 mState.polygonOffsetFactor = factor;
571 mState.polygonOffsetUnits = units;
572 mPolygonOffsetStateDirty = true;
573 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000574}
575
576void Context::setSampleAlphaToCoverage(bool enabled)
577{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000578 if (mState.sampleAlphaToCoverage != enabled)
579 {
580 mState.sampleAlphaToCoverage = enabled;
581 mSampleStateDirty = true;
582 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000583}
584
585bool Context::isSampleAlphaToCoverageEnabled() const
586{
587 return mState.sampleAlphaToCoverage;
588}
589
590void Context::setSampleCoverage(bool enabled)
591{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000592 if (mState.sampleCoverage != enabled)
593 {
594 mState.sampleCoverage = enabled;
595 mSampleStateDirty = true;
596 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000597}
598
599bool Context::isSampleCoverageEnabled() const
600{
601 return mState.sampleCoverage;
602}
603
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000604void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000605{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000606 if (mState.sampleCoverageValue != value ||
607 mState.sampleCoverageInvert != invert)
608 {
609 mState.sampleCoverageValue = value;
610 mState.sampleCoverageInvert = invert;
611 mSampleStateDirty = true;
612 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000613}
614
615void Context::setScissorTest(bool enabled)
616{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000617 if (mState.scissorTest != enabled)
618 {
619 mState.scissorTest = enabled;
620 mScissorStateDirty = true;
621 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622}
623
624bool Context::isScissorTestEnabled() const
625{
626 return mState.scissorTest;
627}
628
629void Context::setDither(bool enabled)
630{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000631 if (mState.dither != enabled)
632 {
633 mState.dither = enabled;
634 mDitherStateDirty = true;
635 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000636}
637
638bool Context::isDitherEnabled() const
639{
640 return mState.dither;
641}
642
643void Context::setLineWidth(GLfloat width)
644{
645 mState.lineWidth = width;
646}
647
648void Context::setGenerateMipmapHint(GLenum hint)
649{
650 mState.generateMipmapHint = hint;
651}
652
653void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
654{
655 mState.viewportX = x;
656 mState.viewportY = y;
657 mState.viewportWidth = width;
658 mState.viewportHeight = height;
659}
660
661void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
662{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000663 if (mState.scissorX != x || mState.scissorY != y ||
664 mState.scissorWidth != width || mState.scissorHeight != height)
665 {
666 mState.scissorX = x;
667 mState.scissorY = y;
668 mState.scissorWidth = width;
669 mState.scissorHeight = height;
670 mScissorStateDirty = true;
671 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000672}
673
674void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
675{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000676 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
677 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
678 {
679 mState.colorMaskRed = red;
680 mState.colorMaskGreen = green;
681 mState.colorMaskBlue = blue;
682 mState.colorMaskAlpha = alpha;
683 mMaskStateDirty = true;
684 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000685}
686
687void Context::setDepthMask(bool mask)
688{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000689 if (mState.depthMask != mask)
690 {
691 mState.depthMask = mask;
692 mMaskStateDirty = true;
693 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000694}
695
696void Context::setActiveSampler(int active)
697{
698 mState.activeSampler = active;
699}
700
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000701GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000702{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000703 return mState.readFramebuffer;
704}
705
706GLuint Context::getDrawFramebufferHandle() const
707{
708 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000709}
710
711GLuint Context::getRenderbufferHandle() const
712{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000713 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000714}
715
716GLuint Context::getArrayBufferHandle() const
717{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000718 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719}
720
721void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
722{
723 mState.vertexAttribute[attribNum].mEnabled = enabled;
724}
725
726const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
727{
728 return mState.vertexAttribute[attribNum];
729}
730
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000731void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000732 GLsizei stride, const void *pointer)
733{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000734 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000735 mState.vertexAttribute[attribNum].mSize = size;
736 mState.vertexAttribute[attribNum].mType = type;
737 mState.vertexAttribute[attribNum].mNormalized = normalized;
738 mState.vertexAttribute[attribNum].mStride = stride;
739 mState.vertexAttribute[attribNum].mPointer = pointer;
740}
741
742const void *Context::getVertexAttribPointer(unsigned int attribNum) const
743{
744 return mState.vertexAttribute[attribNum].mPointer;
745}
746
747// returns entire set of attributes as a block
748const AttributeState *Context::getVertexAttribBlock()
749{
750 return mState.vertexAttribute;
751}
752
753void Context::setPackAlignment(GLint alignment)
754{
755 mState.packAlignment = alignment;
756}
757
758GLint Context::getPackAlignment() const
759{
760 return mState.packAlignment;
761}
762
763void Context::setUnpackAlignment(GLint alignment)
764{
765 mState.unpackAlignment = alignment;
766}
767
768GLint Context::getUnpackAlignment() const
769{
770 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771}
772
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773GLuint Context::createBuffer()
774{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000775 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776}
777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778GLuint Context::createProgram()
779{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000780 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781}
782
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000783GLuint Context::createShader(GLenum type)
784{
785 return mResourceManager->createShader(type);
786}
787
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788GLuint Context::createTexture()
789{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000790 return mResourceManager->createTexture();
791}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000793GLuint Context::createRenderbuffer()
794{
795 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
798// Returns an unused framebuffer name
799GLuint Context::createFramebuffer()
800{
801 unsigned int handle = 1;
802
803 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
804 {
805 handle++;
806 }
807
808 mFramebufferMap[handle] = NULL;
809
810 return handle;
811}
812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813void Context::deleteBuffer(GLuint buffer)
814{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000815 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816 {
817 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000819
820 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
823void Context::deleteShader(GLuint shader)
824{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000825 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826}
827
828void Context::deleteProgram(GLuint program)
829{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000830 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831}
832
833void Context::deleteTexture(GLuint texture)
834{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000835 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836 {
837 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000839
840 mResourceManager->deleteTexture(texture);
841}
842
843void Context::deleteRenderbuffer(GLuint renderbuffer)
844{
845 if (mResourceManager->getRenderbuffer(renderbuffer))
846 {
847 detachRenderbuffer(renderbuffer);
848 }
849
850 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851}
852
853void Context::deleteFramebuffer(GLuint framebuffer)
854{
855 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
856
857 if (framebufferObject != mFramebufferMap.end())
858 {
859 detachFramebuffer(framebuffer);
860
861 delete framebufferObject->second;
862 mFramebufferMap.erase(framebufferObject);
863 }
864}
865
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000866Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868 return mResourceManager->getBuffer(handle);
869}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000871Shader *Context::getShader(GLuint handle)
872{
873 return mResourceManager->getShader(handle);
874}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000876Program *Context::getProgram(GLuint handle)
877{
878 return mResourceManager->getProgram(handle);
879}
880
881Texture *Context::getTexture(GLuint handle)
882{
883 return mResourceManager->getTexture(handle);
884}
885
886Renderbuffer *Context::getRenderbuffer(GLuint handle)
887{
888 return mResourceManager->getRenderbuffer(handle);
889}
890
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000891Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000892{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000893 return getFramebuffer(mState.readFramebuffer);
894}
895
896Framebuffer *Context::getDrawFramebuffer()
897{
898 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
901void Context::bindArrayBuffer(unsigned int buffer)
902{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000903 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000905 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906}
907
908void Context::bindElementArrayBuffer(unsigned int buffer)
909{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000910 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000912 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913}
914
915void Context::bindTexture2D(GLuint texture)
916{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000919 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000921 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922}
923
924void Context::bindTextureCubeMap(GLuint texture)
925{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000926 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000928 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000930 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931}
932
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000933void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934{
935 if (!getFramebuffer(framebuffer))
936 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000937 mFramebufferMap[framebuffer] = new Framebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938 }
939
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000940 mState.readFramebuffer = framebuffer;
941}
942
943void Context::bindDrawFramebuffer(GLuint framebuffer)
944{
945 if (!getFramebuffer(framebuffer))
946 {
947 mFramebufferMap[framebuffer] = new Framebuffer();
948 }
949
950 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951}
952
953void Context::bindRenderbuffer(GLuint renderbuffer)
954{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 mResourceManager->checkRenderbufferAllocation(renderbuffer);
956
957 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958}
959
960void Context::useProgram(GLuint program)
961{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000962 GLuint priorProgram = mState.currentProgram;
963 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000964
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000965 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000967 Program *newProgram = mResourceManager->getProgram(program);
968 Program *oldProgram = mResourceManager->getProgram(priorProgram);
969
970 if (newProgram)
971 {
972 newProgram->addRef();
973 }
974
975 if (oldProgram)
976 {
977 oldProgram->release();
978 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980}
981
982void Context::setFramebufferZero(Framebuffer *buffer)
983{
984 delete mFramebufferMap[0];
985 mFramebufferMap[0] = buffer;
986}
987
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000988void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000990 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
991 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992}
993
994Framebuffer *Context::getFramebuffer(unsigned int handle)
995{
996 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000997
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998 if (framebuffer == mFramebufferMap.end())
999 {
1000 return NULL;
1001 }
1002 else
1003 {
1004 return framebuffer->second;
1005 }
1006}
1007
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008Buffer *Context::getArrayBuffer()
1009{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001010 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011}
1012
1013Buffer *Context::getElementArrayBuffer()
1014{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001015 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
1018Program *Context::getCurrentProgram()
1019{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001020 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021}
1022
1023Texture2D *Context::getTexture2D()
1024{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001025 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 +00001026 {
1027 return mTexture2DZero;
1028 }
1029
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001030 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031}
1032
1033TextureCubeMap *Context::getTextureCubeMap()
1034{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001035 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 +00001036 {
1037 return mTextureCubeMapZero;
1038 }
1039
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001040 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041}
1042
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001043Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001045 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001046
1047 if (texid == 0)
1048 {
1049 switch (type)
1050 {
1051 default: UNREACHABLE();
1052 case SAMPLER_2D: return mTexture2DZero;
1053 case SAMPLER_CUBE: return mTextureCubeMapZero;
1054 }
1055 }
1056
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001057 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058}
1059
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001060bool Context::getBooleanv(GLenum pname, GLboolean *params)
1061{
1062 switch (pname)
1063 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001064 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1065 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1066 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001067 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001068 params[0] = mState.colorMaskRed;
1069 params[1] = mState.colorMaskGreen;
1070 params[2] = mState.colorMaskBlue;
1071 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001072 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001073 case GL_CULL_FACE: *params = mState.cullFace;
1074 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1075 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1076 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1077 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1078 case GL_STENCIL_TEST: *params = mState.stencilTest;
1079 case GL_DEPTH_TEST: *params = mState.depthTest;
1080 case GL_BLEND: *params = mState.blend;
1081 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001082 default:
1083 return false;
1084 }
1085
1086 return true;
1087}
1088
1089bool Context::getFloatv(GLenum pname, GLfloat *params)
1090{
1091 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1092 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1093 // GetIntegerv as its native query function. As it would require conversion in any
1094 // case, this should make no difference to the calling application.
1095 switch (pname)
1096 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001097 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1098 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1099 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1100 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1101 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001102 case GL_ALIASED_LINE_WIDTH_RANGE:
1103 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1104 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1105 break;
1106 case GL_ALIASED_POINT_SIZE_RANGE:
1107 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001108 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 +00001109 break;
1110 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001111 params[0] = mState.zNear;
1112 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001113 break;
1114 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001115 params[0] = mState.colorClearValue.red;
1116 params[1] = mState.colorClearValue.green;
1117 params[2] = mState.colorClearValue.blue;
1118 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001119 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001120 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001121 params[0] = mState.blendColor.red;
1122 params[1] = mState.blendColor.green;
1123 params[2] = mState.blendColor.blue;
1124 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001125 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001126 default:
1127 return false;
1128 }
1129
1130 return true;
1131}
1132
1133bool Context::getIntegerv(GLenum pname, GLint *params)
1134{
1135 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1136 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1137 // GetIntegerv as its native query function. As it would require conversion in any
1138 // case, this should make no difference to the calling application. You may find it in
1139 // Context::getFloatv.
1140 switch (pname)
1141 {
1142 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1143 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1144 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1145 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1146 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1147 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1148 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1149 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001150 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001151 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001152 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1153 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001154 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1155 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1156 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001157 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001158 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1159 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1160 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1161 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1162 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1163 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1164 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1165 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1166 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1167 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1168 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1169 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1170 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1171 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1172 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1173 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1174 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1175 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1176 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1177 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1178 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1179 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1180 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1181 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1182 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1183 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1184 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1185 case GL_SUBPIXEL_BITS: *params = 4; break;
1186 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1187 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001188 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1189 {
1190 if (supportsCompressedTextures())
1191 {
1192 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1193 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1194 *params = 2;
1195 }
1196 else
1197 {
1198 *params = 0;
1199 }
1200 }
1201 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001202 case GL_MAX_SAMPLES_ANGLE:
1203 {
1204 GLsizei maxSamples = getMaxSupportedSamples();
1205 if (maxSamples != 0)
1206 {
1207 *params = maxSamples;
1208 }
1209 else
1210 {
1211 return false;
1212 }
1213
1214 break;
1215 }
1216 case GL_SAMPLE_BUFFERS:
1217 case GL_SAMPLES:
1218 {
1219 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1220 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1221 {
1222 switch (pname)
1223 {
1224 case GL_SAMPLE_BUFFERS:
1225 if (framebuffer->getSamples() != 0)
1226 {
1227 *params = 1;
1228 }
1229 else
1230 {
1231 *params = 0;
1232 }
1233 break;
1234 case GL_SAMPLES:
1235 *params = framebuffer->getSamples();
1236 break;
1237 }
1238 }
1239 else
1240 {
1241 *params = 0;
1242 }
1243 }
1244 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001245 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1246 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1247 case GL_MAX_VIEWPORT_DIMS:
1248 {
1249 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1250 params[0] = maxDimension;
1251 params[1] = maxDimension;
1252 }
1253 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001254 case GL_COMPRESSED_TEXTURE_FORMATS:
1255 {
1256 if (supportsCompressedTextures())
1257 {
1258 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1259 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1260 }
1261 }
1262 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001263 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001264 params[0] = mState.viewportX;
1265 params[1] = mState.viewportY;
1266 params[2] = mState.viewportWidth;
1267 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001268 break;
1269 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001270 params[0] = mState.scissorX;
1271 params[1] = mState.scissorY;
1272 params[2] = mState.scissorWidth;
1273 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001274 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001275 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1276 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001277 case GL_RED_BITS:
1278 case GL_GREEN_BITS:
1279 case GL_BLUE_BITS:
1280 case GL_ALPHA_BITS:
1281 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001282 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001283 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1284
1285 if (colorbuffer)
1286 {
1287 switch (pname)
1288 {
1289 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1290 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1291 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1292 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1293 }
1294 }
1295 else
1296 {
1297 *params = 0;
1298 }
1299 }
1300 break;
1301 case GL_DEPTH_BITS:
1302 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001303 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001304 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001305
1306 if (depthbuffer)
1307 {
1308 *params = depthbuffer->getDepthSize();
1309 }
1310 else
1311 {
1312 *params = 0;
1313 }
1314 }
1315 break;
1316 case GL_STENCIL_BITS:
1317 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001318 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001319 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001320
1321 if (stencilbuffer)
1322 {
1323 *params = stencilbuffer->getStencilSize();
1324 }
1325 else
1326 {
1327 *params = 0;
1328 }
1329 }
1330 break;
1331 case GL_TEXTURE_BINDING_2D:
1332 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001333 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001334 {
1335 error(GL_INVALID_OPERATION);
1336 return false;
1337 }
1338
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001339 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001340 }
1341 break;
1342 case GL_TEXTURE_BINDING_CUBE_MAP:
1343 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001344 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001345 {
1346 error(GL_INVALID_OPERATION);
1347 return false;
1348 }
1349
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001350 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001351 }
1352 break;
1353 default:
1354 return false;
1355 }
1356
1357 return true;
1358}
1359
1360bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1361{
1362 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1363 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1364 // to the fact that it is stored internally as a float, and so would require conversion
1365 // if returned from Context::getIntegerv. Since this conversion is already implemented
1366 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1367 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1368 // application.
1369 switch (pname)
1370 {
1371 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1372 case GL_SHADER_BINARY_FORMATS:
1373 {
1374 *type = GL_INT;
1375 *numParams = 0;
1376 }
1377 break;
1378 case GL_MAX_VERTEX_ATTRIBS:
1379 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1380 case GL_MAX_VARYING_VECTORS:
1381 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1382 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1383 case GL_MAX_TEXTURE_IMAGE_UNITS:
1384 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1385 case GL_MAX_RENDERBUFFER_SIZE:
1386 case GL_NUM_SHADER_BINARY_FORMATS:
1387 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1388 case GL_ARRAY_BUFFER_BINDING:
1389 case GL_FRAMEBUFFER_BINDING:
1390 case GL_RENDERBUFFER_BINDING:
1391 case GL_CURRENT_PROGRAM:
1392 case GL_PACK_ALIGNMENT:
1393 case GL_UNPACK_ALIGNMENT:
1394 case GL_GENERATE_MIPMAP_HINT:
1395 case GL_RED_BITS:
1396 case GL_GREEN_BITS:
1397 case GL_BLUE_BITS:
1398 case GL_ALPHA_BITS:
1399 case GL_DEPTH_BITS:
1400 case GL_STENCIL_BITS:
1401 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1402 case GL_CULL_FACE_MODE:
1403 case GL_FRONT_FACE:
1404 case GL_ACTIVE_TEXTURE:
1405 case GL_STENCIL_FUNC:
1406 case GL_STENCIL_VALUE_MASK:
1407 case GL_STENCIL_REF:
1408 case GL_STENCIL_FAIL:
1409 case GL_STENCIL_PASS_DEPTH_FAIL:
1410 case GL_STENCIL_PASS_DEPTH_PASS:
1411 case GL_STENCIL_BACK_FUNC:
1412 case GL_STENCIL_BACK_VALUE_MASK:
1413 case GL_STENCIL_BACK_REF:
1414 case GL_STENCIL_BACK_FAIL:
1415 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1416 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1417 case GL_DEPTH_FUNC:
1418 case GL_BLEND_SRC_RGB:
1419 case GL_BLEND_SRC_ALPHA:
1420 case GL_BLEND_DST_RGB:
1421 case GL_BLEND_DST_ALPHA:
1422 case GL_BLEND_EQUATION_RGB:
1423 case GL_BLEND_EQUATION_ALPHA:
1424 case GL_STENCIL_WRITEMASK:
1425 case GL_STENCIL_BACK_WRITEMASK:
1426 case GL_STENCIL_CLEAR_VALUE:
1427 case GL_SUBPIXEL_BITS:
1428 case GL_MAX_TEXTURE_SIZE:
1429 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1430 case GL_SAMPLE_BUFFERS:
1431 case GL_SAMPLES:
1432 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1433 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1434 case GL_TEXTURE_BINDING_2D:
1435 case GL_TEXTURE_BINDING_CUBE_MAP:
1436 {
1437 *type = GL_INT;
1438 *numParams = 1;
1439 }
1440 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001441 case GL_MAX_SAMPLES_ANGLE:
1442 {
1443 if (getMaxSupportedSamples() != 0)
1444 {
1445 *type = GL_INT;
1446 *numParams = 1;
1447 }
1448 else
1449 {
1450 return false;
1451 }
1452 }
1453 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001454 case GL_MAX_VIEWPORT_DIMS:
1455 {
1456 *type = GL_INT;
1457 *numParams = 2;
1458 }
1459 break;
1460 case GL_VIEWPORT:
1461 case GL_SCISSOR_BOX:
1462 {
1463 *type = GL_INT;
1464 *numParams = 4;
1465 }
1466 break;
1467 case GL_SHADER_COMPILER:
1468 case GL_SAMPLE_COVERAGE_INVERT:
1469 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001470 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1471 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1472 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1473 case GL_SAMPLE_COVERAGE:
1474 case GL_SCISSOR_TEST:
1475 case GL_STENCIL_TEST:
1476 case GL_DEPTH_TEST:
1477 case GL_BLEND:
1478 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001479 {
1480 *type = GL_BOOL;
1481 *numParams = 1;
1482 }
1483 break;
1484 case GL_COLOR_WRITEMASK:
1485 {
1486 *type = GL_BOOL;
1487 *numParams = 4;
1488 }
1489 break;
1490 case GL_POLYGON_OFFSET_FACTOR:
1491 case GL_POLYGON_OFFSET_UNITS:
1492 case GL_SAMPLE_COVERAGE_VALUE:
1493 case GL_DEPTH_CLEAR_VALUE:
1494 case GL_LINE_WIDTH:
1495 {
1496 *type = GL_FLOAT;
1497 *numParams = 1;
1498 }
1499 break;
1500 case GL_ALIASED_LINE_WIDTH_RANGE:
1501 case GL_ALIASED_POINT_SIZE_RANGE:
1502 case GL_DEPTH_RANGE:
1503 {
1504 *type = GL_FLOAT;
1505 *numParams = 2;
1506 }
1507 break;
1508 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001509 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001510 {
1511 *type = GL_FLOAT;
1512 *numParams = 4;
1513 }
1514 break;
1515 default:
1516 return false;
1517 }
1518
1519 return true;
1520}
1521
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522// Applies the render target surface, depth stencil surface, viewport rectangle and
1523// scissor rectangle to the Direct3D 9 device
1524bool Context::applyRenderTarget(bool ignoreViewport)
1525{
1526 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001527
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001528 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001529
1530 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1531 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001532 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001534 return false;
1535 }
1536
1537 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001538 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001540 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1541 if (renderTargetSerial != mAppliedRenderTargetSerial)
1542 {
1543 device->SetRenderTarget(0, renderTarget);
1544 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001545 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 +00001546 }
1547
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001548 unsigned int depthbufferSerial = 0;
1549 unsigned int stencilbufferSerial = 0;
1550 if (framebufferObject->getDepthbufferType() != GL_NONE)
1551 {
1552 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1553 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1554 }
1555 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1556 {
1557 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1558 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1559 }
1560
1561 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1562 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001563 {
1564 device->SetDepthStencilSurface(depthStencil);
1565 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001566 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001567 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001568
1569 D3DVIEWPORT9 viewport;
1570 D3DSURFACE_DESC desc;
1571 renderTarget->GetDesc(&desc);
1572
1573 if (ignoreViewport)
1574 {
1575 viewport.X = 0;
1576 viewport.Y = 0;
1577 viewport.Width = desc.Width;
1578 viewport.Height = desc.Height;
1579 viewport.MinZ = 0.0f;
1580 viewport.MaxZ = 1.0f;
1581 }
1582 else
1583 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001584 viewport.X = std::max(mState.viewportX, 0);
1585 viewport.Y = std::max(mState.viewportY, 0);
1586 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1587 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1588 viewport.MinZ = clamp01(mState.zNear);
1589 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590 }
1591
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001592 if (viewport.Width <= 0 || viewport.Height <= 0)
1593 {
1594 return false; // Nothing to render
1595 }
1596
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597 device->SetViewport(&viewport);
1598
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001599 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001601 if (mState.scissorTest)
1602 {
1603 RECT rect = {mState.scissorX,
1604 mState.scissorY,
1605 mState.scissorX + mState.scissorWidth,
1606 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001607 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1608 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001609 device->SetScissorRect(&rect);
1610 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1611 }
1612 else
1613 {
1614 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1615 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001616
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001617 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618 }
1619
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001620 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001621 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622 Program *programObject = getCurrentProgram();
1623
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001624 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001625 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001627
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001628 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001629 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1630 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1631 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001632 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1633
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001634 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001635 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001636 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1637
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001638 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001639 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001640
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001641 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001642 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001643
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001644 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001645 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001646 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647 }
1648
1649 return true;
1650}
1651
1652// 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 +00001653void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654{
1655 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001656 Program *programObject = getCurrentProgram();
1657
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001658 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001659 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001660 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001662 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001663 GLint alwaysFront = !isTriangleMode(drawMode);
1664 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1665
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001666 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001667
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001668 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001670 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001672 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673 }
1674 else
1675 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001676 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 }
1678
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001679 mCullStateDirty = false;
1680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001682 if (mDepthStateDirty)
1683 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001684 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001686 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1687 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 }
1689 else
1690 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001691 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001693
1694 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695 }
1696
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001697 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001699 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001700 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001701 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1702
1703 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1704 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1705 {
1706 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1707 }
1708 else
1709 {
1710 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1711 unorm<8>(mState.blendColor.alpha),
1712 unorm<8>(mState.blendColor.alpha),
1713 unorm<8>(mState.blendColor.alpha)));
1714 }
1715
1716 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1717 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1718 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1719
1720 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1721 mState.destBlendRGB != mState.destBlendAlpha ||
1722 mState.blendEquationRGB != mState.blendEquationAlpha)
1723 {
1724 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1725
1726 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1727 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1728 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1729
1730 }
1731 else
1732 {
1733 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1734 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001735 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001736 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001737 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001739 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001740
1741 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
1743
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001744 if (mStencilStateDirty || mFrontFaceDirty)
1745 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001746 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001747 {
1748 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1749 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1750
1751 // FIXME: Unsupported by D3D9
1752 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1753 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1754 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1755 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1756 mState.stencilRef != mState.stencilBackRef ||
1757 mState.stencilMask != mState.stencilBackMask)
1758 {
1759 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1760 return error(GL_INVALID_OPERATION);
1761 }
1762
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001763 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001764 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001765 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1766
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1768 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1769 es2dx::ConvertComparison(mState.stencilFunc));
1770
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001771 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 +00001772 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1773
1774 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1775 es2dx::ConvertStencilOp(mState.stencilFail));
1776 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1777 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1778 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1779 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1780
1781 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1782 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1783 es2dx::ConvertComparison(mState.stencilBackFunc));
1784
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001785 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 +00001786 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1787
1788 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1789 es2dx::ConvertStencilOp(mState.stencilBackFail));
1790 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1791 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1792 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1793 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1794 }
1795 else
1796 {
1797 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1798 }
1799
1800 mStencilStateDirty = false;
1801 }
1802
1803 if (mMaskStateDirty)
1804 {
1805 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1806 mState.colorMaskBlue, mState.colorMaskAlpha));
1807 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1808
1809 mMaskStateDirty = false;
1810 }
1811
1812 if (mPolygonOffsetStateDirty)
1813 {
1814 if (mState.polygonOffsetFill)
1815 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001816 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001817 if (depthbuffer)
1818 {
1819 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1820 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1821 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1822 }
1823 }
1824 else
1825 {
1826 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1827 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1828 }
1829
1830 mPolygonOffsetStateDirty = false;
1831 }
1832
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001833 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001835 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001836 {
1837 FIXME("Sample alpha to coverage is unimplemented.");
1838 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001840 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001841 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001842 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1843 unsigned int mask = 0;
1844 if (mState.sampleCoverageValue != 0)
1845 {
1846 float threshold = 0.5f;
1847
1848 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1849 {
1850 mask <<= 1;
1851
1852 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1853 {
1854 threshold += 1.0f;
1855 mask |= 1;
1856 }
1857 }
1858 }
1859
1860 if (mState.sampleCoverageInvert)
1861 {
1862 mask = ~mask;
1863 }
1864
1865 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1866 }
1867 else
1868 {
1869 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001870 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001871
1872 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 }
1874
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001875 if (mDitherStateDirty)
1876 {
1877 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1878
1879 mDitherStateDirty = false;
1880 }
1881
1882 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883}
1884
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001885// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001886void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001888 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001890 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001892 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 }
1894 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001895}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001897GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001898{
1899 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1900
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001901 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001902 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001903 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001904 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001905 }
1906
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001907 lookupAttributeMapping(translated);
1908
1909 mBufferBackEnd->setupAttributesPreDraw(translated);
1910
1911 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1912 {
1913 if (translated[i].enabled && translated[i].nonArray)
1914 {
1915 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1916 if (err != GL_NO_ERROR)
1917 {
1918 return err;
1919 }
1920
1921 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1922
1923 *useIndexing = true;
1924 return GL_NO_ERROR;
1925 }
1926 }
1927
1928 *useIndexing = false;
1929 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001930}
1931
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001932GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001933{
1934 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1935
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001936 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001937
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001938 if (err == GL_NO_ERROR)
1939 {
1940 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001941
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001942 mBufferBackEnd->setupAttributesPreDraw(translated);
1943 }
1944
1945 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001946}
1947
1948// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001949GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001950{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001951 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001952
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001953 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001954 {
1955 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1956 }
1957
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001958 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959}
1960
1961// Applies the shaders and shader constants to the Direct3D 9 device
1962void Context::applyShaders()
1963{
1964 IDirect3DDevice9 *device = getDevice();
1965 Program *programObject = getCurrentProgram();
1966 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1967 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1968
1969 device->SetVertexShader(vertexShader);
1970 device->SetPixelShader(pixelShader);
1971
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001972 if (programObject->getSerial() != mAppliedProgram)
1973 {
1974 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001975 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001976 mAppliedProgram = programObject->getSerial();
1977 }
1978
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979 programObject->applyUniforms();
1980}
1981
1982// Applies the textures and sampler states to the Direct3D 9 device
1983void Context::applyTextures()
1984{
1985 IDirect3DDevice9 *device = getDevice();
1986 Program *programObject = getCurrentProgram();
1987
1988 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1989 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001990 int textureUnit = programObject->getSamplerMapping(sampler);
1991 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001993 SamplerType textureType = programObject->getSamplerType(sampler);
1994
1995 Texture *texture = getSamplerTexture(textureUnit, textureType);
1996
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001997 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001998 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001999 if (texture->isComplete())
2000 {
2001 GLenum wrapS = texture->getWrapS();
2002 GLenum wrapT = texture->getWrapT();
2003 GLenum minFilter = texture->getMinFilter();
2004 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002006 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2007 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002009 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2010 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2011 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2012 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2013 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002015 device->SetTexture(sampler, texture->getTexture());
2016 }
2017 else
2018 {
2019 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2020 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002021 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002022
2023 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002025 else
2026 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002027 if (programObject->isSamplerDirty(sampler))
2028 {
2029 device->SetTexture(sampler, NULL);
2030 programObject->setSamplerDirty(sampler, false);
2031 }
2032 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033 }
2034}
2035
2036void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2037{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002038 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002039
2040 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2041 {
2042 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2043 }
2044
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002045 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2046 {
2047 return error(GL_INVALID_OPERATION);
2048 }
2049
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2051 IDirect3DDevice9 *device = getDevice();
2052
2053 D3DSURFACE_DESC desc;
2054 renderTarget->GetDesc(&desc);
2055
2056 IDirect3DSurface9 *systemSurface;
2057 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2058
2059 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2060 {
2061 return error(GL_OUT_OF_MEMORY);
2062 }
2063
2064 ASSERT(SUCCEEDED(result));
2065
2066 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2067 {
2068 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2069 }
2070
2071 result = device->GetRenderTargetData(renderTarget, systemSurface);
2072
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 if (FAILED(result))
2074 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 systemSurface->Release();
2076
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002077 switch (result)
2078 {
2079 case D3DERR_DRIVERINTERNALERROR:
2080 case D3DERR_DEVICELOST:
2081 return error(GL_OUT_OF_MEMORY);
2082 default:
2083 UNREACHABLE();
2084 return; // No sensible error to generate
2085 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 }
2087
2088 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002089 RECT rect = {std::max(x, 0),
2090 std::max(y, 0),
2091 std::min(x + width, (int)desc.Width),
2092 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093
2094 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2095
2096 if (FAILED(result))
2097 {
2098 UNREACHABLE();
2099 systemSurface->Release();
2100
2101 return; // No sensible error to generate
2102 }
2103
2104 unsigned char *source = (unsigned char*)lock.pBits;
2105 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002106 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002108 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002109
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 for (int j = 0; j < rect.bottom - rect.top; j++)
2111 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002112 if (desc.Format == D3DFMT_A8R8G8B8 &&
2113 format == GL_BGRA_EXT &&
2114 type == GL_UNSIGNED_BYTE)
2115 {
2116 // Fast path for EXT_read_format_bgra, given
2117 // an RGBA source buffer. Note that buffers with no
2118 // alpha go through the slow path below.
2119 memcpy(dest + j * outputPitch,
2120 source + j * lock.Pitch,
2121 (rect.right - rect.left) * 4);
2122 continue;
2123 }
2124
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 for (int i = 0; i < rect.right - rect.left; i++)
2126 {
2127 float r;
2128 float g;
2129 float b;
2130 float a;
2131
2132 switch (desc.Format)
2133 {
2134 case D3DFMT_R5G6B5:
2135 {
2136 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2137
2138 a = 1.0f;
2139 b = (rgb & 0x001F) * (1.0f / 0x001F);
2140 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2141 r = (rgb & 0xF800) * (1.0f / 0xF800);
2142 }
2143 break;
2144 case D3DFMT_X1R5G5B5:
2145 {
2146 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2147
2148 a = 1.0f;
2149 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2150 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2151 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2152 }
2153 break;
2154 case D3DFMT_A1R5G5B5:
2155 {
2156 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2157
2158 a = (argb & 0x8000) ? 1.0f : 0.0f;
2159 b = (argb & 0x001F) * (1.0f / 0x001F);
2160 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2161 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2162 }
2163 break;
2164 case D3DFMT_A8R8G8B8:
2165 {
2166 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2167
2168 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2169 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2170 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2171 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2172 }
2173 break;
2174 case D3DFMT_X8R8G8B8:
2175 {
2176 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2177
2178 a = 1.0f;
2179 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2180 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2181 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2182 }
2183 break;
2184 case D3DFMT_A2R10G10B10:
2185 {
2186 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2187
2188 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2189 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2190 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2191 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2192 }
2193 break;
2194 default:
2195 UNIMPLEMENTED(); // FIXME
2196 UNREACHABLE();
2197 }
2198
2199 switch (format)
2200 {
2201 case GL_RGBA:
2202 switch (type)
2203 {
2204 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002205 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2206 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2207 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2208 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209 break;
2210 default: UNREACHABLE();
2211 }
2212 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002213 case GL_BGRA_EXT:
2214 switch (type)
2215 {
2216 case GL_UNSIGNED_BYTE:
2217 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2218 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2219 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2220 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2221 break;
2222 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2223 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2224 // this type is packed as follows:
2225 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2226 // --------------------------------------------------------------------------------
2227 // | 4th | 3rd | 2nd | 1st component |
2228 // --------------------------------------------------------------------------------
2229 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2230 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2231 ((unsigned short)(15 * a + 0.5f) << 12)|
2232 ((unsigned short)(15 * r + 0.5f) << 8) |
2233 ((unsigned short)(15 * g + 0.5f) << 4) |
2234 ((unsigned short)(15 * b + 0.5f) << 0);
2235 break;
2236 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2237 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2238 // this type is packed as follows:
2239 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2240 // --------------------------------------------------------------------------------
2241 // | 4th | 3rd | 2nd | 1st component |
2242 // --------------------------------------------------------------------------------
2243 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2244 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2245 ((unsigned short)( a + 0.5f) << 15) |
2246 ((unsigned short)(31 * r + 0.5f) << 10) |
2247 ((unsigned short)(31 * g + 0.5f) << 5) |
2248 ((unsigned short)(31 * b + 0.5f) << 0);
2249 break;
2250 default: UNREACHABLE();
2251 }
2252 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002253 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 switch (type)
2255 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002256 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002257 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2258 ((unsigned short)(31 * b + 0.5f) << 0) |
2259 ((unsigned short)(63 * g + 0.5f) << 5) |
2260 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261 break;
2262 default: UNREACHABLE();
2263 }
2264 break;
2265 default: UNREACHABLE();
2266 }
2267 }
2268 }
2269
2270 systemSurface->UnlockRect();
2271
2272 systemSurface->Release();
2273}
2274
2275void Context::clear(GLbitfield mask)
2276{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002277 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002278
2279 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2280 {
2281 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2282
2283 return;
2284 }
2285
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002286 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 IDirect3DDevice9 *device = getDevice();
2288 DWORD flags = 0;
2289
2290 if (mask & GL_COLOR_BUFFER_BIT)
2291 {
2292 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002293
2294 if (framebufferObject->getColorbufferType() != GL_NONE)
2295 {
2296 flags |= D3DCLEAR_TARGET;
2297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 }
2299
2300 if (mask & GL_DEPTH_BUFFER_BIT)
2301 {
2302 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002303 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 {
2305 flags |= D3DCLEAR_ZBUFFER;
2306 }
2307 }
2308
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309 GLuint stencilUnmasked = 0x0;
2310
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002311 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002314 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002316 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2317 D3DSURFACE_DESC desc;
2318 depthStencil->GetDesc(&desc);
2319
2320 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2321 stencilUnmasked = (0x1 << stencilSize) - 1;
2322
2323 if (stencilUnmasked != 0x0)
2324 {
2325 flags |= D3DCLEAR_STENCIL;
2326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327 }
2328 }
2329
2330 if (mask != 0)
2331 {
2332 return error(GL_INVALID_VALUE);
2333 }
2334
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002335 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2336 {
2337 return;
2338 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002340 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2341 unorm<8>(mState.colorClearValue.red),
2342 unorm<8>(mState.colorClearValue.green),
2343 unorm<8>(mState.colorClearValue.blue));
2344 float depth = clamp01(mState.depthClearValue);
2345 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002346
2347 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2348
2349 D3DSURFACE_DESC desc;
2350 renderTarget->GetDesc(&desc);
2351
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002352 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353
2354 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002355 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002356 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002357 !(mState.colorMaskRed && mState.colorMaskGreen &&
2358 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359
2360 if (needMaskedColorClear || needMaskedStencilClear)
2361 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002362 // State which is altered in all paths from this point to the clear call is saved.
2363 // State which is altered in only some paths will be flagged dirty in the case that
2364 // that path is taken.
2365 HRESULT hr;
2366 if (mMaskedClearSavedState == NULL)
2367 {
2368 hr = device->BeginStateBlock();
2369 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2370
2371 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2372 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2373 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2374 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2375 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2376 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2377 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2378 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2379 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2380 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2381 device->SetPixelShader(NULL);
2382 device->SetVertexShader(NULL);
2383 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2384 device->SetStreamSourceFreq(0, 1);
2385
2386 hr = device->EndStateBlock(&mMaskedClearSavedState);
2387 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2388 }
2389
2390 ASSERT(mMaskedClearSavedState != NULL);
2391
2392 if (mMaskedClearSavedState != NULL)
2393 {
2394 hr = mMaskedClearSavedState->Capture();
2395 ASSERT(SUCCEEDED(hr));
2396 }
2397
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2399 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2400 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2401 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2402 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2403 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2404 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2405 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2406
2407 if (flags & D3DCLEAR_TARGET)
2408 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002409 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2410 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2411 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2412 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 }
2414 else
2415 {
2416 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2417 }
2418
2419 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2420 {
2421 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2422 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2423 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2424 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002425 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002426 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2428 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002429 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 }
2431 else
2432 {
2433 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2434 }
2435
2436 device->SetPixelShader(NULL);
2437 device->SetVertexShader(NULL);
2438 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002439 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002441 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442 {
2443 float x, y, z, w;
2444 D3DCOLOR diffuse;
2445 };
2446
2447 Vertex quad[4];
2448 quad[0].x = 0.0f;
2449 quad[0].y = (float)desc.Height;
2450 quad[0].z = 0.0f;
2451 quad[0].w = 1.0f;
2452 quad[0].diffuse = color;
2453
2454 quad[1].x = (float)desc.Width;
2455 quad[1].y = (float)desc.Height;
2456 quad[1].z = 0.0f;
2457 quad[1].w = 1.0f;
2458 quad[1].diffuse = color;
2459
2460 quad[2].x = 0.0f;
2461 quad[2].y = 0.0f;
2462 quad[2].z = 0.0f;
2463 quad[2].w = 1.0f;
2464 quad[2].diffuse = color;
2465
2466 quad[3].x = (float)desc.Width;
2467 quad[3].y = 0.0f;
2468 quad[3].z = 0.0f;
2469 quad[3].w = 1.0f;
2470 quad[3].diffuse = color;
2471
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002472 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474
2475 if (flags & D3DCLEAR_ZBUFFER)
2476 {
2477 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2478 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2479 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2480 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002481
2482 if (mMaskedClearSavedState != NULL)
2483 {
2484 mMaskedClearSavedState->Apply();
2485 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002487 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 {
2489 device->Clear(0, NULL, flags, color, depth, stencil);
2490 }
2491}
2492
2493void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2494{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002495 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496 {
2497 return error(GL_INVALID_OPERATION);
2498 }
2499
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002500 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 IDirect3DDevice9 *device = getDevice();
2502 D3DPRIMITIVETYPE primitiveType;
2503 int primitiveCount;
2504
2505 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2506 return error(GL_INVALID_ENUM);
2507
2508 if (primitiveCount <= 0)
2509 {
2510 return;
2511 }
2512
2513 if (!applyRenderTarget(false))
2514 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002515 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 }
2517
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002518 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002519
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002520 TranslatedIndexData indexInfo;
2521 bool useIndexing;
2522 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002523 if (err != GL_NO_ERROR)
2524 {
2525 return error(err);
2526 }
2527
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528 applyShaders();
2529 applyTextures();
2530
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002531 if (!getCurrentProgram()->validateSamplers())
2532 {
2533 return error(GL_INVALID_OPERATION);
2534 }
2535
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002536 if (!cullSkipsDraw(mode))
2537 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002538 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002539 if (useIndexing)
2540 {
2541 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2542 }
2543 else
2544 {
2545 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2546 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002547 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548}
2549
2550void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2551{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002552 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553 {
2554 return error(GL_INVALID_OPERATION);
2555 }
2556
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002557 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 {
2559 return error(GL_INVALID_OPERATION);
2560 }
2561
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002562 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563 IDirect3DDevice9 *device = getDevice();
2564 D3DPRIMITIVETYPE primitiveType;
2565 int primitiveCount;
2566
2567 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2568 return error(GL_INVALID_ENUM);
2569
2570 if (primitiveCount <= 0)
2571 {
2572 return;
2573 }
2574
2575 if (!applyRenderTarget(false))
2576 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002577 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578 }
2579
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002580 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002581
2582 TranslatedIndexData indexInfo;
2583 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2584 if (err != GL_NO_ERROR)
2585 {
2586 return error(err);
2587 }
2588
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002589 err = applyVertexBuffer(indexInfo);
2590 if (err != GL_NO_ERROR)
2591 {
2592 return error(err);
2593 }
2594
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 applyShaders();
2596 applyTextures();
2597
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002598 if (!getCurrentProgram()->validateSamplers())
2599 {
2600 return error(GL_INVALID_OPERATION);
2601 }
2602
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002603 if (!cullSkipsDraw(mode))
2604 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002605 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002606 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 +00002607 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608}
2609
2610void Context::finish()
2611{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002612 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613 IDirect3DDevice9 *device = getDevice();
2614 IDirect3DQuery9 *occlusionQuery = NULL;
2615
2616 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2617
2618 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2619 {
2620 return error(GL_OUT_OF_MEMORY);
2621 }
2622
2623 ASSERT(SUCCEEDED(result));
2624
2625 if (occlusionQuery)
2626 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002627 IDirect3DStateBlock9 *savedState = NULL;
2628 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2629
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002630 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2631 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632
2633 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002634 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635 device->SetPixelShader(NULL);
2636 device->SetVertexShader(NULL);
2637 device->SetFVF(D3DFVF_XYZRHW);
2638 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002639 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002640 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002641
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002642 result = occlusionQuery->Issue(D3DISSUE_END);
2643 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644
2645 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2646 {
2647 // Keep polling, but allow other threads to do something useful first
2648 Sleep(0);
2649 }
2650
2651 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002652
2653 if (savedState)
2654 {
2655 savedState->Apply();
2656 savedState->Release();
2657 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002658 }
2659}
2660
2661void Context::flush()
2662{
2663 IDirect3DDevice9 *device = getDevice();
2664 IDirect3DQuery9 *eventQuery = NULL;
2665
2666 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2667
2668 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2669 {
2670 return error(GL_OUT_OF_MEMORY);
2671 }
2672
2673 ASSERT(SUCCEEDED(result));
2674
2675 if (eventQuery)
2676 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002677 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2678 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002680 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002682
2683 if (result == D3DERR_DEVICELOST)
2684 {
2685 error(GL_OUT_OF_MEMORY);
2686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687 }
2688}
2689
2690void Context::recordInvalidEnum()
2691{
2692 mInvalidEnum = true;
2693}
2694
2695void Context::recordInvalidValue()
2696{
2697 mInvalidValue = true;
2698}
2699
2700void Context::recordInvalidOperation()
2701{
2702 mInvalidOperation = true;
2703}
2704
2705void Context::recordOutOfMemory()
2706{
2707 mOutOfMemory = true;
2708}
2709
2710void Context::recordInvalidFramebufferOperation()
2711{
2712 mInvalidFramebufferOperation = true;
2713}
2714
2715// Get one of the recorded errors and clear its flag, if any.
2716// [OpenGL ES 2.0.24] section 2.5 page 13.
2717GLenum Context::getError()
2718{
2719 if (mInvalidEnum)
2720 {
2721 mInvalidEnum = false;
2722
2723 return GL_INVALID_ENUM;
2724 }
2725
2726 if (mInvalidValue)
2727 {
2728 mInvalidValue = false;
2729
2730 return GL_INVALID_VALUE;
2731 }
2732
2733 if (mInvalidOperation)
2734 {
2735 mInvalidOperation = false;
2736
2737 return GL_INVALID_OPERATION;
2738 }
2739
2740 if (mOutOfMemory)
2741 {
2742 mOutOfMemory = false;
2743
2744 return GL_OUT_OF_MEMORY;
2745 }
2746
2747 if (mInvalidFramebufferOperation)
2748 {
2749 mInvalidFramebufferOperation = false;
2750
2751 return GL_INVALID_FRAMEBUFFER_OPERATION;
2752 }
2753
2754 return GL_NO_ERROR;
2755}
2756
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002757bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002758{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002759 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002760}
2761
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002762int Context::getMaxSupportedSamples() const
2763{
2764 return mMaxSupportedSamples;
2765}
2766
2767int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2768{
2769 if (requested == 0)
2770 {
2771 return requested;
2772 }
2773
2774 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2775 if (itr == mMultiSampleSupport.end())
2776 {
2777 return -1;
2778 }
2779
2780 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2781 {
2782 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2783 {
2784 return i;
2785 }
2786 }
2787
2788 return -1;
2789}
2790
daniel@transgaming.com01868132010-08-24 19:21:17 +00002791bool Context::supportsCompressedTextures() const
2792{
2793 return mSupportsCompressedTextures;
2794}
2795
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796void Context::detachBuffer(GLuint buffer)
2797{
2798 // [OpenGL ES 2.0.24] section 2.9 page 22:
2799 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2800 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2801
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002802 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002804 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805 }
2806
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002807 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002809 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810 }
2811
2812 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2813 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002814 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002816 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817 }
2818 }
2819}
2820
2821void Context::detachTexture(GLuint texture)
2822{
2823 // [OpenGL ES 2.0.24] section 3.8 page 84:
2824 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2825 // rebound to texture object zero
2826
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002827 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002829 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002830 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002831 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002832 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002833 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002834 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835 }
2836 }
2837
2838 // [OpenGL ES 2.0.24] section 4.4 page 112:
2839 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2840 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2841 // image was attached in the currently bound framebuffer.
2842
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002843 Framebuffer *readFramebuffer = getReadFramebuffer();
2844 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002846 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002847 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002848 readFramebuffer->detachTexture(texture);
2849 }
2850
2851 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2852 {
2853 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002854 }
2855}
2856
2857void Context::detachFramebuffer(GLuint framebuffer)
2858{
2859 // [OpenGL ES 2.0.24] section 4.4 page 107:
2860 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2861 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2862
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002863 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002865 bindReadFramebuffer(0);
2866 }
2867
2868 if (mState.drawFramebuffer == framebuffer)
2869 {
2870 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871 }
2872}
2873
2874void Context::detachRenderbuffer(GLuint renderbuffer)
2875{
2876 // [OpenGL ES 2.0.24] section 4.4 page 109:
2877 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2878 // had been executed with the target RENDERBUFFER and name of zero.
2879
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002880 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881 {
2882 bindRenderbuffer(0);
2883 }
2884
2885 // [OpenGL ES 2.0.24] section 4.4 page 111:
2886 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2887 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2888 // point to which this image was attached in the currently bound framebuffer.
2889
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002890 Framebuffer *readFramebuffer = getReadFramebuffer();
2891 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002892
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002893 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002895 readFramebuffer->detachRenderbuffer(renderbuffer);
2896 }
2897
2898 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2899 {
2900 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002901 }
2902}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002903
2904Texture *Context::getIncompleteTexture(SamplerType type)
2905{
2906 Texture *t = mIncompleteTextures[type];
2907
2908 if (t == NULL)
2909 {
2910 static const GLubyte color[] = { 0, 0, 0, 255 };
2911
2912 switch (type)
2913 {
2914 default:
2915 UNREACHABLE();
2916 // default falls through to SAMPLER_2D
2917
2918 case SAMPLER_2D:
2919 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002920 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002921 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002922 t = incomplete2d;
2923 }
2924 break;
2925
2926 case SAMPLER_CUBE:
2927 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002928 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002929
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002930 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2931 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2932 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2933 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2934 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2935 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002936
2937 t = incompleteCube;
2938 }
2939 break;
2940 }
2941
2942 mIncompleteTextures[type] = t;
2943 }
2944
2945 return t;
2946}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002947
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002948bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002949{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002950 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002951}
2952
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002953bool Context::isTriangleMode(GLenum drawMode)
2954{
2955 switch (drawMode)
2956 {
2957 case GL_TRIANGLES:
2958 case GL_TRIANGLE_FAN:
2959 case GL_TRIANGLE_STRIP:
2960 return true;
2961 case GL_POINTS:
2962 case GL_LINES:
2963 case GL_LINE_LOOP:
2964 case GL_LINE_STRIP:
2965 return false;
2966 default: UNREACHABLE();
2967 }
2968
2969 return false;
2970}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002971
2972void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2973{
2974 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2975
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002976 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2977 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
2978 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
2979 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002980
2981 mVertexDataManager->dirtyCurrentValues();
2982}
2983
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002984void Context::initExtensionString()
2985{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002986 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002987 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
2988 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00002989 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00002990 mExtensionString += "GL_OES_rgb8_rgba8 ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002991
daniel@transgaming.com01868132010-08-24 19:21:17 +00002992 if (supportsCompressedTextures())
2993 {
2994 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
2995 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002996
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00002997 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002998 {
2999 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3000 }
3001
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003002 if (mBufferBackEnd->supportIntIndices())
3003 {
3004 mExtensionString += "GL_OES_element_index_uint ";
3005 }
3006
3007 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3008 if (end != std::string::npos)
3009 {
3010 mExtensionString.resize(end+1);
3011 }
3012}
3013
3014const char *Context::getExtensionString() const
3015{
3016 return mExtensionString.c_str();
3017}
3018
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003019void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3020 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3021 GLbitfield mask)
3022{
3023 IDirect3DDevice9 *device = getDevice();
3024
3025 Framebuffer *readFramebuffer = getReadFramebuffer();
3026 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3027
3028 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3029 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3030 {
3031 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3032 }
3033
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003034 if (drawFramebuffer->getSamples() != 0)
3035 {
3036 return error(GL_INVALID_OPERATION);
3037 }
3038
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003039 RECT sourceRect;
3040 RECT destRect;
3041
3042 if (srcX0 < srcX1)
3043 {
3044 sourceRect.left = srcX0;
3045 sourceRect.right = srcX1;
3046 destRect.left = dstX0;
3047 destRect.right = dstX1;
3048 }
3049 else
3050 {
3051 sourceRect.left = srcX1;
3052 destRect.left = dstX1;
3053 sourceRect.right = srcX0;
3054 destRect.right = dstX0;
3055 }
3056
3057 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3058 // flip our Y-values here
3059 if (srcY0 < srcY1)
3060 {
3061 sourceRect.bottom = srcY1;
3062 destRect.bottom = dstY1;
3063 sourceRect.top = srcY0;
3064 destRect.top = dstY0;
3065 }
3066 else
3067 {
3068 sourceRect.bottom = srcY0;
3069 destRect.bottom = dstY0;
3070 sourceRect.top = srcY1;
3071 destRect.top = dstY1;
3072 }
3073
3074 RECT sourceScissoredRect = sourceRect;
3075 RECT destScissoredRect = destRect;
3076
3077 if (mState.scissorTest)
3078 {
3079 // Only write to parts of the destination framebuffer which pass the scissor test
3080 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3081 // rect will be checked against scissorY, rather than the bottom.
3082 if (destRect.left < mState.scissorX)
3083 {
3084 int xDiff = mState.scissorX - destRect.left;
3085 destScissoredRect.left = mState.scissorX;
3086 sourceScissoredRect.left += xDiff;
3087 }
3088
3089 if (destRect.right > mState.scissorX + mState.scissorWidth)
3090 {
3091 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3092 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3093 sourceScissoredRect.right -= xDiff;
3094 }
3095
3096 if (destRect.top < mState.scissorY)
3097 {
3098 int yDiff = mState.scissorY - destRect.top;
3099 destScissoredRect.top = mState.scissorY;
3100 sourceScissoredRect.top += yDiff;
3101 }
3102
3103 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3104 {
3105 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3106 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3107 sourceScissoredRect.bottom -= yDiff;
3108 }
3109 }
3110
3111 bool blitRenderTarget = false;
3112 bool blitDepthStencil = false;
3113
3114 RECT sourceTrimmedRect = sourceScissoredRect;
3115 RECT destTrimmedRect = destScissoredRect;
3116
3117 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3118 // the actual draw and read surfaces.
3119 if (sourceTrimmedRect.left < 0)
3120 {
3121 int xDiff = 0 - sourceTrimmedRect.left;
3122 sourceTrimmedRect.left = 0;
3123 destTrimmedRect.left += xDiff;
3124 }
3125
3126 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3127 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3128 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3129 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3130
3131 if (sourceTrimmedRect.right > readBufferWidth)
3132 {
3133 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3134 sourceTrimmedRect.right = readBufferWidth;
3135 destTrimmedRect.right -= xDiff;
3136 }
3137
3138 if (sourceTrimmedRect.top < 0)
3139 {
3140 int yDiff = 0 - sourceTrimmedRect.top;
3141 sourceTrimmedRect.top = 0;
3142 destTrimmedRect.top += yDiff;
3143 }
3144
3145 if (sourceTrimmedRect.bottom > readBufferHeight)
3146 {
3147 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3148 sourceTrimmedRect.bottom = readBufferHeight;
3149 destTrimmedRect.bottom -= yDiff;
3150 }
3151
3152 if (destTrimmedRect.left < 0)
3153 {
3154 int xDiff = 0 - destTrimmedRect.left;
3155 destTrimmedRect.left = 0;
3156 sourceTrimmedRect.left += xDiff;
3157 }
3158
3159 if (destTrimmedRect.right > drawBufferWidth)
3160 {
3161 int xDiff = destTrimmedRect.right - drawBufferWidth;
3162 destTrimmedRect.right = drawBufferWidth;
3163 sourceTrimmedRect.right -= xDiff;
3164 }
3165
3166 if (destTrimmedRect.top < 0)
3167 {
3168 int yDiff = 0 - destTrimmedRect.top;
3169 destTrimmedRect.top = 0;
3170 sourceTrimmedRect.top += yDiff;
3171 }
3172
3173 if (destTrimmedRect.bottom > drawBufferHeight)
3174 {
3175 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3176 destTrimmedRect.bottom = drawBufferHeight;
3177 sourceTrimmedRect.bottom -= yDiff;
3178 }
3179
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003180 bool partialBufferCopy = false;
3181 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3182 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3183 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3184 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3185 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3186 {
3187 partialBufferCopy = true;
3188 }
3189
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003190 if (mask & GL_COLOR_BUFFER_BIT)
3191 {
3192 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3193 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3194 {
3195 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3196 return error(GL_INVALID_OPERATION);
3197 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003198
3199 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3200 {
3201 return error(GL_INVALID_OPERATION);
3202 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003203
3204 blitRenderTarget = true;
3205
3206 }
3207
3208 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3209 {
3210 DepthStencilbuffer *readDSBuffer = NULL;
3211 DepthStencilbuffer *drawDSBuffer = NULL;
3212
3213 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3214 // both a depth and stencil buffer, it will be the same buffer.
3215
3216 if (mask & GL_DEPTH_BUFFER_BIT)
3217 {
3218 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3219 {
3220 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3221 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3222 {
3223 return error(GL_INVALID_OPERATION);
3224 }
3225
3226 blitDepthStencil = true;
3227 readDSBuffer = readFramebuffer->getDepthbuffer();
3228 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3229 }
3230 }
3231
3232 if (mask & GL_STENCIL_BUFFER_BIT)
3233 {
3234 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3235 {
3236 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3237 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3238 {
3239 return error(GL_INVALID_OPERATION);
3240 }
3241
3242 blitDepthStencil = true;
3243 readDSBuffer = readFramebuffer->getStencilbuffer();
3244 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3245 }
3246 }
3247
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003248 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003249 {
3250 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3251 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3252 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003253
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003254 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3255 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003256 {
3257 return error(GL_INVALID_OPERATION);
3258 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003259 }
3260
3261 if (blitRenderTarget || blitDepthStencil)
3262 {
3263 egl::Display *display = getDisplay();
3264 display->endScene();
3265
3266 if (blitRenderTarget)
3267 {
3268 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3269 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3270
3271 if (FAILED(result))
3272 {
3273 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3274 return;
3275 }
3276 }
3277
3278 if (blitDepthStencil)
3279 {
3280 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3281
3282 if (FAILED(result))
3283 {
3284 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3285 return;
3286 }
3287 }
3288 }
3289}
3290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003291}
3292
3293extern "C"
3294{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003295gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003296{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003297 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003298}
3299
3300void glDestroyContext(gl::Context *context)
3301{
3302 delete context;
3303
3304 if (context == gl::getContext())
3305 {
3306 gl::makeCurrent(NULL, NULL, NULL);
3307 }
3308}
3309
3310void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3311{
3312 gl::makeCurrent(context, display, surface);
3313}
3314
3315gl::Context *glGetCurrentContext()
3316{
3317 return gl::getContext();
3318}
3319}