blob: 43d7ee5237f1e532be069d65c975a80e96ace0bc [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();
daniel@transgaming.com0a337e92010-08-28 17:38:27 +0000269 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter);
270 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000271
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000272 initExtensionString();
273
274 mState.viewportX = 0;
275 mState.viewportY = 0;
276 mState.viewportWidth = surface->getWidth();
277 mState.viewportHeight = surface->getHeight();
278
279 mState.scissorX = 0;
280 mState.scissorY = 0;
281 mState.scissorWidth = surface->getWidth();
282 mState.scissorHeight = surface->getHeight();
283
284 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000285 }
286
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
288 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000289 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000292 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000293 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
295 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
297 defaultRenderTarget->Release();
298
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000299 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000301 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000303
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000304 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000305
306 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307}
308
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000309// 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 +0000310void Context::markAllStateDirty()
311{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000312 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000313 mAppliedDepthbufferSerial = 0;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000314 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000315
316 mClearStateDirty = true;
317 mCullStateDirty = true;
318 mDepthStateDirty = true;
319 mMaskStateDirty = true;
320 mBlendStateDirty = true;
321 mStencilStateDirty = true;
322 mPolygonOffsetStateDirty = true;
323 mScissorStateDirty = true;
324 mSampleStateDirty = true;
325 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000326 mFrontFaceDirty = true;
327
328 if (mBufferBackEnd != NULL)
329 {
330 mBufferBackEnd->invalidate();
331 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000332}
333
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334void Context::setClearColor(float red, float green, float blue, float alpha)
335{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000336 mState.colorClearValue.red = red;
337 mState.colorClearValue.green = green;
338 mState.colorClearValue.blue = blue;
339 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340}
341
342void Context::setClearDepth(float depth)
343{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000344 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345}
346
347void Context::setClearStencil(int stencil)
348{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000349 mState.stencilClearValue = stencil;
350}
351
352void Context::setCullFace(bool enabled)
353{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000354 if (mState.cullFace != enabled)
355 {
356 mState.cullFace = enabled;
357 mCullStateDirty = true;
358 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000359}
360
361bool Context::isCullFaceEnabled() const
362{
363 return mState.cullFace;
364}
365
366void Context::setCullMode(GLenum mode)
367{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000368 if (mState.cullMode != mode)
369 {
370 mState.cullMode = mode;
371 mCullStateDirty = true;
372 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000373}
374
375void Context::setFrontFace(GLenum front)
376{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000377 if (mState.frontFace != front)
378 {
379 mState.frontFace = front;
380 mFrontFaceDirty = true;
381 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000382}
383
384void Context::setDepthTest(bool enabled)
385{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000386 if (mState.depthTest != enabled)
387 {
388 mState.depthTest = enabled;
389 mDepthStateDirty = true;
390 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000391}
392
393bool Context::isDepthTestEnabled() const
394{
395 return mState.depthTest;
396}
397
398void Context::setDepthFunc(GLenum depthFunc)
399{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000400 if (mState.depthFunc != depthFunc)
401 {
402 mState.depthFunc = depthFunc;
403 mDepthStateDirty = true;
404 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000405}
406
407void Context::setDepthRange(float zNear, float zFar)
408{
409 mState.zNear = zNear;
410 mState.zFar = zFar;
411}
412
413void Context::setBlend(bool enabled)
414{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000415 if (mState.blend != enabled)
416 {
417 mState.blend = enabled;
418 mBlendStateDirty = true;
419 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420}
421
422bool Context::isBlendEnabled() const
423{
424 return mState.blend;
425}
426
427void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
428{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000429 if (mState.sourceBlendRGB != sourceRGB ||
430 mState.sourceBlendAlpha != sourceAlpha ||
431 mState.destBlendRGB != destRGB ||
432 mState.destBlendAlpha != destAlpha)
433 {
434 mState.sourceBlendRGB = sourceRGB;
435 mState.destBlendRGB = destRGB;
436 mState.sourceBlendAlpha = sourceAlpha;
437 mState.destBlendAlpha = destAlpha;
438 mBlendStateDirty = true;
439 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000440}
441
442void Context::setBlendColor(float red, float green, float blue, float alpha)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.blendColor.red != red ||
445 mState.blendColor.green != green ||
446 mState.blendColor.blue != blue ||
447 mState.blendColor.alpha != alpha)
448 {
449 mState.blendColor.red = red;
450 mState.blendColor.green = green;
451 mState.blendColor.blue = blue;
452 mState.blendColor.alpha = alpha;
453 mBlendStateDirty = true;
454 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000455}
456
457void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
458{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000459 if (mState.blendEquationRGB != rgbEquation ||
460 mState.blendEquationAlpha != alphaEquation)
461 {
462 mState.blendEquationRGB = rgbEquation;
463 mState.blendEquationAlpha = alphaEquation;
464 mBlendStateDirty = true;
465 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000466}
467
468void Context::setStencilTest(bool enabled)
469{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000470 if (mState.stencilTest != enabled)
471 {
472 mState.stencilTest = enabled;
473 mStencilStateDirty = true;
474 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000475}
476
477bool Context::isStencilTestEnabled() const
478{
479 return mState.stencilTest;
480}
481
482void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
483{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000484 if (mState.stencilFunc != stencilFunc ||
485 mState.stencilRef != stencilRef ||
486 mState.stencilMask != stencilMask)
487 {
488 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000489 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000490 mState.stencilMask = stencilMask;
491 mStencilStateDirty = true;
492 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000493}
494
495void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
496{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000497 if (mState.stencilBackFunc != stencilBackFunc ||
498 mState.stencilBackRef != stencilBackRef ||
499 mState.stencilBackMask != stencilBackMask)
500 {
501 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000502 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000503 mState.stencilBackMask = stencilBackMask;
504 mStencilStateDirty = true;
505 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000506}
507
508void Context::setStencilWritemask(GLuint stencilWritemask)
509{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000510 if (mState.stencilWritemask != stencilWritemask)
511 {
512 mState.stencilWritemask = stencilWritemask;
513 mStencilStateDirty = true;
514 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000515}
516
517void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
518{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000519 if (mState.stencilBackWritemask != stencilBackWritemask)
520 {
521 mState.stencilBackWritemask = stencilBackWritemask;
522 mStencilStateDirty = true;
523 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000524}
525
526void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
527{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000528 if (mState.stencilFail != stencilFail ||
529 mState.stencilPassDepthFail != stencilPassDepthFail ||
530 mState.stencilPassDepthPass != stencilPassDepthPass)
531 {
532 mState.stencilFail = stencilFail;
533 mState.stencilPassDepthFail = stencilPassDepthFail;
534 mState.stencilPassDepthPass = stencilPassDepthPass;
535 mStencilStateDirty = true;
536 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000537}
538
539void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
540{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000541 if (mState.stencilBackFail != stencilBackFail ||
542 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
543 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
544 {
545 mState.stencilBackFail = stencilBackFail;
546 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
547 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
548 mStencilStateDirty = true;
549 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000550}
551
552void Context::setPolygonOffsetFill(bool enabled)
553{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000554 if (mState.polygonOffsetFill != enabled)
555 {
556 mState.polygonOffsetFill = enabled;
557 mPolygonOffsetStateDirty = true;
558 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000559}
560
561bool Context::isPolygonOffsetFillEnabled() const
562{
563 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000564
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000565}
566
567void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
568{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000569 if (mState.polygonOffsetFactor != factor ||
570 mState.polygonOffsetUnits != units)
571 {
572 mState.polygonOffsetFactor = factor;
573 mState.polygonOffsetUnits = units;
574 mPolygonOffsetStateDirty = true;
575 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000576}
577
578void Context::setSampleAlphaToCoverage(bool enabled)
579{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000580 if (mState.sampleAlphaToCoverage != enabled)
581 {
582 mState.sampleAlphaToCoverage = enabled;
583 mSampleStateDirty = true;
584 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000585}
586
587bool Context::isSampleAlphaToCoverageEnabled() const
588{
589 return mState.sampleAlphaToCoverage;
590}
591
592void Context::setSampleCoverage(bool enabled)
593{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000594 if (mState.sampleCoverage != enabled)
595 {
596 mState.sampleCoverage = enabled;
597 mSampleStateDirty = true;
598 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000599}
600
601bool Context::isSampleCoverageEnabled() const
602{
603 return mState.sampleCoverage;
604}
605
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000606void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000607{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000608 if (mState.sampleCoverageValue != value ||
609 mState.sampleCoverageInvert != invert)
610 {
611 mState.sampleCoverageValue = value;
612 mState.sampleCoverageInvert = invert;
613 mSampleStateDirty = true;
614 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000615}
616
617void Context::setScissorTest(bool enabled)
618{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000619 if (mState.scissorTest != enabled)
620 {
621 mState.scissorTest = enabled;
622 mScissorStateDirty = true;
623 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000624}
625
626bool Context::isScissorTestEnabled() const
627{
628 return mState.scissorTest;
629}
630
631void Context::setDither(bool enabled)
632{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000633 if (mState.dither != enabled)
634 {
635 mState.dither = enabled;
636 mDitherStateDirty = true;
637 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000638}
639
640bool Context::isDitherEnabled() const
641{
642 return mState.dither;
643}
644
645void Context::setLineWidth(GLfloat width)
646{
647 mState.lineWidth = width;
648}
649
650void Context::setGenerateMipmapHint(GLenum hint)
651{
652 mState.generateMipmapHint = hint;
653}
654
655void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
656{
657 mState.viewportX = x;
658 mState.viewportY = y;
659 mState.viewportWidth = width;
660 mState.viewportHeight = height;
661}
662
663void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
664{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000665 if (mState.scissorX != x || mState.scissorY != y ||
666 mState.scissorWidth != width || mState.scissorHeight != height)
667 {
668 mState.scissorX = x;
669 mState.scissorY = y;
670 mState.scissorWidth = width;
671 mState.scissorHeight = height;
672 mScissorStateDirty = true;
673 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000674}
675
676void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
677{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000678 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
679 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
680 {
681 mState.colorMaskRed = red;
682 mState.colorMaskGreen = green;
683 mState.colorMaskBlue = blue;
684 mState.colorMaskAlpha = alpha;
685 mMaskStateDirty = true;
686 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000687}
688
689void Context::setDepthMask(bool mask)
690{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000691 if (mState.depthMask != mask)
692 {
693 mState.depthMask = mask;
694 mMaskStateDirty = true;
695 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000696}
697
698void Context::setActiveSampler(int active)
699{
700 mState.activeSampler = active;
701}
702
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000703GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000704{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000705 return mState.readFramebuffer;
706}
707
708GLuint Context::getDrawFramebufferHandle() const
709{
710 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000711}
712
713GLuint Context::getRenderbufferHandle() const
714{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000715 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716}
717
718GLuint Context::getArrayBufferHandle() const
719{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000720 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000721}
722
723void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
724{
725 mState.vertexAttribute[attribNum].mEnabled = enabled;
726}
727
728const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
729{
730 return mState.vertexAttribute[attribNum];
731}
732
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000733void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734 GLsizei stride, const void *pointer)
735{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000736 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000737 mState.vertexAttribute[attribNum].mSize = size;
738 mState.vertexAttribute[attribNum].mType = type;
739 mState.vertexAttribute[attribNum].mNormalized = normalized;
740 mState.vertexAttribute[attribNum].mStride = stride;
741 mState.vertexAttribute[attribNum].mPointer = pointer;
742}
743
744const void *Context::getVertexAttribPointer(unsigned int attribNum) const
745{
746 return mState.vertexAttribute[attribNum].mPointer;
747}
748
749// returns entire set of attributes as a block
750const AttributeState *Context::getVertexAttribBlock()
751{
752 return mState.vertexAttribute;
753}
754
755void Context::setPackAlignment(GLint alignment)
756{
757 mState.packAlignment = alignment;
758}
759
760GLint Context::getPackAlignment() const
761{
762 return mState.packAlignment;
763}
764
765void Context::setUnpackAlignment(GLint alignment)
766{
767 mState.unpackAlignment = alignment;
768}
769
770GLint Context::getUnpackAlignment() const
771{
772 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773}
774
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775GLuint Context::createBuffer()
776{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000777 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778}
779
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780GLuint Context::createProgram()
781{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000782 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783}
784
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000785GLuint Context::createShader(GLenum type)
786{
787 return mResourceManager->createShader(type);
788}
789
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790GLuint Context::createTexture()
791{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000792 return mResourceManager->createTexture();
793}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000795GLuint Context::createRenderbuffer()
796{
797 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798}
799
800// Returns an unused framebuffer name
801GLuint Context::createFramebuffer()
802{
803 unsigned int handle = 1;
804
805 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
806 {
807 handle++;
808 }
809
810 mFramebufferMap[handle] = NULL;
811
812 return handle;
813}
814
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815void Context::deleteBuffer(GLuint buffer)
816{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000817 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818 {
819 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000821
822 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823}
824
825void Context::deleteShader(GLuint shader)
826{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000827 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828}
829
830void Context::deleteProgram(GLuint program)
831{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000832 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833}
834
835void Context::deleteTexture(GLuint texture)
836{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000837 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838 {
839 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000841
842 mResourceManager->deleteTexture(texture);
843}
844
845void Context::deleteRenderbuffer(GLuint renderbuffer)
846{
847 if (mResourceManager->getRenderbuffer(renderbuffer))
848 {
849 detachRenderbuffer(renderbuffer);
850 }
851
852 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853}
854
855void Context::deleteFramebuffer(GLuint framebuffer)
856{
857 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
858
859 if (framebufferObject != mFramebufferMap.end())
860 {
861 detachFramebuffer(framebuffer);
862
863 delete framebufferObject->second;
864 mFramebufferMap.erase(framebufferObject);
865 }
866}
867
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870 return mResourceManager->getBuffer(handle);
871}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000873Shader *Context::getShader(GLuint handle)
874{
875 return mResourceManager->getShader(handle);
876}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878Program *Context::getProgram(GLuint handle)
879{
880 return mResourceManager->getProgram(handle);
881}
882
883Texture *Context::getTexture(GLuint handle)
884{
885 return mResourceManager->getTexture(handle);
886}
887
888Renderbuffer *Context::getRenderbuffer(GLuint handle)
889{
890 return mResourceManager->getRenderbuffer(handle);
891}
892
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000893Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000894{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000895 return getFramebuffer(mState.readFramebuffer);
896}
897
898Framebuffer *Context::getDrawFramebuffer()
899{
900 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901}
902
903void Context::bindArrayBuffer(unsigned int buffer)
904{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000905 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000907 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908}
909
910void Context::bindElementArrayBuffer(unsigned int buffer)
911{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000914 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915}
916
917void Context::bindTexture2D(GLuint texture)
918{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000921 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000923 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924}
925
926void Context::bindTextureCubeMap(GLuint texture)
927{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000930 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000932 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
934
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000935void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936{
937 if (!getFramebuffer(framebuffer))
938 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000939 mFramebufferMap[framebuffer] = new Framebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940 }
941
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000942 mState.readFramebuffer = framebuffer;
943}
944
945void Context::bindDrawFramebuffer(GLuint framebuffer)
946{
947 if (!getFramebuffer(framebuffer))
948 {
949 mFramebufferMap[framebuffer] = new Framebuffer();
950 }
951
952 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953}
954
955void Context::bindRenderbuffer(GLuint renderbuffer)
956{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000957 mResourceManager->checkRenderbufferAllocation(renderbuffer);
958
959 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
962void Context::useProgram(GLuint program)
963{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000964 GLuint priorProgram = mState.currentProgram;
965 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000966
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000967 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000969 Program *newProgram = mResourceManager->getProgram(program);
970 Program *oldProgram = mResourceManager->getProgram(priorProgram);
971
972 if (newProgram)
973 {
974 newProgram->addRef();
975 }
976
977 if (oldProgram)
978 {
979 oldProgram->release();
980 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982}
983
984void Context::setFramebufferZero(Framebuffer *buffer)
985{
986 delete mFramebufferMap[0];
987 mFramebufferMap[0] = buffer;
988}
989
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000990void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000992 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
993 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994}
995
996Framebuffer *Context::getFramebuffer(unsigned int handle)
997{
998 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000999
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000 if (framebuffer == mFramebufferMap.end())
1001 {
1002 return NULL;
1003 }
1004 else
1005 {
1006 return framebuffer->second;
1007 }
1008}
1009
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010Buffer *Context::getArrayBuffer()
1011{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001012 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013}
1014
1015Buffer *Context::getElementArrayBuffer()
1016{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001017 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018}
1019
1020Program *Context::getCurrentProgram()
1021{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001022 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023}
1024
1025Texture2D *Context::getTexture2D()
1026{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001027 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 +00001028 {
1029 return mTexture2DZero;
1030 }
1031
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001032 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033}
1034
1035TextureCubeMap *Context::getTextureCubeMap()
1036{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001037 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 +00001038 {
1039 return mTextureCubeMapZero;
1040 }
1041
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001042 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043}
1044
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001045Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001047 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001048
1049 if (texid == 0)
1050 {
1051 switch (type)
1052 {
1053 default: UNREACHABLE();
1054 case SAMPLER_2D: return mTexture2DZero;
1055 case SAMPLER_CUBE: return mTextureCubeMapZero;
1056 }
1057 }
1058
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001059 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060}
1061
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001062bool Context::getBooleanv(GLenum pname, GLboolean *params)
1063{
1064 switch (pname)
1065 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001066 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1067 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1068 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001069 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001070 params[0] = mState.colorMaskRed;
1071 params[1] = mState.colorMaskGreen;
1072 params[2] = mState.colorMaskBlue;
1073 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001074 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001075 case GL_CULL_FACE: *params = mState.cullFace;
1076 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1077 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1078 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1079 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1080 case GL_STENCIL_TEST: *params = mState.stencilTest;
1081 case GL_DEPTH_TEST: *params = mState.depthTest;
1082 case GL_BLEND: *params = mState.blend;
1083 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001084 default:
1085 return false;
1086 }
1087
1088 return true;
1089}
1090
1091bool Context::getFloatv(GLenum pname, GLfloat *params)
1092{
1093 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1094 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1095 // GetIntegerv as its native query function. As it would require conversion in any
1096 // case, this should make no difference to the calling application.
1097 switch (pname)
1098 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001099 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1100 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1101 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1102 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1103 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001104 case GL_ALIASED_LINE_WIDTH_RANGE:
1105 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1106 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1107 break;
1108 case GL_ALIASED_POINT_SIZE_RANGE:
1109 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001110 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 +00001111 break;
1112 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001113 params[0] = mState.zNear;
1114 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001115 break;
1116 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001117 params[0] = mState.colorClearValue.red;
1118 params[1] = mState.colorClearValue.green;
1119 params[2] = mState.colorClearValue.blue;
1120 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001121 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001122 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001123 params[0] = mState.blendColor.red;
1124 params[1] = mState.blendColor.green;
1125 params[2] = mState.blendColor.blue;
1126 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001127 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001128 default:
1129 return false;
1130 }
1131
1132 return true;
1133}
1134
1135bool Context::getIntegerv(GLenum pname, GLint *params)
1136{
1137 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1138 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1139 // GetIntegerv as its native query function. As it would require conversion in any
1140 // case, this should make no difference to the calling application. You may find it in
1141 // Context::getFloatv.
1142 switch (pname)
1143 {
1144 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1145 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1146 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1147 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1148 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1149 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1150 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1151 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001152 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001153 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001154 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1155 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001156 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1157 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1158 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001159 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001160 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1161 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1162 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1163 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1164 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1165 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1166 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1167 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1168 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1169 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1170 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1171 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1172 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1173 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1174 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1175 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1176 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1177 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1178 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1179 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1180 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1181 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1182 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1183 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1184 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1185 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1186 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1187 case GL_SUBPIXEL_BITS: *params = 4; break;
1188 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1189 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001190 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1191 {
1192 if (supportsCompressedTextures())
1193 {
1194 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1195 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1196 *params = 2;
1197 }
1198 else
1199 {
1200 *params = 0;
1201 }
1202 }
1203 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001204 case GL_MAX_SAMPLES_ANGLE:
1205 {
1206 GLsizei maxSamples = getMaxSupportedSamples();
1207 if (maxSamples != 0)
1208 {
1209 *params = maxSamples;
1210 }
1211 else
1212 {
1213 return false;
1214 }
1215
1216 break;
1217 }
1218 case GL_SAMPLE_BUFFERS:
1219 case GL_SAMPLES:
1220 {
1221 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1222 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1223 {
1224 switch (pname)
1225 {
1226 case GL_SAMPLE_BUFFERS:
1227 if (framebuffer->getSamples() != 0)
1228 {
1229 *params = 1;
1230 }
1231 else
1232 {
1233 *params = 0;
1234 }
1235 break;
1236 case GL_SAMPLES:
1237 *params = framebuffer->getSamples();
1238 break;
1239 }
1240 }
1241 else
1242 {
1243 *params = 0;
1244 }
1245 }
1246 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001247 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1248 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1249 case GL_MAX_VIEWPORT_DIMS:
1250 {
1251 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1252 params[0] = maxDimension;
1253 params[1] = maxDimension;
1254 }
1255 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001256 case GL_COMPRESSED_TEXTURE_FORMATS:
1257 {
1258 if (supportsCompressedTextures())
1259 {
1260 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1261 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1262 }
1263 }
1264 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001265 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001266 params[0] = mState.viewportX;
1267 params[1] = mState.viewportY;
1268 params[2] = mState.viewportWidth;
1269 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001270 break;
1271 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001272 params[0] = mState.scissorX;
1273 params[1] = mState.scissorY;
1274 params[2] = mState.scissorWidth;
1275 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001276 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001277 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1278 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001279 case GL_RED_BITS:
1280 case GL_GREEN_BITS:
1281 case GL_BLUE_BITS:
1282 case GL_ALPHA_BITS:
1283 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001284 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001285 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1286
1287 if (colorbuffer)
1288 {
1289 switch (pname)
1290 {
1291 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1292 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1293 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1294 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1295 }
1296 }
1297 else
1298 {
1299 *params = 0;
1300 }
1301 }
1302 break;
1303 case GL_DEPTH_BITS:
1304 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001305 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001306 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001307
1308 if (depthbuffer)
1309 {
1310 *params = depthbuffer->getDepthSize();
1311 }
1312 else
1313 {
1314 *params = 0;
1315 }
1316 }
1317 break;
1318 case GL_STENCIL_BITS:
1319 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001320 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001321 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001322
1323 if (stencilbuffer)
1324 {
1325 *params = stencilbuffer->getStencilSize();
1326 }
1327 else
1328 {
1329 *params = 0;
1330 }
1331 }
1332 break;
1333 case GL_TEXTURE_BINDING_2D:
1334 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001335 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001336 {
1337 error(GL_INVALID_OPERATION);
1338 return false;
1339 }
1340
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001341 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001342 }
1343 break;
1344 case GL_TEXTURE_BINDING_CUBE_MAP:
1345 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001346 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001347 {
1348 error(GL_INVALID_OPERATION);
1349 return false;
1350 }
1351
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001352 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001353 }
1354 break;
1355 default:
1356 return false;
1357 }
1358
1359 return true;
1360}
1361
1362bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1363{
1364 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1365 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1366 // to the fact that it is stored internally as a float, and so would require conversion
1367 // if returned from Context::getIntegerv. Since this conversion is already implemented
1368 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1369 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1370 // application.
1371 switch (pname)
1372 {
1373 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1374 case GL_SHADER_BINARY_FORMATS:
1375 {
1376 *type = GL_INT;
1377 *numParams = 0;
1378 }
1379 break;
1380 case GL_MAX_VERTEX_ATTRIBS:
1381 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1382 case GL_MAX_VARYING_VECTORS:
1383 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1384 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1385 case GL_MAX_TEXTURE_IMAGE_UNITS:
1386 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1387 case GL_MAX_RENDERBUFFER_SIZE:
1388 case GL_NUM_SHADER_BINARY_FORMATS:
1389 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1390 case GL_ARRAY_BUFFER_BINDING:
1391 case GL_FRAMEBUFFER_BINDING:
1392 case GL_RENDERBUFFER_BINDING:
1393 case GL_CURRENT_PROGRAM:
1394 case GL_PACK_ALIGNMENT:
1395 case GL_UNPACK_ALIGNMENT:
1396 case GL_GENERATE_MIPMAP_HINT:
1397 case GL_RED_BITS:
1398 case GL_GREEN_BITS:
1399 case GL_BLUE_BITS:
1400 case GL_ALPHA_BITS:
1401 case GL_DEPTH_BITS:
1402 case GL_STENCIL_BITS:
1403 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1404 case GL_CULL_FACE_MODE:
1405 case GL_FRONT_FACE:
1406 case GL_ACTIVE_TEXTURE:
1407 case GL_STENCIL_FUNC:
1408 case GL_STENCIL_VALUE_MASK:
1409 case GL_STENCIL_REF:
1410 case GL_STENCIL_FAIL:
1411 case GL_STENCIL_PASS_DEPTH_FAIL:
1412 case GL_STENCIL_PASS_DEPTH_PASS:
1413 case GL_STENCIL_BACK_FUNC:
1414 case GL_STENCIL_BACK_VALUE_MASK:
1415 case GL_STENCIL_BACK_REF:
1416 case GL_STENCIL_BACK_FAIL:
1417 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1418 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1419 case GL_DEPTH_FUNC:
1420 case GL_BLEND_SRC_RGB:
1421 case GL_BLEND_SRC_ALPHA:
1422 case GL_BLEND_DST_RGB:
1423 case GL_BLEND_DST_ALPHA:
1424 case GL_BLEND_EQUATION_RGB:
1425 case GL_BLEND_EQUATION_ALPHA:
1426 case GL_STENCIL_WRITEMASK:
1427 case GL_STENCIL_BACK_WRITEMASK:
1428 case GL_STENCIL_CLEAR_VALUE:
1429 case GL_SUBPIXEL_BITS:
1430 case GL_MAX_TEXTURE_SIZE:
1431 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1432 case GL_SAMPLE_BUFFERS:
1433 case GL_SAMPLES:
1434 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1435 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1436 case GL_TEXTURE_BINDING_2D:
1437 case GL_TEXTURE_BINDING_CUBE_MAP:
1438 {
1439 *type = GL_INT;
1440 *numParams = 1;
1441 }
1442 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001443 case GL_MAX_SAMPLES_ANGLE:
1444 {
1445 if (getMaxSupportedSamples() != 0)
1446 {
1447 *type = GL_INT;
1448 *numParams = 1;
1449 }
1450 else
1451 {
1452 return false;
1453 }
1454 }
1455 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001456 case GL_MAX_VIEWPORT_DIMS:
1457 {
1458 *type = GL_INT;
1459 *numParams = 2;
1460 }
1461 break;
1462 case GL_VIEWPORT:
1463 case GL_SCISSOR_BOX:
1464 {
1465 *type = GL_INT;
1466 *numParams = 4;
1467 }
1468 break;
1469 case GL_SHADER_COMPILER:
1470 case GL_SAMPLE_COVERAGE_INVERT:
1471 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001472 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1473 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1474 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1475 case GL_SAMPLE_COVERAGE:
1476 case GL_SCISSOR_TEST:
1477 case GL_STENCIL_TEST:
1478 case GL_DEPTH_TEST:
1479 case GL_BLEND:
1480 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001481 {
1482 *type = GL_BOOL;
1483 *numParams = 1;
1484 }
1485 break;
1486 case GL_COLOR_WRITEMASK:
1487 {
1488 *type = GL_BOOL;
1489 *numParams = 4;
1490 }
1491 break;
1492 case GL_POLYGON_OFFSET_FACTOR:
1493 case GL_POLYGON_OFFSET_UNITS:
1494 case GL_SAMPLE_COVERAGE_VALUE:
1495 case GL_DEPTH_CLEAR_VALUE:
1496 case GL_LINE_WIDTH:
1497 {
1498 *type = GL_FLOAT;
1499 *numParams = 1;
1500 }
1501 break;
1502 case GL_ALIASED_LINE_WIDTH_RANGE:
1503 case GL_ALIASED_POINT_SIZE_RANGE:
1504 case GL_DEPTH_RANGE:
1505 {
1506 *type = GL_FLOAT;
1507 *numParams = 2;
1508 }
1509 break;
1510 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001511 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001512 {
1513 *type = GL_FLOAT;
1514 *numParams = 4;
1515 }
1516 break;
1517 default:
1518 return false;
1519 }
1520
1521 return true;
1522}
1523
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001524// Applies the render target surface, depth stencil surface, viewport rectangle and
1525// scissor rectangle to the Direct3D 9 device
1526bool Context::applyRenderTarget(bool ignoreViewport)
1527{
1528 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001529
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001530 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531
1532 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1533 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001534 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1535
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001536 return false;
1537 }
1538
1539 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001540 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001541
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001542 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1543 if (renderTargetSerial != mAppliedRenderTargetSerial)
1544 {
1545 device->SetRenderTarget(0, renderTarget);
1546 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001547 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 +00001548 }
1549
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001550 unsigned int depthbufferSerial = 0;
1551 unsigned int stencilbufferSerial = 0;
1552 if (framebufferObject->getDepthbufferType() != GL_NONE)
1553 {
1554 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1555 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1556 }
1557 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1558 {
1559 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1560 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1561 }
1562
1563 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1564 stencilbufferSerial != mAppliedStencilbufferSerial)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001565 {
1566 device->SetDepthStencilSurface(depthStencil);
1567 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001568 mAppliedStencilbufferSerial = stencilbufferSerial;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001569 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001570
1571 D3DVIEWPORT9 viewport;
1572 D3DSURFACE_DESC desc;
1573 renderTarget->GetDesc(&desc);
1574
1575 if (ignoreViewport)
1576 {
1577 viewport.X = 0;
1578 viewport.Y = 0;
1579 viewport.Width = desc.Width;
1580 viewport.Height = desc.Height;
1581 viewport.MinZ = 0.0f;
1582 viewport.MaxZ = 1.0f;
1583 }
1584 else
1585 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001586 viewport.X = std::max(mState.viewportX, 0);
1587 viewport.Y = std::max(mState.viewportY, 0);
1588 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1589 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1590 viewport.MinZ = clamp01(mState.zNear);
1591 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592 }
1593
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001594 if (viewport.Width <= 0 || viewport.Height <= 0)
1595 {
1596 return false; // Nothing to render
1597 }
1598
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599 device->SetViewport(&viewport);
1600
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001601 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001603 if (mState.scissorTest)
1604 {
1605 RECT rect = {mState.scissorX,
1606 mState.scissorY,
1607 mState.scissorX + mState.scissorWidth,
1608 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001609 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1610 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001611 device->SetScissorRect(&rect);
1612 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1613 }
1614 else
1615 {
1616 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1617 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001618
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001619 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620 }
1621
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001622 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624 Program *programObject = getCurrentProgram();
1625
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001626 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001627 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001629
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001630 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001631 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1632 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1633 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001634 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1635
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001636 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001637 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001638 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1639
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001640 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001641 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001642
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001643 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001644 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001645
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001646 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001647 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001648 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649 }
1650
1651 return true;
1652}
1653
1654// 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 +00001655void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656{
1657 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001658 Program *programObject = getCurrentProgram();
1659
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001660 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001661 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001662 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001664 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001665 GLint alwaysFront = !isTriangleMode(drawMode);
1666 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1667
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001668 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001669
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001670 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001672 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001674 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675 }
1676 else
1677 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001678 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001679 }
1680
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001681 mCullStateDirty = false;
1682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001684 if (mDepthStateDirty)
1685 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001686 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001688 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1689 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690 }
1691 else
1692 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001693 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001695
1696 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 }
1698
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001699 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001701 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001702 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001703 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1704
1705 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1706 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1707 {
1708 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1709 }
1710 else
1711 {
1712 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1713 unorm<8>(mState.blendColor.alpha),
1714 unorm<8>(mState.blendColor.alpha),
1715 unorm<8>(mState.blendColor.alpha)));
1716 }
1717
1718 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1719 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1720 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1721
1722 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1723 mState.destBlendRGB != mState.destBlendAlpha ||
1724 mState.blendEquationRGB != mState.blendEquationAlpha)
1725 {
1726 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1727
1728 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1729 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1730 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1731
1732 }
1733 else
1734 {
1735 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1736 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001737 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001738 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001739 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001740 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001741 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742
1743 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 }
1745
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001746 if (mStencilStateDirty || mFrontFaceDirty)
1747 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001748 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001749 {
1750 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1751 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1752
1753 // FIXME: Unsupported by D3D9
1754 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1755 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1756 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1757 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1758 mState.stencilRef != mState.stencilBackRef ||
1759 mState.stencilMask != mState.stencilBackMask)
1760 {
1761 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1762 return error(GL_INVALID_OPERATION);
1763 }
1764
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001765 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001766 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001767 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1768
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001769 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1770 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1771 es2dx::ConvertComparison(mState.stencilFunc));
1772
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001773 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 +00001774 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1775
1776 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1777 es2dx::ConvertStencilOp(mState.stencilFail));
1778 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1779 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1780 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1781 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1782
1783 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1784 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1785 es2dx::ConvertComparison(mState.stencilBackFunc));
1786
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001787 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 +00001788 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1789
1790 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1791 es2dx::ConvertStencilOp(mState.stencilBackFail));
1792 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1793 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1794 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1795 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1796 }
1797 else
1798 {
1799 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1800 }
1801
1802 mStencilStateDirty = false;
1803 }
1804
1805 if (mMaskStateDirty)
1806 {
1807 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1808 mState.colorMaskBlue, mState.colorMaskAlpha));
1809 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1810
1811 mMaskStateDirty = false;
1812 }
1813
1814 if (mPolygonOffsetStateDirty)
1815 {
1816 if (mState.polygonOffsetFill)
1817 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001818 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001819 if (depthbuffer)
1820 {
1821 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1822 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1823 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1824 }
1825 }
1826 else
1827 {
1828 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1829 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1830 }
1831
1832 mPolygonOffsetStateDirty = false;
1833 }
1834
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001835 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001837 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001838 {
1839 FIXME("Sample alpha to coverage is unimplemented.");
1840 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001842 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001843 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001844 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1845 unsigned int mask = 0;
1846 if (mState.sampleCoverageValue != 0)
1847 {
1848 float threshold = 0.5f;
1849
1850 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1851 {
1852 mask <<= 1;
1853
1854 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1855 {
1856 threshold += 1.0f;
1857 mask |= 1;
1858 }
1859 }
1860 }
1861
1862 if (mState.sampleCoverageInvert)
1863 {
1864 mask = ~mask;
1865 }
1866
1867 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1868 }
1869 else
1870 {
1871 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001872 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001873
1874 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875 }
1876
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001877 if (mDitherStateDirty)
1878 {
1879 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1880
1881 mDitherStateDirty = false;
1882 }
1883
1884 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885}
1886
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001887// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001888void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001890 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001892 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001894 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895 }
1896 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001897}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001899GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001900{
1901 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1902
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001903 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001904 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001905 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001906 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001907 }
1908
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001909 lookupAttributeMapping(translated);
1910
1911 mBufferBackEnd->setupAttributesPreDraw(translated);
1912
1913 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1914 {
1915 if (translated[i].enabled && translated[i].nonArray)
1916 {
1917 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1918 if (err != GL_NO_ERROR)
1919 {
1920 return err;
1921 }
1922
1923 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1924
1925 *useIndexing = true;
1926 return GL_NO_ERROR;
1927 }
1928 }
1929
1930 *useIndexing = false;
1931 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001932}
1933
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001934GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001935{
1936 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1937
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001938 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001939
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001940 if (err == GL_NO_ERROR)
1941 {
1942 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001943
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001944 mBufferBackEnd->setupAttributesPreDraw(translated);
1945 }
1946
1947 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001948}
1949
1950// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001951GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001952{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001953 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001954
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001955 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00001956 {
1957 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1958 }
1959
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001960 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961}
1962
1963// Applies the shaders and shader constants to the Direct3D 9 device
1964void Context::applyShaders()
1965{
1966 IDirect3DDevice9 *device = getDevice();
1967 Program *programObject = getCurrentProgram();
1968 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1969 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1970
1971 device->SetVertexShader(vertexShader);
1972 device->SetPixelShader(pixelShader);
1973
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001974 if (programObject->getSerial() != mAppliedProgram)
1975 {
1976 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001977 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00001978 mAppliedProgram = programObject->getSerial();
1979 }
1980
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981 programObject->applyUniforms();
1982}
1983
1984// Applies the textures and sampler states to the Direct3D 9 device
1985void Context::applyTextures()
1986{
1987 IDirect3DDevice9 *device = getDevice();
1988 Program *programObject = getCurrentProgram();
1989
1990 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1991 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001992 int textureUnit = programObject->getSamplerMapping(sampler);
1993 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001995 SamplerType textureType = programObject->getSamplerType(sampler);
1996
1997 Texture *texture = getSamplerTexture(textureUnit, textureType);
1998
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00001999 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002000 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002001 if (texture->isComplete())
2002 {
2003 GLenum wrapS = texture->getWrapS();
2004 GLenum wrapT = texture->getWrapT();
2005 GLenum minFilter = texture->getMinFilter();
2006 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002008 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2009 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002011 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2012 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2013 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2014 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2015 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002017 device->SetTexture(sampler, texture->getTexture());
2018 }
2019 else
2020 {
2021 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2022 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002023 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002024
2025 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002027 else
2028 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002029 if (programObject->isSamplerDirty(sampler))
2030 {
2031 device->SetTexture(sampler, NULL);
2032 programObject->setSamplerDirty(sampler, false);
2033 }
2034 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 }
2036}
2037
2038void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2039{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002040 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002041
2042 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2043 {
2044 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2045 }
2046
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002047 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2048 {
2049 return error(GL_INVALID_OPERATION);
2050 }
2051
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2053 IDirect3DDevice9 *device = getDevice();
2054
2055 D3DSURFACE_DESC desc;
2056 renderTarget->GetDesc(&desc);
2057
2058 IDirect3DSurface9 *systemSurface;
2059 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2060
2061 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2062 {
2063 return error(GL_OUT_OF_MEMORY);
2064 }
2065
2066 ASSERT(SUCCEEDED(result));
2067
2068 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2069 {
2070 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2071 }
2072
2073 result = device->GetRenderTargetData(renderTarget, systemSurface);
2074
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 if (FAILED(result))
2076 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 systemSurface->Release();
2078
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002079 switch (result)
2080 {
2081 case D3DERR_DRIVERINTERNALERROR:
2082 case D3DERR_DEVICELOST:
2083 return error(GL_OUT_OF_MEMORY);
2084 default:
2085 UNREACHABLE();
2086 return; // No sensible error to generate
2087 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 }
2089
2090 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002091 RECT rect = {std::max(x, 0),
2092 std::max(y, 0),
2093 std::min(x + width, (int)desc.Width),
2094 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095
2096 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2097
2098 if (FAILED(result))
2099 {
2100 UNREACHABLE();
2101 systemSurface->Release();
2102
2103 return; // No sensible error to generate
2104 }
2105
2106 unsigned char *source = (unsigned char*)lock.pBits;
2107 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002108 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002110 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002111
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 for (int j = 0; j < rect.bottom - rect.top; j++)
2113 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002114 if (desc.Format == D3DFMT_A8R8G8B8 &&
2115 format == GL_BGRA_EXT &&
2116 type == GL_UNSIGNED_BYTE)
2117 {
2118 // Fast path for EXT_read_format_bgra, given
2119 // an RGBA source buffer. Note that buffers with no
2120 // alpha go through the slow path below.
2121 memcpy(dest + j * outputPitch,
2122 source + j * lock.Pitch,
2123 (rect.right - rect.left) * 4);
2124 continue;
2125 }
2126
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 for (int i = 0; i < rect.right - rect.left; i++)
2128 {
2129 float r;
2130 float g;
2131 float b;
2132 float a;
2133
2134 switch (desc.Format)
2135 {
2136 case D3DFMT_R5G6B5:
2137 {
2138 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2139
2140 a = 1.0f;
2141 b = (rgb & 0x001F) * (1.0f / 0x001F);
2142 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2143 r = (rgb & 0xF800) * (1.0f / 0xF800);
2144 }
2145 break;
2146 case D3DFMT_X1R5G5B5:
2147 {
2148 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2149
2150 a = 1.0f;
2151 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2152 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2153 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2154 }
2155 break;
2156 case D3DFMT_A1R5G5B5:
2157 {
2158 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2159
2160 a = (argb & 0x8000) ? 1.0f : 0.0f;
2161 b = (argb & 0x001F) * (1.0f / 0x001F);
2162 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2163 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2164 }
2165 break;
2166 case D3DFMT_A8R8G8B8:
2167 {
2168 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2169
2170 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2171 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2172 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2173 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2174 }
2175 break;
2176 case D3DFMT_X8R8G8B8:
2177 {
2178 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2179
2180 a = 1.0f;
2181 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2182 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2183 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2184 }
2185 break;
2186 case D3DFMT_A2R10G10B10:
2187 {
2188 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2189
2190 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2191 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2192 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2193 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2194 }
2195 break;
2196 default:
2197 UNIMPLEMENTED(); // FIXME
2198 UNREACHABLE();
2199 }
2200
2201 switch (format)
2202 {
2203 case GL_RGBA:
2204 switch (type)
2205 {
2206 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002207 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2208 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2209 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2210 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211 break;
2212 default: UNREACHABLE();
2213 }
2214 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002215 case GL_BGRA_EXT:
2216 switch (type)
2217 {
2218 case GL_UNSIGNED_BYTE:
2219 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2220 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2221 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2222 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2223 break;
2224 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2225 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2226 // this type is packed as follows:
2227 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2228 // --------------------------------------------------------------------------------
2229 // | 4th | 3rd | 2nd | 1st component |
2230 // --------------------------------------------------------------------------------
2231 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2232 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2233 ((unsigned short)(15 * a + 0.5f) << 12)|
2234 ((unsigned short)(15 * r + 0.5f) << 8) |
2235 ((unsigned short)(15 * g + 0.5f) << 4) |
2236 ((unsigned short)(15 * b + 0.5f) << 0);
2237 break;
2238 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2239 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2240 // this type is packed as follows:
2241 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2242 // --------------------------------------------------------------------------------
2243 // | 4th | 3rd | 2nd | 1st component |
2244 // --------------------------------------------------------------------------------
2245 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2246 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2247 ((unsigned short)( a + 0.5f) << 15) |
2248 ((unsigned short)(31 * r + 0.5f) << 10) |
2249 ((unsigned short)(31 * g + 0.5f) << 5) |
2250 ((unsigned short)(31 * b + 0.5f) << 0);
2251 break;
2252 default: UNREACHABLE();
2253 }
2254 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002255 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256 switch (type)
2257 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002258 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002259 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2260 ((unsigned short)(31 * b + 0.5f) << 0) |
2261 ((unsigned short)(63 * g + 0.5f) << 5) |
2262 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 break;
2264 default: UNREACHABLE();
2265 }
2266 break;
2267 default: UNREACHABLE();
2268 }
2269 }
2270 }
2271
2272 systemSurface->UnlockRect();
2273
2274 systemSurface->Release();
2275}
2276
2277void Context::clear(GLbitfield mask)
2278{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002279 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002280
2281 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2282 {
2283 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2284
2285 return;
2286 }
2287
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002288 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 IDirect3DDevice9 *device = getDevice();
2290 DWORD flags = 0;
2291
2292 if (mask & GL_COLOR_BUFFER_BIT)
2293 {
2294 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002295
2296 if (framebufferObject->getColorbufferType() != GL_NONE)
2297 {
2298 flags |= D3DCLEAR_TARGET;
2299 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 }
2301
2302 if (mask & GL_DEPTH_BUFFER_BIT)
2303 {
2304 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002305 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 {
2307 flags |= D3DCLEAR_ZBUFFER;
2308 }
2309 }
2310
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 GLuint stencilUnmasked = 0x0;
2312
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002313 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002316 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002318 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2319 D3DSURFACE_DESC desc;
2320 depthStencil->GetDesc(&desc);
2321
2322 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2323 stencilUnmasked = (0x1 << stencilSize) - 1;
2324
2325 if (stencilUnmasked != 0x0)
2326 {
2327 flags |= D3DCLEAR_STENCIL;
2328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 }
2330 }
2331
2332 if (mask != 0)
2333 {
2334 return error(GL_INVALID_VALUE);
2335 }
2336
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002337 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2338 {
2339 return;
2340 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002342 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2343 unorm<8>(mState.colorClearValue.red),
2344 unorm<8>(mState.colorClearValue.green),
2345 unorm<8>(mState.colorClearValue.blue));
2346 float depth = clamp01(mState.depthClearValue);
2347 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348
2349 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2350
2351 D3DSURFACE_DESC desc;
2352 renderTarget->GetDesc(&desc);
2353
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002354 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355
2356 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002357 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002359 !(mState.colorMaskRed && mState.colorMaskGreen &&
2360 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361
2362 if (needMaskedColorClear || needMaskedStencilClear)
2363 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002364 // State which is altered in all paths from this point to the clear call is saved.
2365 // State which is altered in only some paths will be flagged dirty in the case that
2366 // that path is taken.
2367 HRESULT hr;
2368 if (mMaskedClearSavedState == NULL)
2369 {
2370 hr = device->BeginStateBlock();
2371 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2372
2373 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2374 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2375 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2376 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2377 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2378 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2379 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2380 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2381 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2382 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2383 device->SetPixelShader(NULL);
2384 device->SetVertexShader(NULL);
2385 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2386 device->SetStreamSourceFreq(0, 1);
2387
2388 hr = device->EndStateBlock(&mMaskedClearSavedState);
2389 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2390 }
2391
2392 ASSERT(mMaskedClearSavedState != NULL);
2393
2394 if (mMaskedClearSavedState != NULL)
2395 {
2396 hr = mMaskedClearSavedState->Capture();
2397 ASSERT(SUCCEEDED(hr));
2398 }
2399
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2401 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2402 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2403 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2404 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2405 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2406 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2407 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2408
2409 if (flags & D3DCLEAR_TARGET)
2410 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002411 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2412 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2413 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2414 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415 }
2416 else
2417 {
2418 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2419 }
2420
2421 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2422 {
2423 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2424 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2425 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2426 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002427 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002428 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2430 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002431 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 }
2433 else
2434 {
2435 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2436 }
2437
2438 device->SetPixelShader(NULL);
2439 device->SetVertexShader(NULL);
2440 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002441 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002443 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 {
2445 float x, y, z, w;
2446 D3DCOLOR diffuse;
2447 };
2448
2449 Vertex quad[4];
2450 quad[0].x = 0.0f;
2451 quad[0].y = (float)desc.Height;
2452 quad[0].z = 0.0f;
2453 quad[0].w = 1.0f;
2454 quad[0].diffuse = color;
2455
2456 quad[1].x = (float)desc.Width;
2457 quad[1].y = (float)desc.Height;
2458 quad[1].z = 0.0f;
2459 quad[1].w = 1.0f;
2460 quad[1].diffuse = color;
2461
2462 quad[2].x = 0.0f;
2463 quad[2].y = 0.0f;
2464 quad[2].z = 0.0f;
2465 quad[2].w = 1.0f;
2466 quad[2].diffuse = color;
2467
2468 quad[3].x = (float)desc.Width;
2469 quad[3].y = 0.0f;
2470 quad[3].z = 0.0f;
2471 quad[3].w = 1.0f;
2472 quad[3].diffuse = color;
2473
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002474 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476
2477 if (flags & D3DCLEAR_ZBUFFER)
2478 {
2479 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2480 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2481 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2482 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002483
2484 if (mMaskedClearSavedState != NULL)
2485 {
2486 mMaskedClearSavedState->Apply();
2487 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002489 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490 {
2491 device->Clear(0, NULL, flags, color, depth, stencil);
2492 }
2493}
2494
2495void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2496{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002497 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498 {
2499 return error(GL_INVALID_OPERATION);
2500 }
2501
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002502 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503 IDirect3DDevice9 *device = getDevice();
2504 D3DPRIMITIVETYPE primitiveType;
2505 int primitiveCount;
2506
2507 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2508 return error(GL_INVALID_ENUM);
2509
2510 if (primitiveCount <= 0)
2511 {
2512 return;
2513 }
2514
2515 if (!applyRenderTarget(false))
2516 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002517 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518 }
2519
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002520 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002521
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002522 TranslatedIndexData indexInfo;
2523 bool useIndexing;
2524 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002525 if (err != GL_NO_ERROR)
2526 {
2527 return error(err);
2528 }
2529
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530 applyShaders();
2531 applyTextures();
2532
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002533 if (!getCurrentProgram()->validateSamplers())
2534 {
2535 return error(GL_INVALID_OPERATION);
2536 }
2537
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002538 if (!cullSkipsDraw(mode))
2539 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002540 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002541 if (useIndexing)
2542 {
2543 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2544 }
2545 else
2546 {
2547 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2548 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550}
2551
2552void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2553{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002554 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 {
2556 return error(GL_INVALID_OPERATION);
2557 }
2558
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002559 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560 {
2561 return error(GL_INVALID_OPERATION);
2562 }
2563
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002564 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565 IDirect3DDevice9 *device = getDevice();
2566 D3DPRIMITIVETYPE primitiveType;
2567 int primitiveCount;
2568
2569 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2570 return error(GL_INVALID_ENUM);
2571
2572 if (primitiveCount <= 0)
2573 {
2574 return;
2575 }
2576
2577 if (!applyRenderTarget(false))
2578 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002579 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580 }
2581
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002582 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002583
2584 TranslatedIndexData indexInfo;
2585 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2586 if (err != GL_NO_ERROR)
2587 {
2588 return error(err);
2589 }
2590
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002591 err = applyVertexBuffer(indexInfo);
2592 if (err != GL_NO_ERROR)
2593 {
2594 return error(err);
2595 }
2596
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597 applyShaders();
2598 applyTextures();
2599
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002600 if (!getCurrentProgram()->validateSamplers())
2601 {
2602 return error(GL_INVALID_OPERATION);
2603 }
2604
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002605 if (!cullSkipsDraw(mode))
2606 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002607 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002608 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 +00002609 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610}
2611
2612void Context::finish()
2613{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002614 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615 IDirect3DDevice9 *device = getDevice();
2616 IDirect3DQuery9 *occlusionQuery = NULL;
2617
2618 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2619
2620 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2621 {
2622 return error(GL_OUT_OF_MEMORY);
2623 }
2624
2625 ASSERT(SUCCEEDED(result));
2626
2627 if (occlusionQuery)
2628 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002629 IDirect3DStateBlock9 *savedState = NULL;
2630 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2631
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002632 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2633 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634
2635 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002636 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 device->SetPixelShader(NULL);
2638 device->SetVertexShader(NULL);
2639 device->SetFVF(D3DFVF_XYZRHW);
2640 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002641 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002643
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002644 result = occlusionQuery->Issue(D3DISSUE_END);
2645 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646
2647 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2648 {
2649 // Keep polling, but allow other threads to do something useful first
2650 Sleep(0);
2651 }
2652
2653 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002654
2655 if (savedState)
2656 {
2657 savedState->Apply();
2658 savedState->Release();
2659 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660 }
2661}
2662
2663void Context::flush()
2664{
2665 IDirect3DDevice9 *device = getDevice();
2666 IDirect3DQuery9 *eventQuery = NULL;
2667
2668 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2669
2670 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2671 {
2672 return error(GL_OUT_OF_MEMORY);
2673 }
2674
2675 ASSERT(SUCCEEDED(result));
2676
2677 if (eventQuery)
2678 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002679 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2680 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002682 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002683 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002684
2685 if (result == D3DERR_DEVICELOST)
2686 {
2687 error(GL_OUT_OF_MEMORY);
2688 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002689 }
2690}
2691
2692void Context::recordInvalidEnum()
2693{
2694 mInvalidEnum = true;
2695}
2696
2697void Context::recordInvalidValue()
2698{
2699 mInvalidValue = true;
2700}
2701
2702void Context::recordInvalidOperation()
2703{
2704 mInvalidOperation = true;
2705}
2706
2707void Context::recordOutOfMemory()
2708{
2709 mOutOfMemory = true;
2710}
2711
2712void Context::recordInvalidFramebufferOperation()
2713{
2714 mInvalidFramebufferOperation = true;
2715}
2716
2717// Get one of the recorded errors and clear its flag, if any.
2718// [OpenGL ES 2.0.24] section 2.5 page 13.
2719GLenum Context::getError()
2720{
2721 if (mInvalidEnum)
2722 {
2723 mInvalidEnum = false;
2724
2725 return GL_INVALID_ENUM;
2726 }
2727
2728 if (mInvalidValue)
2729 {
2730 mInvalidValue = false;
2731
2732 return GL_INVALID_VALUE;
2733 }
2734
2735 if (mInvalidOperation)
2736 {
2737 mInvalidOperation = false;
2738
2739 return GL_INVALID_OPERATION;
2740 }
2741
2742 if (mOutOfMemory)
2743 {
2744 mOutOfMemory = false;
2745
2746 return GL_OUT_OF_MEMORY;
2747 }
2748
2749 if (mInvalidFramebufferOperation)
2750 {
2751 mInvalidFramebufferOperation = false;
2752
2753 return GL_INVALID_FRAMEBUFFER_OPERATION;
2754 }
2755
2756 return GL_NO_ERROR;
2757}
2758
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002759bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002760{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002761 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002762}
2763
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002764int Context::getMaxSupportedSamples() const
2765{
2766 return mMaxSupportedSamples;
2767}
2768
2769int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2770{
2771 if (requested == 0)
2772 {
2773 return requested;
2774 }
2775
2776 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2777 if (itr == mMultiSampleSupport.end())
2778 {
2779 return -1;
2780 }
2781
2782 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2783 {
2784 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2785 {
2786 return i;
2787 }
2788 }
2789
2790 return -1;
2791}
2792
daniel@transgaming.com01868132010-08-24 19:21:17 +00002793bool Context::supportsCompressedTextures() const
2794{
2795 return mSupportsCompressedTextures;
2796}
2797
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002798bool Context::supportsFloatTextures() const
2799{
2800 return mSupportsFloatTextures;
2801}
2802
2803bool Context::supportsFloatLinearFilter() const
2804{
2805 return mSupportsFloatLinearFilter;
2806}
2807
2808bool Context::supportsHalfFloatTextures() const
2809{
2810 return mSupportsHalfFloatTextures;
2811}
2812
2813bool Context::supportsHalfFloatLinearFilter() const
2814{
2815 return mSupportsHalfFloatLinearFilter;
2816}
2817
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818void Context::detachBuffer(GLuint buffer)
2819{
2820 // [OpenGL ES 2.0.24] section 2.9 page 22:
2821 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2822 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2823
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002824 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002825 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002826 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002827 }
2828
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002829 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002830 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002831 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002832 }
2833
2834 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2835 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002836 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002837 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002838 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839 }
2840 }
2841}
2842
2843void Context::detachTexture(GLuint texture)
2844{
2845 // [OpenGL ES 2.0.24] section 3.8 page 84:
2846 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2847 // rebound to texture object zero
2848
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002849 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002851 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002852 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002853 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002854 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002855 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002857 }
2858 }
2859
2860 // [OpenGL ES 2.0.24] section 4.4 page 112:
2861 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2862 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2863 // image was attached in the currently bound framebuffer.
2864
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002865 Framebuffer *readFramebuffer = getReadFramebuffer();
2866 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002868 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002870 readFramebuffer->detachTexture(texture);
2871 }
2872
2873 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2874 {
2875 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002876 }
2877}
2878
2879void Context::detachFramebuffer(GLuint framebuffer)
2880{
2881 // [OpenGL ES 2.0.24] section 4.4 page 107:
2882 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2883 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2884
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002885 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002886 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002887 bindReadFramebuffer(0);
2888 }
2889
2890 if (mState.drawFramebuffer == framebuffer)
2891 {
2892 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002893 }
2894}
2895
2896void Context::detachRenderbuffer(GLuint renderbuffer)
2897{
2898 // [OpenGL ES 2.0.24] section 4.4 page 109:
2899 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2900 // had been executed with the target RENDERBUFFER and name of zero.
2901
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002902 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002903 {
2904 bindRenderbuffer(0);
2905 }
2906
2907 // [OpenGL ES 2.0.24] section 4.4 page 111:
2908 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2909 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2910 // point to which this image was attached in the currently bound framebuffer.
2911
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002912 Framebuffer *readFramebuffer = getReadFramebuffer();
2913 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002914
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002915 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002916 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002917 readFramebuffer->detachRenderbuffer(renderbuffer);
2918 }
2919
2920 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2921 {
2922 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002923 }
2924}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002925
2926Texture *Context::getIncompleteTexture(SamplerType type)
2927{
2928 Texture *t = mIncompleteTextures[type];
2929
2930 if (t == NULL)
2931 {
2932 static const GLubyte color[] = { 0, 0, 0, 255 };
2933
2934 switch (type)
2935 {
2936 default:
2937 UNREACHABLE();
2938 // default falls through to SAMPLER_2D
2939
2940 case SAMPLER_2D:
2941 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002942 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002943 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002944 t = incomplete2d;
2945 }
2946 break;
2947
2948 case SAMPLER_CUBE:
2949 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002950 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002951
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002952 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2953 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2954 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2955 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2956 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2957 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002958
2959 t = incompleteCube;
2960 }
2961 break;
2962 }
2963
2964 mIncompleteTextures[type] = t;
2965 }
2966
2967 return t;
2968}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002969
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002970bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002971{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002972 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002973}
2974
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002975bool Context::isTriangleMode(GLenum drawMode)
2976{
2977 switch (drawMode)
2978 {
2979 case GL_TRIANGLES:
2980 case GL_TRIANGLE_FAN:
2981 case GL_TRIANGLE_STRIP:
2982 return true;
2983 case GL_POINTS:
2984 case GL_LINES:
2985 case GL_LINE_LOOP:
2986 case GL_LINE_STRIP:
2987 return false;
2988 default: UNREACHABLE();
2989 }
2990
2991 return false;
2992}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00002993
2994void Context::setVertexAttrib(GLuint index, const GLfloat *values)
2995{
2996 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
2997
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002998 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
2999 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3000 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3001 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003002
3003 mVertexDataManager->dirtyCurrentValues();
3004}
3005
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003006void Context::initExtensionString()
3007{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003008 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003009 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3010 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003011 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003012 mExtensionString += "GL_OES_rgb8_rgba8 ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003013
daniel@transgaming.com01868132010-08-24 19:21:17 +00003014 if (supportsCompressedTextures())
3015 {
3016 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3017 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003018
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003019 if (supportsFloatTextures())
3020 {
3021 mExtensionString += "GL_OES_texture_float ";
3022 }
3023
3024 if (supportsHalfFloatTextures())
3025 {
3026 mExtensionString += "GL_OES_texture_half_float ";
3027 }
3028
3029 if (supportsFloatLinearFilter())
3030 {
3031 mExtensionString += "GL_OES_texture_float_linear ";
3032 }
3033
3034 if (supportsHalfFloatLinearFilter())
3035 {
3036 mExtensionString += "GL_OES_texture_half_float_linear ";
3037 }
3038
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003039 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003040 {
3041 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3042 }
3043
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003044 if (mBufferBackEnd->supportIntIndices())
3045 {
3046 mExtensionString += "GL_OES_element_index_uint ";
3047 }
3048
3049 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3050 if (end != std::string::npos)
3051 {
3052 mExtensionString.resize(end+1);
3053 }
3054}
3055
3056const char *Context::getExtensionString() const
3057{
3058 return mExtensionString.c_str();
3059}
3060
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003061void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3062 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3063 GLbitfield mask)
3064{
3065 IDirect3DDevice9 *device = getDevice();
3066
3067 Framebuffer *readFramebuffer = getReadFramebuffer();
3068 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3069
3070 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3071 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3072 {
3073 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3074 }
3075
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003076 if (drawFramebuffer->getSamples() != 0)
3077 {
3078 return error(GL_INVALID_OPERATION);
3079 }
3080
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003081 RECT sourceRect;
3082 RECT destRect;
3083
3084 if (srcX0 < srcX1)
3085 {
3086 sourceRect.left = srcX0;
3087 sourceRect.right = srcX1;
3088 destRect.left = dstX0;
3089 destRect.right = dstX1;
3090 }
3091 else
3092 {
3093 sourceRect.left = srcX1;
3094 destRect.left = dstX1;
3095 sourceRect.right = srcX0;
3096 destRect.right = dstX0;
3097 }
3098
3099 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3100 // flip our Y-values here
3101 if (srcY0 < srcY1)
3102 {
3103 sourceRect.bottom = srcY1;
3104 destRect.bottom = dstY1;
3105 sourceRect.top = srcY0;
3106 destRect.top = dstY0;
3107 }
3108 else
3109 {
3110 sourceRect.bottom = srcY0;
3111 destRect.bottom = dstY0;
3112 sourceRect.top = srcY1;
3113 destRect.top = dstY1;
3114 }
3115
3116 RECT sourceScissoredRect = sourceRect;
3117 RECT destScissoredRect = destRect;
3118
3119 if (mState.scissorTest)
3120 {
3121 // Only write to parts of the destination framebuffer which pass the scissor test
3122 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3123 // rect will be checked against scissorY, rather than the bottom.
3124 if (destRect.left < mState.scissorX)
3125 {
3126 int xDiff = mState.scissorX - destRect.left;
3127 destScissoredRect.left = mState.scissorX;
3128 sourceScissoredRect.left += xDiff;
3129 }
3130
3131 if (destRect.right > mState.scissorX + mState.scissorWidth)
3132 {
3133 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3134 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3135 sourceScissoredRect.right -= xDiff;
3136 }
3137
3138 if (destRect.top < mState.scissorY)
3139 {
3140 int yDiff = mState.scissorY - destRect.top;
3141 destScissoredRect.top = mState.scissorY;
3142 sourceScissoredRect.top += yDiff;
3143 }
3144
3145 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3146 {
3147 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3148 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3149 sourceScissoredRect.bottom -= yDiff;
3150 }
3151 }
3152
3153 bool blitRenderTarget = false;
3154 bool blitDepthStencil = false;
3155
3156 RECT sourceTrimmedRect = sourceScissoredRect;
3157 RECT destTrimmedRect = destScissoredRect;
3158
3159 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3160 // the actual draw and read surfaces.
3161 if (sourceTrimmedRect.left < 0)
3162 {
3163 int xDiff = 0 - sourceTrimmedRect.left;
3164 sourceTrimmedRect.left = 0;
3165 destTrimmedRect.left += xDiff;
3166 }
3167
3168 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3169 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3170 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3171 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3172
3173 if (sourceTrimmedRect.right > readBufferWidth)
3174 {
3175 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3176 sourceTrimmedRect.right = readBufferWidth;
3177 destTrimmedRect.right -= xDiff;
3178 }
3179
3180 if (sourceTrimmedRect.top < 0)
3181 {
3182 int yDiff = 0 - sourceTrimmedRect.top;
3183 sourceTrimmedRect.top = 0;
3184 destTrimmedRect.top += yDiff;
3185 }
3186
3187 if (sourceTrimmedRect.bottom > readBufferHeight)
3188 {
3189 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3190 sourceTrimmedRect.bottom = readBufferHeight;
3191 destTrimmedRect.bottom -= yDiff;
3192 }
3193
3194 if (destTrimmedRect.left < 0)
3195 {
3196 int xDiff = 0 - destTrimmedRect.left;
3197 destTrimmedRect.left = 0;
3198 sourceTrimmedRect.left += xDiff;
3199 }
3200
3201 if (destTrimmedRect.right > drawBufferWidth)
3202 {
3203 int xDiff = destTrimmedRect.right - drawBufferWidth;
3204 destTrimmedRect.right = drawBufferWidth;
3205 sourceTrimmedRect.right -= xDiff;
3206 }
3207
3208 if (destTrimmedRect.top < 0)
3209 {
3210 int yDiff = 0 - destTrimmedRect.top;
3211 destTrimmedRect.top = 0;
3212 sourceTrimmedRect.top += yDiff;
3213 }
3214
3215 if (destTrimmedRect.bottom > drawBufferHeight)
3216 {
3217 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3218 destTrimmedRect.bottom = drawBufferHeight;
3219 sourceTrimmedRect.bottom -= yDiff;
3220 }
3221
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003222 bool partialBufferCopy = false;
3223 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3224 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3225 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3226 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3227 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3228 {
3229 partialBufferCopy = true;
3230 }
3231
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003232 if (mask & GL_COLOR_BUFFER_BIT)
3233 {
3234 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3235 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3236 {
3237 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3238 return error(GL_INVALID_OPERATION);
3239 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003240
3241 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3242 {
3243 return error(GL_INVALID_OPERATION);
3244 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003245
3246 blitRenderTarget = true;
3247
3248 }
3249
3250 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3251 {
3252 DepthStencilbuffer *readDSBuffer = NULL;
3253 DepthStencilbuffer *drawDSBuffer = NULL;
3254
3255 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3256 // both a depth and stencil buffer, it will be the same buffer.
3257
3258 if (mask & GL_DEPTH_BUFFER_BIT)
3259 {
3260 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3261 {
3262 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3263 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3264 {
3265 return error(GL_INVALID_OPERATION);
3266 }
3267
3268 blitDepthStencil = true;
3269 readDSBuffer = readFramebuffer->getDepthbuffer();
3270 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3271 }
3272 }
3273
3274 if (mask & GL_STENCIL_BUFFER_BIT)
3275 {
3276 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3277 {
3278 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3279 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3280 {
3281 return error(GL_INVALID_OPERATION);
3282 }
3283
3284 blitDepthStencil = true;
3285 readDSBuffer = readFramebuffer->getStencilbuffer();
3286 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3287 }
3288 }
3289
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003290 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003291 {
3292 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3293 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3294 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003295
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003296 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3297 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003298 {
3299 return error(GL_INVALID_OPERATION);
3300 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003301 }
3302
3303 if (blitRenderTarget || blitDepthStencil)
3304 {
3305 egl::Display *display = getDisplay();
3306 display->endScene();
3307
3308 if (blitRenderTarget)
3309 {
3310 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3311 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3312
3313 if (FAILED(result))
3314 {
3315 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3316 return;
3317 }
3318 }
3319
3320 if (blitDepthStencil)
3321 {
3322 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3323
3324 if (FAILED(result))
3325 {
3326 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3327 return;
3328 }
3329 }
3330 }
3331}
3332
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003333}
3334
3335extern "C"
3336{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003337gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003338{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003339 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003340}
3341
3342void glDestroyContext(gl::Context *context)
3343{
3344 delete context;
3345
3346 if (context == gl::getContext())
3347 {
3348 gl::makeCurrent(NULL, NULL, NULL);
3349 }
3350}
3351
3352void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3353{
3354 gl::makeCurrent(context, display, surface);
3355}
3356
3357gl::Context *glGetCurrentContext()
3358{
3359 return gl::getContext();
3360}
3361}