blob: 9c5ed829ce6ada979ca086c8bcb329430e4da842 [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"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
28#include "libGLESv2/geometry/backend.h"
29#include "libGLESv2/geometry/VertexDataManager.h"
30#include "libGLESv2/geometry/IndexDataManager.h"
31#include "libGLESv2/geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.com86487c22010-03-11 19:41:43 +000033#undef near
34#undef far
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036namespace gl
37{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +000038Context::Context(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000039 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040{
41 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000042
daniel@transgaming.com428d1582010-05-04 03:35:25 +000043 mState.depthClearValue = 1.0f;
44 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045
daniel@transgaming.com428d1582010-05-04 03:35:25 +000046 mState.cullFace = false;
47 mState.cullMode = GL_BACK;
48 mState.frontFace = GL_CCW;
49 mState.depthTest = false;
50 mState.depthFunc = GL_LESS;
51 mState.blend = false;
52 mState.sourceBlendRGB = GL_ONE;
53 mState.sourceBlendAlpha = GL_ONE;
54 mState.destBlendRGB = GL_ZERO;
55 mState.destBlendAlpha = GL_ZERO;
56 mState.blendEquationRGB = GL_FUNC_ADD;
57 mState.blendEquationAlpha = GL_FUNC_ADD;
58 mState.blendColor.red = 0;
59 mState.blendColor.green = 0;
60 mState.blendColor.blue = 0;
61 mState.blendColor.alpha = 0;
62 mState.stencilTest = false;
63 mState.stencilFunc = GL_ALWAYS;
64 mState.stencilRef = 0;
65 mState.stencilMask = -1;
66 mState.stencilWritemask = -1;
67 mState.stencilBackFunc = GL_ALWAYS;
68 mState.stencilBackRef = 0;
69 mState.stencilBackMask = - 1;
70 mState.stencilBackWritemask = -1;
71 mState.stencilFail = GL_KEEP;
72 mState.stencilPassDepthFail = GL_KEEP;
73 mState.stencilPassDepthPass = GL_KEEP;
74 mState.stencilBackFail = GL_KEEP;
75 mState.stencilBackPassDepthFail = GL_KEEP;
76 mState.stencilBackPassDepthPass = GL_KEEP;
77 mState.polygonOffsetFill = false;
78 mState.polygonOffsetFactor = 0.0f;
79 mState.polygonOffsetUnits = 0.0f;
80 mState.sampleAlphaToCoverage = false;
81 mState.sampleCoverage = false;
82 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000083 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000084 mState.scissorTest = false;
85 mState.dither = true;
86 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000087 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000090
daniel@transgaming.com428d1582010-05-04 03:35:25 +000091 mState.viewportX = 0;
92 mState.viewportY = 0;
93 mState.viewportWidth = config->mDisplayMode.Width;
94 mState.viewportHeight = config->mDisplayMode.Height;
95 mState.zNear = 0.0f;
96 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.scissorX = 0;
99 mState.scissorY = 0;
100 mState.scissorWidth = config->mDisplayMode.Width;
101 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.colorMaskRed = true;
104 mState.colorMaskGreen = true;
105 mState.colorMaskBlue = true;
106 mState.colorMaskAlpha = true;
107 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000109 if (shareContext != NULL)
110 {
111 mResourceManager = shareContext->mResourceManager;
112 mResourceManager->addRef();
113 }
114 else
115 {
116 mResourceManager = new ResourceManager();
117 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 // [OpenGL ES 2.0.24] section 3.7 page 83:
120 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
121 // and cube map texture state vectors respectively associated with them.
122 // In order that access to these initial textures not be lost, they are treated as texture
123 // objects all of whose names are 0.
124
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000125 mTexture2DZero = new Texture2D(0);
126 mTextureCubeMapZero = new TextureCubeMap(0);
127
128 mColorbufferZero = NULL;
129 mDepthStencilbufferZero = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000131 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000132 bindArrayBuffer(0);
133 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134 bindTextureCubeMap(0);
135 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000136 bindReadFramebuffer(0);
137 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138 bindRenderbuffer(0);
139
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000140 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
141 {
142 mIncompleteTextures[type] = NULL;
143 }
144
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000145 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000147 mState.packAlignment = 4;
148 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000149
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000150 mBufferBackEnd = NULL;
151 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000152 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000153 mBlit = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000154
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155 mInvalidEnum = false;
156 mInvalidValue = false;
157 mInvalidOperation = false;
158 mOutOfMemory = false;
159 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000160
161 mHasBeenCurrent = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000162
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000163 mSupportsCompressedTextures = false;
164 mSupportsEventQueries = false;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000165 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000166 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000167 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168}
169
170Context::~Context()
171{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000172 if (mState.currentProgram != 0)
173 {
174 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
175 if (programObject)
176 {
177 programObject->release();
178 }
179 mState.currentProgram = 0;
180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000182 while (!mFramebufferMap.empty())
183 {
184 deleteFramebuffer(mFramebufferMap.begin()->first);
185 }
186
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000187 while (!mFenceMap.empty())
188 {
189 deleteFence(mFenceMap.begin()->first);
190 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000191
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000192 while (!mMultiSampleSupport.empty())
193 {
194 delete [] mMultiSampleSupport.begin()->second;
195 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
196 }
197
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000198 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
199 {
200 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
201 {
202 mState.samplerTexture[type][sampler].set(NULL);
203 }
204 }
205
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000206 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
207 {
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000208 delete mIncompleteTextures[type];
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000209 }
210
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000211 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
212 {
213 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
214 }
215
216 mState.arrayBuffer.set(NULL);
217 mState.elementArrayBuffer.set(NULL);
218 mState.texture2D.set(NULL);
219 mState.textureCubeMap.set(NULL);
220 mState.renderbuffer.set(NULL);
221
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000222 delete mTexture2DZero;
223 delete mTextureCubeMapZero;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000225 delete mBufferBackEnd;
226 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000227 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000228 delete mBlit;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000229
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000230 if (mMaskedClearSavedState)
231 {
232 mMaskedClearSavedState->Release();
233 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000234
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000235 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236}
237
238void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
239{
240 IDirect3DDevice9 *device = display->getDevice();
241
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000242 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000243 {
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000244 mDeviceCaps = display->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000245
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000246 mBufferBackEnd = new Dx9BackEnd(this, device);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000247 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000248 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000249 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000250
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000251 const D3DFORMAT renderBufferFormats[] =
252 {
253 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000254 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000255 D3DFMT_R5G6B5,
256 D3DFMT_D24S8
257 };
258
259 int max = 0;
260 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
261 {
262 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
263 display->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
264 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
265
266 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
267 {
268 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
269 {
270 max = j;
271 }
272 }
273 }
274
275 mMaxSupportedSamples = max;
276
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000277 mSupportsEventQueries = display->getEventQuerySupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000278 mSupportsCompressedTextures = display->getCompressedTextureSupport();
daniel@transgaming.com1297d922010-09-01 15:47:47 +0000279 mSupportsFloatTextures = display->getFloatTextureSupport(&mSupportsFloatLinearFilter, &mSupportsFloatRenderableTextures);
280 mSupportsHalfFloatTextures = display->getHalfFloatTextureSupport(&mSupportsHalfFloatLinearFilter, &mSupportsHalfFloatRenderableTextures);
daniel@transgaming.com01868132010-08-24 19:21:17 +0000281
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000282 initExtensionString();
283
284 mState.viewportX = 0;
285 mState.viewportY = 0;
286 mState.viewportWidth = surface->getWidth();
287 mState.viewportHeight = surface->getHeight();
288
289 mState.scissorX = 0;
290 mState.scissorY = 0;
291 mState.scissorWidth = surface->getWidth();
292 mState.scissorHeight = surface->getHeight();
293
294 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000295 }
296
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
298 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000299 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000302 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000303 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
305 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000307 if (defaultRenderTarget)
308 {
309 defaultRenderTarget->Release();
310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000312 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000314 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000316
daniel@transgaming.combe5a0862010-07-28 19:20:37 +0000317 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000318
319 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320}
321
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000322// 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 +0000323void Context::markAllStateDirty()
324{
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000325 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000326 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000327 mAppliedStencilbufferSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000328 mDepthStencilInitialized = false;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000329 mAppliedProgram = 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000330
331 mClearStateDirty = true;
332 mCullStateDirty = true;
333 mDepthStateDirty = true;
334 mMaskStateDirty = true;
335 mBlendStateDirty = true;
336 mStencilStateDirty = true;
337 mPolygonOffsetStateDirty = true;
338 mScissorStateDirty = true;
339 mSampleStateDirty = true;
340 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000341 mFrontFaceDirty = true;
342
343 if (mBufferBackEnd != NULL)
344 {
345 mBufferBackEnd->invalidate();
346 }
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000347}
348
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349void Context::setClearColor(float red, float green, float blue, float alpha)
350{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000351 mState.colorClearValue.red = red;
352 mState.colorClearValue.green = green;
353 mState.colorClearValue.blue = blue;
354 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355}
356
357void Context::setClearDepth(float depth)
358{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000359 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
362void Context::setClearStencil(int stencil)
363{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000364 mState.stencilClearValue = stencil;
365}
366
367void Context::setCullFace(bool enabled)
368{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000369 if (mState.cullFace != enabled)
370 {
371 mState.cullFace = enabled;
372 mCullStateDirty = true;
373 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000374}
375
376bool Context::isCullFaceEnabled() const
377{
378 return mState.cullFace;
379}
380
381void Context::setCullMode(GLenum mode)
382{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000383 if (mState.cullMode != mode)
384 {
385 mState.cullMode = mode;
386 mCullStateDirty = true;
387 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000388}
389
390void Context::setFrontFace(GLenum front)
391{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000392 if (mState.frontFace != front)
393 {
394 mState.frontFace = front;
395 mFrontFaceDirty = true;
396 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000397}
398
399void Context::setDepthTest(bool enabled)
400{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000401 if (mState.depthTest != enabled)
402 {
403 mState.depthTest = enabled;
404 mDepthStateDirty = true;
405 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000406}
407
408bool Context::isDepthTestEnabled() const
409{
410 return mState.depthTest;
411}
412
413void Context::setDepthFunc(GLenum depthFunc)
414{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000415 if (mState.depthFunc != depthFunc)
416 {
417 mState.depthFunc = depthFunc;
418 mDepthStateDirty = true;
419 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420}
421
422void Context::setDepthRange(float zNear, float zFar)
423{
424 mState.zNear = zNear;
425 mState.zFar = zFar;
426}
427
428void Context::setBlend(bool enabled)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.blend != enabled)
431 {
432 mState.blend = enabled;
433 mBlendStateDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437bool Context::isBlendEnabled() const
438{
439 return mState.blend;
440}
441
442void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.sourceBlendRGB != sourceRGB ||
445 mState.sourceBlendAlpha != sourceAlpha ||
446 mState.destBlendRGB != destRGB ||
447 mState.destBlendAlpha != destAlpha)
448 {
449 mState.sourceBlendRGB = sourceRGB;
450 mState.destBlendRGB = destRGB;
451 mState.sourceBlendAlpha = sourceAlpha;
452 mState.destBlendAlpha = destAlpha;
453 mBlendStateDirty = true;
454 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000455}
456
457void Context::setBlendColor(float red, float green, float blue, float alpha)
458{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000459 if (mState.blendColor.red != red ||
460 mState.blendColor.green != green ||
461 mState.blendColor.blue != blue ||
462 mState.blendColor.alpha != alpha)
463 {
464 mState.blendColor.red = red;
465 mState.blendColor.green = green;
466 mState.blendColor.blue = blue;
467 mState.blendColor.alpha = alpha;
468 mBlendStateDirty = true;
469 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000470}
471
472void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
473{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000474 if (mState.blendEquationRGB != rgbEquation ||
475 mState.blendEquationAlpha != alphaEquation)
476 {
477 mState.blendEquationRGB = rgbEquation;
478 mState.blendEquationAlpha = alphaEquation;
479 mBlendStateDirty = true;
480 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000481}
482
483void Context::setStencilTest(bool enabled)
484{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000485 if (mState.stencilTest != enabled)
486 {
487 mState.stencilTest = enabled;
488 mStencilStateDirty = true;
489 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000490}
491
492bool Context::isStencilTestEnabled() const
493{
494 return mState.stencilTest;
495}
496
497void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
498{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000499 if (mState.stencilFunc != stencilFunc ||
500 mState.stencilRef != stencilRef ||
501 mState.stencilMask != stencilMask)
502 {
503 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000504 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000505 mState.stencilMask = stencilMask;
506 mStencilStateDirty = true;
507 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000508}
509
510void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
511{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000512 if (mState.stencilBackFunc != stencilBackFunc ||
513 mState.stencilBackRef != stencilBackRef ||
514 mState.stencilBackMask != stencilBackMask)
515 {
516 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000517 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000518 mState.stencilBackMask = stencilBackMask;
519 mStencilStateDirty = true;
520 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000521}
522
523void Context::setStencilWritemask(GLuint stencilWritemask)
524{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000525 if (mState.stencilWritemask != stencilWritemask)
526 {
527 mState.stencilWritemask = stencilWritemask;
528 mStencilStateDirty = true;
529 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000530}
531
532void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
533{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000534 if (mState.stencilBackWritemask != stencilBackWritemask)
535 {
536 mState.stencilBackWritemask = stencilBackWritemask;
537 mStencilStateDirty = true;
538 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000539}
540
541void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
542{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000543 if (mState.stencilFail != stencilFail ||
544 mState.stencilPassDepthFail != stencilPassDepthFail ||
545 mState.stencilPassDepthPass != stencilPassDepthPass)
546 {
547 mState.stencilFail = stencilFail;
548 mState.stencilPassDepthFail = stencilPassDepthFail;
549 mState.stencilPassDepthPass = stencilPassDepthPass;
550 mStencilStateDirty = true;
551 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000552}
553
554void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
555{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000556 if (mState.stencilBackFail != stencilBackFail ||
557 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
558 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
559 {
560 mState.stencilBackFail = stencilBackFail;
561 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
562 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
563 mStencilStateDirty = true;
564 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000565}
566
567void Context::setPolygonOffsetFill(bool enabled)
568{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000569 if (mState.polygonOffsetFill != enabled)
570 {
571 mState.polygonOffsetFill = enabled;
572 mPolygonOffsetStateDirty = true;
573 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000574}
575
576bool Context::isPolygonOffsetFillEnabled() const
577{
578 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000580}
581
582void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
583{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000584 if (mState.polygonOffsetFactor != factor ||
585 mState.polygonOffsetUnits != units)
586 {
587 mState.polygonOffsetFactor = factor;
588 mState.polygonOffsetUnits = units;
589 mPolygonOffsetStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setSampleAlphaToCoverage(bool enabled)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.sampleAlphaToCoverage != enabled)
596 {
597 mState.sampleAlphaToCoverage = enabled;
598 mSampleStateDirty = true;
599 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000600}
601
602bool Context::isSampleAlphaToCoverageEnabled() const
603{
604 return mState.sampleAlphaToCoverage;
605}
606
607void Context::setSampleCoverage(bool enabled)
608{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000609 if (mState.sampleCoverage != enabled)
610 {
611 mState.sampleCoverage = enabled;
612 mSampleStateDirty = true;
613 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614}
615
616bool Context::isSampleCoverageEnabled() const
617{
618 return mState.sampleCoverage;
619}
620
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000621void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000623 if (mState.sampleCoverageValue != value ||
624 mState.sampleCoverageInvert != invert)
625 {
626 mState.sampleCoverageValue = value;
627 mState.sampleCoverageInvert = invert;
628 mSampleStateDirty = true;
629 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000630}
631
632void Context::setScissorTest(bool enabled)
633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.scissorTest != enabled)
635 {
636 mState.scissorTest = enabled;
637 mScissorStateDirty = true;
638 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000639}
640
641bool Context::isScissorTestEnabled() const
642{
643 return mState.scissorTest;
644}
645
646void Context::setDither(bool enabled)
647{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000648 if (mState.dither != enabled)
649 {
650 mState.dither = enabled;
651 mDitherStateDirty = true;
652 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000653}
654
655bool Context::isDitherEnabled() const
656{
657 return mState.dither;
658}
659
660void Context::setLineWidth(GLfloat width)
661{
662 mState.lineWidth = width;
663}
664
665void Context::setGenerateMipmapHint(GLenum hint)
666{
667 mState.generateMipmapHint = hint;
668}
669
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000670void Context::setFragmentShaderDerivativeHint(GLenum hint)
671{
672 mState.fragmentShaderDerivativeHint = hint;
673 // TODO: Propagate the hint to shader translator so we can write
674 // ddx, ddx_coarse, or ddx_fine depending on the hint.
675 // Ignore for now. It is valid for implementations to ignore hint.
676}
677
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000678void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
679{
680 mState.viewportX = x;
681 mState.viewportY = y;
682 mState.viewportWidth = width;
683 mState.viewportHeight = height;
684}
685
686void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
687{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000688 if (mState.scissorX != x || mState.scissorY != y ||
689 mState.scissorWidth != width || mState.scissorHeight != height)
690 {
691 mState.scissorX = x;
692 mState.scissorY = y;
693 mState.scissorWidth = width;
694 mState.scissorHeight = height;
695 mScissorStateDirty = true;
696 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000697}
698
699void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
700{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000701 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
702 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
703 {
704 mState.colorMaskRed = red;
705 mState.colorMaskGreen = green;
706 mState.colorMaskBlue = blue;
707 mState.colorMaskAlpha = alpha;
708 mMaskStateDirty = true;
709 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000710}
711
712void Context::setDepthMask(bool mask)
713{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000714 if (mState.depthMask != mask)
715 {
716 mState.depthMask = mask;
717 mMaskStateDirty = true;
718 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000719}
720
721void Context::setActiveSampler(int active)
722{
723 mState.activeSampler = active;
724}
725
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000726GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000727{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000728 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000729}
730
731GLuint Context::getDrawFramebufferHandle() const
732{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000733 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734}
735
736GLuint Context::getRenderbufferHandle() const
737{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000738 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739}
740
741GLuint Context::getArrayBufferHandle() const
742{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000743 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000744}
745
746void Context::setVertexAttribEnabled(unsigned int attribNum, bool enabled)
747{
748 mState.vertexAttribute[attribNum].mEnabled = enabled;
749}
750
751const AttributeState &Context::getVertexAttribState(unsigned int attribNum)
752{
753 return mState.vertexAttribute[attribNum];
754}
755
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000756void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000757 GLsizei stride, const void *pointer)
758{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000759 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000760 mState.vertexAttribute[attribNum].mSize = size;
761 mState.vertexAttribute[attribNum].mType = type;
762 mState.vertexAttribute[attribNum].mNormalized = normalized;
763 mState.vertexAttribute[attribNum].mStride = stride;
764 mState.vertexAttribute[attribNum].mPointer = pointer;
765}
766
767const void *Context::getVertexAttribPointer(unsigned int attribNum) const
768{
769 return mState.vertexAttribute[attribNum].mPointer;
770}
771
772// returns entire set of attributes as a block
773const AttributeState *Context::getVertexAttribBlock()
774{
775 return mState.vertexAttribute;
776}
777
778void Context::setPackAlignment(GLint alignment)
779{
780 mState.packAlignment = alignment;
781}
782
783GLint Context::getPackAlignment() const
784{
785 return mState.packAlignment;
786}
787
788void Context::setUnpackAlignment(GLint alignment)
789{
790 mState.unpackAlignment = alignment;
791}
792
793GLint Context::getUnpackAlignment() const
794{
795 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798GLuint Context::createBuffer()
799{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000800 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803GLuint Context::createProgram()
804{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000805 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806}
807
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000808GLuint Context::createShader(GLenum type)
809{
810 return mResourceManager->createShader(type);
811}
812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813GLuint Context::createTexture()
814{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000815 return mResourceManager->createTexture();
816}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000818GLuint Context::createRenderbuffer()
819{
820 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000823// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824GLuint Context::createFramebuffer()
825{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000826 unsigned int handle = 1;
827
828 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
829 {
830 handle++;
831 }
832
833 mFramebufferMap[handle] = NULL;
834
835 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000838GLuint Context::createFence()
839{
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000840 unsigned int handle = 0;
841
842 while (mFenceMap.find(handle) != mFenceMap.end())
843 {
844 handle++;
845 }
846
847 mFenceMap[handle] = new Fence;
848
849 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000850}
851
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852void Context::deleteBuffer(GLuint buffer)
853{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000854 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855 {
856 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858
859 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void Context::deleteShader(GLuint shader)
863{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000864 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867void Context::deleteProgram(GLuint program)
868{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000869 mResourceManager->deleteProgram(program);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870}
871
872void Context::deleteTexture(GLuint texture)
873{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000874 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 {
876 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878
879 mResourceManager->deleteTexture(texture);
880}
881
882void Context::deleteRenderbuffer(GLuint renderbuffer)
883{
884 if (mResourceManager->getRenderbuffer(renderbuffer))
885 {
886 detachRenderbuffer(renderbuffer);
887 }
888
889 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
892void Context::deleteFramebuffer(GLuint framebuffer)
893{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000894 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
895
896 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897 {
898 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000899
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000900 delete framebufferObject->second;
901 mFramebufferMap.erase(framebufferObject);
902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000904
905void Context::deleteFence(GLuint fence)
906{
907 FenceMap::iterator fenceObject = mFenceMap.find(fence);
908
909 if (fenceObject != mFenceMap.end())
910 {
911 delete fenceObject->second;
912 mFenceMap.erase(fenceObject);
913 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000914}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000916Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918 return mResourceManager->getBuffer(handle);
919}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921Shader *Context::getShader(GLuint handle)
922{
923 return mResourceManager->getShader(handle);
924}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000926Program *Context::getProgram(GLuint handle)
927{
928 return mResourceManager->getProgram(handle);
929}
930
931Texture *Context::getTexture(GLuint handle)
932{
933 return mResourceManager->getTexture(handle);
934}
935
936Renderbuffer *Context::getRenderbuffer(GLuint handle)
937{
938 return mResourceManager->getRenderbuffer(handle);
939}
940
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000941Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000942{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000943 return getFramebuffer(mState.readFramebuffer);
944}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000945
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000946Framebuffer *Context::getDrawFramebuffer()
947{
948 return getFramebuffer(mState.drawFramebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
951void Context::bindArrayBuffer(unsigned int buffer)
952{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000953 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000955 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
958void Context::bindElementArrayBuffer(unsigned int buffer)
959{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000960 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000962 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963}
964
965void Context::bindTexture2D(GLuint texture)
966{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000967 mResourceManager->checkTextureAllocation(texture, SAMPLER_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000969 mState.texture2D.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000971 mState.samplerTexture[SAMPLER_2D][mState.activeSampler].set(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::bindTextureCubeMap(GLuint texture)
975{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000976 mResourceManager->checkTextureAllocation(texture, SAMPLER_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000978 mState.textureCubeMap.set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000980 mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].set(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981}
982
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000983void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000985 if (!getFramebuffer(framebuffer))
986 {
987 mFramebufferMap[framebuffer] = new Framebuffer();
988 }
989
990 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000991}
992
993void Context::bindDrawFramebuffer(GLuint framebuffer)
994{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000995 if (!getFramebuffer(framebuffer))
996 {
997 mFramebufferMap[framebuffer] = new Framebuffer();
998 }
999
1000 mState.drawFramebuffer = framebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001}
1002
1003void Context::bindRenderbuffer(GLuint renderbuffer)
1004{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001005 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1006
1007 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008}
1009
1010void Context::useProgram(GLuint program)
1011{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001012 GLuint priorProgram = mState.currentProgram;
1013 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001014
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001015 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001017 Program *newProgram = mResourceManager->getProgram(program);
1018 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1019
1020 if (newProgram)
1021 {
1022 newProgram->addRef();
1023 }
1024
1025 if (oldProgram)
1026 {
1027 oldProgram->release();
1028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030}
1031
1032void Context::setFramebufferZero(Framebuffer *buffer)
1033{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001034 delete mFramebufferMap[0];
1035 mFramebufferMap[0] = buffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036}
1037
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001038void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001040 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1041 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042}
1043
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001044Framebuffer *Context::getFramebuffer(unsigned int handle)
1045{
1046 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1047
1048 if (framebuffer == mFramebufferMap.end())
1049 {
1050 return NULL;
1051 }
1052 else
1053 {
1054 return framebuffer->second;
1055 }
1056}
1057
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001058Fence *Context::getFence(unsigned int handle)
1059{
1060 FenceMap::iterator fence = mFenceMap.find(handle);
1061
1062 if (fence == mFenceMap.end())
1063 {
1064 return NULL;
1065 }
1066 else
1067 {
1068 return fence->second;
1069 }
1070}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001071
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072Buffer *Context::getArrayBuffer()
1073{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001074 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075}
1076
1077Buffer *Context::getElementArrayBuffer()
1078{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001079 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080}
1081
1082Program *Context::getCurrentProgram()
1083{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001084 return mResourceManager->getProgram(mState.currentProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085}
1086
1087Texture2D *Context::getTexture2D()
1088{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001089 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 +00001090 {
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001091 return mTexture2DZero;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092 }
1093
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001094 return static_cast<Texture2D*>(mState.texture2D.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095}
1096
1097TextureCubeMap *Context::getTextureCubeMap()
1098{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001099 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 +00001100 {
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001101 return mTextureCubeMapZero;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102 }
1103
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001104 return static_cast<TextureCubeMap*>(mState.textureCubeMap.get());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105}
1106
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001107Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001109 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001110
1111 if (texid == 0)
1112 {
1113 switch (type)
1114 {
1115 default: UNREACHABLE();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001116 case SAMPLER_2D: return mTexture2DZero;
1117 case SAMPLER_CUBE: return mTextureCubeMapZero;
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001118 }
1119 }
1120
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001121 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122}
1123
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001124bool Context::getBooleanv(GLenum pname, GLboolean *params)
1125{
1126 switch (pname)
1127 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001128 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1129 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1130 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001131 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001132 params[0] = mState.colorMaskRed;
1133 params[1] = mState.colorMaskGreen;
1134 params[2] = mState.colorMaskBlue;
1135 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001136 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001137 case GL_CULL_FACE: *params = mState.cullFace;
1138 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill;
1139 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;
1140 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage;
1141 case GL_SCISSOR_TEST: *params = mState.scissorTest;
1142 case GL_STENCIL_TEST: *params = mState.stencilTest;
1143 case GL_DEPTH_TEST: *params = mState.depthTest;
1144 case GL_BLEND: *params = mState.blend;
1145 case GL_DITHER: *params = mState.dither;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001146 default:
1147 return false;
1148 }
1149
1150 return true;
1151}
1152
1153bool Context::getFloatv(GLenum pname, GLfloat *params)
1154{
1155 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1156 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1157 // GetIntegerv as its native query function. As it would require conversion in any
1158 // case, this should make no difference to the calling application.
1159 switch (pname)
1160 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001161 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1162 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1163 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1164 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1165 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001166 case GL_ALIASED_LINE_WIDTH_RANGE:
1167 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1168 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1169 break;
1170 case GL_ALIASED_POINT_SIZE_RANGE:
1171 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001172 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 +00001173 break;
1174 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001175 params[0] = mState.zNear;
1176 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001177 break;
1178 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001179 params[0] = mState.colorClearValue.red;
1180 params[1] = mState.colorClearValue.green;
1181 params[2] = mState.colorClearValue.blue;
1182 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001183 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001184 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001185 params[0] = mState.blendColor.red;
1186 params[1] = mState.blendColor.green;
1187 params[2] = mState.blendColor.blue;
1188 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001189 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001190 default:
1191 return false;
1192 }
1193
1194 return true;
1195}
1196
1197bool Context::getIntegerv(GLenum pname, GLint *params)
1198{
1199 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1200 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1201 // GetIntegerv as its native query function. As it would require conversion in any
1202 // case, this should make no difference to the calling application. You may find it in
1203 // Context::getFloatv.
1204 switch (pname)
1205 {
1206 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1207 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1208 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
1209 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1210 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1211 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1212 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
1213 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001214 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001215 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001216 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1217 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001218 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001219 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1220 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001221 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001222 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1223 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1224 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1225 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001226 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001227 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1228 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1229 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1230 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1231 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1232 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1233 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1234 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1235 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1236 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1237 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1238 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1239 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1240 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1241 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1242 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1243 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1244 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1245 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1246 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1247 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1248 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1249 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1250 case GL_SUBPIXEL_BITS: *params = 4; break;
1251 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
1252 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001253 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1254 {
1255 if (supportsCompressedTextures())
1256 {
1257 // at current, only GL_COMPRESSED_RGB_S3TC_DXT1_EXT and
1258 // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT are supported
1259 *params = 2;
1260 }
1261 else
1262 {
1263 *params = 0;
1264 }
1265 }
1266 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001267 case GL_MAX_SAMPLES_ANGLE:
1268 {
1269 GLsizei maxSamples = getMaxSupportedSamples();
1270 if (maxSamples != 0)
1271 {
1272 *params = maxSamples;
1273 }
1274 else
1275 {
1276 return false;
1277 }
1278
1279 break;
1280 }
1281 case GL_SAMPLE_BUFFERS:
1282 case GL_SAMPLES:
1283 {
1284 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1285 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1286 {
1287 switch (pname)
1288 {
1289 case GL_SAMPLE_BUFFERS:
1290 if (framebuffer->getSamples() != 0)
1291 {
1292 *params = 1;
1293 }
1294 else
1295 {
1296 *params = 0;
1297 }
1298 break;
1299 case GL_SAMPLES:
1300 *params = framebuffer->getSamples();
1301 break;
1302 }
1303 }
1304 else
1305 {
1306 *params = 0;
1307 }
1308 }
1309 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001310 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1311 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1312 case GL_MAX_VIEWPORT_DIMS:
1313 {
1314 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
1315 params[0] = maxDimension;
1316 params[1] = maxDimension;
1317 }
1318 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001319 case GL_COMPRESSED_TEXTURE_FORMATS:
1320 {
1321 if (supportsCompressedTextures())
1322 {
1323 params[0] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1324 params[1] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1325 }
1326 }
1327 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001328 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001329 params[0] = mState.viewportX;
1330 params[1] = mState.viewportY;
1331 params[2] = mState.viewportWidth;
1332 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001333 break;
1334 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001335 params[0] = mState.scissorX;
1336 params[1] = mState.scissorY;
1337 params[2] = mState.scissorWidth;
1338 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001339 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001340 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1341 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001342 case GL_RED_BITS:
1343 case GL_GREEN_BITS:
1344 case GL_BLUE_BITS:
1345 case GL_ALPHA_BITS:
1346 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001347 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001348 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
1349
1350 if (colorbuffer)
1351 {
1352 switch (pname)
1353 {
1354 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1355 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1356 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1357 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1358 }
1359 }
1360 else
1361 {
1362 *params = 0;
1363 }
1364 }
1365 break;
1366 case GL_DEPTH_BITS:
1367 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001368 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001369 gl::DepthStencilbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001370
1371 if (depthbuffer)
1372 {
1373 *params = depthbuffer->getDepthSize();
1374 }
1375 else
1376 {
1377 *params = 0;
1378 }
1379 }
1380 break;
1381 case GL_STENCIL_BITS:
1382 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001383 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001384 gl::DepthStencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001385
1386 if (stencilbuffer)
1387 {
1388 *params = stencilbuffer->getStencilSize();
1389 }
1390 else
1391 {
1392 *params = 0;
1393 }
1394 }
1395 break;
1396 case GL_TEXTURE_BINDING_2D:
1397 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001398 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001399 {
1400 error(GL_INVALID_OPERATION);
1401 return false;
1402 }
1403
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001404 *params = mState.samplerTexture[SAMPLER_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001405 }
1406 break;
1407 case GL_TEXTURE_BINDING_CUBE_MAP:
1408 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001409 if (mState.activeSampler < 0 || mState.activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001410 {
1411 error(GL_INVALID_OPERATION);
1412 return false;
1413 }
1414
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001415 *params = mState.samplerTexture[SAMPLER_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001416 }
1417 break;
1418 default:
1419 return false;
1420 }
1421
1422 return true;
1423}
1424
1425bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1426{
1427 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1428 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1429 // to the fact that it is stored internally as a float, and so would require conversion
1430 // if returned from Context::getIntegerv. Since this conversion is already implemented
1431 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1432 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1433 // application.
1434 switch (pname)
1435 {
1436 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1437 case GL_SHADER_BINARY_FORMATS:
1438 {
1439 *type = GL_INT;
1440 *numParams = 0;
1441 }
1442 break;
1443 case GL_MAX_VERTEX_ATTRIBS:
1444 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1445 case GL_MAX_VARYING_VECTORS:
1446 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1447 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1448 case GL_MAX_TEXTURE_IMAGE_UNITS:
1449 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1450 case GL_MAX_RENDERBUFFER_SIZE:
1451 case GL_NUM_SHADER_BINARY_FORMATS:
1452 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1453 case GL_ARRAY_BUFFER_BINDING:
1454 case GL_FRAMEBUFFER_BINDING:
1455 case GL_RENDERBUFFER_BINDING:
1456 case GL_CURRENT_PROGRAM:
1457 case GL_PACK_ALIGNMENT:
1458 case GL_UNPACK_ALIGNMENT:
1459 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001460 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001461 case GL_RED_BITS:
1462 case GL_GREEN_BITS:
1463 case GL_BLUE_BITS:
1464 case GL_ALPHA_BITS:
1465 case GL_DEPTH_BITS:
1466 case GL_STENCIL_BITS:
1467 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1468 case GL_CULL_FACE_MODE:
1469 case GL_FRONT_FACE:
1470 case GL_ACTIVE_TEXTURE:
1471 case GL_STENCIL_FUNC:
1472 case GL_STENCIL_VALUE_MASK:
1473 case GL_STENCIL_REF:
1474 case GL_STENCIL_FAIL:
1475 case GL_STENCIL_PASS_DEPTH_FAIL:
1476 case GL_STENCIL_PASS_DEPTH_PASS:
1477 case GL_STENCIL_BACK_FUNC:
1478 case GL_STENCIL_BACK_VALUE_MASK:
1479 case GL_STENCIL_BACK_REF:
1480 case GL_STENCIL_BACK_FAIL:
1481 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1482 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1483 case GL_DEPTH_FUNC:
1484 case GL_BLEND_SRC_RGB:
1485 case GL_BLEND_SRC_ALPHA:
1486 case GL_BLEND_DST_RGB:
1487 case GL_BLEND_DST_ALPHA:
1488 case GL_BLEND_EQUATION_RGB:
1489 case GL_BLEND_EQUATION_ALPHA:
1490 case GL_STENCIL_WRITEMASK:
1491 case GL_STENCIL_BACK_WRITEMASK:
1492 case GL_STENCIL_CLEAR_VALUE:
1493 case GL_SUBPIXEL_BITS:
1494 case GL_MAX_TEXTURE_SIZE:
1495 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1496 case GL_SAMPLE_BUFFERS:
1497 case GL_SAMPLES:
1498 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1499 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1500 case GL_TEXTURE_BINDING_2D:
1501 case GL_TEXTURE_BINDING_CUBE_MAP:
1502 {
1503 *type = GL_INT;
1504 *numParams = 1;
1505 }
1506 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001507 case GL_MAX_SAMPLES_ANGLE:
1508 {
1509 if (getMaxSupportedSamples() != 0)
1510 {
1511 *type = GL_INT;
1512 *numParams = 1;
1513 }
1514 else
1515 {
1516 return false;
1517 }
1518 }
1519 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001520 case GL_MAX_VIEWPORT_DIMS:
1521 {
1522 *type = GL_INT;
1523 *numParams = 2;
1524 }
1525 break;
1526 case GL_VIEWPORT:
1527 case GL_SCISSOR_BOX:
1528 {
1529 *type = GL_INT;
1530 *numParams = 4;
1531 }
1532 break;
1533 case GL_SHADER_COMPILER:
1534 case GL_SAMPLE_COVERAGE_INVERT:
1535 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001536 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1537 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1538 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1539 case GL_SAMPLE_COVERAGE:
1540 case GL_SCISSOR_TEST:
1541 case GL_STENCIL_TEST:
1542 case GL_DEPTH_TEST:
1543 case GL_BLEND:
1544 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001545 {
1546 *type = GL_BOOL;
1547 *numParams = 1;
1548 }
1549 break;
1550 case GL_COLOR_WRITEMASK:
1551 {
1552 *type = GL_BOOL;
1553 *numParams = 4;
1554 }
1555 break;
1556 case GL_POLYGON_OFFSET_FACTOR:
1557 case GL_POLYGON_OFFSET_UNITS:
1558 case GL_SAMPLE_COVERAGE_VALUE:
1559 case GL_DEPTH_CLEAR_VALUE:
1560 case GL_LINE_WIDTH:
1561 {
1562 *type = GL_FLOAT;
1563 *numParams = 1;
1564 }
1565 break;
1566 case GL_ALIASED_LINE_WIDTH_RANGE:
1567 case GL_ALIASED_POINT_SIZE_RANGE:
1568 case GL_DEPTH_RANGE:
1569 {
1570 *type = GL_FLOAT;
1571 *numParams = 2;
1572 }
1573 break;
1574 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001575 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001576 {
1577 *type = GL_FLOAT;
1578 *numParams = 4;
1579 }
1580 break;
1581 default:
1582 return false;
1583 }
1584
1585 return true;
1586}
1587
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588// Applies the render target surface, depth stencil surface, viewport rectangle and
1589// scissor rectangle to the Direct3D 9 device
1590bool Context::applyRenderTarget(bool ignoreViewport)
1591{
1592 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001593
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001594 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595
1596 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1597 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001598 error(GL_INVALID_FRAMEBUFFER_OPERATION);
1599
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 return false;
1601 }
1602
1603 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00001604
1605 if (!renderTarget)
1606 {
1607 return false; // Context must be lost
1608 }
1609
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001610 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001612 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1613 if (renderTargetSerial != mAppliedRenderTargetSerial)
1614 {
1615 device->SetRenderTarget(0, renderTarget);
1616 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001617 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 +00001618 }
1619
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001620 unsigned int depthbufferSerial = 0;
1621 unsigned int stencilbufferSerial = 0;
1622 if (framebufferObject->getDepthbufferType() != GL_NONE)
1623 {
1624 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
1625 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1626 }
1627 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1628 {
1629 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
1630 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1631 }
1632
1633 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001634 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001635 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001636 {
1637 device->SetDepthStencilSurface(depthStencil);
1638 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001639 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001640 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642
1643 D3DVIEWPORT9 viewport;
1644 D3DSURFACE_DESC desc;
1645 renderTarget->GetDesc(&desc);
1646
1647 if (ignoreViewport)
1648 {
1649 viewport.X = 0;
1650 viewport.Y = 0;
1651 viewport.Width = desc.Width;
1652 viewport.Height = desc.Height;
1653 viewport.MinZ = 0.0f;
1654 viewport.MaxZ = 1.0f;
1655 }
1656 else
1657 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001658 viewport.X = std::max(mState.viewportX, 0);
1659 viewport.Y = std::max(mState.viewportY, 0);
1660 viewport.Width = std::min(mState.viewportWidth, (int)desc.Width - (int)viewport.X);
1661 viewport.Height = std::min(mState.viewportHeight, (int)desc.Height - (int)viewport.Y);
1662 viewport.MinZ = clamp01(mState.zNear);
1663 viewport.MaxZ = clamp01(mState.zFar);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 }
1665
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001666 if (viewport.Width <= 0 || viewport.Height <= 0)
1667 {
1668 return false; // Nothing to render
1669 }
1670
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 device->SetViewport(&viewport);
1672
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001673 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001675 if (mState.scissorTest)
1676 {
1677 RECT rect = {mState.scissorX,
1678 mState.scissorY,
1679 mState.scissorX + mState.scissorWidth,
1680 mState.scissorY + mState.scissorHeight};
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001681 rect.right = std::min(static_cast<UINT>(rect.right), desc.Width);
1682 rect.bottom = std::min(static_cast<UINT>(rect.bottom), desc.Height);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001683 device->SetScissorRect(&rect);
1684 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1685 }
1686 else
1687 {
1688 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1689 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001690
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001691 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692 }
1693
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001694 if (mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696 Program *programObject = getCurrentProgram();
1697
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001698 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
daniel@transgaming.com8ee00ea2010-04-29 03:38:47 +00001699 GLfloat xy[2] = {1.0f / viewport.Width, 1.0f / viewport.Height};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001701
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +00001702 GLint window = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001703 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1704 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1705 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001706 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1707
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001708 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001709 GLfloat dz[2] = {(mState.zFar - mState.zNear) / 2.0f, (mState.zNear + mState.zFar) / 2.0f};
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001710 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1711
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001712 GLint near = programObject->getDepthRangeNearLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001713 programObject->setUniform1fv(near, 1, &mState.zNear);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001714
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001715 GLint far = programObject->getDepthRangeFarLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001716 programObject->setUniform1fv(far, 1, &mState.zFar);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001717
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001718 GLint diff = programObject->getDepthRangeDiffLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001719 GLfloat zDiff = mState.zFar - mState.zNear;
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001720 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721 }
1722
1723 return true;
1724}
1725
1726// 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 +00001727void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728{
1729 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001730 Program *programObject = getCurrentProgram();
1731
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001732 GLint frontCCW = programObject->getDxFrontCCWLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001733 GLint ccw = (mState.frontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001734 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001736 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001737 GLint alwaysFront = !isTriangleMode(drawMode);
1738 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1739
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001740 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001741
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001742 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001744 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001746 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747 }
1748 else
1749 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001750 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 }
1752
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001753 mCullStateDirty = false;
1754 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 if (mDepthStateDirty)
1757 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001758 if (mState.depthTest && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001760 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1761 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762 }
1763 else
1764 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767
1768 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 }
1770
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001771 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001773 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001774 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001775 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1776
1777 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1778 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1779 {
1780 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
1781 }
1782 else
1783 {
1784 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
1785 unorm<8>(mState.blendColor.alpha),
1786 unorm<8>(mState.blendColor.alpha),
1787 unorm<8>(mState.blendColor.alpha)));
1788 }
1789
1790 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1791 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1792 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
1793
1794 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1795 mState.destBlendRGB != mState.destBlendAlpha ||
1796 mState.blendEquationRGB != mState.blendEquationAlpha)
1797 {
1798 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1799
1800 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1801 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1802 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
1803
1804 }
1805 else
1806 {
1807 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1808 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001809 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001810 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001811 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001812 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001813 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001814
1815 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816 }
1817
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001818 if (mStencilStateDirty || mFrontFaceDirty)
1819 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001820 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001821 {
1822 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1823 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1824
1825 // FIXME: Unsupported by D3D9
1826 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1827 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1828 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1829 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1830 mState.stencilRef != mState.stencilBackRef ||
1831 mState.stencilMask != mState.stencilBackMask)
1832 {
1833 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1834 return error(GL_INVALID_OPERATION);
1835 }
1836
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001837 // get the maximum size of the stencil ref
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001838 gl::DepthStencilbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001839 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1840
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001841 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1842 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1843 es2dx::ConvertComparison(mState.stencilFunc));
1844
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001845 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 +00001846 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
1847
1848 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1849 es2dx::ConvertStencilOp(mState.stencilFail));
1850 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1851 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
1852 device->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1853 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1854
1855 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1856 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
1857 es2dx::ConvertComparison(mState.stencilBackFunc));
1858
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001859 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 +00001860 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
1861
1862 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
1863 es2dx::ConvertStencilOp(mState.stencilBackFail));
1864 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
1865 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
1866 device->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
1867 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1868 }
1869 else
1870 {
1871 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1872 }
1873
1874 mStencilStateDirty = false;
1875 }
1876
1877 if (mMaskStateDirty)
1878 {
1879 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1880 mState.colorMaskBlue, mState.colorMaskAlpha));
1881 device->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
1882
1883 mMaskStateDirty = false;
1884 }
1885
1886 if (mPolygonOffsetStateDirty)
1887 {
1888 if (mState.polygonOffsetFill)
1889 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001890 gl::DepthStencilbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001891 if (depthbuffer)
1892 {
1893 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
1894 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1895 device->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
1896 }
1897 }
1898 else
1899 {
1900 device->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
1901 device->SetRenderState(D3DRS_DEPTHBIAS, 0);
1902 }
1903
1904 mPolygonOffsetStateDirty = false;
1905 }
1906
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001907 if (framebufferObject->isMultisample() && mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001909 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001910 {
1911 FIXME("Sample alpha to coverage is unimplemented.");
1912 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001914 if (mState.sampleCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001915 {
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00001916 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
1917 unsigned int mask = 0;
1918 if (mState.sampleCoverageValue != 0)
1919 {
1920 float threshold = 0.5f;
1921
1922 for (int i = 0; i < framebufferObject->getSamples(); ++i)
1923 {
1924 mask <<= 1;
1925
1926 if ((i + 1) * mState.sampleCoverageValue >= threshold)
1927 {
1928 threshold += 1.0f;
1929 mask |= 1;
1930 }
1931 }
1932 }
1933
1934 if (mState.sampleCoverageInvert)
1935 {
1936 mask = ~mask;
1937 }
1938
1939 device->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
1940 }
1941 else
1942 {
1943 device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00001944 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001945
1946 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 }
1948
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001949 if (mDitherStateDirty)
1950 {
1951 device->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
1952
1953 mDitherStateDirty = false;
1954 }
1955
1956 mFrontFaceDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957}
1958
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001959// Fill in the semanticIndex field of the array of TranslatedAttributes based on the active GLSL program.
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001960void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001962 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001964 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965 {
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +00001966 attributes[i].semanticIndex = getCurrentProgram()->getSemanticIndex(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967 }
1968 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001969}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001970
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001971GLenum Context::applyVertexBuffer(GLenum mode, GLint first, GLsizei count, bool *useIndexing, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001972{
1973 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1974
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001975 GLenum err = mVertexDataManager->preRenderValidate(first, count, translated);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001976 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001977 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001978 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00001979 }
1980
daniel@transgaming.com81655a72010-05-20 19:18:17 +00001981 lookupAttributeMapping(translated);
1982
1983 mBufferBackEnd->setupAttributesPreDraw(translated);
1984
1985 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1986 {
1987 if (translated[i].enabled && translated[i].nonArray)
1988 {
1989 err = mIndexDataManager->preRenderValidateUnindexed(mode, count, indexInfo);
1990 if (err != GL_NO_ERROR)
1991 {
1992 return err;
1993 }
1994
1995 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
1996
1997 *useIndexing = true;
1998 return GL_NO_ERROR;
1999 }
2000 }
2001
2002 *useIndexing = false;
2003 return GL_NO_ERROR;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002004}
2005
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002006GLenum Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002007{
2008 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
2009
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002010 GLenum err = mVertexDataManager->preRenderValidate(indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002011
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002012 if (err == GL_NO_ERROR)
2013 {
2014 lookupAttributeMapping(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002015
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002016 mBufferBackEnd->setupAttributesPreDraw(translated);
2017 }
2018
2019 return err;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002020}
2021
2022// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002023GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002024{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002025 GLenum err = mIndexDataManager->preRenderValidate(mode, type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002026
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002027 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002028 {
2029 mBufferBackEnd->setupIndicesPreDraw(*indexInfo);
2030 }
2031
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002032 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033}
2034
2035// Applies the shaders and shader constants to the Direct3D 9 device
2036void Context::applyShaders()
2037{
2038 IDirect3DDevice9 *device = getDevice();
2039 Program *programObject = getCurrentProgram();
2040 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2041 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2042
2043 device->SetVertexShader(vertexShader);
2044 device->SetPixelShader(pixelShader);
2045
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002046 if (programObject->getSerial() != mAppliedProgram)
2047 {
2048 programObject->dirtyAllUniforms();
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002049 programObject->dirtyAllSamplers();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002050 mAppliedProgram = programObject->getSerial();
2051 }
2052
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 programObject->applyUniforms();
2054}
2055
2056// Applies the textures and sampler states to the Direct3D 9 device
2057void Context::applyTextures()
2058{
2059 IDirect3DDevice9 *device = getDevice();
2060 Program *programObject = getCurrentProgram();
2061
2062 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
2063 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002064 int textureUnit = programObject->getSamplerMapping(sampler);
2065 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002067 SamplerType textureType = programObject->getSamplerType(sampler);
2068
2069 Texture *texture = getSamplerTexture(textureUnit, textureType);
2070
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002071 if (programObject->isSamplerDirty(sampler) || texture->isDirty())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002072 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002073 if (texture->isComplete())
2074 {
2075 GLenum wrapS = texture->getWrapS();
2076 GLenum wrapT = texture->getWrapT();
2077 GLenum minFilter = texture->getMinFilter();
2078 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002080 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2081 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002082
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002083 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
2084 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2085 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
2086 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
2087 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002089 device->SetTexture(sampler, texture->getTexture());
2090 }
2091 else
2092 {
2093 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
2094 }
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002095 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002096
2097 programObject->setSamplerDirty(sampler, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002099 else
2100 {
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002101 if (programObject->isSamplerDirty(sampler))
2102 {
2103 device->SetTexture(sampler, NULL);
2104 programObject->setSamplerDirty(sampler, false);
2105 }
2106 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 }
2108}
2109
2110void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2111{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002112 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002113
2114 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2115 {
2116 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2117 }
2118
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002119 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2120 {
2121 return error(GL_INVALID_OPERATION);
2122 }
2123
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002125
2126 if (!renderTarget)
2127 {
2128 return; // Context must be lost, return silently
2129 }
2130
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131 IDirect3DDevice9 *device = getDevice();
2132
2133 D3DSURFACE_DESC desc;
2134 renderTarget->GetDesc(&desc);
2135
2136 IDirect3DSurface9 *systemSurface;
2137 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2138
2139 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2140 {
2141 return error(GL_OUT_OF_MEMORY);
2142 }
2143
2144 ASSERT(SUCCEEDED(result));
2145
2146 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2147 {
2148 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2149 }
2150
2151 result = device->GetRenderTargetData(renderTarget, systemSurface);
2152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153 if (FAILED(result))
2154 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 systemSurface->Release();
2156
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002157 switch (result)
2158 {
2159 case D3DERR_DRIVERINTERNALERROR:
2160 case D3DERR_DEVICELOST:
2161 return error(GL_OUT_OF_MEMORY);
2162 default:
2163 UNREACHABLE();
2164 return; // No sensible error to generate
2165 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 }
2167
2168 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00002169 RECT rect = {std::max(x, 0),
2170 std::max(y, 0),
2171 std::min(x + width, (int)desc.Width),
2172 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173
2174 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2175
2176 if (FAILED(result))
2177 {
2178 UNREACHABLE();
2179 systemSurface->Release();
2180
2181 return; // No sensible error to generate
2182 }
2183
2184 unsigned char *source = (unsigned char*)lock.pBits;
2185 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002186 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002188 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002189
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 for (int j = 0; j < rect.bottom - rect.top; j++)
2191 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002192 if (desc.Format == D3DFMT_A8R8G8B8 &&
2193 format == GL_BGRA_EXT &&
2194 type == GL_UNSIGNED_BYTE)
2195 {
2196 // Fast path for EXT_read_format_bgra, given
2197 // an RGBA source buffer. Note that buffers with no
2198 // alpha go through the slow path below.
2199 memcpy(dest + j * outputPitch,
2200 source + j * lock.Pitch,
2201 (rect.right - rect.left) * 4);
2202 continue;
2203 }
2204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205 for (int i = 0; i < rect.right - rect.left; i++)
2206 {
2207 float r;
2208 float g;
2209 float b;
2210 float a;
2211
2212 switch (desc.Format)
2213 {
2214 case D3DFMT_R5G6B5:
2215 {
2216 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2217
2218 a = 1.0f;
2219 b = (rgb & 0x001F) * (1.0f / 0x001F);
2220 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2221 r = (rgb & 0xF800) * (1.0f / 0xF800);
2222 }
2223 break;
2224 case D3DFMT_X1R5G5B5:
2225 {
2226 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2227
2228 a = 1.0f;
2229 b = (xrgb & 0x001F) * (1.0f / 0x001F);
2230 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
2231 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
2232 }
2233 break;
2234 case D3DFMT_A1R5G5B5:
2235 {
2236 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
2237
2238 a = (argb & 0x8000) ? 1.0f : 0.0f;
2239 b = (argb & 0x001F) * (1.0f / 0x001F);
2240 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2241 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2242 }
2243 break;
2244 case D3DFMT_A8R8G8B8:
2245 {
2246 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2247
2248 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2249 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2250 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2251 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2252 }
2253 break;
2254 case D3DFMT_X8R8G8B8:
2255 {
2256 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2257
2258 a = 1.0f;
2259 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2260 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2261 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2262 }
2263 break;
2264 case D3DFMT_A2R10G10B10:
2265 {
2266 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
2267
2268 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2269 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2270 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2271 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2272 }
2273 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002274 case D3DFMT_A32B32G32R32F:
2275 {
2276 // float formats in D3D are stored rgba, rather than the other way round
2277 r = *((float*)(source + 16 * i + j * lock.Pitch) + 0);
2278 g = *((float*)(source + 16 * i + j * lock.Pitch) + 1);
2279 b = *((float*)(source + 16 * i + j * lock.Pitch) + 2);
2280 a = *((float*)(source + 16 * i + j * lock.Pitch) + 3);
2281 }
2282 break;
2283 case D3DFMT_A16B16G16R16F:
2284 {
2285 // float formats in D3D are stored rgba, rather than the other way round
2286 float abgr[4];
2287
2288 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * lock.Pitch), 4);
2289
2290 a = abgr[3];
2291 b = abgr[2];
2292 g = abgr[1];
2293 r = abgr[0];
2294 }
2295 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 default:
2297 UNIMPLEMENTED(); // FIXME
2298 UNREACHABLE();
2299 }
2300
2301 switch (format)
2302 {
2303 case GL_RGBA:
2304 switch (type)
2305 {
2306 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002307 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2308 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2309 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2310 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 break;
2312 default: UNREACHABLE();
2313 }
2314 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002315 case GL_BGRA_EXT:
2316 switch (type)
2317 {
2318 case GL_UNSIGNED_BYTE:
2319 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2320 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2321 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2322 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2323 break;
2324 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2325 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2326 // this type is packed as follows:
2327 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2328 // --------------------------------------------------------------------------------
2329 // | 4th | 3rd | 2nd | 1st component |
2330 // --------------------------------------------------------------------------------
2331 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2332 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2333 ((unsigned short)(15 * a + 0.5f) << 12)|
2334 ((unsigned short)(15 * r + 0.5f) << 8) |
2335 ((unsigned short)(15 * g + 0.5f) << 4) |
2336 ((unsigned short)(15 * b + 0.5f) << 0);
2337 break;
2338 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2339 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2340 // this type is packed as follows:
2341 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2342 // --------------------------------------------------------------------------------
2343 // | 4th | 3rd | 2nd | 1st component |
2344 // --------------------------------------------------------------------------------
2345 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2346 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2347 ((unsigned short)( a + 0.5f) << 15) |
2348 ((unsigned short)(31 * r + 0.5f) << 10) |
2349 ((unsigned short)(31 * g + 0.5f) << 5) |
2350 ((unsigned short)(31 * b + 0.5f) << 0);
2351 break;
2352 default: UNREACHABLE();
2353 }
2354 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002355 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002356 switch (type)
2357 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002358 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002359 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2360 ((unsigned short)(31 * b + 0.5f) << 0) |
2361 ((unsigned short)(63 * g + 0.5f) << 5) |
2362 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 break;
2364 default: UNREACHABLE();
2365 }
2366 break;
2367 default: UNREACHABLE();
2368 }
2369 }
2370 }
2371
2372 systemSurface->UnlockRect();
2373
2374 systemSurface->Release();
2375}
2376
2377void Context::clear(GLbitfield mask)
2378{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002379 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002380
2381 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2382 {
2383 error(GL_INVALID_FRAMEBUFFER_OPERATION);
2384
2385 return;
2386 }
2387
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002388 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 IDirect3DDevice9 *device = getDevice();
2390 DWORD flags = 0;
2391
2392 if (mask & GL_COLOR_BUFFER_BIT)
2393 {
2394 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002395
2396 if (framebufferObject->getColorbufferType() != GL_NONE)
2397 {
2398 flags |= D3DCLEAR_TARGET;
2399 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 }
2401
2402 if (mask & GL_DEPTH_BUFFER_BIT)
2403 {
2404 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002405 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 {
2407 flags |= D3DCLEAR_ZBUFFER;
2408 }
2409 }
2410
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411 GLuint stencilUnmasked = 0x0;
2412
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002413 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002416 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002418 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2419 D3DSURFACE_DESC desc;
2420 depthStencil->GetDesc(&desc);
2421
2422 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
2423 stencilUnmasked = (0x1 << stencilSize) - 1;
2424
2425 if (stencilUnmasked != 0x0)
2426 {
2427 flags |= D3DCLEAR_STENCIL;
2428 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 }
2430 }
2431
2432 if (mask != 0)
2433 {
2434 return error(GL_INVALID_VALUE);
2435 }
2436
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002437 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2438 {
2439 return;
2440 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002442 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2443 unorm<8>(mState.colorClearValue.red),
2444 unorm<8>(mState.colorClearValue.green),
2445 unorm<8>(mState.colorClearValue.blue));
2446 float depth = clamp01(mState.depthClearValue);
2447 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448
2449 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2450
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002451 if (!renderTarget)
2452 {
2453 return; // Context must be lost, return silently
2454 }
2455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 D3DSURFACE_DESC desc;
2457 renderTarget->GetDesc(&desc);
2458
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002459 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460
2461 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002462 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002464 !(mState.colorMaskRed && mState.colorMaskGreen &&
2465 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466
2467 if (needMaskedColorClear || needMaskedStencilClear)
2468 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002469 // State which is altered in all paths from this point to the clear call is saved.
2470 // State which is altered in only some paths will be flagged dirty in the case that
2471 // that path is taken.
2472 HRESULT hr;
2473 if (mMaskedClearSavedState == NULL)
2474 {
2475 hr = device->BeginStateBlock();
2476 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2477
2478 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2479 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2480 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2481 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2482 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2483 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2484 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2485 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2486 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2487 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2488 device->SetPixelShader(NULL);
2489 device->SetVertexShader(NULL);
2490 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2491 device->SetStreamSourceFreq(0, 1);
2492
2493 hr = device->EndStateBlock(&mMaskedClearSavedState);
2494 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2495 }
2496
2497 ASSERT(mMaskedClearSavedState != NULL);
2498
2499 if (mMaskedClearSavedState != NULL)
2500 {
2501 hr = mMaskedClearSavedState->Capture();
2502 ASSERT(SUCCEEDED(hr));
2503 }
2504
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2506 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2507 device->SetRenderState(D3DRS_ZENABLE, FALSE);
2508 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2509 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2510 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2511 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2512 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2513
2514 if (flags & D3DCLEAR_TARGET)
2515 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002516 device->SetRenderState(D3DRS_COLORWRITEENABLE, (mState.colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
2517 (mState.colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
2518 (mState.colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
2519 (mState.colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520 }
2521 else
2522 {
2523 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2524 }
2525
2526 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2527 {
2528 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2529 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2530 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2531 device->SetRenderState(D3DRS_STENCILREF, stencil);
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002532 device->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002533 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2535 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002536 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 }
2538 else
2539 {
2540 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2541 }
2542
2543 device->SetPixelShader(NULL);
2544 device->SetVertexShader(NULL);
2545 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002546 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002547
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00002548 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002549 {
2550 float x, y, z, w;
2551 D3DCOLOR diffuse;
2552 };
2553
2554 Vertex quad[4];
2555 quad[0].x = 0.0f;
2556 quad[0].y = (float)desc.Height;
2557 quad[0].z = 0.0f;
2558 quad[0].w = 1.0f;
2559 quad[0].diffuse = color;
2560
2561 quad[1].x = (float)desc.Width;
2562 quad[1].y = (float)desc.Height;
2563 quad[1].z = 0.0f;
2564 quad[1].w = 1.0f;
2565 quad[1].diffuse = color;
2566
2567 quad[2].x = 0.0f;
2568 quad[2].y = 0.0f;
2569 quad[2].z = 0.0f;
2570 quad[2].w = 1.0f;
2571 quad[2].diffuse = color;
2572
2573 quad[3].x = (float)desc.Width;
2574 quad[3].y = 0.0f;
2575 quad[3].z = 0.0f;
2576 quad[3].w = 1.0f;
2577 quad[3].diffuse = color;
2578
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002579 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581
2582 if (flags & D3DCLEAR_ZBUFFER)
2583 {
2584 device->SetRenderState(D3DRS_ZENABLE, TRUE);
2585 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2586 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2587 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002588
2589 if (mMaskedClearSavedState != NULL)
2590 {
2591 mMaskedClearSavedState->Apply();
2592 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002593 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002594 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 {
2596 device->Clear(0, NULL, flags, color, depth, stencil);
2597 }
2598}
2599
2600void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2601{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002602 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603 {
2604 return error(GL_INVALID_OPERATION);
2605 }
2606
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002607 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608 IDirect3DDevice9 *device = getDevice();
2609 D3DPRIMITIVETYPE primitiveType;
2610 int primitiveCount;
2611
2612 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2613 return error(GL_INVALID_ENUM);
2614
2615 if (primitiveCount <= 0)
2616 {
2617 return;
2618 }
2619
2620 if (!applyRenderTarget(false))
2621 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002622 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623 }
2624
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002625 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002626
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002627 TranslatedIndexData indexInfo;
2628 bool useIndexing;
2629 GLenum err = applyVertexBuffer(mode, first, count, &useIndexing, &indexInfo);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002630 if (err != GL_NO_ERROR)
2631 {
2632 return error(err);
2633 }
2634
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635 applyShaders();
2636 applyTextures();
2637
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002638 if (!getCurrentProgram()->validateSamplers())
2639 {
2640 return error(GL_INVALID_OPERATION);
2641 }
2642
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002643 if (!cullSkipsDraw(mode))
2644 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002645 display->startScene();
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002646 if (useIndexing)
2647 {
2648 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/indexInfo.indexSize, primitiveCount);
2649 }
2650 else
2651 {
2652 device->DrawPrimitive(primitiveType, 0, primitiveCount);
2653 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002654 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655}
2656
2657void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
2658{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002659 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660 {
2661 return error(GL_INVALID_OPERATION);
2662 }
2663
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002664 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002665 {
2666 return error(GL_INVALID_OPERATION);
2667 }
2668
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002669 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002670 IDirect3DDevice9 *device = getDevice();
2671 D3DPRIMITIVETYPE primitiveType;
2672 int primitiveCount;
2673
2674 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2675 return error(GL_INVALID_ENUM);
2676
2677 if (primitiveCount <= 0)
2678 {
2679 return;
2680 }
2681
2682 if (!applyRenderTarget(false))
2683 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002684 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685 }
2686
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002687 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002688
2689 TranslatedIndexData indexInfo;
2690 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2691 if (err != GL_NO_ERROR)
2692 {
2693 return error(err);
2694 }
2695
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002696 err = applyVertexBuffer(indexInfo);
2697 if (err != GL_NO_ERROR)
2698 {
2699 return error(err);
2700 }
2701
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702 applyShaders();
2703 applyTextures();
2704
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002705 if (!getCurrentProgram()->validateSamplers())
2706 {
2707 return error(GL_INVALID_OPERATION);
2708 }
2709
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002710 if (!cullSkipsDraw(mode))
2711 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002712 display->startScene();
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00002713 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 +00002714 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715}
2716
2717void Context::finish()
2718{
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002719 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 IDirect3DDevice9 *device = getDevice();
2721 IDirect3DQuery9 *occlusionQuery = NULL;
2722
2723 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
2724
2725 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2726 {
2727 return error(GL_OUT_OF_MEMORY);
2728 }
2729
2730 ASSERT(SUCCEEDED(result));
2731
2732 if (occlusionQuery)
2733 {
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002734 IDirect3DStateBlock9 *savedState = NULL;
2735 device->CreateStateBlock(D3DSBT_ALL, &savedState);
2736
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002737 HRESULT result = occlusionQuery->Issue(D3DISSUE_BEGIN);
2738 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739
2740 // Render something outside the render target
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002741 device->SetStreamSourceFreq(0, 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 device->SetPixelShader(NULL);
2743 device->SetVertexShader(NULL);
2744 device->SetFVF(D3DFVF_XYZRHW);
2745 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
daniel@transgaming.comae072af2010-05-05 18:47:28 +00002746 display->startScene();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002749 result = occlusionQuery->Issue(D3DISSUE_END);
2750 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751
2752 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
2753 {
2754 // Keep polling, but allow other threads to do something useful first
2755 Sleep(0);
2756 }
2757
2758 occlusionQuery->Release();
daniel@transgaming.coma71cdd72010-05-12 16:51:14 +00002759
2760 if (savedState)
2761 {
2762 savedState->Apply();
2763 savedState->Release();
2764 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002765 }
2766}
2767
2768void Context::flush()
2769{
2770 IDirect3DDevice9 *device = getDevice();
2771 IDirect3DQuery9 *eventQuery = NULL;
2772
2773 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
2774
2775 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2776 {
2777 return error(GL_OUT_OF_MEMORY);
2778 }
2779
2780 ASSERT(SUCCEEDED(result));
2781
2782 if (eventQuery)
2783 {
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002784 HRESULT result = eventQuery->Issue(D3DISSUE_END);
2785 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002787 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788 eventQuery->Release();
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002789
2790 if (result == D3DERR_DEVICELOST)
2791 {
2792 error(GL_OUT_OF_MEMORY);
2793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794 }
2795}
2796
2797void Context::recordInvalidEnum()
2798{
2799 mInvalidEnum = true;
2800}
2801
2802void Context::recordInvalidValue()
2803{
2804 mInvalidValue = true;
2805}
2806
2807void Context::recordInvalidOperation()
2808{
2809 mInvalidOperation = true;
2810}
2811
2812void Context::recordOutOfMemory()
2813{
2814 mOutOfMemory = true;
2815}
2816
2817void Context::recordInvalidFramebufferOperation()
2818{
2819 mInvalidFramebufferOperation = true;
2820}
2821
2822// Get one of the recorded errors and clear its flag, if any.
2823// [OpenGL ES 2.0.24] section 2.5 page 13.
2824GLenum Context::getError()
2825{
2826 if (mInvalidEnum)
2827 {
2828 mInvalidEnum = false;
2829
2830 return GL_INVALID_ENUM;
2831 }
2832
2833 if (mInvalidValue)
2834 {
2835 mInvalidValue = false;
2836
2837 return GL_INVALID_VALUE;
2838 }
2839
2840 if (mInvalidOperation)
2841 {
2842 mInvalidOperation = false;
2843
2844 return GL_INVALID_OPERATION;
2845 }
2846
2847 if (mOutOfMemory)
2848 {
2849 mOutOfMemory = false;
2850
2851 return GL_OUT_OF_MEMORY;
2852 }
2853
2854 if (mInvalidFramebufferOperation)
2855 {
2856 mInvalidFramebufferOperation = false;
2857
2858 return GL_INVALID_FRAMEBUFFER_OPERATION;
2859 }
2860
2861 return GL_NO_ERROR;
2862}
2863
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002864bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002865{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00002866 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002867}
2868
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002869int Context::getMaxSupportedSamples() const
2870{
2871 return mMaxSupportedSamples;
2872}
2873
2874int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
2875{
2876 if (requested == 0)
2877 {
2878 return requested;
2879 }
2880
2881 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
2882 if (itr == mMultiSampleSupport.end())
2883 {
2884 return -1;
2885 }
2886
2887 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
2888 {
2889 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
2890 {
2891 return i;
2892 }
2893 }
2894
2895 return -1;
2896}
2897
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00002898bool Context::supportsEventQueries() const
2899{
2900 return mSupportsEventQueries;
2901}
2902
daniel@transgaming.com01868132010-08-24 19:21:17 +00002903bool Context::supportsCompressedTextures() const
2904{
2905 return mSupportsCompressedTextures;
2906}
2907
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002908bool Context::supportsFloatTextures() const
2909{
2910 return mSupportsFloatTextures;
2911}
2912
2913bool Context::supportsFloatLinearFilter() const
2914{
2915 return mSupportsFloatLinearFilter;
2916}
2917
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002918bool Context::supportsFloatRenderableTextures() const
2919{
2920 return mSupportsFloatRenderableTextures;
2921}
2922
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00002923bool Context::supportsHalfFloatTextures() const
2924{
2925 return mSupportsHalfFloatTextures;
2926}
2927
2928bool Context::supportsHalfFloatLinearFilter() const
2929{
2930 return mSupportsHalfFloatLinearFilter;
2931}
2932
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002933bool Context::supportsHalfFloatRenderableTextures() const
2934{
2935 return mSupportsHalfFloatRenderableTextures;
2936}
2937
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002938void Context::detachBuffer(GLuint buffer)
2939{
2940 // [OpenGL ES 2.0.24] section 2.9 page 22:
2941 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2942 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2943
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002944 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002945 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002946 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002947 }
2948
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002949 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002950 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002951 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002952 }
2953
2954 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2955 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002956 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002957 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002958 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002959 }
2960 }
2961}
2962
2963void Context::detachTexture(GLuint texture)
2964{
2965 // [OpenGL ES 2.0.24] section 3.8 page 84:
2966 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2967 // rebound to texture object zero
2968
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002969 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002970 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002971 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002972 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002973 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002974 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00002975 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002976 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002977 }
2978 }
2979
2980 // [OpenGL ES 2.0.24] section 4.4 page 112:
2981 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2982 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2983 // image was attached in the currently bound framebuffer.
2984
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002985 Framebuffer *readFramebuffer = getReadFramebuffer();
2986 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002987
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002988 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002989 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002990 readFramebuffer->detachTexture(texture);
2991 }
2992
2993 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
2994 {
2995 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002996 }
2997}
2998
2999void Context::detachFramebuffer(GLuint framebuffer)
3000{
3001 // [OpenGL ES 2.0.24] section 4.4 page 107:
3002 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3003 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3004
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003005 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003006 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003007 bindReadFramebuffer(0);
3008 }
3009
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003010 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003011 {
3012 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003013 }
3014}
3015
3016void Context::detachRenderbuffer(GLuint renderbuffer)
3017{
3018 // [OpenGL ES 2.0.24] section 4.4 page 109:
3019 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3020 // had been executed with the target RENDERBUFFER and name of zero.
3021
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003022 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003023 {
3024 bindRenderbuffer(0);
3025 }
3026
3027 // [OpenGL ES 2.0.24] section 4.4 page 111:
3028 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3029 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3030 // point to which this image was attached in the currently bound framebuffer.
3031
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003032 Framebuffer *readFramebuffer = getReadFramebuffer();
3033 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003034
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003035 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003036 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003037 readFramebuffer->detachRenderbuffer(renderbuffer);
3038 }
3039
3040 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3041 {
3042 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003043 }
3044}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003045
3046Texture *Context::getIncompleteTexture(SamplerType type)
3047{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003048 Texture *t = mIncompleteTextures[type];
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003049
3050 if (t == NULL)
3051 {
3052 static const GLubyte color[] = { 0, 0, 0, 255 };
3053
3054 switch (type)
3055 {
3056 default:
3057 UNREACHABLE();
3058 // default falls through to SAMPLER_2D
3059
3060 case SAMPLER_2D:
3061 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003062 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003063 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003064 t = incomplete2d;
3065 }
3066 break;
3067
3068 case SAMPLER_CUBE:
3069 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003070 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003071
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00003072 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3073 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3074 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3075 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3076 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3077 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003078
3079 t = incompleteCube;
3080 }
3081 break;
3082 }
3083
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003084 mIncompleteTextures[type] = t;
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003085 }
3086
3087 return t;
3088}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003089
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003090bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003091{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003092 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003093}
3094
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003095bool Context::isTriangleMode(GLenum drawMode)
3096{
3097 switch (drawMode)
3098 {
3099 case GL_TRIANGLES:
3100 case GL_TRIANGLE_FAN:
3101 case GL_TRIANGLE_STRIP:
3102 return true;
3103 case GL_POINTS:
3104 case GL_LINES:
3105 case GL_LINE_LOOP:
3106 case GL_LINE_STRIP:
3107 return false;
3108 default: UNREACHABLE();
3109 }
3110
3111 return false;
3112}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003113
3114void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3115{
3116 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3117
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003118 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3119 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3120 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3121 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003122
3123 mVertexDataManager->dirtyCurrentValues();
3124}
3125
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003126void Context::initExtensionString()
3127{
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003128 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00003129 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3130 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003131 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003132 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003133 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003134
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003135 if (supportsEventQueries())
3136 {
3137 mExtensionString += "GL_NV_fence ";
3138 }
3139
daniel@transgaming.com01868132010-08-24 19:21:17 +00003140 if (supportsCompressedTextures())
3141 {
3142 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3143 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003144
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003145 if (supportsFloatTextures())
3146 {
3147 mExtensionString += "GL_OES_texture_float ";
3148 }
3149
3150 if (supportsHalfFloatTextures())
3151 {
3152 mExtensionString += "GL_OES_texture_half_float ";
3153 }
3154
3155 if (supportsFloatLinearFilter())
3156 {
3157 mExtensionString += "GL_OES_texture_float_linear ";
3158 }
3159
3160 if (supportsHalfFloatLinearFilter())
3161 {
3162 mExtensionString += "GL_OES_texture_half_float_linear ";
3163 }
3164
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003165 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003166 {
3167 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3168 }
3169
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003170 if (mBufferBackEnd->supportIntIndices())
3171 {
3172 mExtensionString += "GL_OES_element_index_uint ";
3173 }
3174
3175 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3176 if (end != std::string::npos)
3177 {
3178 mExtensionString.resize(end+1);
3179 }
3180}
3181
3182const char *Context::getExtensionString() const
3183{
3184 return mExtensionString.c_str();
3185}
3186
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003187void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3188 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3189 GLbitfield mask)
3190{
3191 IDirect3DDevice9 *device = getDevice();
3192
3193 Framebuffer *readFramebuffer = getReadFramebuffer();
3194 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3195
3196 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3197 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3198 {
3199 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3200 }
3201
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003202 if (drawFramebuffer->getSamples() != 0)
3203 {
3204 return error(GL_INVALID_OPERATION);
3205 }
3206
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003207 RECT sourceRect;
3208 RECT destRect;
3209
3210 if (srcX0 < srcX1)
3211 {
3212 sourceRect.left = srcX0;
3213 sourceRect.right = srcX1;
3214 destRect.left = dstX0;
3215 destRect.right = dstX1;
3216 }
3217 else
3218 {
3219 sourceRect.left = srcX1;
3220 destRect.left = dstX1;
3221 sourceRect.right = srcX0;
3222 destRect.right = dstX0;
3223 }
3224
3225 // Arguments to StretchRect must be in D3D-style (0-top) coordinates, so we must
3226 // flip our Y-values here
3227 if (srcY0 < srcY1)
3228 {
3229 sourceRect.bottom = srcY1;
3230 destRect.bottom = dstY1;
3231 sourceRect.top = srcY0;
3232 destRect.top = dstY0;
3233 }
3234 else
3235 {
3236 sourceRect.bottom = srcY0;
3237 destRect.bottom = dstY0;
3238 sourceRect.top = srcY1;
3239 destRect.top = dstY1;
3240 }
3241
3242 RECT sourceScissoredRect = sourceRect;
3243 RECT destScissoredRect = destRect;
3244
3245 if (mState.scissorTest)
3246 {
3247 // Only write to parts of the destination framebuffer which pass the scissor test
3248 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3249 // rect will be checked against scissorY, rather than the bottom.
3250 if (destRect.left < mState.scissorX)
3251 {
3252 int xDiff = mState.scissorX - destRect.left;
3253 destScissoredRect.left = mState.scissorX;
3254 sourceScissoredRect.left += xDiff;
3255 }
3256
3257 if (destRect.right > mState.scissorX + mState.scissorWidth)
3258 {
3259 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3260 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3261 sourceScissoredRect.right -= xDiff;
3262 }
3263
3264 if (destRect.top < mState.scissorY)
3265 {
3266 int yDiff = mState.scissorY - destRect.top;
3267 destScissoredRect.top = mState.scissorY;
3268 sourceScissoredRect.top += yDiff;
3269 }
3270
3271 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3272 {
3273 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3274 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3275 sourceScissoredRect.bottom -= yDiff;
3276 }
3277 }
3278
3279 bool blitRenderTarget = false;
3280 bool blitDepthStencil = false;
3281
3282 RECT sourceTrimmedRect = sourceScissoredRect;
3283 RECT destTrimmedRect = destScissoredRect;
3284
3285 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3286 // the actual draw and read surfaces.
3287 if (sourceTrimmedRect.left < 0)
3288 {
3289 int xDiff = 0 - sourceTrimmedRect.left;
3290 sourceTrimmedRect.left = 0;
3291 destTrimmedRect.left += xDiff;
3292 }
3293
3294 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3295 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3296 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3297 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3298
3299 if (sourceTrimmedRect.right > readBufferWidth)
3300 {
3301 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3302 sourceTrimmedRect.right = readBufferWidth;
3303 destTrimmedRect.right -= xDiff;
3304 }
3305
3306 if (sourceTrimmedRect.top < 0)
3307 {
3308 int yDiff = 0 - sourceTrimmedRect.top;
3309 sourceTrimmedRect.top = 0;
3310 destTrimmedRect.top += yDiff;
3311 }
3312
3313 if (sourceTrimmedRect.bottom > readBufferHeight)
3314 {
3315 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3316 sourceTrimmedRect.bottom = readBufferHeight;
3317 destTrimmedRect.bottom -= yDiff;
3318 }
3319
3320 if (destTrimmedRect.left < 0)
3321 {
3322 int xDiff = 0 - destTrimmedRect.left;
3323 destTrimmedRect.left = 0;
3324 sourceTrimmedRect.left += xDiff;
3325 }
3326
3327 if (destTrimmedRect.right > drawBufferWidth)
3328 {
3329 int xDiff = destTrimmedRect.right - drawBufferWidth;
3330 destTrimmedRect.right = drawBufferWidth;
3331 sourceTrimmedRect.right -= xDiff;
3332 }
3333
3334 if (destTrimmedRect.top < 0)
3335 {
3336 int yDiff = 0 - destTrimmedRect.top;
3337 destTrimmedRect.top = 0;
3338 sourceTrimmedRect.top += yDiff;
3339 }
3340
3341 if (destTrimmedRect.bottom > drawBufferHeight)
3342 {
3343 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3344 destTrimmedRect.bottom = drawBufferHeight;
3345 sourceTrimmedRect.bottom -= yDiff;
3346 }
3347
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003348 bool partialBufferCopy = false;
3349 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readFramebuffer->getColorbuffer()->getHeight() ||
3350 sourceTrimmedRect.right - sourceTrimmedRect.left < readFramebuffer->getColorbuffer()->getWidth() ||
3351 destTrimmedRect.bottom - destTrimmedRect.top < drawFramebuffer->getColorbuffer()->getHeight() ||
3352 destTrimmedRect.right - destTrimmedRect.left < drawFramebuffer->getColorbuffer()->getWidth() ||
3353 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3354 {
3355 partialBufferCopy = true;
3356 }
3357
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003358 if (mask & GL_COLOR_BUFFER_BIT)
3359 {
3360 if (readFramebuffer->getColorbufferType() != drawFramebuffer->getColorbufferType() ||
3361 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3362 {
3363 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3364 return error(GL_INVALID_OPERATION);
3365 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003366
3367 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3368 {
3369 return error(GL_INVALID_OPERATION);
3370 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003371
3372 blitRenderTarget = true;
3373
3374 }
3375
3376 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3377 {
3378 DepthStencilbuffer *readDSBuffer = NULL;
3379 DepthStencilbuffer *drawDSBuffer = NULL;
3380
3381 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3382 // both a depth and stencil buffer, it will be the same buffer.
3383
3384 if (mask & GL_DEPTH_BUFFER_BIT)
3385 {
3386 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3387 {
3388 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3389 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3390 {
3391 return error(GL_INVALID_OPERATION);
3392 }
3393
3394 blitDepthStencil = true;
3395 readDSBuffer = readFramebuffer->getDepthbuffer();
3396 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3397 }
3398 }
3399
3400 if (mask & GL_STENCIL_BUFFER_BIT)
3401 {
3402 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3403 {
3404 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3405 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3406 {
3407 return error(GL_INVALID_OPERATION);
3408 }
3409
3410 blitDepthStencil = true;
3411 readDSBuffer = readFramebuffer->getStencilbuffer();
3412 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3413 }
3414 }
3415
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003416 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003417 {
3418 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3419 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3420 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003421
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003422 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3423 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003424 {
3425 return error(GL_INVALID_OPERATION);
3426 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003427 }
3428
3429 if (blitRenderTarget || blitDepthStencil)
3430 {
3431 egl::Display *display = getDisplay();
3432 display->endScene();
3433
3434 if (blitRenderTarget)
3435 {
3436 HRESULT result = device->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
3437 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3438
3439 if (FAILED(result))
3440 {
3441 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3442 return;
3443 }
3444 }
3445
3446 if (blitDepthStencil)
3447 {
3448 HRESULT result = device->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
3449
3450 if (FAILED(result))
3451 {
3452 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3453 return;
3454 }
3455 }
3456 }
3457}
3458
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003459}
3460
3461extern "C"
3462{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003463gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003464{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003465 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003466}
3467
3468void glDestroyContext(gl::Context *context)
3469{
3470 delete context;
3471
3472 if (context == gl::getContext())
3473 {
3474 gl::makeCurrent(NULL, NULL, NULL);
3475 }
3476}
3477
3478void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3479{
3480 gl::makeCurrent(context, display, surface);
3481}
3482
3483gl::Context *glGetCurrentContext()
3484{
3485 return gl::getContext();
3486}
3487}