1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef gl_Device_hpp
16#define gl_Device_hpp
17
18#include "Renderer/Renderer.hpp"
19
20namespace gl
21{
22	class Image;
23	class Texture;
24
25	enum PrimitiveType
26	{
27		DRAW_POINTLIST,
28		DRAW_LINELIST,
29		DRAW_LINESTRIP,
30		DRAW_LINELOOP,
31		DRAW_TRIANGLELIST,
32		DRAW_TRIANGLESTRIP,
33		DRAW_TRIANGLEFAN,
34		DRAW_QUADLIST
35	};
36
37	struct Viewport
38	{
39		int x0;
40		int y0;
41		unsigned int width;
42		unsigned int height;
43		float minZ;
44		float maxZ;
45	};
46
47	class Device : public sw::Renderer
48	{
49	public:
50		explicit Device(sw::Context *context);
51
52		virtual ~Device();
53
54		void *operator new(size_t size);
55		void operator delete(void * mem);
56
57		void clearColor(float red, float green, float blue, float alpha, unsigned int rgbaMask);
58		void clearDepth(float z);
59		void clearStencil(unsigned int stencil, unsigned int mask);
60		Image *createDepthStencilSurface(unsigned int width, unsigned int height, sw::Format format, int multiSampleDepth, bool discard);
61		Image *createRenderTarget(unsigned int width, unsigned int height, sw::Format format, int multiSampleDepth, bool lockable);
62		void drawIndexedPrimitive(PrimitiveType type, unsigned int indexOffset, unsigned int primitiveCount, int indexSize);
63		void drawPrimitive(PrimitiveType primitiveType, unsigned int primiveCount);
64		void setDepthStencilSurface(Image *newDepthStencil);
65		void setPixelShader(sw::PixelShader *shader);
66		void setPixelShaderConstantF(unsigned int startRegister, const float *constantData, unsigned int count);
67		void setScissorEnable(bool enable);
68		void setRenderTarget(int index, Image *renderTarget);
69		void setScissorRect(const sw::Rect &rect);
70		void setVertexShader(sw::VertexShader *shader);
71		void setVertexShaderConstantF(unsigned int startRegister, const float *constantData, unsigned int count);
72		void setViewport(const Viewport &viewport);
73
74		virtual bool stretchRect(Image *sourceSurface, const sw::SliceRect *sourceRect, Image *destSurface, const sw::SliceRect *destRect, bool filter);
75		virtual void finish();
76
77	private:
78		sw::Context *const context;
79
80		bool bindResources();
81		void bindShaderConstants();
82		bool bindViewport();   // Also adjusts for scissoring
83
84		bool validRectangle(const sw::Rect *rect, Image *surface);
85
86		Viewport viewport;
87		sw::Rect scissorRect;
88		bool scissorEnable;
89
90		sw::PixelShader *pixelShader;
91		sw::VertexShader *vertexShader;
92
93		bool pixelShaderDirty;
94		unsigned int pixelShaderConstantsFDirty;
95		bool vertexShaderDirty;
96		unsigned int vertexShaderConstantsFDirty;
97
98		float pixelShaderConstantF[sw::FRAGMENT_UNIFORM_VECTORS][4];
99		float vertexShaderConstantF[sw::VERTEX_UNIFORM_VECTORS][4];
100
101		Image *renderTarget;
102		Image *depthStencil;
103	};
104}
105
106#endif   // gl_Device_hpp
107