blob: 37db9a79202ea35c50dd215401fc8db879a3e6c5 [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"
15#include "Display.h"
16#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"
26#include "geometry/dx9.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027
daniel@transgaming.com86487c22010-03-11 19:41:43 +000028#undef near
29#undef far
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031namespace gl
32{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +000033Context::Context(const egl::Config *config)
34 : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035{
36 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
37 depthClearValue = 1.0f;
38 stencilClearValue = 0;
39
40 cullFace = false;
41 cullMode = GL_BACK;
42 frontFace = GL_CCW;
43 depthTest = false;
44 depthFunc = GL_LESS;
45 blend = false;
46 sourceBlendRGB = GL_ONE;
47 sourceBlendAlpha = GL_ONE;
48 destBlendRGB = GL_ZERO;
49 destBlendAlpha = GL_ZERO;
50 blendEquationRGB = GL_FUNC_ADD;
51 blendEquationAlpha = GL_FUNC_ADD;
52 blendColor.red = 0;
53 blendColor.green = 0;
54 blendColor.blue = 0;
55 blendColor.alpha = 0;
56 stencilTest = false;
57 stencilFunc = GL_ALWAYS;
58 stencilRef = 0;
59 stencilMask = -1;
60 stencilWritemask = -1;
61 stencilBackFunc = GL_ALWAYS;
62 stencilBackRef = 0;
63 stencilBackMask = - 1;
64 stencilBackWritemask = -1;
65 stencilFail = GL_KEEP;
66 stencilPassDepthFail = GL_KEEP;
67 stencilPassDepthPass = GL_KEEP;
68 stencilBackFail = GL_KEEP;
69 stencilBackPassDepthFail = GL_KEEP;
70 stencilBackPassDepthPass = GL_KEEP;
71 polygonOffsetFill = false;
72 sampleAlphaToCoverage = false;
73 sampleCoverage = false;
74 sampleCoverageValue = 1.0f;
75 sampleCoverageInvert = GL_FALSE;
76 scissorTest = false;
77 dither = true;
78
79 viewportX = 0;
80 viewportY = 0;
81 viewportWidth = config->mDisplayMode.Width;
82 viewportHeight = config->mDisplayMode.Height;
83 zNear = 0.0f;
84 zFar = 1.0f;
85
86 scissorX = 0;
87 scissorY = 0;
88 scissorWidth = config->mDisplayMode.Width;
89 scissorHeight = config->mDisplayMode.Height;
90
91 colorMaskRed = true;
92 colorMaskGreen = true;
93 colorMaskBlue = true;
94 colorMaskAlpha = true;
95 depthMask = true;
96
97 // [OpenGL ES 2.0.24] section 3.7 page 83:
98 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
99 // and cube map texture state vectors respectively associated with them.
100 // In order that access to these initial textures not be lost, they are treated as texture
101 // objects all of whose names are 0.
102
103 mTexture2DZero = new Texture2D();
104 mTextureCubeMapZero = new TextureCubeMap();
105
106 mColorbufferZero = NULL;
107 mDepthbufferZero = NULL;
108 mStencilbufferZero = NULL;
109
110 activeSampler = 0;
111 arrayBuffer = 0;
112 elementArrayBuffer = 0;
113 bindTextureCubeMap(0);
114 bindTexture2D(0);
115 bindFramebuffer(0);
116 bindRenderbuffer(0);
117
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000118 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000120 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
121 {
122 samplerTexture[type][sampler] = 0;
123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 }
125
126 currentProgram = 0;
127
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000128 mBufferBackEnd = NULL;
129 mVertexDataManager = NULL;
130
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 mInvalidEnum = false;
132 mInvalidValue = false;
133 mInvalidOperation = false;
134 mOutOfMemory = false;
135 mInvalidFramebufferOperation = false;
136}
137
138Context::~Context()
139{
140 currentProgram = 0;
141
142 delete mTexture2DZero;
143 delete mTextureCubeMapZero;
144
145 delete mColorbufferZero;
146 delete mDepthbufferZero;
147 delete mStencilbufferZero;
148
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000149 delete mBufferBackEnd;
150 delete mVertexDataManager;
151
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152 while (!mBufferMap.empty())
153 {
154 deleteBuffer(mBufferMap.begin()->first);
155 }
156
157 while (!mProgramMap.empty())
158 {
159 deleteProgram(mProgramMap.begin()->first);
160 }
161
162 while (!mShaderMap.empty())
163 {
164 deleteShader(mShaderMap.begin()->first);
165 }
166
167 while (!mFramebufferMap.empty())
168 {
169 deleteFramebuffer(mFramebufferMap.begin()->first);
170 }
171
172 while (!mRenderbufferMap.empty())
173 {
174 deleteRenderbuffer(mRenderbufferMap.begin()->first);
175 }
176
177 while (!mTextureMap.empty())
178 {
179 deleteTexture(mTextureMap.begin()->first);
180 }
181}
182
183void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
184{
185 IDirect3DDevice9 *device = display->getDevice();
186
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000187 if (!mBufferBackEnd)
188 {
189 mBufferBackEnd = new Dx9BackEnd(device);
190 mVertexDataManager = new VertexDataManager(this, mBufferBackEnd);
191 }
192
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
194 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
195 IDirect3DSurface9 *defaultDepthStencil = NULL;
196 device->GetDepthStencilSurface(&defaultDepthStencil);
197
198 Framebuffer *framebufferZero = new Framebuffer();
199 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
200 Depthbuffer *depthbufferZero = new Depthbuffer(defaultDepthStencil);
201 Stencilbuffer *stencilbufferZero = new Stencilbuffer(defaultDepthStencil);
202
203 setFramebufferZero(framebufferZero);
204 setColorbufferZero(colorbufferZero);
205 setDepthbufferZero(depthbufferZero);
206 setStencilbufferZero(stencilbufferZero);
207
208 framebufferZero->setColorbuffer(GL_RENDERBUFFER, 0);
209 framebufferZero->setDepthbuffer(GL_RENDERBUFFER, 0);
210 framebufferZero->setStencilbuffer(GL_RENDERBUFFER, 0);
211
212 defaultRenderTarget->Release();
213
214 if (defaultDepthStencil)
215 {
216 defaultDepthStencil->Release();
217 }
218}
219
220void Context::setClearColor(float red, float green, float blue, float alpha)
221{
222 colorClearValue.red = red;
223 colorClearValue.green = green;
224 colorClearValue.blue = blue;
225 colorClearValue.alpha = alpha;
226}
227
228void Context::setClearDepth(float depth)
229{
230 depthClearValue = depth;
231}
232
233void Context::setClearStencil(int stencil)
234{
235 stencilClearValue = stencil;
236}
237
238// Returns an unused buffer name
239GLuint Context::createBuffer()
240{
241 unsigned int handle = 1;
242
243 while (mBufferMap.find(handle) != mBufferMap.end())
244 {
245 handle++;
246 }
247
248 mBufferMap[handle] = NULL;
249
250 return handle;
251}
252
253// Returns an unused shader/program name
254GLuint Context::createShader(GLenum type)
255{
256 unsigned int handle = 1;
257
258 while (mShaderMap.find(handle) != mShaderMap.end() || mProgramMap.find(handle) != mProgramMap.end()) // Shared name space
259 {
260 handle++;
261 }
262
263 if (type == GL_VERTEX_SHADER)
264 {
265 mShaderMap[handle] = new VertexShader();
266 }
267 else if (type == GL_FRAGMENT_SHADER)
268 {
269 mShaderMap[handle] = new FragmentShader();
270 }
271 else UNREACHABLE();
272
273 return handle;
274}
275
276// Returns an unused program/shader name
277GLuint Context::createProgram()
278{
279 unsigned int handle = 1;
280
281 while (mProgramMap.find(handle) != mProgramMap.end() || mShaderMap.find(handle) != mShaderMap.end()) // Shared name space
282 {
283 handle++;
284 }
285
286 mProgramMap[handle] = new Program();
287
288 return handle;
289}
290
291// Returns an unused texture name
292GLuint Context::createTexture()
293{
294 unsigned int handle = 1;
295
296 while (mTextureMap.find(handle) != mTextureMap.end())
297 {
298 handle++;
299 }
300
301 mTextureMap[handle] = NULL;
302
303 return handle;
304}
305
306// Returns an unused framebuffer name
307GLuint Context::createFramebuffer()
308{
309 unsigned int handle = 1;
310
311 while (mFramebufferMap.find(handle) != mFramebufferMap.end())
312 {
313 handle++;
314 }
315
316 mFramebufferMap[handle] = NULL;
317
318 return handle;
319}
320
321// Returns an unused renderbuffer name
322GLuint Context::createRenderbuffer()
323{
324 unsigned int handle = 1;
325
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000326 while (mRenderbufferMap.find(handle) != mRenderbufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327 {
328 handle++;
329 }
330
331 mRenderbufferMap[handle] = NULL;
332
333 return handle;
334}
335
336void Context::deleteBuffer(GLuint buffer)
337{
338 BufferMap::iterator bufferObject = mBufferMap.find(buffer);
339
340 if (bufferObject != mBufferMap.end())
341 {
342 detachBuffer(buffer);
343
344 delete bufferObject->second;
345 mBufferMap.erase(bufferObject);
346 }
347}
348
349void Context::deleteShader(GLuint shader)
350{
351 ShaderMap::iterator shaderObject = mShaderMap.find(shader);
352
353 if (shaderObject != mShaderMap.end())
354 {
355 if (!shaderObject->second->isAttached())
356 {
357 delete shaderObject->second;
358 mShaderMap.erase(shaderObject);
359 }
360 else
361 {
362 shaderObject->second->flagForDeletion();
363 }
364 }
365}
366
367void Context::deleteProgram(GLuint program)
368{
369 ProgramMap::iterator programObject = mProgramMap.find(program);
370
371 if (programObject != mProgramMap.end())
372 {
373 if (program != currentProgram)
374 {
375 delete programObject->second;
376 mProgramMap.erase(programObject);
377 }
378 else
379 {
380 programObject->second->flagForDeletion();
381 }
382 }
383}
384
385void Context::deleteTexture(GLuint texture)
386{
387 TextureMap::iterator textureObject = mTextureMap.find(texture);
388
389 if (textureObject != mTextureMap.end())
390 {
391 detachTexture(texture);
392
393 if (texture != 0)
394 {
395 delete textureObject->second;
396 }
397
398 mTextureMap.erase(textureObject);
399 }
400}
401
402void Context::deleteFramebuffer(GLuint framebuffer)
403{
404 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
405
406 if (framebufferObject != mFramebufferMap.end())
407 {
408 detachFramebuffer(framebuffer);
409
410 delete framebufferObject->second;
411 mFramebufferMap.erase(framebufferObject);
412 }
413}
414
415void Context::deleteRenderbuffer(GLuint renderbuffer)
416{
417 RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
418
419 if (renderbufferObject != mRenderbufferMap.end())
420 {
421 detachRenderbuffer(renderbuffer);
422
423 delete renderbufferObject->second;
424 mRenderbufferMap.erase(renderbufferObject);
425 }
426}
427
428void Context::bindArrayBuffer(unsigned int buffer)
429{
430 if (buffer != 0 && !getBuffer(buffer))
431 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000432 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433 }
434
435 arrayBuffer = buffer;
436}
437
438void Context::bindElementArrayBuffer(unsigned int buffer)
439{
440 if (buffer != 0 && !getBuffer(buffer))
441 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000442 mBufferMap[buffer] = new Buffer(mBufferBackEnd);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443 }
444
445 elementArrayBuffer = buffer;
446}
447
448void Context::bindTexture2D(GLuint texture)
449{
450 if (!getTexture(texture) || texture == 0)
451 {
452 if (texture != 0)
453 {
454 mTextureMap[texture] = new Texture2D();
455 }
456 else // Special case: 0 refers to different initial textures based on the target
457 {
458 mTextureMap[0] = mTexture2DZero;
459 }
460 }
461
462 texture2D = texture;
463
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000464 samplerTexture[SAMPLER_2D][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465}
466
467void Context::bindTextureCubeMap(GLuint texture)
468{
469 if (!getTexture(texture) || texture == 0)
470 {
471 if (texture != 0)
472 {
473 mTextureMap[texture] = new TextureCubeMap();
474 }
475 else // Special case: 0 refers to different initial textures based on the target
476 {
477 mTextureMap[0] = mTextureCubeMapZero;
478 }
479 }
480
481 textureCubeMap = texture;
482
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000483 samplerTexture[SAMPLER_CUBE][activeSampler] = texture;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484}
485
486void Context::bindFramebuffer(GLuint framebuffer)
487{
488 if (!getFramebuffer(framebuffer))
489 {
490 mFramebufferMap[framebuffer] = new Framebuffer();
491 }
492
493 this->framebuffer = framebuffer;
494}
495
496void Context::bindRenderbuffer(GLuint renderbuffer)
497{
498 if (renderbuffer != 0 && !getRenderbuffer(renderbuffer))
499 {
500 mRenderbufferMap[renderbuffer] = new Renderbuffer();
501 }
502
503 this->renderbuffer = renderbuffer;
504}
505
506void Context::useProgram(GLuint program)
507{
508 Program *programObject = getCurrentProgram();
509
510 if (programObject && programObject->isFlaggedForDeletion())
511 {
512 deleteProgram(currentProgram);
513 }
514
515 currentProgram = program;
516}
517
518void Context::setFramebufferZero(Framebuffer *buffer)
519{
520 delete mFramebufferMap[0];
521 mFramebufferMap[0] = buffer;
522}
523
524void Context::setColorbufferZero(Colorbuffer *buffer)
525{
526 delete mColorbufferZero;
527 mColorbufferZero = buffer;
528}
529
530void Context::setDepthbufferZero(Depthbuffer *buffer)
531{
532 delete mDepthbufferZero;
533 mDepthbufferZero = buffer;
534}
535
536void Context::setStencilbufferZero(Stencilbuffer *buffer)
537{
538 delete mStencilbufferZero;
539 mStencilbufferZero = buffer;
540}
541
542void Context::setRenderbuffer(Renderbuffer *buffer)
543{
544 delete mRenderbufferMap[renderbuffer];
545 mRenderbufferMap[renderbuffer] = buffer;
546}
547
548Buffer *Context::getBuffer(unsigned int handle)
549{
550 BufferMap::iterator buffer = mBufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000551
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552 if (buffer == mBufferMap.end())
553 {
554 return NULL;
555 }
556 else
557 {
558 return buffer->second;
559 }
560}
561
562Shader *Context::getShader(unsigned int handle)
563{
564 ShaderMap::iterator shader = mShaderMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000565
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566 if (shader == mShaderMap.end())
567 {
568 return NULL;
569 }
570 else
571 {
572 return shader->second;
573 }
574}
575
576Program *Context::getProgram(unsigned int handle)
577{
578 ProgramMap::iterator program = mProgramMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000579
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580 if (program == mProgramMap.end())
581 {
582 return NULL;
583 }
584 else
585 {
586 return program->second;
587 }
588}
589
590Texture *Context::getTexture(unsigned int handle)
591{
592 TextureMap::iterator texture = mTextureMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000593
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594 if (texture == mTextureMap.end())
595 {
596 return NULL;
597 }
598 else
599 {
600 return texture->second;
601 }
602}
603
604Framebuffer *Context::getFramebuffer(unsigned int handle)
605{
606 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000607
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608 if (framebuffer == mFramebufferMap.end())
609 {
610 return NULL;
611 }
612 else
613 {
614 return framebuffer->second;
615 }
616}
617
618Renderbuffer *Context::getRenderbuffer(unsigned int handle)
619{
620 RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000621
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622 if (renderbuffer == mRenderbufferMap.end())
623 {
624 return NULL;
625 }
626 else
627 {
628 return renderbuffer->second;
629 }
630}
631
632Colorbuffer *Context::getColorbuffer(GLuint handle)
633{
634 if (handle != 0)
635 {
636 Renderbuffer *renderbuffer = getRenderbuffer(handle);
637
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000638 if (renderbuffer && renderbuffer->isColorbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639 {
640 return static_cast<Colorbuffer*>(renderbuffer);
641 }
642 }
643 else // Special case: 0 refers to different initial render targets based on the attachment type
644 {
645 return mColorbufferZero;
646 }
647
648 return NULL;
649}
650
651Depthbuffer *Context::getDepthbuffer(GLuint handle)
652{
653 if (handle != 0)
654 {
655 Renderbuffer *renderbuffer = getRenderbuffer(handle);
656
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000657 if (renderbuffer && renderbuffer->isDepthbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658 {
659 return static_cast<Depthbuffer*>(renderbuffer);
660 }
661 }
662 else // Special case: 0 refers to different initial render targets based on the attachment type
663 {
664 return mDepthbufferZero;
665 }
666
667 return NULL;
668}
669
670Stencilbuffer *Context::getStencilbuffer(GLuint handle)
671{
672 if (handle != 0)
673 {
674 Renderbuffer *renderbuffer = getRenderbuffer(handle);
675
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000676 if (renderbuffer && renderbuffer->isStencilbuffer())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677 {
678 return static_cast<Stencilbuffer*>(renderbuffer);
679 }
680 }
681 else
682 {
683 return mStencilbufferZero;
684 }
685
686 return NULL;
687}
688
689Buffer *Context::getArrayBuffer()
690{
691 return getBuffer(arrayBuffer);
692}
693
694Buffer *Context::getElementArrayBuffer()
695{
696 return getBuffer(elementArrayBuffer);
697}
698
699Program *Context::getCurrentProgram()
700{
701 return getProgram(currentProgram);
702}
703
704Texture2D *Context::getTexture2D()
705{
706 if (texture2D == 0) // Special case: 0 refers to different initial textures based on the target
707 {
708 return mTexture2DZero;
709 }
710
711 return (Texture2D*)getTexture(texture2D);
712}
713
714TextureCubeMap *Context::getTextureCubeMap()
715{
716 if (textureCubeMap == 0) // Special case: 0 refers to different initial textures based on the target
717 {
718 return mTextureCubeMapZero;
719 }
720
721 return (TextureCubeMap*)getTexture(textureCubeMap);
722}
723
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000724Texture *Context::getSamplerTexture(unsigned int sampler, SamplerType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725{
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000726 return getTexture(samplerTexture[type][sampler]);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727}
728
729Framebuffer *Context::getFramebuffer()
730{
731 return getFramebuffer(framebuffer);
732}
733
734// Applies the render target surface, depth stencil surface, viewport rectangle and
735// scissor rectangle to the Direct3D 9 device
736bool Context::applyRenderTarget(bool ignoreViewport)
737{
738 IDirect3DDevice9 *device = getDevice();
739 Framebuffer *framebufferObject = getFramebuffer();
740
741 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
742 {
743 return false;
744 }
745
746 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
747 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
748
749 device->SetRenderTarget(0, renderTarget);
750 device->SetDepthStencilSurface(depthStencil);
751
752 D3DVIEWPORT9 viewport;
753 D3DSURFACE_DESC desc;
754 renderTarget->GetDesc(&desc);
755
756 if (ignoreViewport)
757 {
758 viewport.X = 0;
759 viewport.Y = 0;
760 viewport.Width = desc.Width;
761 viewport.Height = desc.Height;
762 viewport.MinZ = 0.0f;
763 viewport.MaxZ = 1.0f;
764 }
765 else
766 {
daniel@transgaming.com16973022010-03-11 19:22:19 +0000767 viewport.X = std::max(viewportX, 0);
768 viewport.Y = std::max(viewportY, 0);
769 viewport.Width = std::min(viewportWidth, (int)desc.Width - (int)viewport.X);
770 viewport.Height = std::min(viewportHeight, (int)desc.Height - (int)viewport.Y);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771 viewport.MinZ = clamp01(zNear);
772 viewport.MaxZ = clamp01(zFar);
773 }
774
775 device->SetViewport(&viewport);
776
777 if (scissorTest)
778 {
779 RECT rect = {scissorX,
780 scissorY,
781 scissorX + scissorWidth,
782 scissorY + scissorHeight};
783
784 device->SetScissorRect(&rect);
785 device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
786 }
787 else
788 {
789 device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
790 }
791
792 if (currentProgram)
793 {
794 D3DSURFACE_DESC description;
795 renderTarget->GetDesc(&description);
796 Program *programObject = getCurrentProgram();
797
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000798 GLint halfPixelSize = programObject->getUniformLocation("gl_HalfPixelSize");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799 GLfloat xy[2] = {1.0f / description.Width, 1.0f / description.Height};
800 programObject->setUniform2fv(halfPixelSize, 1, (GLfloat*)&xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000801
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000802 GLint window = programObject->getUniformLocation("gl_Window");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000803 GLfloat whxy[4] = {viewportWidth / 2.0f, viewportHeight / 2.0f, (float)viewportX + viewportWidth / 2.0f, (float)viewportY + viewportHeight / 2.0f};
804 programObject->setUniform4fv(window, 1, (GLfloat*)&whxy);
805
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000806 GLint depth = programObject->getUniformLocation("gl_Depth");
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000807 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
808 programObject->setUniform2fv(depth, 1, (GLfloat*)&dz);
809
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000810 GLint near = programObject->getUniformLocation("gl_DepthRange.near");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000811 programObject->setUniform1fv(near, 1, &zNear);
812
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000813 GLint far = programObject->getUniformLocation("gl_DepthRange.far");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000814 programObject->setUniform1fv(far, 1, &zFar);
815
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000816 GLint diff = programObject->getUniformLocation("gl_DepthRange.diff");
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000817 GLfloat zDiff = zFar - zNear;
818 programObject->setUniform1fv(diff, 1, &zDiff);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819 }
820
821 return true;
822}
823
824// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
825void Context::applyState()
826{
827 IDirect3DDevice9 *device = getDevice();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000828 Program *programObject = getCurrentProgram();
829
830 GLint frontCCW = programObject->getUniformLocation("__frontCCW");
831 GLint ccw = (frontFace == GL_CCW);
832 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833
834 if (cullFace)
835 {
836 device->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(cullMode, frontFace));
837 }
838 else
839 {
840 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
841 }
842
843 if (depthTest)
844 {
845 device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
846 device->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(depthFunc));
847 }
848 else
849 {
850 device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
851 }
852
853 if (blend)
854 {
855 device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
856
857 if (sourceBlendRGB != GL_CONSTANT_ALPHA && sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
858 destBlendRGB != GL_CONSTANT_ALPHA && destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
859 {
860 device->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(blendColor));
861 }
862 else
863 {
864 device->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(blendColor.alpha),
865 unorm<8>(blendColor.alpha),
866 unorm<8>(blendColor.alpha),
867 unorm<8>(blendColor.alpha)));
868 }
869
870 device->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(sourceBlendRGB));
871 device->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(destBlendRGB));
872 device->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(blendEquationRGB));
873
874 if (sourceBlendRGB != sourceBlendAlpha || destBlendRGB != destBlendAlpha || blendEquationRGB != blendEquationAlpha)
875 {
876 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
877
878 device->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(sourceBlendAlpha));
879 device->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(destBlendAlpha));
880 device->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(blendEquationAlpha));
881
882 }
883 else
884 {
885 device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
886 }
887 }
888 else
889 {
890 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
891 }
892
893 if (stencilTest)
894 {
895 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
896 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
897
898 // FIXME: Unsupported by D3D9
899 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
900 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
901 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
902 ASSERT(stencilRef == stencilBackRef);
903 ASSERT(stencilMask == stencilBackMask);
904 ASSERT(stencilWritemask == stencilBackWritemask);
905
906 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilWritemask);
907 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilFunc));
908
909 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilRef); // FIXME: Clamp to range
910 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilMask);
911
912 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilFail));
913 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilPassDepthFail));
914 device->SetRenderState(frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilPassDepthPass));
915
916 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, stencilBackWritemask);
917 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, es2dx::ConvertComparison(stencilBackFunc));
918
919 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, stencilBackRef); // FIXME: Clamp to range
920 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, stencilBackMask);
921
922 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, es2dx::ConvertStencilOp(stencilBackFail));
923 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, es2dx::ConvertStencilOp(stencilBackPassDepthFail));
924 device->SetRenderState(frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, es2dx::ConvertStencilOp(stencilBackPassDepthPass));
925 }
926 else
927 {
928 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
929 }
930
931 device->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(colorMaskRed, colorMaskGreen, colorMaskBlue, colorMaskAlpha));
932 device->SetRenderState(D3DRS_ZWRITEENABLE, depthMask ? TRUE : FALSE);
933
934 if (polygonOffsetFill)
935 {
936 UNIMPLEMENTED(); // FIXME
937 }
938
939 if (sampleAlphaToCoverage)
940 {
941 UNIMPLEMENTED(); // FIXME
942 }
943
944 if (sampleCoverage)
945 {
946 UNIMPLEMENTED(); // FIXME: Ignore when SAMPLE_BUFFERS is not one
947 }
948
949 device->SetRenderState(D3DRS_DITHERENABLE, dither ? TRUE : FALSE);
950}
951
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000952// Fill in the programAttribute field of the array of TranslatedAttributes based on the active GLSL program.
953void Context::lookupAttributeMapping(TranslatedAttribute *attributes)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954{
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000955 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000957 if (attributes[i].enabled)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000959 attributes[i].programAttribute = getCurrentProgram()->getInputMapping(i);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960 }
961 }
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000962}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000964// The indices parameter to glDrawElements can have two interpretations:
965// - as a pointer into client memory
966// - as an offset into the current GL_ELEMENT_ARRAY_BUFFER buffer
967// Handle these cases here and return a pointer to the index data.
968const Index *Context::adjustIndexPointer(const void *indices)
969{
970 if (elementArrayBuffer)
971 {
972 Buffer *buffer = getBuffer(elementArrayBuffer);
973 return reinterpret_cast<const Index*>(static_cast<unsigned char*>(buffer->data()) + reinterpret_cast<GLsizei>(indices));
974 }
975 else
976 {
977 return static_cast<const Index*>(indices);
978 }
979}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000981void Context::applyVertexBuffer(GLint first, GLsizei count)
982{
983 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
984
985 mVertexDataManager->preRenderValidate(first, count, translated);
986
987 lookupAttributeMapping(translated);
988
989 mBufferBackEnd->preDraw(translated);
990}
991
992void Context::applyVertexBuffer(GLsizei count, const void *indices, GLenum indexType)
993{
994 TranslatedAttribute translated[MAX_VERTEX_ATTRIBS];
995
996 mVertexDataManager->preRenderValidate(adjustIndexPointer(indices), count, translated);
997
998 lookupAttributeMapping(translated);
999
1000 mBufferBackEnd->preDraw(translated);
1001}
1002
1003// Applies the indices and element array bindings to the Direct3D 9 device
1004void Context::applyIndexBuffer(const void *indices, GLsizei count)
1005{
1006 GLsizei length = count * sizeof(Index);
1007
1008 IDirect3DDevice9 *device = getDevice();
1009
1010 IDirect3DIndexBuffer9 *indexBuffer = NULL;
1011 void *data;
1012
1013 HRESULT result = device->CreateIndexBuffer(length, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &indexBuffer, NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014
1015 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1016 {
1017 return error(GL_OUT_OF_MEMORY);
1018 }
1019
1020 ASSERT(SUCCEEDED(result));
1021
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001022 if (indexBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001024 indexBuffer->Lock(0, length, &data, 0);
1025 memcpy(data, adjustIndexPointer(indices), length);
1026 indexBuffer->Unlock();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027
1028 device->SetIndices(indexBuffer);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001029 indexBuffer->Release(); // Will only effectively be deleted when no longer in use
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001032 startIndex = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033}
1034
1035// Applies the shaders and shader constants to the Direct3D 9 device
1036void Context::applyShaders()
1037{
1038 IDirect3DDevice9 *device = getDevice();
1039 Program *programObject = getCurrentProgram();
1040 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
1041 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
1042
1043 device->SetVertexShader(vertexShader);
1044 device->SetPixelShader(pixelShader);
1045
1046 programObject->applyUniforms();
1047}
1048
1049// Applies the textures and sampler states to the Direct3D 9 device
1050void Context::applyTextures()
1051{
1052 IDirect3DDevice9 *device = getDevice();
1053 Program *programObject = getCurrentProgram();
1054
1055 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
1056 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001057 int textureUnit = programObject->getSamplerMapping(sampler);
1058 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001060 SamplerType textureType = programObject->getSamplerType(sampler);
1061
1062 Texture *texture = getSamplerTexture(textureUnit, textureType);
1063
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064 GLenum wrapS = texture->getWrapS();
1065 GLenum wrapT = texture->getWrapT();
1066 GLenum minFilter = texture->getMinFilter();
1067 GLenum magFilter = texture->getMagFilter();
1068
1069 device->SetSamplerState(sampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
1070 device->SetSamplerState(sampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
1071
1072 device->SetSamplerState(sampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
1073 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
1074 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
1075 device->SetSamplerState(sampler, D3DSAMP_MINFILTER, d3dMinFilter);
1076 device->SetSamplerState(sampler, D3DSAMP_MIPFILTER, d3dMipFilter);
1077
1078 device->SetTexture(sampler, texture->getTexture());
1079 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001080 else
1081 {
1082 device->SetTexture(sampler, NULL);
1083 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084 }
1085}
1086
1087void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
1088{
1089 Framebuffer *framebuffer = getFramebuffer();
1090 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
1091 IDirect3DDevice9 *device = getDevice();
1092
1093 D3DSURFACE_DESC desc;
1094 renderTarget->GetDesc(&desc);
1095
1096 IDirect3DSurface9 *systemSurface;
1097 HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
1098
1099 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1100 {
1101 return error(GL_OUT_OF_MEMORY);
1102 }
1103
1104 ASSERT(SUCCEEDED(result));
1105
1106 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
1107 {
1108 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
1109 }
1110
1111 result = device->GetRenderTargetData(renderTarget, systemSurface);
1112
1113 if (result == D3DERR_DRIVERINTERNALERROR)
1114 {
1115 systemSurface->Release();
1116
1117 return error(GL_OUT_OF_MEMORY);
1118 }
1119
1120 if (FAILED(result))
1121 {
1122 UNREACHABLE();
1123 systemSurface->Release();
1124
1125 return; // No sensible error to generate
1126 }
1127
1128 D3DLOCKED_RECT lock;
daniel@transgaming.com16973022010-03-11 19:22:19 +00001129 RECT rect = {std::max(x, 0),
1130 std::max(y, 0),
1131 std::min(x + width, (int)desc.Width),
1132 std::min(y + height, (int)desc.Height)};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133
1134 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
1135
1136 if (FAILED(result))
1137 {
1138 UNREACHABLE();
1139 systemSurface->Release();
1140
1141 return; // No sensible error to generate
1142 }
1143
1144 unsigned char *source = (unsigned char*)lock.pBits;
1145 unsigned char *dest = (unsigned char*)pixels;
1146
1147 for (int j = 0; j < rect.bottom - rect.top; j++)
1148 {
1149 for (int i = 0; i < rect.right - rect.left; i++)
1150 {
1151 float r;
1152 float g;
1153 float b;
1154 float a;
1155
1156 switch (desc.Format)
1157 {
1158 case D3DFMT_R5G6B5:
1159 {
1160 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1161
1162 a = 1.0f;
1163 b = (rgb & 0x001F) * (1.0f / 0x001F);
1164 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
1165 r = (rgb & 0xF800) * (1.0f / 0xF800);
1166 }
1167 break;
1168 case D3DFMT_X1R5G5B5:
1169 {
1170 unsigned short xrgb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1171
1172 a = 1.0f;
1173 b = (xrgb & 0x001F) * (1.0f / 0x001F);
1174 g = (xrgb & 0x03E0) * (1.0f / 0x03E0);
1175 r = (xrgb & 0x7C00) * (1.0f / 0x7C00);
1176 }
1177 break;
1178 case D3DFMT_A1R5G5B5:
1179 {
1180 unsigned short argb = *(unsigned short*)(source + 2 * i + j * lock.Pitch);
1181
1182 a = (argb & 0x8000) ? 1.0f : 0.0f;
1183 b = (argb & 0x001F) * (1.0f / 0x001F);
1184 g = (argb & 0x03E0) * (1.0f / 0x03E0);
1185 r = (argb & 0x7C00) * (1.0f / 0x7C00);
1186 }
1187 break;
1188 case D3DFMT_A8R8G8B8:
1189 {
1190 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1191
1192 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
1193 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
1194 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
1195 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
1196 }
1197 break;
1198 case D3DFMT_X8R8G8B8:
1199 {
1200 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1201
1202 a = 1.0f;
1203 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
1204 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
1205 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
1206 }
1207 break;
1208 case D3DFMT_A2R10G10B10:
1209 {
1210 unsigned int argb = *(unsigned int*)(source + 4 * i + j * lock.Pitch);
1211
1212 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
1213 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
1214 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
1215 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
1216 }
1217 break;
1218 default:
1219 UNIMPLEMENTED(); // FIXME
1220 UNREACHABLE();
1221 }
1222
1223 switch (format)
1224 {
1225 case GL_RGBA:
1226 switch (type)
1227 {
1228 case GL_UNSIGNED_BYTE:
1229 dest[4 * (i + j * width) + 0] = (unsigned char)(255 * r + 0.5f);
1230 dest[4 * (i + j * width) + 1] = (unsigned char)(255 * g + 0.5f);
1231 dest[4 * (i + j * width) + 2] = (unsigned char)(255 * b + 0.5f);
1232 dest[4 * (i + j * width) + 3] = (unsigned char)(255 * a + 0.5f);
1233 break;
1234 default: UNREACHABLE();
1235 }
1236 break;
1237 case IMPLEMENTATION_COLOR_READ_FORMAT:
1238 switch (type)
1239 {
1240 case IMPLEMENTATION_COLOR_READ_TYPE:
1241 break;
1242 default: UNREACHABLE();
1243 }
1244 break;
1245 default: UNREACHABLE();
1246 }
1247 }
1248 }
1249
1250 systemSurface->UnlockRect();
1251
1252 systemSurface->Release();
1253}
1254
1255void Context::clear(GLbitfield mask)
1256{
1257 IDirect3DDevice9 *device = getDevice();
1258 DWORD flags = 0;
1259
1260 if (mask & GL_COLOR_BUFFER_BIT)
1261 {
1262 mask &= ~GL_COLOR_BUFFER_BIT;
1263 flags |= D3DCLEAR_TARGET;
1264 }
1265
1266 if (mask & GL_DEPTH_BUFFER_BIT)
1267 {
1268 mask &= ~GL_DEPTH_BUFFER_BIT;
1269 if (depthMask)
1270 {
1271 flags |= D3DCLEAR_ZBUFFER;
1272 }
1273 }
1274
1275 Framebuffer *framebufferObject = getFramebuffer();
1276 IDirect3DSurface9 *depthStencil = framebufferObject->getDepthStencil();
1277
1278 GLuint stencilUnmasked = 0x0;
1279
1280 if ((mask & GL_STENCIL_BUFFER_BIT) && depthStencil)
1281 {
1282 D3DSURFACE_DESC desc;
1283 depthStencil->GetDesc(&desc);
1284
1285 mask &= ~GL_STENCIL_BUFFER_BIT;
1286 unsigned int stencilSize = es2dx::GetStencilSize(desc.Format);
1287 stencilUnmasked = (0x1 << stencilSize) - 1;
1288
1289 if (stencilUnmasked != 0x0)
1290 {
1291 flags |= D3DCLEAR_STENCIL;
1292 }
1293 }
1294
1295 if (mask != 0)
1296 {
1297 return error(GL_INVALID_VALUE);
1298 }
1299
1300 applyRenderTarget(true); // Clips the clear to the scissor rectangle but not the viewport
1301
1302 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(colorClearValue.alpha), unorm<8>(colorClearValue.red), unorm<8>(colorClearValue.green), unorm<8>(colorClearValue.blue));
1303 float depth = clamp01(depthClearValue);
1304 int stencil = stencilClearValue & 0x000000FF;
1305
1306 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
1307
1308 D3DSURFACE_DESC desc;
1309 renderTarget->GetDesc(&desc);
1310
1311 bool alphaUnmasked = (es2dx::GetAlphaSize(desc.Format) == 0) || colorMaskAlpha;
1312
1313 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
1314 (stencilWritemask & stencilUnmasked) != stencilUnmasked;
1315 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001316 !(colorMaskRed && colorMaskGreen &&
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317 colorMaskBlue && alphaUnmasked);
1318
1319 if (needMaskedColorClear || needMaskedStencilClear)
1320 {
1321 device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1322 device->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
1323 device->SetRenderState(D3DRS_ZENABLE, FALSE);
1324 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
1325 device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1326 device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
1327 device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1328 device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
1329
1330 if (flags & D3DCLEAR_TARGET)
1331 {
1332 device->SetRenderState(D3DRS_COLORWRITEENABLE, (colorMaskRed ? D3DCOLORWRITEENABLE_RED : 0) |
1333 (colorMaskGreen ? D3DCOLORWRITEENABLE_GREEN : 0) |
1334 (colorMaskBlue ? D3DCOLORWRITEENABLE_BLUE : 0) |
1335 (colorMaskAlpha ? D3DCOLORWRITEENABLE_ALPHA : 0));
1336 }
1337 else
1338 {
1339 device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
1340 }
1341
1342 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
1343 {
1344 device->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1345 device->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
1346 device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
1347 device->SetRenderState(D3DRS_STENCILREF, stencil);
1348 device->SetRenderState(D3DRS_STENCILWRITEMASK, stencilWritemask);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001349 device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001350 device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
1351 device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
1352 }
1353 else
1354 {
1355 device->SetRenderState(D3DRS_STENCILENABLE, FALSE);
1356 }
1357
1358 device->SetPixelShader(NULL);
1359 device->SetVertexShader(NULL);
1360 device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
1361
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +00001362 struct Vertex
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001363 {
1364 float x, y, z, w;
1365 D3DCOLOR diffuse;
1366 };
1367
1368 Vertex quad[4];
1369 quad[0].x = 0.0f;
1370 quad[0].y = (float)desc.Height;
1371 quad[0].z = 0.0f;
1372 quad[0].w = 1.0f;
1373 quad[0].diffuse = color;
1374
1375 quad[1].x = (float)desc.Width;
1376 quad[1].y = (float)desc.Height;
1377 quad[1].z = 0.0f;
1378 quad[1].w = 1.0f;
1379 quad[1].diffuse = color;
1380
1381 quad[2].x = 0.0f;
1382 quad[2].y = 0.0f;
1383 quad[2].z = 0.0f;
1384 quad[2].w = 1.0f;
1385 quad[2].diffuse = color;
1386
1387 quad[3].x = (float)desc.Width;
1388 quad[3].y = 0.0f;
1389 quad[3].z = 0.0f;
1390 quad[3].w = 1.0f;
1391 quad[3].diffuse = color;
1392
1393 device->BeginScene();
1394 device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex));
1395 device->EndScene();
1396
1397 if (flags & D3DCLEAR_ZBUFFER)
1398 {
1399 device->SetRenderState(D3DRS_ZENABLE, TRUE);
1400 device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1401 device->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
1402 }
1403 }
1404 else
1405 {
1406 device->Clear(0, NULL, flags, color, depth, stencil);
1407 }
1408}
1409
1410void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
1411{
1412 if (!currentProgram)
1413 {
1414 return error(GL_INVALID_OPERATION);
1415 }
1416
1417 IDirect3DDevice9 *device = getDevice();
1418 D3DPRIMITIVETYPE primitiveType;
1419 int primitiveCount;
1420
1421 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1422 return error(GL_INVALID_ENUM);
1423
1424 if (primitiveCount <= 0)
1425 {
1426 return;
1427 }
1428
1429 if (!applyRenderTarget(false))
1430 {
1431 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1432 }
1433
1434 applyState();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001435 applyVertexBuffer(first, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001436 applyShaders();
1437 applyTextures();
1438
1439 device->BeginScene();
1440 device->DrawPrimitive(primitiveType, first, primitiveCount);
1441 device->EndScene();
1442}
1443
1444void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void* indices)
1445{
1446 if (!currentProgram)
1447 {
1448 return error(GL_INVALID_OPERATION);
1449 }
1450
1451 if (!indices && !elementArrayBuffer)
1452 {
1453 return error(GL_INVALID_OPERATION);
1454 }
1455
1456 IDirect3DDevice9 *device = getDevice();
1457 D3DPRIMITIVETYPE primitiveType;
1458 int primitiveCount;
1459
1460 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
1461 return error(GL_INVALID_ENUM);
1462
1463 if (primitiveCount <= 0)
1464 {
1465 return;
1466 }
1467
1468 if (!applyRenderTarget(false))
1469 {
1470 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
1471 }
1472
1473 applyState();
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001474 applyVertexBuffer(count, indices, type);
1475 applyIndexBuffer(indices, count);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001476 applyShaders();
1477 applyTextures();
1478
1479 device->BeginScene();
1480 device->DrawIndexedPrimitive(primitiveType, 0, 0, count, startIndex, primitiveCount);
1481 device->EndScene();
1482}
1483
1484void Context::finish()
1485{
1486 IDirect3DDevice9 *device = getDevice();
1487 IDirect3DQuery9 *occlusionQuery = NULL;
1488
1489 HRESULT result = device->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery);
1490
1491 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1492 {
1493 return error(GL_OUT_OF_MEMORY);
1494 }
1495
1496 ASSERT(SUCCEEDED(result));
1497
1498 if (occlusionQuery)
1499 {
1500 occlusionQuery->Issue(D3DISSUE_BEGIN);
1501
1502 // Render something outside the render target
1503 device->SetPixelShader(NULL);
1504 device->SetVertexShader(NULL);
1505 device->SetFVF(D3DFVF_XYZRHW);
1506 float data[4] = {-1.0f, -1.0f, -1.0f, 1.0f};
1507 device->BeginScene();
1508 device->DrawPrimitiveUP(D3DPT_POINTLIST, 1, data, sizeof(data));
1509 device->EndScene();
1510
1511 occlusionQuery->Issue(D3DISSUE_END);
1512
1513 while (occlusionQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1514 {
1515 // Keep polling, but allow other threads to do something useful first
1516 Sleep(0);
1517 }
1518
1519 occlusionQuery->Release();
1520 }
1521}
1522
1523void Context::flush()
1524{
1525 IDirect3DDevice9 *device = getDevice();
1526 IDirect3DQuery9 *eventQuery = NULL;
1527
1528 HRESULT result = device->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
1529
1530 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
1531 {
1532 return error(GL_OUT_OF_MEMORY);
1533 }
1534
1535 ASSERT(SUCCEEDED(result));
1536
1537 if (eventQuery)
1538 {
1539 eventQuery->Issue(D3DISSUE_END);
1540
1541 while (eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH) == S_FALSE)
1542 {
1543 // Keep polling, but allow other threads to do something useful first
1544 Sleep(0);
1545 }
1546
1547 eventQuery->Release();
1548 }
1549}
1550
1551void Context::recordInvalidEnum()
1552{
1553 mInvalidEnum = true;
1554}
1555
1556void Context::recordInvalidValue()
1557{
1558 mInvalidValue = true;
1559}
1560
1561void Context::recordInvalidOperation()
1562{
1563 mInvalidOperation = true;
1564}
1565
1566void Context::recordOutOfMemory()
1567{
1568 mOutOfMemory = true;
1569}
1570
1571void Context::recordInvalidFramebufferOperation()
1572{
1573 mInvalidFramebufferOperation = true;
1574}
1575
1576// Get one of the recorded errors and clear its flag, if any.
1577// [OpenGL ES 2.0.24] section 2.5 page 13.
1578GLenum Context::getError()
1579{
1580 if (mInvalidEnum)
1581 {
1582 mInvalidEnum = false;
1583
1584 return GL_INVALID_ENUM;
1585 }
1586
1587 if (mInvalidValue)
1588 {
1589 mInvalidValue = false;
1590
1591 return GL_INVALID_VALUE;
1592 }
1593
1594 if (mInvalidOperation)
1595 {
1596 mInvalidOperation = false;
1597
1598 return GL_INVALID_OPERATION;
1599 }
1600
1601 if (mOutOfMemory)
1602 {
1603 mOutOfMemory = false;
1604
1605 return GL_OUT_OF_MEMORY;
1606 }
1607
1608 if (mInvalidFramebufferOperation)
1609 {
1610 mInvalidFramebufferOperation = false;
1611
1612 return GL_INVALID_FRAMEBUFFER_OPERATION;
1613 }
1614
1615 return GL_NO_ERROR;
1616}
1617
1618void Context::detachBuffer(GLuint buffer)
1619{
1620 // [OpenGL ES 2.0.24] section 2.9 page 22:
1621 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
1622 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
1623
1624 if (arrayBuffer == buffer)
1625 {
1626 arrayBuffer = 0;
1627 }
1628
1629 if (elementArrayBuffer == buffer)
1630 {
1631 elementArrayBuffer = 0;
1632 }
1633
1634 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
1635 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001636 if (vertexAttribute[attribute].mBoundBuffer == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001637 {
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00001638 vertexAttribute[attribute].mBoundBuffer = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639 }
1640 }
1641}
1642
1643void Context::detachTexture(GLuint texture)
1644{
1645 // [OpenGL ES 2.0.24] section 3.8 page 84:
1646 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
1647 // rebound to texture object zero
1648
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001649 for (int type = 0; type < SAMPLER_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001651 for (int sampler = 0; sampler < MAX_TEXTURE_IMAGE_UNITS; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652 {
daniel@transgaming.com416485f2010-03-16 06:23:23 +00001653 if (samplerTexture[type][sampler] == texture)
1654 {
1655 samplerTexture[type][sampler] = 0;
1656 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657 }
1658 }
1659
1660 // [OpenGL ES 2.0.24] section 4.4 page 112:
1661 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
1662 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
1663 // image was attached in the currently bound framebuffer.
1664
1665 Framebuffer *framebuffer = getFramebuffer();
1666
1667 if (framebuffer)
1668 {
1669 framebuffer->detachTexture(texture);
1670 }
1671}
1672
1673void Context::detachFramebuffer(GLuint framebuffer)
1674{
1675 // [OpenGL ES 2.0.24] section 4.4 page 107:
1676 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
1677 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
1678
1679 if (this->framebuffer == framebuffer)
1680 {
1681 bindFramebuffer(0);
1682 }
1683}
1684
1685void Context::detachRenderbuffer(GLuint renderbuffer)
1686{
1687 // [OpenGL ES 2.0.24] section 4.4 page 109:
1688 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
1689 // had been executed with the target RENDERBUFFER and name of zero.
1690
1691 if (this->renderbuffer == renderbuffer)
1692 {
1693 bindRenderbuffer(0);
1694 }
1695
1696 // [OpenGL ES 2.0.24] section 4.4 page 111:
1697 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
1698 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
1699 // point to which this image was attached in the currently bound framebuffer.
1700
1701 Framebuffer *framebuffer = getFramebuffer();
1702
1703 if (framebuffer)
1704 {
1705 framebuffer->detachRenderbuffer(renderbuffer);
1706 }
1707}
1708}
1709
1710extern "C"
1711{
1712gl::Context *glCreateContext(const egl::Config *config)
1713{
1714 return new gl::Context(config);
1715}
1716
1717void glDestroyContext(gl::Context *context)
1718{
1719 delete context;
1720
1721 if (context == gl::getContext())
1722 {
1723 gl::makeCurrent(NULL, NULL, NULL);
1724 }
1725}
1726
1727void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
1728{
1729 gl::makeCurrent(context, display, surface);
1730}
1731
1732gl::Context *glGetCurrentContext()
1733{
1734 return gl::getContext();
1735}
1736}