blob: 18335bc0daed6bbf9faaa0f584ec1f0254313237 [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.comfab5a1a2010-03-11 19:22:30 +000010#include "Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014#include "main.h"
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000015#include "libEGL/Display.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016#include "Buffer.h"
17#include "Shader.h"
18#include "Program.h"
19#include "Texture.h"
20#include "FrameBuffer.h"
21#include "RenderBuffer.h"
22#include "mathutil.h"
23#include "utilities.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000024#include "geometry/backend.h"
25#include "geometry/VertexDataManager.h"
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +000026#include "geometry/IndexDataManager.h"
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000027#include "geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028
daniel@transgaming.com86487c22010-03-11 19:41:43 +000029#undef near
30#undef far
31
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032namespace gl
33{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000034Context::Context(const egl::Config *config)
35 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036{
37 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
38 depthClearValue = 1.0f;
39 stencilClearValue = 0;
40
41 cullFace = false;
42 cullMode = GL_BACK;
43 frontFace = GL_CCW;
44 depthTest = false;
45 depthFunc = GL_LESS;
46 blend = false;
47 sourceBlendRGB = GL_ONE;
48 sourceBlendAlpha = GL_ONE;
49 destBlendRGB = GL_ZERO;
50 destBlendAlpha = GL_ZERO;
51 blendEquationRGB = GL_FUNC_ADD;
52 blendEquationAlpha = GL_FUNC_ADD;
53 blendColor.red = 0;
54 blendColor.green = 0;
55 blendColor.blue = 0;
56 blendColor.alpha = 0;
57 stencilTest = false;
58 stencilFunc = GL_ALWAYS;
59 stencilRef = 0;
60 stencilMask = -1;
61 stencilWritemask = -1;
62 stencilBackFunc = GL_ALWAYS;
63 stencilBackRef = 0;
64 stencilBackMask = - 1;
65 stencilBackWritemask = -1;
66 stencilFail = GL_KEEP;
67 stencilPassDepthFail = GL_KEEP;
68 stencilPassDepthPass = GL_KEEP;
69 stencilBackFail = GL_KEEP;
70 stencilBackPassDepthFail = GL_KEEP;
71 stencilBackPassDepthPass = GL_KEEP;
72 polygonOffsetFill = false;
daniel@transgaming.com777f2672010-04-07 03:25:16 +000073 polygonOffsetFactor = 0.0f;
74 polygonOffsetUnits = 0.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075 sampleAlphaToCoverage = false;
76 sampleCoverage = false;
77 sampleCoverageValue = 1.0f;
78 sampleCoverageInvert = GL_FALSE;
79 scissorTest = false;
80 dither = true;
daniel@transgaming.com5949aa12010-03-21 04:31:15 +000081 generateMipmapHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000082
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000083 lineWidth = 1.0f;
84
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085 viewportX = 0;
86 viewportY = 0;
87 viewportWidth = config->mDisplayMode.Width;
88 viewportHeight = config->mDisplayMode.Height;
89 zNear = 0.0f;
90 zFar = 1.0f;
91
92 scissorX = 0;
93 scissorY = 0;
94 scissorWidth = config->mDisplayMode.Width;
95 scissorHeight = config->mDisplayMode.Height;
96
97 colorMaskRed = true;
98 colorMaskGreen = true;
99 colorMaskBlue = true;
100 colorMaskAlpha = true;
101 depthMask = true;
102
103 // [OpenGL ES 2.0.24] section 3.7 page 83:
104 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
105 // and cube map texture state vectors respectively associated with them.
106 // In order that access to these initial textures not be lost, they are treated as texture
107 // objects all of whose names are 0.
108
109 mTexture2DZero = new Texture2D();
110 mTextureCubeMapZero = new TextureCubeMap();
111
112 mColorbufferZero = NULL;
113 mDepthbufferZero = NULL;
114 mStencilbufferZero = NULL;
115
116 activeSampler = 0;
117 arrayBuffer = 0;
118 elementArrayBuffer = 0;
119 bindTextureCubeMap(0);
120 bindTexture2D(0);
121 bindFramebuffer(0);
122 bindRenderbuffer(0);
123
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000124 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000126 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
127 {
128 samplerTexture[type][sampler] = 0;
129 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130 }
131
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000132 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
133 {
134 mIncompleteTextures[type] = NULL;
135 }
136
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 currentProgram = 0;
138
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000139 packAlignment = 4;
140 unpackAlignment = 4;
141
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000142 mBufferBackEnd = NULL;
143 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000144 mIndexDataManager = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000145
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146 mInvalidEnum = false;
147 mInvalidValue = false;
148 mInvalidOperation = false;
149 mOutOfMemory = false;
150 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000151
152 mHasBeenCurrent = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153}
154
155Context::~Context()
156{
157 currentProgram = 0;
158
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000159 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
160 {
161 delete mIncompleteTextures[type];
162 }
163
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164 delete mTexture2DZero;
165 delete mTextureCubeMapZero;
166
167 delete mColorbufferZero;
168 delete mDepthbufferZero;
169 delete mStencilbufferZero;
170
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000171 delete mBufferBackEnd;
172 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000173 delete mIndexDataManager;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000174
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175 while (!mBufferMap.empty())
176 {
177 deleteBuffer(mBufferMap.begin()->first);
178 }
179
180 while (!mProgramMap.empty())
181 {
182 deleteProgram(mProgramMap.begin()->first);
183 }
184
185 while (!mShaderMap.empty())
186 {
187 deleteShader(mShaderMap.begin()->first);
188 }
189
190 while (!mFramebufferMap.empty())
191 {
192 deleteFramebuffer(mFramebufferMap.begin()->first);
193 }
194
195 while (!mRenderbufferMap.empty())
196 {
197 deleteRenderbuffer(mRenderbufferMap.begin()->first);
198 }
199
200 while (!mTextureMap.empty())
201 {
202 deleteTexture(mTextureMap.begin()->first);
203 }
204}
205
206void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
207{
208 IDirect3DDevice9 *device = display->getDevice();
209
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000210 if (!mBufferBackEnd)
211 {
212 mBufferBackEnd = new Dx9BackEnd(device);
213 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000214 mIndexDataManager = new IndexDataManager(this, mBufferBackEnd);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000215 }
216
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
218 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000219 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220
221 Framebuffer *framebufferZero = new Framebuffer();
222 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000223 Depthbuffer *depthbufferZero = new Depthbuffer(depthStencil);
224 Stencilbuffer *stencilbufferZero = new Stencilbuffer(depthStencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225
226 setFramebufferZero(framebufferZero);
227 setColorbufferZero(colorbufferZero);
228 setDepthbufferZero(depthbufferZero);
229 setStencilbufferZero(stencilbufferZero);
230
231 framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0);
232 framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0);
233 framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0);
234
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000235 if(!mHasBeenCurrent)
236 {
237 viewportX = 0;
238 viewportY = 0;
239 viewportWidth = surface->getWidth();
240 viewportHeight = surface->getHeight();
241
242 scissorX = 0;
243 scissorY = 0;
244 scissorWidth = surface->getWidth();
245 scissorHeight = surface->getHeight();
246
247 mHasBeenCurrent = true;
248 }
249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250 defaultRenderTarget->Release();
251
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000252 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000254 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000256
257 D3DCAPS9 capabilities;
258 device->GetDeviceCaps(&capabilities);
259
260 if (capabilities.PixelShaderVersion == D3DPS_VERSION(3, 0))
261 {
262 mPsProfile = "ps_3_0";
263 mVsProfile = "vs_3_0";
264 }
265 else // egl::Display guarantees support for at least 2.0
266 {
267 mPsProfile = "ps_2_0";
268 mVsProfile = "vs_2_0";
269 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270}
271
272void Context::setClearColor(float red, float green, float blue, float alpha)
273{
274 colorClearValue.red = red;
275 colorClearValue.green = green;
276 colorClearValue.blue = blue;
277 colorClearValue.alpha = alpha;
278}
279
280void Context::setClearDepth(float depth)
281{
282 depthClearValue = depth;
283}
284
285void Context::setClearStencil(int stencil)
286{
287 stencilClearValue = stencil;
288}
289
290// Returns an unused buffer name
291GLuint Context::createBuffer()
292{
293 unsigned int handle = 1;
294
295 while (mBufferMap.find(handle) != mBufferMap.end())
296 {
297 handle++;
298 }
299
300 mBufferMap[handle] = NULL;
301
302 return handle;
303}
304
305// Returns an unused shader/program name
306GLuint Context::createShader(GLenum type)
307{
308 unsigned int handle = 1;
309
310 while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space
311 {
312 handle++;
313 }
314
315 if (type == GL_VERTEX_SHADER)
316 {
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000317 mShaderMap[handle] = new VertexShader(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318 }
319 else if (type == GL_FRAGMENT_SHADER)
320 {
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000321 mShaderMap[handle] = new FragmentShader(handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 }
323 else UNREACHABLE();
324
325 return handle;
326}
327
328// Returns an unused program/shader name
329GLuint Context::createProgram()
330{
331 unsigned int handle = 1;
332
333 while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space
334 {
335 handle++;
336 }
337
338 mProgramMap[handle] = new Program();
339
340 return handle;
341}
342
343// Returns an unused texture name
344GLuint Context::createTexture()
345{
346 unsigned int handle = 1;
347
348 while (mTextureMap.find(handle) != mTextureMap.end())
349 {
350 handle++;
351 }
352
353 mTextureMap[handle] = NULL;
354
355 return handle;
356}
357
358// Returns an unused framebuffer name
359GLuint Context::createFramebuffer()
360{
361 unsigned int handle = 1;
362
363 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
364 {
365 handle++;
366 }
367
368 mFramebufferMap[handle] = NULL;
369
370 return handle;
371}
372
373// Returns an unused renderbuffer name
374GLuint Context::createRenderbuffer()
375{
376 unsigned int handle = 1;
377
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000378 while (mRenderbufferMap.find(handle) != mRenderbufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000379 {
380 handle++;
381 }
382
383 mRenderbufferMap[handle] = NULL;
384
385 return handle;
386}
387
388void Context::deleteBuffer(GLuint buffer)
389{
390 BufferMap::iterator bufferObject = mBufferMap.find(buffer);
391
392 if (bufferObject != mBufferMap.end())
393 {
394 detachBuffer(buffer);
395
396 delete bufferObject->second;
397 mBufferMap.erase(bufferObject);
398 }
399}
400
401void Context::deleteShader(GLuint shader)
402{
403 ShaderMap::iterator shaderObject = mShaderMap.find(shader);
404
405 if (shaderObject != mShaderMap.end())
406 {
407 if (!shaderObject->second->isAttached())
408 {
409 delete shaderObject->second;
410 mShaderMap.erase(shaderObject);
411 }
412 else
413 {
414 shaderObject->second->flagForDeletion();
415 }
416 }
417}
418
419void Context::deleteProgram(GLuint program)
420{
421 ProgramMap::iterator programObject = mProgramMap.find(program);
422
423 if (programObject != mProgramMap.end())
424 {
425 if (program != currentProgram)
426 {
427 delete programObject->second;
428 mProgramMap.erase(programObject);
429 }
430 else
431 {
432 programObject->second->flagForDeletion();
433 }
434 }
435}
436
437void Context::deleteTexture(GLuint texture)
438{
439 TextureMap::iterator textureObject = mTextureMap.find(texture);
440
441 if (textureObject != mTextureMap.end())
442 {
443 detachTexture(texture);
444
445 if (texture != 0)
446 {
447 delete textureObject->second;
448 }
449
450 mTextureMap.erase(textureObject);
451 }
452}
453
454void Context::deleteFramebuffer(GLuint framebuffer)
455{
456 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
457
458 if (framebufferObject != mFramebufferMap.end())
459 {
460 detachFramebuffer(framebuffer);
461
462 delete framebufferObject->second;
463 mFramebufferMap.erase(framebufferObject);
464 }
465}
466
467void Context::deleteRenderbuffer(GLuint renderbuffer)
468{
469 RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
470
471 if (renderbufferObject != mRenderbufferMap.end())
472 {
473 detachRenderbuffer(renderbuffer);
474
475 delete renderbufferObject->second;
476 mRenderbufferMap.erase(renderbufferObject);
477 }
478}
479
480void Context::bindArrayBuffer(unsigned int buffer)
481{
482 if (buffer != 0 && !getBuffer(buffer))
483 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000484 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485 }
486
487 arrayBuffer = buffer;
488}
489
490void Context::bindElementArrayBuffer(unsigned int buffer)
491{
492 if (buffer != 0 && !getBuffer(buffer))
493 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000494 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495 }
496
497 elementArrayBuffer = buffer;
498}
499
500void Context::bindTexture2D(GLuint texture)
501{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000502 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503 {
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000504 mTextureMap[texture] = new Texture2D();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505 }
506
507 texture2D = texture;
508
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000509 samplerTexture[SAMPLER_2D][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510}
511
512void Context::bindTextureCubeMap(GLuint texture)
513{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000514 if (!getTexture(texture) && texture != 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515 {
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000516 mTextureMap[texture] = new TextureCubeMap();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517 }
518
519 textureCubeMap = texture;
520
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000521 samplerTexture[SAMPLER_CUBE][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522}
523
524void Context::bindFramebuffer(GLuint framebuffer)
525{
526 if (!getFramebuffer(framebuffer))
527 {
528 mFramebufferMap[framebuffer] = new Framebuffer();
529 }
530
531 this->framebuffer = framebuffer;
532}
533
534void Context::bindRenderbuffer(GLuint renderbuffer)
535{
536 if (renderbuffer != 0 && !getRenderbuffer(renderbuffer))
537 {
538 mRenderbufferMap[renderbuffer] = new Renderbuffer();
539 }
540
541 this->renderbuffer = renderbuffer;
542}
543
544void Context::useProgram(GLuint program)
545{
546 Program *programObject = getCurrentProgram();
547
548 if (programObject && programObject->isFlaggedForDeletion())
549 {
550 deleteProgram(currentProgram);
551 }
552
553 currentProgram = program;
554}
555
556void Context::setFramebufferZero(Framebuffer *buffer)
557{
558 delete mFramebufferMap[0];
559 mFramebufferMap[0] = buffer;
560}
561
562void Context::setColorbufferZero(Colorbuffer *buffer)
563{
564 delete mColorbufferZero;
565 mColorbufferZero = buffer;
566}
567
568void Context::setDepthbufferZero(Depthbuffer *buffer)
569{
570 delete mDepthbufferZero;
571 mDepthbufferZero = buffer;
572}
573
574void Context::setStencilbufferZero(Stencilbuffer *buffer)
575{
576 delete mStencilbufferZero;
577 mStencilbufferZero = buffer;
578}
579
580void Context::setRenderbuffer(Renderbuffer *buffer)
581{
582 delete mRenderbufferMap[renderbuffer];
583 mRenderbufferMap[renderbuffer] = buffer;
584}
585
586Buffer *Context::getBuffer(unsigned int handle)
587{
588 BufferMap::iterator buffer = mBufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000589
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590 if (buffer == mBufferMap.end())
591 {
592 return NULL;
593 }
594 else
595 {
596 return buffer->second;
597 }
598}
599
600Shader *Context::getShader(unsigned int handle)
601{
602 ShaderMap::iterator shader = mShaderMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000603
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604 if (shader == mShaderMap.end())
605 {
606 return NULL;
607 }
608 else
609 {
610 return shader->second;
611 }
612}
613
614Program *Context::getProgram(unsigned int handle)
615{
616 ProgramMap::iterator program = mProgramMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000617
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618 if (program == mProgramMap.end())
619 {
620 return NULL;
621 }
622 else
623 {
624 return program->second;
625 }
626}
627
628Texture *Context::getTexture(unsigned int handle)
629{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000630 if (handle == 0) return NULL;
631
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632 TextureMap::iterator texture = mTextureMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000633
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634 if (texture == mTextureMap.end())
635 {
636 return NULL;
637 }
638 else
639 {
640 return texture->second;
641 }
642}
643
644Framebuffer *Context::getFramebuffer(unsigned int handle)
645{
646 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000647
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648 if (framebuffer == mFramebufferMap.end())
649 {
650 return NULL;
651 }
652 else
653 {
654 return framebuffer->second;
655 }
656}
657
658Renderbuffer *Context::getRenderbuffer(unsigned int handle)
659{
660 RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000661
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662 if (renderbuffer == mRenderbufferMap.end())
663 {
664 return NULL;
665 }
666 else
667 {
668 return renderbuffer->second;
669 }
670}
671
672Colorbuffer *Context::getColorbuffer(GLuint handle)
673{
674 if (handle != 0)
675 {
676 Renderbuffer *renderbuffer = getRenderbuffer(handle);
677
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000678 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679 {
680 return static_cast<Colorbuffer*>(renderbuffer);
681 }
682 }
683 else // Special case: 0 refers to different initial render targets based on the attachment type
684 {
685 return mColorbufferZero;
686 }
687
688 return NULL;
689}
690
691Depthbuffer *Context::getDepthbuffer(GLuint handle)
692{
693 if (handle != 0)
694 {
695 Renderbuffer *renderbuffer = getRenderbuffer(handle);
696
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000697 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698 {
699 return static_cast<Depthbuffer*>(renderbuffer);
700 }
701 }
702 else // Special case: 0 refers to different initial render targets based on the attachment type
703 {
704 return mDepthbufferZero;
705 }
706
707 return NULL;
708}
709
710Stencilbuffer *Context::getStencilbuffer(GLuint handle)
711{
712 if (handle != 0)
713 {
714 Renderbuffer *renderbuffer = getRenderbuffer(handle);
715
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000716 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000717 {
718 return static_cast<Stencilbuffer*>(renderbuffer);
719 }
720 }
721 else
722 {
723 return mStencilbufferZero;
724 }
725
726 return NULL;
727}
728
729Buffer *Context::getArrayBuffer()
730{
731 return getBuffer(arrayBuffer);
732}
733
734Buffer *Context::getElementArrayBuffer()
735{
736 return getBuffer(elementArrayBuffer);
737}
738
739Program *Context::getCurrentProgram()
740{
741 return getProgram(currentProgram);
742}
743
744Texture2D *Context::getTexture2D()
745{
746 if (texture2D == 0) // Special case: 0 refers to different initial textures based on the target
747 {
748 return mTexture2DZero;
749 }
750
751 return (Texture2D*)getTexture(texture2D);
752}
753
754TextureCubeMap *Context::getTextureCubeMap()
755{
756 if (textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
757 {
758 return mTextureCubeMapZero;
759 }
760
761 return (TextureCubeMap*)getTexture(textureCubeMap);
762}
763
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000764Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765{
daniel@transgaming.com4195fc42010-04-08 03:51:09 +0000766 GLuint texid = samplerTexture[type][sampler];
767
768 if (texid == 0)
769 {
770 switch (type)
771 {
772 default: UNREACHABLE();
773 case SAMPLER_2D: return mTexture2DZero;
774 case SAMPLER_CUBE: return mTextureCubeMapZero;
775 }
776 }
777
778 return getTexture(texid);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779}
780
781Framebuffer *Context::getFramebuffer()
782{
783 return getFramebuffer(framebuffer);
784}
785
daniel@transgaming.com777f2672010-04-07 03:25:16 +0000786bool Context::getBooleanv(GLenum pname, GLboolean *params)
787{
788 switch (pname)
789 {
790 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
791 case GL_SAMPLE_COVERAGE_INVERT: *params = sampleCoverageInvert; break;
792 case GL_DEPTH_WRITEMASK: *params = depthMask; break;
793 case GL_COLOR_WRITEMASK:
794 params[0] = colorMaskRed;
795 params[1] = colorMaskGreen;
796 params[2] = colorMaskBlue;
797 params[3] = colorMaskAlpha;
798 break;
799 default:
800 return false;
801 }
802
803 return true;
804}
805
806bool Context::getFloatv(GLenum pname, GLfloat *params)
807{
808 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
809 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
810 // GetIntegerv as its native query function. As it would require conversion in any
811 // case, this should make no difference to the calling application.
812 switch (pname)
813 {
814 case GL_LINE_WIDTH: *params = lineWidth; break;
815 case GL_SAMPLE_COVERAGE_VALUE: *params = sampleCoverageValue; break;
816 case GL_DEPTH_CLEAR_VALUE: *params = depthClearValue; break;
817 case GL_POLYGON_OFFSET_FACTOR: *params = polygonOffsetFactor; break;
818 case GL_POLYGON_OFFSET_UNITS: *params = polygonOffsetUnits; break;
819 case GL_ALIASED_LINE_WIDTH_RANGE:
820 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
821 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
822 break;
823 case GL_ALIASED_POINT_SIZE_RANGE:
824 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
825 params[1] = gl::ALIASED_POINT_SIZE_RANGE_MAX;
826 break;
827 case GL_DEPTH_RANGE:
828 params[0] = zNear;
829 params[1] = zFar;
830 break;
831 case GL_COLOR_CLEAR_VALUE:
832 params[0] = colorClearValue.red;
833 params[1] = colorClearValue.green;
834 params[2] = colorClearValue.blue;
835 params[3] = colorClearValue.alpha;
836 break;
837 default:
838 return false;
839 }
840
841 return true;
842}
843
844bool Context::getIntegerv(GLenum pname, GLint *params)
845{
846 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
847 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
848 // GetIntegerv as its native query function. As it would require conversion in any
849 // case, this should make no difference to the calling application. You may find it in
850 // Context::getFloatv.
851 switch (pname)
852 {
853 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
854 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
855 case GL_MAX_VARYING_VECTORS: *params = gl::MAX_VARYING_VECTORS; break;
856 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = gl::MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
857 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
858 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
859 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = gl::MAX_FRAGMENT_UNIFORM_VECTORS; break;
860 case GL_MAX_RENDERBUFFER_SIZE: *params = gl::MAX_RENDERBUFFER_SIZE; break;
861 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
862 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = 0; break;
863 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */ break;
864 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
865 case GL_ARRAY_BUFFER_BINDING: *params = arrayBuffer; break;
866 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = elementArrayBuffer; break;
867 case GL_FRAMEBUFFER_BINDING: *params = framebuffer; break;
868 case GL_RENDERBUFFER_BINDING: *params = renderbuffer; break;
869 case GL_CURRENT_PROGRAM: *params = currentProgram; break;
870 case GL_PACK_ALIGNMENT: *params = packAlignment; break;
871 case GL_UNPACK_ALIGNMENT: *params = unpackAlignment; break;
872 case GL_GENERATE_MIPMAP_HINT: *params = generateMipmapHint; break;
873 case GL_ACTIVE_TEXTURE: *params = activeSampler; break;
874 case GL_STENCIL_FUNC: *params = stencilFunc; break;
875 case GL_STENCIL_REF: *params = stencilRef; break;
876 case GL_STENCIL_VALUE_MASK: *params = stencilMask; break;
877 case GL_STENCIL_BACK_FUNC: *params = stencilBackFunc; break;
878 case GL_STENCIL_BACK_REF: *params = stencilBackRef; break;
879 case GL_STENCIL_BACK_VALUE_MASK: *params = stencilBackMask; break;
880 case GL_STENCIL_FAIL: *params = stencilFail; break;
881 case GL_STENCIL_PASS_DEPTH_FAIL: *params = stencilPassDepthFail; break;
882 case GL_STENCIL_PASS_DEPTH_PASS: *params = stencilPassDepthPass; break;
883 case GL_STENCIL_BACK_FAIL: *params = stencilBackFail; break;
884 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = stencilBackPassDepthFail; break;
885 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = stencilBackPassDepthPass; break;
886 case GL_DEPTH_FUNC: *params = depthFunc; break;
887 case GL_BLEND_SRC_RGB: *params = sourceBlendRGB; break;
888 case GL_BLEND_SRC_ALPHA: *params = sourceBlendAlpha; break;
889 case GL_BLEND_DST_RGB: *params = destBlendRGB; break;
890 case GL_BLEND_DST_ALPHA: *params = destBlendAlpha; break;
891 case GL_BLEND_EQUATION_RGB: *params = blendEquationRGB; break;
892 case GL_BLEND_EQUATION_ALPHA: *params = blendEquationAlpha; break;
893 case GL_STENCIL_WRITEMASK: *params = stencilWritemask; break;
894 case GL_STENCIL_BACK_WRITEMASK: *params = stencilBackWritemask; break;
895 case GL_STENCIL_CLEAR_VALUE: *params = stencilClearValue; break;
896 case GL_SUBPIXEL_BITS: *params = 4; break;
897 case GL_MAX_TEXTURE_SIZE: *params = gl::MAX_TEXTURE_SIZE; break;
898 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = gl::MAX_CUBE_MAP_TEXTURE_SIZE; break;
899 case GL_SAMPLE_BUFFERS: *params = 0; break;
900 case GL_SAMPLES: *params = 0; break;
901 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
902 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
903 case GL_MAX_VIEWPORT_DIMS:
904 {
905 int maxDimension = std::max((int)gl::MAX_RENDERBUFFER_SIZE, (int)gl::MAX_TEXTURE_SIZE);
906 params[0] = maxDimension;
907 params[1] = maxDimension;
908 }
909 break;
910 case GL_VIEWPORT:
911 params[0] = viewportX;
912 params[1] = viewportY;
913 params[2] = viewportWidth;
914 params[3] = viewportHeight;
915 break;
916 case GL_SCISSOR_BOX:
917 params[0] = scissorX;
918 params[1] = scissorY;
919 params[2] = scissorWidth;
920 params[3] = scissorHeight;
921 break;
922 case GL_CULL_FACE_MODE: *params = cullMode; break;
923 case GL_FRONT_FACE: *params = frontFace; break;
924 case GL_RED_BITS:
925 case GL_GREEN_BITS:
926 case GL_BLUE_BITS:
927 case GL_ALPHA_BITS:
928 {
929 gl::Framebuffer *framebuffer = getFramebuffer();
930 gl::Colorbuffer *colorbuffer = framebuffer->getColorbuffer();
931
932 if (colorbuffer)
933 {
934 switch (pname)
935 {
936 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
937 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
938 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
939 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
940 }
941 }
942 else
943 {
944 *params = 0;
945 }
946 }
947 break;
948 case GL_DEPTH_BITS:
949 {
950 gl::Framebuffer *framebuffer = getFramebuffer();
951 gl::Depthbuffer *depthbuffer = framebuffer->getDepthbuffer();
952
953 if (depthbuffer)
954 {
955 *params = depthbuffer->getDepthSize();
956 }
957 else
958 {
959 *params = 0;
960 }
961 }
962 break;
963 case GL_STENCIL_BITS:
964 {
965 gl::Framebuffer *framebuffer = getFramebuffer();
966 gl::Stencilbuffer *stencilbuffer = framebuffer->getStencilbuffer();
967
968 if (stencilbuffer)
969 {
970 *params = stencilbuffer->getStencilSize();
971 }
972 else
973 {
974 *params = 0;
975 }
976 }
977 break;
978 case GL_TEXTURE_BINDING_2D:
979 {
980 if (activeSampler < 0 || activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
981 {
982 error(GL_INVALID_OPERATION);
983 return false;
984 }
985
986 *params = samplerTexture[SAMPLER_2D][activeSampler];
987 }
988 break;
989 case GL_TEXTURE_BINDING_CUBE_MAP:
990 {
991 if (activeSampler < 0 || activeSampler > gl::MAX_TEXTURE_IMAGE_UNITS - 1)
992 {
993 error(GL_INVALID_OPERATION);
994 return false;
995 }
996
997 *params = samplerTexture[SAMPLER_CUBE][activeSampler];
998 }
999 break;
1000 default:
1001 return false;
1002 }
1003
1004 return true;
1005}
1006
1007bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1008{
1009 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1010 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1011 // to the fact that it is stored internally as a float, and so would require conversion
1012 // if returned from Context::getIntegerv. Since this conversion is already implemented
1013 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1014 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1015 // application.
1016 switch (pname)
1017 {
1018 case GL_COMPRESSED_TEXTURE_FORMATS: /* no compressed texture formats are supported */
1019 case GL_SHADER_BINARY_FORMATS:
1020 {
1021 *type = GL_INT;
1022 *numParams = 0;
1023 }
1024 break;
1025 case GL_MAX_VERTEX_ATTRIBS:
1026 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1027 case GL_MAX_VARYING_VECTORS:
1028 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1029 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1030 case GL_MAX_TEXTURE_IMAGE_UNITS:
1031 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1032 case GL_MAX_RENDERBUFFER_SIZE:
1033 case GL_NUM_SHADER_BINARY_FORMATS:
1034 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1035 case GL_ARRAY_BUFFER_BINDING:
1036 case GL_FRAMEBUFFER_BINDING:
1037 case GL_RENDERBUFFER_BINDING:
1038 case GL_CURRENT_PROGRAM:
1039 case GL_PACK_ALIGNMENT:
1040 case GL_UNPACK_ALIGNMENT:
1041 case GL_GENERATE_MIPMAP_HINT:
1042 case GL_RED_BITS:
1043 case GL_GREEN_BITS:
1044 case GL_BLUE_BITS:
1045 case GL_ALPHA_BITS:
1046 case GL_DEPTH_BITS:
1047 case GL_STENCIL_BITS:
1048 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1049 case GL_CULL_FACE_MODE:
1050 case GL_FRONT_FACE:
1051 case GL_ACTIVE_TEXTURE:
1052 case GL_STENCIL_FUNC:
1053 case GL_STENCIL_VALUE_MASK:
1054 case GL_STENCIL_REF:
1055 case GL_STENCIL_FAIL:
1056 case GL_STENCIL_PASS_DEPTH_FAIL:
1057 case GL_STENCIL_PASS_DEPTH_PASS:
1058 case GL_STENCIL_BACK_FUNC:
1059 case GL_STENCIL_BACK_VALUE_MASK:
1060 case GL_STENCIL_BACK_REF:
1061 case GL_STENCIL_BACK_FAIL:
1062 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1063 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1064 case GL_DEPTH_FUNC:
1065 case GL_BLEND_SRC_RGB:
1066 case GL_BLEND_SRC_ALPHA:
1067 case GL_BLEND_DST_RGB:
1068 case GL_BLEND_DST_ALPHA:
1069 case GL_BLEND_EQUATION_RGB:
1070 case GL_BLEND_EQUATION_ALPHA:
1071 case GL_STENCIL_WRITEMASK:
1072 case GL_STENCIL_BACK_WRITEMASK:
1073 case GL_STENCIL_CLEAR_VALUE:
1074 case GL_SUBPIXEL_BITS:
1075 case GL_MAX_TEXTURE_SIZE:
1076 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1077 case GL_SAMPLE_BUFFERS:
1078 case GL_SAMPLES:
1079 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1080 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1081 case GL_TEXTURE_BINDING_2D:
1082 case GL_TEXTURE_BINDING_CUBE_MAP:
1083 {
1084 *type = GL_INT;
1085 *numParams = 1;
1086 }
1087 break;
1088 case GL_MAX_VIEWPORT_DIMS:
1089 {
1090 *type = GL_INT;
1091 *numParams = 2;
1092 }
1093 break;
1094 case GL_VIEWPORT:
1095 case GL_SCISSOR_BOX:
1096 {
1097 *type = GL_INT;
1098 *numParams = 4;
1099 }
1100 break;
1101 case GL_SHADER_COMPILER:
1102 case GL_SAMPLE_COVERAGE_INVERT:
1103 case GL_DEPTH_WRITEMASK:
1104 {
1105 *type = GL_BOOL;
1106 *numParams = 1;
1107 }
1108 break;
1109 case GL_COLOR_WRITEMASK:
1110 {
1111 *type = GL_BOOL;
1112 *numParams = 4;
1113 }
1114 break;
1115 case GL_POLYGON_OFFSET_FACTOR:
1116 case GL_POLYGON_OFFSET_UNITS:
1117 case GL_SAMPLE_COVERAGE_VALUE:
1118 case GL_DEPTH_CLEAR_VALUE:
1119 case GL_LINE_WIDTH:
1120 {
1121 *type = GL_FLOAT;
1122 *numParams = 1;
1123 }
1124 break;
1125 case GL_ALIASED_LINE_WIDTH_RANGE:
1126 case GL_ALIASED_POINT_SIZE_RANGE:
1127 case GL_DEPTH_RANGE:
1128 {
1129 *type = GL_FLOAT;
1130 *numParams = 2;
1131 }
1132 break;
1133 case GL_COLOR_CLEAR_VALUE:
1134 {
1135 *type = GL_FLOAT;
1136 *numParams = 4;
1137 }
1138 break;
1139 default:
1140 return false;
1141 }
1142
1143 return true;
1144}
1145
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146// Applies the render target surface, depth stencil surface, viewport rectangle and
1147// scissor rectangle to the Direct3D 9 device
1148bool Context::applyRenderTarget(bool ignoreViewport)
1149{
1150 IDirect3DDevice9 *device = getDevice();
1151 Framebuffer *framebufferObject = getFramebuffer();
1152
1153 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1154 {
1155 return false;
1156 }
1157
1158 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1159 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1160
1161 device->SetRenderTarget(0, renderTarget);
1162 device->SetDepthStencilSurface(depthStencil);
1163
1164 D3DVIEWPORT9 viewport;
1165 D3DSURFACE_DESC desc;
1166 renderTarget->GetDesc(&desc);
1167
1168 if (ignoreViewport)
1169 {
1170 viewport.X = 0;
1171 viewport.Y = 0;
1172 viewport.Width = desc.Width;
1173 viewport.Height = desc.Height;
1174 viewport.MinZ = 0.0f;
1175 viewport.MaxZ = 1.0f;
1176 }
1177 else
1178 {
daniel@transgaming.com16973022010-03-11 19:22:19 +00001179 viewport.X = std::max(viewportX, 0);
1180 viewport.Y = std::max(viewportY, 0);
1181 viewport.Width = std::min(viewportWidth, (int)desc.Width - (int)viewport.X);
1182 viewport.Height = std::min(viewportHeight, (int)desc.Height - (int)viewport.Y);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183 viewport.MinZ = clamp01(zNear);
1184 viewport.MaxZ = clamp01(zFar);
1185 }
1186
1187 device->SetViewport(&viewport);
1188
1189 if (scissorTest)
1190 {
1191 RECT rect = {scissorX,
1192 scissorY,
1193 scissorX + scissorWidth,
1194 scissorY + scissorHeight};
1195
1196 device->SetScissorRect(&rect);
1197 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
1198 }
1199 else
1200 {
1201 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
1202 }
1203
1204 if (currentProgram)
1205 {
1206 D3DSURFACE_DESC description;
1207 renderTarget->GetDesc(&description);
1208 Program *programObject = getCurrentProgram();
1209
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001210 GLint halfPixelSize = programObject->getUniformLocation("gl_HalfPixelSize");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001211 GLfloat xy[2] = {1.0f / description.Width, 1.0f / description.Height};
1212 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001213
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001214 GLint window = programObject->getUniformLocation("gl_Window");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001215 GLfloat whxy[4] = {viewportWidth / 2.0f, viewportHeight / 2.0f, (float)viewportX + viewportWidth / 2.0f, (float)viewportY + viewportHeight / 2.0f};
1216 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
1217
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001218 GLint depth = programObject->getUniformLocation("gl_Depth");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001219 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
1220 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
1221
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001222 GLint near = programObject->getUniformLocation("gl_DepthRange.near");
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001223 programObject->setUniform1fv(near, 1, &zNear);
1224
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001225 GLint far = programObject->getUniformLocation("gl_DepthRange.far");
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001226 programObject->setUniform1fv(far, 1, &zFar);
1227
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001228 GLint diff = programObject->getUniformLocation("gl_DepthRange.diff");
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001229 GLfloat zDiff = zFar - zNear;
1230 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001231 }
1232
1233 return true;
1234}
1235
1236// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
1237void Context::applyState()
1238{
1239 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001240 Program *programObject = getCurrentProgram();
1241
1242 GLint frontCCW = programObject->getUniformLocation("__frontCCW");
1243 GLint ccw = (frontFace == GL_CCW);
1244 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001245
1246 if (cullFace)
1247 {
1248 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(cullMode, frontFace));
1249 }
1250 else
1251 {
1252 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1253 }
1254
1255 if (depthTest)
1256 {
1257 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1258 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(depthFunc));
1259 }
1260 else
1261 {
1262 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
1263 }
1264
1265 if (blend)
1266 {
1267 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1268
1269 if (sourceBlendRGB != GL_CONSTANT_ALPHA && sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1270 destBlendRGB != GL_CONSTANT_ALPHA && destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1271 {
1272 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(blendColor));
1273 }
1274 else
1275 {
1276 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(blendColor.alpha),
1277 unorm<8>(blendColor.alpha),
1278 unorm<8>(blendColor.alpha),
1279 unorm<8>(blendColor.alpha)));
1280 }
1281
1282 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(sourceBlendRGB));
1283 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(destBlendRGB));
1284 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(blendEquationRGB));
1285
1286 if (sourceBlendRGB != sourceBlendAlpha || destBlendRGB != destBlendAlpha || blendEquationRGB != blendEquationAlpha)
1287 {
1288 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
1289
1290 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(sourceBlendAlpha));
1291 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(destBlendAlpha));
1292 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(blendEquationAlpha));
1293
1294 }
1295 else
1296 {
1297 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1298 }
1299 }
1300 else
1301 {
1302 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1303 }
1304
1305 if (stencilTest)
1306 {
1307 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1308 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
1309
1310 // FIXME: Unsupported by D3D9
1311 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1312 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1313 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001314 if(stencilWritemask != stencilBackWritemask || stencilRef != stencilBackRef || stencilMask != stencilBackMask)
1315 {
1316 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1317 return error(GL_INVALID_OPERATION);
1318 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319
1320 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilWritemask);
1321 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilFunc));
1322
1323 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilRef); // FIXME: Clamp to range
1324 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilMask);
1325
1326 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilFail));
1327 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilPassDepthFail));
1328 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilPassDepthPass));
1329
1330 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilBackWritemask);
1331 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilBackFunc));
1332
1333 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilBackRef); // FIXME: Clamp to range
1334 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilBackMask);
1335
1336 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilBackFail));
1337 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilBackPassDepthFail));
1338 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilBackPassDepthPass));
1339 }
1340 else
1341 {
1342 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1343 }
1344
1345 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(colorMaskRed, colorMaskGreen, colorMaskBlue, colorMaskAlpha));
1346 device->SetRenderState(D3DRS_ZWRITEENABLE, depthMask ? TRUE : FALSE);
1347
1348 if (polygonOffsetFill)
1349 {
1350 UNIMPLEMENTED(); // FIXME
1351 }
1352
1353 if (sampleAlphaToCoverage)
1354 {
1355 UNIMPLEMENTED(); // FIXME
1356 }
1357
1358 if (sampleCoverage)
1359 {
1360 UNIMPLEMENTED(); // FIXME: Ignore when SAMPLE_BUFFERS is not one
1361 }
1362
1363 device->SetRenderState(D3DRS_DITHERENABLE, dither ? TRUE : FALSE);
1364}
1365
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001366// Fill in the programAttribute field of the array of TranslatedAttributes based on the active GLSL program.
1367void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001368{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001369 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001370 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001371 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001372 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001373 attributes[i].programAttribute = getCurrentProgram()->getInputMapping(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374 }
1375 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001376}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001377
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001378// The indices parameter to glDrawElements can have two interpretations:
1379// - as a pointer into client memory
1380// - as an offset into the current GL_ELEMENT_ARRAY_BUFFER buffer
1381// Handle these cases here and return a pointer to the index data.
1382const Index *Context::adjustIndexPointer(const void *indices)
1383{
1384 if (elementArrayBuffer)
1385 {
1386 Buffer *buffer = getBuffer(elementArrayBuffer);
1387 return reinterpret_cast<const Index*>(static_cast<unsigned char*>(buffer->data()) + reinterpret_cast<GLsizei>(indices));
1388 }
1389 else
1390 {
1391 return static_cast<const Index*>(indices);
1392 }
1393}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001394
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001395void Context::applyVertexBuffer(GLint first, GLsizei count)
1396{
1397 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1398
1399 mVertexDataManager->preRenderValidate(first, count, translated);
1400
1401 lookupAttributeMapping(translated);
1402
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001403 mBufferBackEnd->setupAttributesPreDraw(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001404}
1405
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001406void Context::applyVertexBuffer(const TranslatedIndexData &indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001407{
1408 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
1409
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001410 mVertexDataManager->preRenderValidate(indexInfo, translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001411
1412 lookupAttributeMapping(translated);
1413
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001414 mBufferBackEnd->setupAttributesPreDraw(translated);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001415}
1416
1417// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001418TranslatedIndexData Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001419{
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001420 TranslatedIndexData indexInfo = mIndexDataManager->preRenderValidate(mode, type, count, getBuffer(elementArrayBuffer), indices);
1421 mBufferBackEnd->setupIndicesPreDraw(indexInfo);
1422 return indexInfo;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001423}
1424
1425// Applies the shaders and shader constants to the Direct3D 9 device
1426void Context::applyShaders()
1427{
1428 IDirect3DDevice9 *device = getDevice();
1429 Program *programObject = getCurrentProgram();
1430 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1431 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1432
1433 device->SetVertexShader(vertexShader);
1434 device->SetPixelShader(pixelShader);
1435
1436 programObject->applyUniforms();
1437}
1438
1439// Applies the textures and sampler states to the Direct3D 9 device
1440void Context::applyTextures()
1441{
1442 IDirect3DDevice9 *device = getDevice();
1443 Program *programObject = getCurrentProgram();
1444
1445 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1446 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001447 int textureUnit = programObject->getSamplerMapping(sampler);
1448 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001449 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001450 SamplerType textureType = programObject->getSamplerType(sampler);
1451
1452 Texture *texture = getSamplerTexture(textureUnit, textureType);
1453
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001454 if (texture->isComplete())
1455 {
1456 GLenum wrapS = texture->getWrapS();
1457 GLenum wrapT = texture->getWrapT();
1458 GLenum minFilter = texture->getMinFilter();
1459 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001460
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001461 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1462 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001463
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001464 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1465 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1466 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1467 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1468 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469
daniel@transgaming.com12d54072010-03-16 06:23:26 +00001470 device->SetTexture(sampler, texture->getTexture());
1471 }
1472 else
1473 {
1474 device->SetTexture(sampler, getIncompleteTexture(textureType)->getTexture());
1475 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001476 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001477 else
1478 {
1479 device->SetTexture(sampler, NULL);
1480 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001481 }
1482}
1483
1484void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1485{
1486 Framebuffer *framebuffer = getFramebuffer();
1487 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
1488 IDirect3DDevice9 *device = getDevice();
1489
1490 D3DSURFACE_DESC desc;
1491 renderTarget->GetDesc(&desc);
1492
1493 IDirect3DSurface9 *systemSurface;
1494 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1495
1496 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1497 {
1498 return error(GL_OUT_OF_MEMORY);
1499 }
1500
1501 ASSERT(SUCCEEDED(result));
1502
1503 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1504 {
1505 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
1506 }
1507
1508 result = device->GetRenderTargetData(renderTarget, systemSurface);
1509
1510 if (result == D3DERR_DRIVERINTERNALERROR)
1511 {
1512 systemSurface->Release();
1513
1514 return error(GL_OUT_OF_MEMORY);
1515 }
1516
1517 if (FAILED(result))
1518 {
1519 UNREACHABLE();
1520 systemSurface->Release();
1521
1522 return; // No sensible error to generate
1523 }
1524
1525 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00001526 RECT rect = {std::max(x, 0),
1527 std::max(y, 0),
1528 std::min(x + width, (int)desc.Width),
1529 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530
1531 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1532
1533 if (FAILED(result))
1534 {
1535 UNREACHABLE();
1536 systemSurface->Release();
1537
1538 return; // No sensible error to generate
1539 }
1540
1541 unsigned char *source = (unsigned char*)lock.pBits;
1542 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001543 unsigned short *dest16 = (unsigned short*)pixels;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001544
1545 for (int j = 0; j < rect.bottom - rect.top; j++)
1546 {
1547 for (int i = 0; i < rect.right - rect.left; i++)
1548 {
1549 float r;
1550 float g;
1551 float b;
1552 float a;
1553
1554 switch (desc.Format)
1555 {
1556 case D3DFMT_R5G6B5:
1557 {
1558 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1559
1560 a = 1.0f;
1561 b = (rgb & 0x001F) * (1.0f / 0x001F);
1562 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1563 r = (rgb & 0xF800) * (1.0f / 0xF800);
1564 }
1565 break;
1566 case D3DFMT_X1R5G5B5:
1567 {
1568 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1569
1570 a = 1.0f;
1571 b = (xrgb & 0x001F) * (1.0f / 0x001F);
1572 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
1573 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
1574 }
1575 break;
1576 case D3DFMT_A1R5G5B5:
1577 {
1578 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1579
1580 a = (argb & 0x8000) ? 1.0f : 0.0f;
1581 b = (argb & 0x001F) * (1.0f / 0x001F);
1582 g = (argb & 0x03E0) * (1.0f / 0x03E0);
1583 r = (argb & 0x7C00) * (1.0f / 0x7C00);
1584 }
1585 break;
1586 case D3DFMT_A8R8G8B8:
1587 {
1588 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1589
1590 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
1591 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
1592 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
1593 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
1594 }
1595 break;
1596 case D3DFMT_X8R8G8B8:
1597 {
1598 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1599
1600 a = 1.0f;
1601 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
1602 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
1603 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
1604 }
1605 break;
1606 case D3DFMT_A2R10G10B10:
1607 {
1608 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1609
1610 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
1611 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
1612 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
1613 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
1614 }
1615 break;
1616 default:
1617 UNIMPLEMENTED(); // FIXME
1618 UNREACHABLE();
1619 }
1620
1621 switch (format)
1622 {
1623 case GL_RGBA:
1624 switch (type)
1625 {
1626 case GL_UNSIGNED_BYTE:
1627 dest[4 * (i + j * width) + 0] = (unsigned char)(255 * r + 0.5f);
1628 dest[4 * (i + j * width) + 1] = (unsigned char)(255 * g + 0.5f);
1629 dest[4 * (i + j * width) + 2] = (unsigned char)(255 * b + 0.5f);
1630 dest[4 * (i + j * width) + 3] = (unsigned char)(255 * a + 0.5f);
1631 break;
1632 default: UNREACHABLE();
1633 }
1634 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001635 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636 switch (type)
1637 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00001638 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
1639 dest16[i + j * width] = ((unsigned short)(31 * b + 0.5f) << 0) |
1640 ((unsigned short)(63 * g + 0.5f) << 5) |
1641 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642 break;
1643 default: UNREACHABLE();
1644 }
1645 break;
1646 default: UNREACHABLE();
1647 }
1648 }
1649 }
1650
1651 systemSurface->UnlockRect();
1652
1653 systemSurface->Release();
1654}
1655
1656void Context::clear(GLbitfield mask)
1657{
1658 IDirect3DDevice9 *device = getDevice();
1659 DWORD flags = 0;
1660
1661 if (mask & GL_COLOR_BUFFER_BIT)
1662 {
1663 mask &= ~GL_COLOR_BUFFER_BIT;
1664 flags |= D3DCLEAR_TARGET;
1665 }
1666
1667 if (mask & GL_DEPTH_BUFFER_BIT)
1668 {
1669 mask &= ~GL_DEPTH_BUFFER_BIT;
1670 if (depthMask)
1671 {
1672 flags |= D3DCLEAR_ZBUFFER;
1673 }
1674 }
1675
1676 Framebuffer *framebufferObject = getFramebuffer();
1677 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1678
1679 GLuint stencilUnmasked = 0x0;
1680
1681 if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil)
1682 {
1683 D3DSURFACE_DESC desc;
1684 depthStencil->GetDesc(&desc);
1685
1686 mask &= ~GL_STENCIL_BUFFER_BIT;
1687 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
1688 stencilUnmasked = (0x1 << stencilSize) - 1;
1689
1690 if (stencilUnmasked != 0x0)
1691 {
1692 flags |= D3DCLEAR_STENCIL;
1693 }
1694 }
1695
1696 if (mask != 0)
1697 {
1698 return error(GL_INVALID_VALUE);
1699 }
1700
1701 applyRenderTarget(true); // Clips the clear to the scissor rectangle but not the viewport
1702
1703 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(colorClearValue.alpha), unorm<8>(colorClearValue.red), unorm<8>(colorClearValue.green), unorm<8>(colorClearValue.blue));
1704 float depth = clamp01(depthClearValue);
1705 int stencil = stencilClearValue & 0x000000FF;
1706
1707 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1708
1709 D3DSURFACE_DESC desc;
1710 renderTarget->GetDesc(&desc);
1711
1712 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || colorMaskAlpha;
1713
1714 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
1715 (stencilWritemask & stencilUnmasked) != stencilUnmasked;
1716 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001717 !(colorMaskRed && colorMaskGreen &&
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001718 colorMaskBlue && alphaUnmasked);
1719
1720 if (needMaskedColorClear || needMaskedStencilClear)
1721 {
1722 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1723 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1724 device->SetRenderState(D3DRS_ZENABLE, FALSE);
1725 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1726 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1727 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1728 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1729 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1730
1731 if (flags & D3DCLEAR_TARGET)
1732 {
1733 device->SetRenderState(D3DRS_COLORWRITEENABLE, (colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
1734 (colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
1735 (colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
1736 (colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
1737 }
1738 else
1739 {
1740 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1741 }
1742
1743 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
1744 {
1745 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1746 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1747 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1748 device->SetRenderState(D3DRS_STENCILREF, stencil);
1749 device->SetRenderState(D3DRS_STENCILWRITEMASK, stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001750 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1752 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1753 }
1754 else
1755 {
1756 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1757 }
1758
1759 device->SetPixelShader(NULL);
1760 device->SetVertexShader(NULL);
1761 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1762
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001763 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 {
1765 float x, y, z, w;
1766 D3DCOLOR diffuse;
1767 };
1768
1769 Vertex quad[4];
1770 quad[0].x = 0.0f;
1771 quad[0].y = (float)desc.Height;
1772 quad[0].z = 0.0f;
1773 quad[0].w = 1.0f;
1774 quad[0].diffuse = color;
1775
1776 quad[1].x = (float)desc.Width;
1777 quad[1].y = (float)desc.Height;
1778 quad[1].z = 0.0f;
1779 quad[1].w = 1.0f;
1780 quad[1].diffuse = color;
1781
1782 quad[2].x = 0.0f;
1783 quad[2].y = 0.0f;
1784 quad[2].z = 0.0f;
1785 quad[2].w = 1.0f;
1786 quad[2].diffuse = color;
1787
1788 quad[3].x = (float)desc.Width;
1789 quad[3].y = 0.0f;
1790 quad[3].z = 0.0f;
1791 quad[3].w = 1.0f;
1792 quad[3].diffuse = color;
1793
1794 device->BeginScene();
1795 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
1796 device->EndScene();
1797
1798 if (flags & D3DCLEAR_ZBUFFER)
1799 {
1800 device->SetRenderState(D3DRS_ZENABLE, TRUE);
1801 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1802 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1803 }
1804 }
1805 else
1806 {
1807 device->Clear(0, NULL, flags, color, depth, stencil);
1808 }
1809}
1810
1811void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
1812{
1813 if (!currentProgram)
1814 {
1815 return error(GL_INVALID_OPERATION);
1816 }
1817
1818 IDirect3DDevice9 *device = getDevice();
1819 D3DPRIMITIVETYPE primitiveType;
1820 int primitiveCount;
1821
1822 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1823 return error(GL_INVALID_ENUM);
1824
1825 if (primitiveCount <= 0)
1826 {
1827 return;
1828 }
1829
1830 if (!applyRenderTarget(false))
1831 {
1832 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1833 }
1834
1835 applyState();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001836 applyVertexBuffer(first, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 applyShaders();
1838 applyTextures();
1839
daniel@transgaming.comace5e662010-03-21 04:31:20 +00001840 if (!cullSkipsDraw(mode))
1841 {
1842 device->BeginScene();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001843 device->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00001844 device->EndScene();
1845 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846}
1847
1848void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
1849{
1850 if (!currentProgram)
1851 {
1852 return error(GL_INVALID_OPERATION);
1853 }
1854
1855 if (!indices && !elementArrayBuffer)
1856 {
1857 return error(GL_INVALID_OPERATION);
1858 }
1859
1860 IDirect3DDevice9 *device = getDevice();
1861 D3DPRIMITIVETYPE primitiveType;
1862 int primitiveCount;
1863
1864 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1865 return error(GL_INVALID_ENUM);
1866
1867 if (primitiveCount <= 0)
1868 {
1869 return;
1870 }
1871
1872 if (!applyRenderTarget(false))
1873 {
1874 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1875 }
1876
1877 applyState();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001878 TranslatedIndexData indexInfo = applyIndexBuffer(indices, count, mode, type);
1879 applyVertexBuffer(indexInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880 applyShaders();
1881 applyTextures();
1882
daniel@transgaming.comace5e662010-03-21 04:31:20 +00001883 if (!cullSkipsDraw(mode))
1884 {
1885 device->BeginScene();
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +00001886 device->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, indexInfo.maxIndex-indexInfo.minIndex+1, indexInfo.offset/sizeof(Index), primitiveCount);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00001887 device->EndScene();
1888 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001889}
1890
1891void Context::finish()
1892{
1893 IDirect3DDevice9 *device = getDevice();
1894 IDirect3DQuery9 *occlusionQuery = NULL;
1895
1896 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
1897
1898 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1899 {
1900 return error(GL_OUT_OF_MEMORY);
1901 }
1902
1903 ASSERT(SUCCEEDED(result));
1904
1905 if (occlusionQuery)
1906 {
1907 occlusionQuery->Issue(D3DISSUE_BEGIN);
1908
1909 // Render something outside the render target
1910 device->SetPixelShader(NULL);
1911 device->SetVertexShader(NULL);
1912 device->SetFVF(D3DFVF_XYZRHW);
1913 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
1914 device->BeginScene();
1915 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
1916 device->EndScene();
1917
1918 occlusionQuery->Issue(D3DISSUE_END);
1919
1920 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1921 {
1922 // Keep polling, but allow other threads to do something useful first
1923 Sleep(0);
1924 }
1925
1926 occlusionQuery->Release();
1927 }
1928}
1929
1930void Context::flush()
1931{
1932 IDirect3DDevice9 *device = getDevice();
1933 IDirect3DQuery9 *eventQuery = NULL;
1934
1935 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
1936
1937 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1938 {
1939 return error(GL_OUT_OF_MEMORY);
1940 }
1941
1942 ASSERT(SUCCEEDED(result));
1943
1944 if (eventQuery)
1945 {
1946 eventQuery->Issue(D3DISSUE_END);
1947
1948 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1949 {
1950 // Keep polling, but allow other threads to do something useful first
1951 Sleep(0);
1952 }
1953
1954 eventQuery->Release();
1955 }
1956}
1957
1958void Context::recordInvalidEnum()
1959{
1960 mInvalidEnum = true;
1961}
1962
1963void Context::recordInvalidValue()
1964{
1965 mInvalidValue = true;
1966}
1967
1968void Context::recordInvalidOperation()
1969{
1970 mInvalidOperation = true;
1971}
1972
1973void Context::recordOutOfMemory()
1974{
1975 mOutOfMemory = true;
1976}
1977
1978void Context::recordInvalidFramebufferOperation()
1979{
1980 mInvalidFramebufferOperation = true;
1981}
1982
1983// Get one of the recorded errors and clear its flag, if any.
1984// [OpenGL ES 2.0.24] section 2.5 page 13.
1985GLenum Context::getError()
1986{
1987 if (mInvalidEnum)
1988 {
1989 mInvalidEnum = false;
1990
1991 return GL_INVALID_ENUM;
1992 }
1993
1994 if (mInvalidValue)
1995 {
1996 mInvalidValue = false;
1997
1998 return GL_INVALID_VALUE;
1999 }
2000
2001 if (mInvalidOperation)
2002 {
2003 mInvalidOperation = false;
2004
2005 return GL_INVALID_OPERATION;
2006 }
2007
2008 if (mOutOfMemory)
2009 {
2010 mOutOfMemory = false;
2011
2012 return GL_OUT_OF_MEMORY;
2013 }
2014
2015 if (mInvalidFramebufferOperation)
2016 {
2017 mInvalidFramebufferOperation = false;
2018
2019 return GL_INVALID_FRAMEBUFFER_OPERATION;
2020 }
2021
2022 return GL_NO_ERROR;
2023}
2024
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00002025const char *Context::getPixelShaderProfile()
2026{
2027 return mPsProfile;
2028}
2029
2030const char *Context::getVertexShaderProfile()
2031{
2032 return mVsProfile;
2033}
2034
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035void Context::detachBuffer(GLuint buffer)
2036{
2037 // [OpenGL ES 2.0.24] section 2.9 page 22:
2038 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2039 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2040
2041 if (arrayBuffer == buffer)
2042 {
2043 arrayBuffer = 0;
2044 }
2045
2046 if (elementArrayBuffer == buffer)
2047 {
2048 elementArrayBuffer = 0;
2049 }
2050
2051 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2052 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002053 if (vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002055 vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056 }
2057 }
2058}
2059
2060void Context::detachTexture(GLuint texture)
2061{
2062 // [OpenGL ES 2.0.24] section 3.8 page 84:
2063 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2064 // rebound to texture object zero
2065
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002066 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002068 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002070 if (samplerTexture[type][sampler] == texture)
2071 {
2072 samplerTexture[type][sampler] = 0;
2073 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 }
2075 }
2076
2077 // [OpenGL ES 2.0.24] section 4.4 page 112:
2078 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2079 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2080 // image was attached in the currently bound framebuffer.
2081
2082 Framebuffer *framebuffer = getFramebuffer();
2083
2084 if (framebuffer)
2085 {
2086 framebuffer->detachTexture(texture);
2087 }
2088}
2089
2090void Context::detachFramebuffer(GLuint framebuffer)
2091{
2092 // [OpenGL ES 2.0.24] section 4.4 page 107:
2093 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2094 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2095
2096 if (this->framebuffer == framebuffer)
2097 {
2098 bindFramebuffer(0);
2099 }
2100}
2101
2102void Context::detachRenderbuffer(GLuint renderbuffer)
2103{
2104 // [OpenGL ES 2.0.24] section 4.4 page 109:
2105 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2106 // had been executed with the target RENDERBUFFER and name of zero.
2107
2108 if (this->renderbuffer == renderbuffer)
2109 {
2110 bindRenderbuffer(0);
2111 }
2112
2113 // [OpenGL ES 2.0.24] section 4.4 page 111:
2114 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2115 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2116 // point to which this image was attached in the currently bound framebuffer.
2117
2118 Framebuffer *framebuffer = getFramebuffer();
2119
2120 if (framebuffer)
2121 {
2122 framebuffer->detachRenderbuffer(renderbuffer);
2123 }
2124}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002125
2126Texture *Context::getIncompleteTexture(SamplerType type)
2127{
2128 Texture *t = mIncompleteTextures[type];
2129
2130 if (t == NULL)
2131 {
2132 static const GLubyte color[] = { 0, 0, 0, 255 };
2133
2134 switch (type)
2135 {
2136 default:
2137 UNREACHABLE();
2138 // default falls through to SAMPLER_2D
2139
2140 case SAMPLER_2D:
2141 {
2142 Texture2D *incomplete2d = new Texture2D;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002143 incomplete2d->setImage(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002144 t = incomplete2d;
2145 }
2146 break;
2147
2148 case SAMPLER_CUBE:
2149 {
2150 TextureCubeMap *incompleteCube = new TextureCubeMap;
2151
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +00002152 incompleteCube->setImagePosX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2153 incompleteCube->setImageNegX(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2154 incompleteCube->setImagePosY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2155 incompleteCube->setImageNegY(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2156 incompleteCube->setImagePosZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
2157 incompleteCube->setImageNegZ(0, GL_RGBA, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002158
2159 t = incompleteCube;
2160 }
2161 break;
2162 }
2163
2164 mIncompleteTextures[type] = t;
2165 }
2166
2167 return t;
2168}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002169
2170bool Context::cullSkipsDraw(GLenum primitiveType)
2171{
2172 if (cullFace && cullMode == GL_FRONT_AND_BACK &&
2173 (primitiveType == GL_TRIANGLES || primitiveType == GL_TRIANGLE_FAN || primitiveType == GL_TRIANGLE_STRIP))
2174 {
2175 return true;
2176 }
2177 else
2178 {
2179 return false;
2180 }
2181}
2182
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183}
2184
2185extern "C"
2186{
2187gl::Context *glCreateContext(const egl::Config *config)
2188{
2189 return new gl::Context(config);
2190}
2191
2192void glDestroyContext(gl::Context *context)
2193{
2194 delete context;
2195
2196 if (context == gl::getContext())
2197 {
2198 gl::makeCurrent(NULL, NULL, NULL);
2199 }
2200}
2201
2202void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
2203{
2204 gl::makeCurrent(context, display, surface);
2205}
2206
2207gl::Context *glGetCurrentContext()
2208{
2209 return gl::getContext();
2210}
2211}