Use de::ArrayBuffer, not raw arrays, in sglrReferenceContext.
[platform/upstream/VK-GL-CTS.git] / framework / opengl / simplereference / sglrReferenceContext.hpp
1 #ifndef _SGLRREFERENCECONTEXT_HPP
2 #define _SGLRREFERENCECONTEXT_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program OpenGL ES Utilities
5  * ------------------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Reference Rendering Context.
24  *//*--------------------------------------------------------------------*/
25
26 #include "tcuDefs.hpp"
27 #include "sglrContext.hpp"
28 #include "tcuPixelFormat.hpp"
29 #include "tcuSurface.hpp"
30 #include "tcuTexture.hpp"
31 #include "tcuVector.hpp"
32 #include "rrFragmentOperations.hpp"
33 #include "rrRenderState.hpp"
34 #include "rrRenderer.hpp"
35 #include "rrMultisamplePixelBufferAccess.hpp"
36 #include "gluRenderContext.hpp"
37 #include "gluShaderUtil.hpp"
38 #include "deArrayBuffer.hpp"
39
40 #include <map>
41 #include <vector>
42
43 namespace sglr
44 {
45 namespace rc
46 {
47
48 enum
49 {
50         MAX_TEXTURE_SIZE_LOG2           = 14,
51         MAX_TEXTURE_SIZE                        = 1<<MAX_TEXTURE_SIZE_LOG2
52 };
53
54 class NamedObject
55 {
56 public:
57         virtual                 ~NamedObject            (void) {}
58
59         deUint32                getName                         (void) const    { return m_name;                                                                }
60
61         int                             getRefCount                     (void) const    { return m_refCount;                                                    }
62         void                    incRefCount                     (void)                  { m_refCount += 1;                                                              }
63         void                    decRefCount                     (void)                  { DE_ASSERT(m_refCount > 0); m_refCount -= 1;   }
64
65 protected:
66                                         NamedObject                     (deUint32 name) : m_name(name), m_refCount(1) {}
67
68 private:
69         deUint32                m_name;
70         int                             m_refCount;
71 };
72
73 class Texture : public NamedObject
74 {
75 public:
76         enum Type
77         {
78                 TYPE_1D,
79                 TYPE_2D,
80                 TYPE_CUBE_MAP,
81                 TYPE_2D_ARRAY,
82                 TYPE_3D,
83                 TYPE_CUBE_MAP_ARRAY,
84
85                 TYPE_LAST
86         };
87
88                                                                 Texture                 (deUint32 name, Type type);
89         virtual                                         ~Texture                (void) {}
90
91         Type                                            getType                 (void) const    { return m_type;                        }
92
93         int                                                     getBaseLevel    (void) const    { return m_baseLevel;           }
94         int                                                     getMaxLevel             (void) const    { return m_maxLevel;            }
95         bool                                            isImmutable             (void) const    { return m_immutable;           }
96
97         void                                            setBaseLevel    (int baseLevel) { m_baseLevel = baseLevel;      }
98         void                                            setMaxLevel             (int maxLevel)  { m_maxLevel = maxLevel;        }
99         void                                            setImmutable    (void)                  { m_immutable = true;           }
100
101         const tcu::Sampler&                     getSampler              (void) const    { return m_sampler;                     }
102         tcu::Sampler&                           getSampler              (void)                  { return m_sampler;                     }
103
104 private:
105         Type                                            m_type;
106
107         bool                                            m_immutable;
108
109         tcu::Sampler                            m_sampler;
110         int                                                     m_baseLevel;
111         int                                                     m_maxLevel;
112 };
113
114 //! Class for managing list of texture levels.
115 class TextureLevelArray
116 {
117 public:
118                                                                                 TextureLevelArray       (void);
119                                                                                 ~TextureLevelArray      (void);
120
121         bool                                                            hasLevel                        (int level) const       { return deInBounds32(level, 0, DE_LENGTH_OF_ARRAY(m_data)) && !m_data[level].empty();  }
122         const tcu::PixelBufferAccess&           getLevel                        (int level)                     { DE_ASSERT(hasLevel(level)); return m_access[level];                                                                   }
123         const tcu::ConstPixelBufferAccess&      getLevel                        (int level) const       { DE_ASSERT(hasLevel(level)); return m_access[level];                                                                   }
124
125         const tcu::ConstPixelBufferAccess*      getLevels                       (void) const            { return &m_access[0];                                                                                                                                  }
126
127         void                                                            allocLevel                      (int level, const tcu::TextureFormat& format, int width, int height, int depth);
128         void                                                            clearLevel                      (int level);
129
130         void                                                            clear                           (void);
131
132 private:
133         de::ArrayBuffer<deUint8>                        m_data[MAX_TEXTURE_SIZE_LOG2];
134         tcu::PixelBufferAccess                          m_access[MAX_TEXTURE_SIZE_LOG2];
135 };
136
137 class Texture1D : public Texture
138 {
139 public:
140                                                                                 Texture1D               (deUint32 name = 0);
141         virtual                                                         ~Texture1D              (void);
142
143         void                                                            clearLevels             (void) { m_levels.clear(); }
144
145         bool                                                            hasLevel                (int level) const       { return m_levels.hasLevel(level);      }
146         const tcu::ConstPixelBufferAccess&      getLevel                (int level) const       { return m_levels.getLevel(level);      }
147         const tcu::PixelBufferAccess&           getLevel                (int level)                     { return m_levels.getLevel(level);      }
148
149         void                                                            allocLevel              (int level, const tcu::TextureFormat& format, int width);
150
151         bool                                                            isComplete              (void) const;
152
153         void                                                            updateView              (void); // \note View must be refreshed after texture parameter/size changes, before calling sample*()
154
155         tcu::Vec4                                                       sample                  (float s, float lod) const;
156         void                                                            sample4                 (tcu::Vec4 output[4], const float packetTexcoords[4], float lodBias = 0.0f) const;
157
158 private:
159         TextureLevelArray                                       m_levels;
160         tcu::Texture2DView                                      m_view;
161 };
162
163 class Texture2D : public Texture
164 {
165 public:
166                                                                                 Texture2D               (deUint32 name = 0);
167         virtual                                                         ~Texture2D              (void);
168
169         void                                                            clearLevels             (void) { m_levels.clear(); }
170
171         bool                                                            hasLevel                (int level) const       { return m_levels.hasLevel(level);      }
172         const tcu::ConstPixelBufferAccess&      getLevel                (int level) const       { return m_levels.getLevel(level);      }
173         const tcu::PixelBufferAccess&           getLevel                (int level)                     { return m_levels.getLevel(level);      }
174
175         void                                                            allocLevel              (int level, const tcu::TextureFormat& format, int width, int height);
176
177         bool                                                            isComplete              (void) const;
178
179         void                                                            updateView              (void); // \note View must be refreshed after texture parameter/size changes, before calling sample*()
180
181         tcu::Vec4                                                       sample                  (float s, float t, float lod) const;
182         void                                                            sample4                 (tcu::Vec4 output[4], const tcu::Vec2 packetTexcoords[4], float lodBias = 0.0f) const;
183
184 private:
185         TextureLevelArray                                       m_levels;
186         tcu::Texture2DView                                      m_view;
187 };
188
189 class TextureCube : public Texture
190 {
191 public:
192                                                                                 TextureCube             (deUint32 name = 0);
193         virtual                                                         ~TextureCube    (void);
194
195         void                                                            clearLevels             (void);
196
197         bool                                                            hasFace                 (int level, tcu::CubeFace face) const   { return m_levels[face].hasLevel(level);        }
198         const tcu::PixelBufferAccess&           getFace                 (int level, tcu::CubeFace face)                 { return m_levels[face].getLevel(level);        }
199         const tcu::ConstPixelBufferAccess&      getFace                 (int level, tcu::CubeFace face) const   { return m_levels[face].getLevel(level);        }
200
201         void                                                            allocFace               (int level, tcu::CubeFace face, const tcu::TextureFormat& format, int width, int height);
202
203         bool                                                            isComplete              (void) const;
204         void                                                            updateView              (void); // \note View must be refreshed after texture parameter/size changes, before calling sample*()
205
206         tcu::Vec4                                                       sample                  (float s, float t, float p, float lod) const;
207         void                                                            sample4                 (tcu::Vec4 output[4], const tcu::Vec3 packetTexcoords[4], float lodBias = 0.0f) const;
208
209 private:
210         TextureLevelArray                                       m_levels[tcu::CUBEFACE_LAST];
211         tcu::TextureCubeView                            m_view;
212 };
213
214 class Texture2DArray : public Texture
215 {
216 public:
217                                                                                 Texture2DArray  (deUint32 name = 0);
218         virtual                                                         ~Texture2DArray (void);
219
220         void                                                            clearLevels             (void) { m_levels.clear(); }
221
222         bool                                                            hasLevel                (int level) const       { return m_levels.hasLevel(level);      }
223         const tcu::ConstPixelBufferAccess&      getLevel                (int level) const       { return m_levels.getLevel(level);      }
224         const tcu::PixelBufferAccess&           getLevel                (int level)                     { return m_levels.getLevel(level);      }
225
226         void                                                            allocLevel              (int level, const tcu::TextureFormat& format, int width, int height, int numLayers);
227
228         bool                                                            isComplete              (void) const;
229
230         void                                                            updateView              (void); // \note View must be refreshed after texture parameter/size changes, before calling sample*()
231
232         tcu::Vec4                                                       sample                  (float s, float t, float r, float lod) const;
233         void                                                            sample4                 (tcu::Vec4 output[4], const tcu::Vec3 packetTexcoords[4], float lodBias = 0.0f) const;
234
235 private:
236         TextureLevelArray                                       m_levels;
237         tcu::Texture2DArrayView                         m_view;
238 };
239
240 class Texture3D : public Texture
241 {
242 public:
243                                                                                 Texture3D               (deUint32 name = 0);
244         virtual                                                         ~Texture3D              (void);
245
246         void                                                            clearLevels             (void) { m_levels.clear(); }
247
248         bool                                                            hasLevel                (int level) const       { return m_levels.hasLevel(level);      }
249         const tcu::ConstPixelBufferAccess&      getLevel                (int level) const       { return m_levels.getLevel(level);      }
250         const tcu::PixelBufferAccess&           getLevel                (int level)                     { return m_levels.getLevel(level);      }
251
252         void                                                            allocLevel              (int level, const tcu::TextureFormat& format, int width, int height, int numLayers);
253
254         bool                                                            isComplete              (void) const;
255
256         void                                                            updateView              (void); // \note View must be refreshed after texture parameter/size changes, before calling sample*()
257
258         tcu::Vec4                                                       sample                  (float s, float t, float r, float lod) const;
259         void                                                            sample4                 (tcu::Vec4 output[4], const tcu::Vec3 packetTexcoords[4], float lodBias = 0.0f) const;
260
261 private:
262         TextureLevelArray                                       m_levels;
263         tcu::Texture3DView                                      m_view;
264 };
265
266 class TextureCubeArray : public Texture
267 {
268 public:
269                                                                                 TextureCubeArray        (deUint32 name = 0);
270         virtual                                                         ~TextureCubeArray       (void);
271
272         void                                                            clearLevels                     (void) { m_levels.clear(); }
273
274         bool                                                            hasLevel                        (int level) const       { return m_levels.hasLevel(level);      }
275         const tcu::ConstPixelBufferAccess&      getLevel                        (int level) const       { return m_levels.getLevel(level);      }
276         const tcu::PixelBufferAccess&           getLevel                        (int level)                     { return m_levels.getLevel(level);      }
277
278         void                                                            allocLevel                      (int level, const tcu::TextureFormat& format, int width, int height, int numLayers);
279
280         bool                                                            isComplete                      (void) const;
281
282         void                                                            updateView                      (void); // \note View must be refreshed after texture parameter/size changes, before calling sample*()
283
284         tcu::Vec4                                                       sample                          (float s, float t, float r, float q, float lod) const;
285         void                                                            sample4                         (tcu::Vec4 output[4], const tcu::Vec4 packetTexcoords[4], float lodBias = 0.0f) const;
286
287 private:
288         TextureLevelArray                                       m_levels;
289         tcu::TextureCubeArrayView                       m_view;
290 };
291
292 class Renderbuffer : public NamedObject
293 {
294 public:
295         enum Format
296         {
297                 FORMAT_DEPTH_COMPONENT16,
298                 FORMAT_RGBA4,
299                 FORMAT_RGB5_A1,
300                 FORMAT_RGB565,
301                 FORMAT_STENCIL_INDEX8,
302
303                 FORMAT_LAST
304         };
305
306                                                                 Renderbuffer            (deUint32 name);
307         virtual                                         ~Renderbuffer           (void);
308
309         void                                            setStorage                      (const tcu::TextureFormat& format, int width, int height);
310
311         int                                                     getWidth                        (void) const    { return m_data.getWidth();             }
312         int                                                     getHeight                       (void) const    { return m_data.getHeight();    }
313         tcu::TextureFormat                      getFormat                       (void) const    { return m_data.getFormat();    }
314
315         tcu::PixelBufferAccess          getAccess                       (void)                  { return m_data.getAccess();    }
316         tcu::ConstPixelBufferAccess     getAccess                       (void) const    { return m_data.getAccess();    }
317
318 private:
319         tcu::TextureLevel                       m_data;
320 };
321
322 class Framebuffer : public NamedObject
323 {
324 public:
325         enum AttachmentPoint
326         {
327                 ATTACHMENTPOINT_COLOR0,
328                 ATTACHMENTPOINT_DEPTH,
329                 ATTACHMENTPOINT_STENCIL,
330
331                 ATTACHMENTPOINT_LAST
332         };
333
334         enum AttachmentType
335         {
336                 ATTACHMENTTYPE_RENDERBUFFER,
337                 ATTACHMENTTYPE_TEXTURE,
338
339                 ATTACHMENTTYPE_LAST
340         };
341
342         enum TexTarget
343         {
344                 TEXTARGET_2D,
345                 TEXTARGET_CUBE_MAP_POSITIVE_X,
346                 TEXTARGET_CUBE_MAP_POSITIVE_Y,
347                 TEXTARGET_CUBE_MAP_POSITIVE_Z,
348                 TEXTARGET_CUBE_MAP_NEGATIVE_X,
349                 TEXTARGET_CUBE_MAP_NEGATIVE_Y,
350                 TEXTARGET_CUBE_MAP_NEGATIVE_Z,
351                 TEXTARGET_2D_ARRAY,
352                 TEXTARGET_3D,
353                 TEXTARGET_CUBE_MAP_ARRAY,
354
355                 TEXTARGET_LAST
356         };
357
358         struct Attachment
359         {
360                 AttachmentType  type;
361                 deUint32                name;
362                 TexTarget               texTarget;
363                 int                             level;
364                 int                             layer;
365
366                 Attachment (void)
367                         : type          (ATTACHMENTTYPE_LAST)
368                         , name          (0)
369                         , texTarget     (TEXTARGET_LAST)
370                         , level         (0)
371                         , layer         (0)
372                 {
373                 }
374         };
375
376                                                         Framebuffer             (deUint32 name);
377         virtual                                 ~Framebuffer    (void);
378
379         Attachment&                             getAttachment   (AttachmentPoint point)                 { return m_attachments[point]; }
380         const Attachment&               getAttachment   (AttachmentPoint point) const   { return m_attachments[point]; }
381
382 private:
383
384         Attachment                      m_attachments[ATTACHMENTPOINT_LAST];
385 };
386
387 class DataBuffer : public NamedObject
388 {
389 public:
390                                                         DataBuffer                      (deUint32 name) : NamedObject(name) {}
391                                                         ~DataBuffer                     (void) {}
392
393         void                                    setStorage                      (int size) { m_data.resize(size); }
394
395         int                                             getSize                         (void) const    { return (int)m_data.size();                                    }
396         const deUint8*                  getData                         (void) const    { return m_data.empty() ? DE_NULL : &m_data[0]; }
397         deUint8*                                getData                         (void)                  { return m_data.empty() ? DE_NULL : &m_data[0]; }
398
399 private:
400         std::vector<deUint8>    m_data;
401 };
402
403 class VertexArray : public NamedObject
404 {
405 public:
406         struct VertexAttribArray
407         {
408                 bool                    enabled;
409                 int                             size;
410                 int                             stride;
411                 deUint32                type;
412
413                 bool                    normalized;
414                 bool                    integer;
415                 int                             divisor;
416
417                 /**
418                   ! These three variables define the state. bufferDeleted is needed to distinguish
419                   ! drawing from user pointer and offset to a deleted buffer from each other.
420                   !
421                   ! Only these three combinations are possible:
422                   ! 1) bufferDeleted = false, bufferBinding = NULL, pointer = user_ptr.   < render from a user ptr
423                   ! 2) bufferDeleted = false, bufferBinding = ptr,  pointer = offset.     < render from a buffer with offset
424                   ! 3) bufferDeleted = true,  bufferBinding = NULL, pointer = offset      < render from a deleted buffer. Don't do anything
425                   !
426                   ! (bufferFreed = true) implies (bufferBinding = NULL)
427                  */
428                 bool                    bufferDeleted;
429                 rc::DataBuffer* bufferBinding;
430                 const void*             pointer;
431         };
432
433                                                                         VertexArray             (deUint32 name, int maxVertexAttribs);
434                                                                         ~VertexArray    (void) {}
435
436         rc::DataBuffer*                                 m_elementArrayBufferBinding;
437         std::vector<VertexAttribArray>  m_arrays;
438 };
439
440 class ShaderProgramObjectContainer : public NamedObject
441 {
442 public:
443                                                                         ShaderProgramObjectContainer    (deUint32 name, ShaderProgram* program);
444                                                                         ~ShaderProgramObjectContainer   (void);
445
446         ShaderProgram*                                  m_program;
447         bool                                                    m_deleteFlag;
448 };
449
450 template <typename T>
451 class ObjectManager
452 {
453 public:
454                                                         ObjectManager                   (void);
455                                                         ~ObjectManager                  (void);
456
457         deUint32                                allocateName                    (void);
458         void                                    insert                                  (T* object);
459         T*                                              find                                    (deUint32 name);
460
461         void                                    acquireReference                (T* object);
462         void                                    releaseReference                (T* object);
463
464         int                                             getCount                                (void) const { return (int)m_objects.size(); }
465         void                                    getAll                                  (typename std::vector<T*>& objects) const;
466
467 private:
468                                                         ObjectManager                   (const ObjectManager<T>& other);
469         ObjectManager&                  operator=                               (const ObjectManager<T>& other);
470
471         deUint32                                m_lastName;
472         std::map<deUint32, T*>  m_objects;
473 };
474
475 template <typename T>
476 ObjectManager<T>::ObjectManager (void)
477         : m_lastName(0)
478 {
479 }
480
481 template <typename T>
482 ObjectManager<T>::~ObjectManager (void)
483 {
484         DE_ASSERT(m_objects.size() == 0);
485 }
486
487 template <typename T>
488 deUint32 ObjectManager<T>::allocateName (void)
489 {
490         TCU_CHECK(m_lastName != 0xffffffff);
491         return ++m_lastName;
492 }
493
494 template <typename T>
495 void ObjectManager<T>::insert (T* object)
496 {
497         deUint32 name = object->getName();
498         DE_ASSERT(object->getName() != 0);
499
500         if (name > m_lastName)
501                 m_lastName = name;
502
503         m_objects.insert(std::pair<deUint32, T*>(name, object));
504 }
505
506 template <typename T>
507 T* ObjectManager<T>::find (deUint32 name)
508 {
509         typename std::map<deUint32, T*>::iterator it = m_objects.find(name);
510         if (it != m_objects.end())
511                 return it->second;
512         else
513                 return DE_NULL;
514 }
515
516 template <typename T>
517 void ObjectManager<T>::acquireReference (T* object)
518 {
519         DE_ASSERT(find(object->getName()) == object);
520         object->incRefCount();
521 }
522
523 template <typename T>
524 void ObjectManager<T>::releaseReference (T* object)
525 {
526         DE_ASSERT(find(object->getName()) == object);
527         object->decRefCount();
528
529         if (object->getRefCount() == 0)
530         {
531                 m_objects.erase(object->getName());
532                 delete object;
533         }
534 }
535
536 template <typename T>
537 void ObjectManager<T>::getAll (typename std::vector<T*>& objects) const
538 {
539         objects.resize(m_objects.size());
540         typename std::vector<T*>::iterator dst = objects.begin();
541
542         for (typename std::map<deUint32, T*>::const_iterator i = m_objects.begin();
543                  i != m_objects.end(); i++)
544         {
545                 *dst++ = i->second;
546         }
547 }
548
549 } // rc
550
551 struct ReferenceContextLimits
552 {
553         ReferenceContextLimits (void)
554                 : contextType                           (glu::ApiType::es(3,0))
555                 , maxTextureImageUnits          (16)
556                 , maxTexture2DSize                      (2048)
557                 , maxTextureCubeSize            (2048)
558                 , maxTexture2DArrayLayers       (256)
559                 , maxTexture3DSize                      (256)
560                 , maxRenderbufferSize           (2048)
561                 , maxVertexAttribs                      (16)
562         {
563         }
564
565                                                                 ReferenceContextLimits  (const glu::RenderContext& renderCtx);
566
567         void                                            addExtension                    (const char* extension);
568
569         glu::ContextType                        contextType;
570
571         int                                                     maxTextureImageUnits;
572         int                                                     maxTexture2DSize;
573         int                                                     maxTextureCubeSize;
574         int                                                     maxTexture2DArrayLayers;
575         int                                                     maxTexture3DSize;
576         int                                                     maxRenderbufferSize;
577         int                                                     maxVertexAttribs;
578
579         // Both variants are needed since there are glGetString() and glGetStringi()
580         std::vector<std::string>        extensionList;
581         std::string                                     extensionStr;
582 };
583
584 class ReferenceContextBuffers
585 {
586 public:
587         ReferenceContextBuffers (const tcu::PixelFormat& colorBits, int depthBits, int stencilBits, int width, int height, int samples = 1);
588
589         rr::MultisamplePixelBufferAccess        getColorbuffer          (void) { return rr::MultisamplePixelBufferAccess::fromMultisampleAccess(m_colorbuffer.getAccess());     }
590         rr::MultisamplePixelBufferAccess        getDepthbuffer          (void) { return rr::MultisamplePixelBufferAccess::fromMultisampleAccess(m_depthbuffer.getAccess());     }
591         rr::MultisamplePixelBufferAccess        getStencilbuffer        (void) { return rr::MultisamplePixelBufferAccess::fromMultisampleAccess(m_stencilbuffer.getAccess());   }
592
593 private:
594         tcu::TextureLevel       m_colorbuffer;
595         tcu::TextureLevel       m_depthbuffer;
596         tcu::TextureLevel       m_stencilbuffer;
597 };
598
599 class ReferenceContext : public Context
600 {
601 public:
602                                                         ReferenceContext                (const ReferenceContextLimits& limits, const rr::MultisamplePixelBufferAccess& colorbuffer, const rr::MultisamplePixelBufferAccess& depthbuffer, const rr::MultisamplePixelBufferAccess& stencilbuffer);
603         virtual                                 ~ReferenceContext               (void);
604
605         virtual int                             getWidth                                (void) const    { return m_defaultColorbuffer.raw().getHeight();        }
606         virtual int                             getHeight                               (void) const    { return m_defaultColorbuffer.raw().getDepth();         }
607
608         virtual void                    viewport                                (int x, int y, int width, int height) { m_viewport = tcu::IVec4(x, y, width, height); }
609         virtual void                    activeTexture                   (deUint32 texture);
610
611         virtual void                    bindTexture                             (deUint32 target, deUint32 texture);
612         virtual void                    genTextures                             (int numTextures, deUint32* textures);
613         virtual void                    deleteTextures                  (int numTextures, const deUint32* textures);
614
615         virtual void                    bindFramebuffer                 (deUint32 target, deUint32 framebuffer);
616         virtual void                    genFramebuffers                 (int numFramebuffers, deUint32* framebuffers);
617         virtual void                    deleteFramebuffers              (int numFramebuffers, const deUint32* framebuffers);
618
619         virtual void                    bindRenderbuffer                (deUint32 target, deUint32 renderbuffer);
620         virtual void                    genRenderbuffers                (int numRenderbuffers, deUint32* renderbuffers);
621         virtual void                    deleteRenderbuffers             (int numRenderbuffers, const deUint32* renderbuffers);
622
623         virtual void                    pixelStorei                             (deUint32 pname, int param);
624         virtual void                    texImage1D                              (deUint32 target, int level, deUint32 internalFormat, int width, int border, deUint32 format, deUint32 type, const void* data);
625         virtual void                    texImage2D                              (deUint32 target, int level, deUint32 internalFormat, int width, int height, int border, deUint32 format, deUint32 type, const void* data);
626         virtual void                    texImage3D                              (deUint32 target, int level, deUint32 internalFormat, int width, int height, int depth, int border, deUint32 format, deUint32 type, const void* data);
627         virtual void                    texSubImage1D                   (deUint32 target, int level, int xoffset, int width, deUint32 format, deUint32 type, const void* data);
628         virtual void                    texSubImage2D                   (deUint32 target, int level, int xoffset, int yoffset, int width, int height, deUint32 format, deUint32 type, const void* data);
629         virtual void                    texSubImage3D                   (deUint32 target, int level, int xoffset, int yoffset, int zoffset, int width, int height, int depth, deUint32 format, deUint32 type, const void* data);
630         virtual void                    copyTexImage1D                  (deUint32 target, int level, deUint32 internalFormat, int x, int y, int width, int border);
631         virtual void                    copyTexImage2D                  (deUint32 target, int level, deUint32 internalFormat, int x, int y, int width, int height, int border);
632         virtual void                    copyTexSubImage1D               (deUint32 target, int level, int xoffset, int x, int y, int width);
633         virtual void                    copyTexSubImage2D               (deUint32 target, int level, int xoffset, int yoffset, int x, int y, int width, int height);
634         virtual void                    copyTexSubImage3D               (deUint32 target, int level, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height);
635
636         virtual void                    texStorage2D                    (deUint32 target, int levels, deUint32 internalFormat, int width, int height);
637         virtual void                    texStorage3D                    (deUint32 target, int levels, deUint32 internalFormat, int width, int height, int depth);
638
639         virtual void                    texParameteri                   (deUint32 target, deUint32 pname, int value);
640
641         virtual void                    framebufferTexture2D    (deUint32 target, deUint32 attachment, deUint32 textarget, deUint32 texture, int level);
642         virtual void                    framebufferTextureLayer (deUint32 target, deUint32 attachment, deUint32 texture, int level, int layer);
643         virtual void                    framebufferRenderbuffer (deUint32 target, deUint32 attachment, deUint32 renderbuffertarget, deUint32 renderbuffer);
644         virtual deUint32                checkFramebufferStatus  (deUint32 target);
645
646         virtual void                    getFramebufferAttachmentParameteriv     (deUint32 target, deUint32 attachment, deUint32 pname, int* params);
647
648         virtual void                    renderbufferStorage                             (deUint32 target, deUint32 internalformat, int width, int height);
649         virtual void                    renderbufferStorageMultisample  (deUint32 target, int samples, deUint32 internalFormat, int width, int height);
650
651         virtual void                    bindBuffer                              (deUint32 target, deUint32 buffer);
652         virtual void                    genBuffers                              (int numBuffers, deUint32* buffers);
653         virtual void                    deleteBuffers                   (int numBuffers, const deUint32* buffers);
654
655         virtual void                    bufferData                              (deUint32 target, deIntptr size, const void* data, deUint32 usage);
656         virtual void                    bufferSubData                   (deUint32 target, deIntptr offset, deIntptr size, const void* data);
657
658         virtual void                    clearColor                              (float red, float green, float blue, float alpha);
659         virtual void                    clearDepthf                             (float depth);
660         virtual void                    clearStencil                    (int stencil);
661
662         virtual void                    clear                                   (deUint32 buffers);
663         virtual void                    clearBufferiv                   (deUint32 buffer, int drawbuffer, const int* value);
664         virtual void                    clearBufferfv                   (deUint32 buffer, int drawbuffer, const float* value);
665         virtual void                    clearBufferuiv                  (deUint32 buffer, int drawbuffer, const deUint32* value);
666         virtual void                    clearBufferfi                   (deUint32 buffer, int drawbuffer, float depth, int stencil);
667         virtual void                    scissor                                 (int x, int y, int width, int height);
668
669         virtual void                    enable                                  (deUint32 cap);
670         virtual void                    disable                                 (deUint32 cap);
671
672         virtual void                    stencilFunc                             (deUint32 func, int ref, deUint32 mask);
673         virtual void                    stencilOp                               (deUint32 sfail, deUint32 dpfail, deUint32 dppass);
674         virtual void                    stencilFuncSeparate             (deUint32 face, deUint32 func, int ref, deUint32 mask);
675         virtual void                    stencilOpSeparate               (deUint32 face, deUint32 sfail, deUint32 dpfail, deUint32 dppass);
676
677         virtual void                    depthFunc                               (deUint32 func);
678         virtual void                    depthRangef                             (float n, float f);
679         virtual void                    depthRange                              (double n, double f);
680
681         virtual void                    polygonOffset                   (float factor, float units);
682         virtual void                    provokingVertex                 (deUint32 convention);
683         virtual void                    primitiveRestartIndex   (deUint32 index);
684
685         virtual void                    blendEquation                   (deUint32 mode);
686         virtual void                    blendEquationSeparate   (deUint32 modeRGB, deUint32 modeAlpha);
687         virtual void                    blendFunc                               (deUint32 src, deUint32 dst);
688         virtual void                    blendFuncSeparate               (deUint32 srcRGB, deUint32 dstRGB, deUint32 srcAlpha, deUint32 dstAlpha);
689         virtual void                    blendColor                              (float red, float green, float blue, float alpha);
690
691         virtual void                    colorMask                               (deBool r, deBool g, deBool b, deBool a);
692         virtual void                    depthMask                               (deBool mask);
693         virtual void                    stencilMask                             (deUint32 mask);
694         virtual void                    stencilMaskSeparate             (deUint32 face, deUint32 mask);
695
696         virtual void                    blitFramebuffer                 (int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, deUint32 mask, deUint32 filter);
697
698         virtual void                    invalidateSubFramebuffer(deUint32 target, int numAttachments, const deUint32* attachments, int x, int y, int width, int height);
699         virtual void                    invalidateFramebuffer   (deUint32 target, int numAttachments, const deUint32* attachments);
700
701         virtual void                    bindVertexArray                 (deUint32 array);
702         virtual void                    genVertexArrays                 (int numArrays, deUint32* vertexArrays);
703         virtual void                    deleteVertexArrays              (int numArrays, const deUint32* vertexArrays);
704
705         virtual void                    vertexAttribPointer             (deUint32 index, int size, deUint32 type, deBool normalized, int stride, const void *pointer);
706         virtual void                    vertexAttribIPointer    (deUint32 index, int size, deUint32 type, int stride, const void *pointer);
707         virtual void                    enableVertexAttribArray (deUint32 index);
708         virtual void                    disableVertexAttribArray(deUint32 index);
709         virtual void                    vertexAttribDivisor             (deUint32 index, deUint32 divisor);
710
711         virtual void                    vertexAttrib1f                  (deUint32 index, float);
712         virtual void                    vertexAttrib2f                  (deUint32 index, float, float);
713         virtual void                    vertexAttrib3f                  (deUint32 index, float, float, float);
714         virtual void                    vertexAttrib4f                  (deUint32 index, float, float, float, float);
715         virtual void                    vertexAttribI4i                 (deUint32 index, deInt32, deInt32, deInt32, deInt32);
716         virtual void                    vertexAttribI4ui                (deUint32 index, deUint32, deUint32, deUint32, deUint32);
717
718         virtual deInt32                 getAttribLocation               (deUint32 program, const char *name);
719
720         virtual void                    uniform1f                               (deInt32 location, float);
721         virtual void                    uniform1i                               (deInt32 location, deInt32);
722         virtual void                    uniform1fv                              (deInt32 index, deInt32 count, const float*);
723         virtual void                    uniform2fv                              (deInt32 index, deInt32 count, const float*);
724         virtual void                    uniform3fv                              (deInt32 index, deInt32 count, const float*);
725         virtual void                    uniform4fv                              (deInt32 index, deInt32 count, const float*);
726         virtual void                    uniform1iv                              (deInt32 index, deInt32 count, const deInt32*);
727         virtual void                    uniform2iv                              (deInt32 index, deInt32 count, const deInt32*);
728         virtual void                    uniform3iv                              (deInt32 index, deInt32 count, const deInt32*);
729         virtual void                    uniform4iv                              (deInt32 index, deInt32 count, const deInt32*);
730         virtual void                    uniformMatrix3fv                (deInt32 location, deInt32 count, deInt32 transpose, const float *value);
731         virtual void                    uniformMatrix4fv                (deInt32 location, deInt32 count, deInt32 transpose, const float *value);
732         virtual deInt32                 getUniformLocation              (deUint32 program, const char *name);
733
734         virtual void                    lineWidth                               (float);
735
736         virtual void                    drawArrays                              (deUint32 mode, int first, int count);
737         virtual void                    drawArraysInstanced             (deUint32 mode, int first, int count, int instanceCount);
738         virtual void                    drawElements                    (deUint32 mode, int count, deUint32 type, const void *indices);
739         virtual void                    drawElementsBaseVertex  (deUint32 mode, int count, deUint32 type, const void *indices, int baseVertex);
740         virtual void                    drawElementsInstanced   (deUint32 mode, int count, deUint32 type, const void *indices, int instanceCount);
741         virtual void                    drawElementsInstancedBaseVertex (deUint32 mode, int count, deUint32 type, const void *indices, int instanceCount, int baseVertex);
742         virtual void                    drawRangeElements               (deUint32 mode, deUint32 start, deUint32 end, int count, deUint32 type, const void *indices);
743         virtual void                    drawRangeElementsBaseVertex (deUint32 mode, deUint32 start, deUint32 end, int count, deUint32 type, const void *indices, int baseVertex);
744         virtual void                    drawArraysIndirect              (deUint32 mode, const void *indirect);
745         virtual void                    drawElementsIndirect    (deUint32 mode, deUint32 type, const void *indirect);
746
747         virtual void                    multiDrawArrays                 (deUint32 mode, const int* first, const int* count, int primCount);
748         virtual void                    multiDrawElements               (deUint32 mode, const int* count, deUint32 type, const void** indices, int primCount);
749         virtual void                    multiDrawElementsBaseVertex (deUint32 mode, const int* count, deUint32 type, const void** indices, int primCount, const int* baseVertex);
750
751         virtual deUint32                createProgram                   (ShaderProgram* program);
752         virtual void                    useProgram                              (deUint32 program);
753         virtual void                    deleteProgram                   (deUint32 program);
754
755         virtual void                    readPixels                              (int x, int y, int width, int height, deUint32 format, deUint32 type, void* data);
756         virtual deUint32                getError                                (void);
757         virtual void                    finish                                  (void);
758
759         virtual void                    getIntegerv                             (deUint32 pname, int* params);
760         virtual const char*             getString                               (deUint32 pname);
761
762         // Expose helpers from Context.
763         using Context::readPixels;
764         using Context::texImage2D;
765         using Context::texSubImage2D;
766
767 private:
768                                                         ReferenceContext                (const ReferenceContext& other); // Not allowed!
769         ReferenceContext&               operator=                               (const ReferenceContext& other); // Not allowed!
770
771         void                                    deleteTexture                   (rc::Texture* texture);
772         void                                    deleteFramebuffer               (rc::Framebuffer* framebuffer);
773         void                                    deleteRenderbuffer              (rc::Renderbuffer* renderbuffer);
774         void                                    deleteBuffer                    (rc::DataBuffer* buffer);
775         void                                    deleteVertexArray               (rc::VertexArray* vertexArray);
776         void                                    deleteProgramObject             (rc::ShaderProgramObjectContainer* sp);
777
778         void                                    acquireFboAttachmentReference   (const rc::Framebuffer::Attachment& attachment);
779         void                                    releaseFboAttachmentReference   (const rc::Framebuffer::Attachment& attachment);
780         tcu::PixelBufferAccess  getFboAttachment                (const rc::Framebuffer& framebuffer, rc::Framebuffer::AttachmentPoint point);
781
782         deUint32                                blitResolveMultisampleFramebuffer (deUint32 mask, const tcu::IVec4& srcRect, const tcu::IVec4& dstRect, bool flipX, bool flipY);
783
784         rr::MultisamplePixelBufferAccess        getDrawColorbuffer              (void)  { return (m_drawFramebufferBinding) ? (rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(getFboAttachment(*m_drawFramebufferBinding, rc::Framebuffer::ATTACHMENTPOINT_COLOR0)))  :       (m_defaultColorbuffer);         }
785         rr::MultisamplePixelBufferAccess        getDrawDepthbuffer              (void)  { return (m_drawFramebufferBinding) ? (rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(getFboAttachment(*m_drawFramebufferBinding, rc::Framebuffer::ATTACHMENTPOINT_DEPTH)))   :       (m_defaultDepthbuffer);         }
786         rr::MultisamplePixelBufferAccess        getDrawStencilbuffer    (void)  { return (m_drawFramebufferBinding) ? (rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(getFboAttachment(*m_drawFramebufferBinding, rc::Framebuffer::ATTACHMENTPOINT_STENCIL))) :       (m_defaultStencilbuffer);       }
787         rr::MultisamplePixelBufferAccess        getReadColorbuffer              (void)  { return (m_readFramebufferBinding) ? (rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(getFboAttachment(*m_readFramebufferBinding, rc::Framebuffer::ATTACHMENTPOINT_COLOR0)))  :       (m_defaultColorbuffer);         }
788         rr::MultisamplePixelBufferAccess        getReadDepthbuffer              (void)  { return (m_readFramebufferBinding) ? (rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(getFboAttachment(*m_readFramebufferBinding, rc::Framebuffer::ATTACHMENTPOINT_DEPTH)))   :       (m_defaultDepthbuffer);         }
789         rr::MultisamplePixelBufferAccess        getReadStencilbuffer    (void)  { return (m_readFramebufferBinding) ? (rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(getFboAttachment(*m_readFramebufferBinding, rc::Framebuffer::ATTACHMENTPOINT_STENCIL))) :       (m_defaultStencilbuffer);       }
790
791         const rc::Texture2D&    getTexture2D                    (int unitNdx) const;
792         const rc::TextureCube&  getTextureCube                  (int unitNdx) const;
793         const tcu::IVec4&               getViewport                             (void) const { return m_viewport; }
794
795         void                                    setError                                (deUint32 error);
796
797         void                                    setTex1DBinding                 (int unit, rc::Texture1D*                       tex1D);
798         void                                    setTex2DBinding                 (int unit, rc::Texture2D*                       tex2D);
799         void                                    setTexCubeBinding               (int unit, rc::TextureCube*                     texCube);
800         void                                    setTex2DArrayBinding    (int unit, rc::Texture2DArray*          tex2DArray);
801         void                                    setTex3DBinding                 (int unit, rc::Texture3D*                       tex3D);
802         void                                    setTexCubeArrayBinding  (int unit, rc::TextureCubeArray*        texCubeArray);
803
804         void                                    setBufferBinding                (deUint32 target, rc::DataBuffer* buffer);
805         rc::DataBuffer*                 getBufferBinding                (deUint32 target) const;
806
807         void*                                   getPixelPackPtr                 (void* ptrOffset) const                 { return m_pixelPackBufferBinding ? (void*)((deUintptr)m_pixelPackBufferBinding->getData()+(deUintptr)ptrOffset) : ptrOffset;   }
808         const void*                             getPixelUnpackPtr               (const void* ptrOffset) const   { return m_pixelUnpackBufferBinding ? (const void*)((deUintptr)m_pixelUnpackBufferBinding->getData()+(deUintptr)ptrOffset) : ptrOffset; }
809
810         bool                                    predrawErrorChecks              (deUint32 mode);
811         void                                    drawWithReference               (const rr::PrimitiveList& primitives, int instanceCount);
812
813         // Helpers for getting valid access object based on current unpack state.
814         tcu::ConstPixelBufferAccess             getUnpack2DAccess               (const tcu::TextureFormat& format, int width, int height, const void* data);
815         tcu::ConstPixelBufferAccess             getUnpack3DAccess               (const tcu::TextureFormat& format, int width, int height, int depth, const void* data);
816
817         void                                    uniformv                                (deInt32 index, glu::DataType type, deInt32 count, const void*);
818
819         struct TextureUnit
820         {
821
822                 rc::Texture1D*                  tex1DBinding;
823                 rc::Texture2D*                  tex2DBinding;
824                 rc::TextureCube*                texCubeBinding;
825                 rc::Texture2DArray*             tex2DArrayBinding;
826                 rc::Texture3D*                  tex3DBinding;
827                 rc::TextureCubeArray*   texCubeArrayBinding;
828
829                 rc::Texture1D                   default1DTex;
830                 rc::Texture2D                   default2DTex;
831                 rc::TextureCube                 defaultCubeTex;
832                 rc::Texture2DArray              default2DArrayTex;
833                 rc::Texture3D                   default3DTex;
834                 rc::TextureCubeArray    defaultCubeArrayTex;
835
836                 TextureUnit (void)
837                         : tex1DBinding                  (DE_NULL)
838                         , tex2DBinding                  (DE_NULL)
839                         , texCubeBinding                (DE_NULL)
840                         , tex2DArrayBinding             (DE_NULL)
841                         , tex3DBinding                  (DE_NULL)
842                         , texCubeArrayBinding   (DE_NULL)
843                         , default1DTex                  (0)
844                         , default2DTex                  (0)
845                         , defaultCubeTex                (0)
846                         , default2DArrayTex             (0)
847                         , default3DTex                  (0)
848                         , defaultCubeArrayTex   (0)
849                 {
850                 }
851         };
852
853         struct StencilState
854         {
855                 deUint32                func;
856                 int                             ref;
857                 deUint32                opMask;
858                 deUint32                opStencilFail;
859                 deUint32                opDepthFail;
860                 deUint32                opDepthPass;
861                 deUint32                writeMask;
862
863                 StencilState (void);
864         };
865
866         ReferenceContextLimits                                          m_limits;
867
868         rr::MultisamplePixelBufferAccess                        m_defaultColorbuffer;
869         rr::MultisamplePixelBufferAccess                        m_defaultDepthbuffer;
870         rr::MultisamplePixelBufferAccess                        m_defaultStencilbuffer;
871         rc::VertexArray                                                         m_clientVertexArray;
872
873         tcu::IVec4                                                                      m_viewport;
874
875         rc::ObjectManager<rc::Texture>                          m_textures;
876         rc::ObjectManager<rc::Framebuffer>                      m_framebuffers;
877         rc::ObjectManager<rc::Renderbuffer>                     m_renderbuffers;
878         rc::ObjectManager<rc::DataBuffer>                       m_buffers;
879         rc::ObjectManager<rc::VertexArray>                      m_vertexArrays;
880         rc::ObjectManager<rc::ShaderProgramObjectContainer>             m_programs;
881
882         int                                                                                     m_activeTexture;
883         std::vector<TextureUnit>                                        m_textureUnits;
884         rc::Texture1D                                                           m_emptyTex1D;
885         rc::Texture2D                                                           m_emptyTex2D;
886         rc::TextureCube                                                         m_emptyTexCube;
887         rc::Texture2DArray                                                      m_emptyTex2DArray;
888         rc::Texture3D                                                           m_emptyTex3D;
889         rc::TextureCubeArray                                            m_emptyTexCubeArray;
890
891         int                                                                                     m_pixelUnpackRowLength;
892         int                                                                                     m_pixelUnpackSkipRows;
893         int                                                                                     m_pixelUnpackSkipPixels;
894         int                                                                                     m_pixelUnpackImageHeight;
895         int                                                                                     m_pixelUnpackSkipImages;
896         int                                                                                     m_pixelUnpackAlignment;
897         int                                                                                     m_pixelPackAlignment;
898
899         rc::Framebuffer*                                                        m_readFramebufferBinding;
900         rc::Framebuffer*                                                        m_drawFramebufferBinding;
901         rc::Renderbuffer*                                                       m_renderbufferBinding;
902         rc::VertexArray*                                                        m_vertexArrayBinding;
903         rc::ShaderProgramObjectContainer*                       m_currentProgram;
904
905         rc::DataBuffer*                                                         m_arrayBufferBinding;
906         rc::DataBuffer*                                                         m_pixelPackBufferBinding;
907         rc::DataBuffer*                                                         m_pixelUnpackBufferBinding;
908         rc::DataBuffer*                                                         m_transformFeedbackBufferBinding;
909         rc::DataBuffer*                                                         m_uniformBufferBinding;
910         rc::DataBuffer*                                                         m_copyReadBufferBinding;
911         rc::DataBuffer*                                                         m_copyWriteBufferBinding;
912         rc::DataBuffer*                                                         m_drawIndirectBufferBinding;
913
914         tcu::Vec4                                                                       m_clearColor;
915         float                                                                           m_clearDepth;
916         int                                                                                     m_clearStencil;
917
918         bool                                                                            m_scissorEnabled;
919         tcu::IVec4                                                                      m_scissorBox;
920
921         bool                                                                            m_stencilTestEnabled;
922         StencilState                                                            m_stencil[rr::FACETYPE_LAST];
923
924         bool                                                                            m_depthTestEnabled;
925         deUint32                                                                        m_depthFunc;
926         float                                                                           m_depthRangeNear;
927         float                                                                           m_depthRangeFar;
928
929         float                                                                           m_polygonOffsetFactor;
930         float                                                                           m_polygonOffsetUnits;
931         bool                                                                            m_polygonOffsetFillEnabled;
932
933         bool                                                                            m_provokingFirstVertexConvention;
934
935         bool                                                                            m_blendEnabled;
936         deUint32                                                                        m_blendModeRGB;
937         deUint32                                                                        m_blendModeAlpha;
938         deUint32                                                                        m_blendFactorSrcRGB;
939         deUint32                                                                        m_blendFactorDstRGB;
940         deUint32                                                                        m_blendFactorSrcAlpha;
941         deUint32                                                                        m_blendFactorDstAlpha;
942         tcu::Vec4                                                                       m_blendColor;
943
944         bool                                                                            m_sRGBUpdateEnabled;
945
946         bool                                                                            m_depthClampEnabled;
947
948         tcu::BVec4                                                                      m_colorMask;
949         bool                                                                            m_depthMask;
950
951         std::vector<rr::GenericVec4>                            m_currentAttribs;
952         float                                                                           m_lineWidth;
953
954         bool                                                                            m_primitiveRestartFixedIndex;
955         bool                                                                            m_primitiveRestartSettableIndex;
956         deUint32                                                                        m_primitiveRestartIndex;
957
958         deUint32                                                                        m_lastError;
959
960         rr::FragmentProcessor                                           m_fragmentProcessor;
961         std::vector<rr::Fragment>                                       m_fragmentBuffer;
962         std::vector<float>                                                      m_fragmentDepths;
963 } DE_WARN_UNUSED_TYPE;
964
965 } // sglr
966
967 #endif // _SGLRREFERENCECONTEXT_HPP