openGL API glShaderSource详解

openGL API glShaderSource详解

官方文档

 [函数说明](https://www.khronos.org/registry/OpenGL-Refpages/gl4/)
 Name

glShaderSource — Replaces the source code in a shader object

C Specification
void glShaderSource( GLuint shader,
GLsizei count,
const GLchar **string,
const GLint *length);

Parameters
shader
Specifies the handle of the shader object whose source code is to be replaced.

count
Specifies the number of elements in the string and length arrays.

string
Specifies an array of pointers to strings containing the source code to be loaded into the shader.

length
Specifies an array of string lengths.

Description
glShaderSource sets the source code in shader to the source code in the array of strings specified by string. Any source code previously stored in the shader object is completely replaced. The number of strings in the array is specified by count. If length is NULL, each string is assumed to be null terminated. If length is a value other than NULL, it points to an array containing a string length for each of the corresponding elements of string. Each element in the length array may contain the length of the corresponding string (the null character is not counted as part of the string length) or a value less than 0 to indicate that the string is null terminated. The source code strings are not scanned or parsed at this time; they are simply copied into the specified shader object.

Notes
OpenGL copies the shader source code strings when glShaderSource is called, so an application may free its copy of the source code strings immediately after the function returns.

Errors
GL_INVALID_VALUE is generated if shader is not a value generated by OpenGL.

GL_INVALID_OPERATION is generated if shader is not a shader object.

GL_INVALID_VALUE is generated if count is less than 0.

Associated Gets
glGetShader with arguments shader and GL_SHADER_SOURCE_LENGTH

glGetShaderSource with argument shader

glIsShader

Version Support
OpenGL Version
Function / Feature Name 2.0 2.1 3.0 3.1 3.2 3.3 4.0 4.1 4.2 4.3 4.4 4.5
glShaderSource ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
See Also
glCompileShader, glCreateShader, glDeleteShader

Copyright
Copyright © 2003-2005 3Dlabs Inc. Ltd. Copyright © 2010-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. http://opencontent.org/openpub/.

翻译

名称
glShaderSource - 替换着色器对象中的源代码

C规范
void glShaderSource(GLuint shader,GLsizei count,const GLchar * const *string,const GLint *length);

参数
shader

要被替换源代码的着色器对象的句柄(ID)。

count

指定字符串和长度数组中的元素数。

string

指定指向包含要加载到着色器的源代码的字符串的指针数组。

length

指定字符串长度的数组。

描述
对于支持着色器编译器的实现,glShaderSource将着色器中的源代码设置为string指定的字符串数组中的源代码。先前存储在着色器对象中的任何源代码都将被完全替换。数组中的字符串数由count指定。 如果length为NULL,则认为每个字符串都以null结尾。如果length不是NULL,则它指向包含字符串的每个相应元素的字符串长度的数组。length数组中的每个元素可以包含相应字符串的长度(空字符不计为字符串长度的一部分)或小于0的值以表示该字符串为空终止。此时不扫描或解析源代码字符串; 它们只是复制到指定的着色器对象中。

注意
着色器编译器支持是可选的,因此必须在使用之前通过使用参数GL_SHADER_COMPILER调用glGet来查询。glShaderSource,glCompileShader,glGetShaderPrecisionFormat,glReleaseShaderCompiler等在不支持着色器编译器的实现上都将生成GL_INVALID_OPERATION。这样的实现提供了glShaderBinary替代方案,用于提供预编译的着色器二进制文件。

调用glShaderSource时,OpenGL会复制着色器源代码字符串,因此应用程序可以在函数返回后立即释放源代码字符串的副本。

错误
GL_INVALID_OPERATION:不支持着色器编译器

GL_INVALID_VALUE:shader不是OpenGL生成的值

GL_INVALID_OPERATION:shader不是着色器对象

GL_INVALID_VALUE:count比0小

相关Gets
glGet 参数GL_SHADER_COMPILER

glGetShaderiv 参数shader和GL_SHADER_SOURCE_LENGTH

glGetShaderSource 参数shader

glIsShader

另见
glCompileShader,glGetShaderPrecisionFormat,glCreateShader,glDeleteShader

版权
https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glShaderSource.xml

https://blog.csdn.net/flycatdeng

Copyright © 1991-2006 Silicon Graphics, Inc.本文档的许可是根据SGI Free Software B License.详见http://oss.sgi.com/projects/FreeB/.

例子

GLuint createShaderProgram()
{
	GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
	GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
	GLuint vfProagram = glCreateProgram();
	
	string vertexShaderStr = readFile("vertShader.glsl");
	string fragShaderStr = readFile("fragShader.glsl");
	const char* vertShaderSrc = vertexShaderStr.c_str();
	const char* fragShaderSrc = fragShaderStr.c_str();

	glShaderSource(vShader, 1, &vertShaderSrc, nullptr);
	glShaderSource(fShader, 1, &fragShaderSrc, nullptr);
	glCompileShader(vShader);
	glCompileShader(fShader);
	
	glAttachShader(vfProagram, vShader);
	glAttachShader(vfProagram, fShader);
	glLinkProgram(vfProagram);

	return vfProagram;
}

运行效果

openGL API glShaderSource详解

工程下载

工程源码下载

上一篇:详解 OpenGL ES 2.x 渲染流程


下一篇:Linux之SDL2+OpenGL+EGL绘制(十六)