MCPcopy Create free account
hub / github.com/android/ndk-samples / Compile

Method Compile

endless-tunnel/app/src/main/cpp/shader.cpp:60–127  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

58}
59
60void Shader::Compile() {
61 const char *vsrc = 0, *fsrc = 0;
62 GLint status = 0;
63
64 LOGD("Compiling shader.");
65 LOGD("Shader name: %s", GetShaderName());
66
67 vsrc = GetVertShaderSource();
68 fsrc = GetFragShaderSource();
69
70 mVertShaderH = glCreateShader(GL_VERTEX_SHADER);
71 mFragShaderH = glCreateShader(GL_FRAGMENT_SHADER);
72 if (!mVertShaderH || !mFragShaderH) {
73 LOGE("*** Failed to create shader.");
74 ABORT_GAME;
75 }
76 glShaderSource(mVertShaderH, 1, &vsrc, NULL);
77 glCompileShader(mVertShaderH);
78 glGetShaderiv(mVertShaderH, GL_COMPILE_STATUS, &status);
79 if (status == 0) {
80 LOGE("*** Vertex shader compilation failed.");
81 _printShaderLog(mVertShaderH);
82 ABORT_GAME;
83 }
84 LOGD("Vertex shader compilation succeeded.");
85
86 glShaderSource(mFragShaderH, 1, &fsrc, NULL);
87 glCompileShader(mFragShaderH);
88 glGetShaderiv(mFragShaderH, GL_COMPILE_STATUS, &status);
89 if (status == 0) {
90 LOGE("*** Fragment shader compilation failed, %d", status);
91 _printShaderLog(mFragShaderH);
92 ABORT_GAME;
93 }
94 LOGD("Fragment shader compilation succeeded.");
95
96 mProgramH = glCreateProgram();
97 if (!mProgramH) {
98 LOGE("*** Failed to create program");
99 _printProgramLog(mProgramH);
100 ABORT_GAME;
101 }
102
103 glAttachShader(mProgramH, mVertShaderH);
104 glAttachShader(mProgramH, mFragShaderH);
105 glLinkProgram(mProgramH);
106 glGetProgramiv(mProgramH, GL_LINK_STATUS, &status);
107 if (status == 0) {
108 LOGE("*** Shader program link failed, %d", status);
109 _printProgramLog(mProgramH);
110 ABORT_GAME;
111 }
112 LOGD("Program linking succeeded.");
113
114 glUseProgram(mProgramH);
115 mMVPMatrixLoc = glGetUniformLocation(mProgramH, "u_MVP");
116 if (mMVPMatrixLoc < 0) {
117 LOGE("*** Couldn't get shader's u_MVP matrix location from shader.");

Callers 2

OnStartGraphicsMethod · 0.45
OnStartGraphicsMethod · 0.45

Calls 2

_printShaderLogFunction · 0.85
_printProgramLogFunction · 0.85

Tested by

no test coverage detected