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