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// Shader.h: Defines the abstract Shader class and its concrete derived
16// classes VertexShader and FragmentShader. Implements GL shader objects and
17// related functionality.
18
19
20#ifndef LIBGL_SHADER_H_
21#define LIBGL_SHADER_H_
22
23#include "ResourceManager.h"
24
25#include "compiler/TranslatorASM.h"
26
27#define _GDI32_
28#include <windows.h>
29#include <GL/GL.h>
30#include <GL/glext.h>
31
32#include <string>
33#include <list>
34#include <vector>
35
36namespace glsl
37{
38	class OutputASM;
39}
40
41namespace gl
42{
43
44class Shader : public glsl::Shader
45{
46	friend class Program;
47
48public:
49	Shader(ResourceManager *manager, GLuint handle);
50
51	virtual ~Shader();
52
53	virtual GLenum getType() = 0;
54	GLuint getName() const;
55
56	void deleteSource();
57	void setSource(GLsizei count, const char *const *string, const GLint *length);
58	int getInfoLogLength() const;
59	void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
60	int getSourceLength() const;
61	void getSource(GLsizei bufSize, GLsizei *length, char *source);
62
63	void compile();
64	bool isCompiled();
65
66	void addRef();
67	void release();
68	unsigned int getRefCount() const;
69	bool isFlaggedForDeletion() const;
70	void flagForDeletion();
71
72	static void releaseCompiler();
73
74protected:
75	static bool compilerInitialized;
76	TranslatorASM *createCompiler(GLenum shaderType);
77	void clear();
78
79	static bool compareVarying(const glsl::Varying &x, const glsl::Varying &y);
80
81	char *mSource;
82	std::string infoLog;
83
84private:
85	virtual void createShader() = 0;
86	virtual void deleteShader() = 0;
87
88	const GLuint mHandle;
89	unsigned int mRefCount;     // Number of program objects this shader is attached to
90	bool mDeleteStatus;         // Flag to indicate that the shader can be deleted when no longer in use
91
92	ResourceManager *mResourceManager;
93};
94
95class VertexShader : public Shader
96{
97	friend class Program;
98
99public:
100	VertexShader(ResourceManager *manager, GLuint handle);
101
102	~VertexShader();
103
104	virtual GLenum getType();
105	int getSemanticIndex(const std::string &attributeName);
106
107	virtual sw::Shader *getShader() const;
108	virtual sw::VertexShader *getVertexShader() const;
109
110private:
111	virtual void createShader();
112	virtual void deleteShader();
113
114	sw::VertexShader *vertexShader;
115};
116
117class FragmentShader : public Shader
118{
119public:
120	FragmentShader(ResourceManager *manager, GLuint handle);
121
122	~FragmentShader();
123
124	virtual GLenum getType();
125
126	virtual sw::Shader *getShader() const;
127	virtual sw::PixelShader *getPixelShader() const;
128
129private:
130	virtual void createShader();
131	virtual void deleteShader();
132
133	sw::PixelShader *pixelShader;
134};
135}
136
137#endif   // LIBGL_SHADER_H_
138