79 const std::shared_ptr<PipelineGLES>& pipeline,
80 const std::shared_ptr<const ShaderFunction>& vert_function,
81 const std::shared_ptr<const ShaderFunction>& frag_function) {
84 const auto& descriptor = pipeline->GetDescriptor();
87 ShaderFunctionGLES::Cast(*vert_function).GetSourceMapping();
89 ShaderFunctionGLES::Cast(*frag_function).GetSourceMapping();
93 auto vert_shader = gl.CreateShader(GL_VERTEX_SHADER);
94 auto frag_shader = gl.CreateShader(GL_FRAGMENT_SHADER);
96 if (vert_shader == 0 || frag_shader == 0) {
101 gl.SetDebugLabel(DebugResourceType::kShader, vert_shader,
102 SPrintF(
"%s Vertex Shader", descriptor.GetLabel().c_str()));
104 DebugResourceType::kShader, frag_shader,
105 SPrintF(
"%s Fragment Shader", descriptor.GetLabel().c_str()));
108 [&gl, vert_shader]() { gl.DeleteShader(vert_shader); });
110 [&gl, frag_shader]() { gl.DeleteShader(frag_shader); });
112 gl.ShaderSourceMapping(vert_shader, *vert_mapping,
113 descriptor.GetSpecializationConstants());
114 gl.ShaderSourceMapping(frag_shader, *frag_mapping,
115 descriptor.GetSpecializationConstants());
117 gl.CompileShader(vert_shader);
118 gl.CompileShader(frag_shader);
120 GLint vert_status = GL_FALSE;
121 GLint frag_status = GL_FALSE;
123 gl.GetShaderiv(vert_shader, GL_COMPILE_STATUS, &vert_status);
124 gl.GetShaderiv(frag_shader, GL_COMPILE_STATUS, &frag_status);
126 if (vert_status != GL_TRUE) {
128 *vert_mapping, ShaderStage::kVertex);
132 if (frag_status != GL_TRUE) {
134 *frag_mapping, ShaderStage::kFragment);
138 auto program = reactor.
GetGLHandle(pipeline->GetProgramHandle());
139 if (!program.has_value()) {
144 gl.AttachShader(*program, vert_shader);
145 gl.AttachShader(*program, frag_shader);
148 [&gl, program = *program, vert_shader]() {
149 gl.DetachShader(program, vert_shader);
152 [&gl, program = *program, frag_shader]() {
153 gl.DetachShader(program, frag_shader);
156 for (
const auto& stage_input :
157 descriptor.GetVertexDescriptor()->GetStageInputs()) {
158 gl.BindAttribLocation(*program,
159 static_cast<GLuint
>(stage_input.location),
164 gl.LinkProgram(*program);
166 GLint link_status = GL_FALSE;
167 gl.GetProgramiv(*program, GL_LINK_STATUS, &link_status);
169 if (link_status != GL_TRUE) {
171 << gl.GetProgramInfoLogString(*program);
185 if (
auto found = pipelines_.find(descriptor); found != pipelines_.end()) {
186 return found->second;
192 RealizedFuture<std::shared_ptr<Pipeline<PipelineDescriptor>>>(
nullptr)};
198 if (!vert_function || !frag_function) {
200 <<
"Could not find stage entrypoint functions in pipeline descriptor.";
203 RealizedFuture<std::shared_ptr<Pipeline<PipelineDescriptor>>>(
nullptr)};
206 auto promise = std::make_shared<
207 std::promise<std::shared_ptr<Pipeline<PipelineDescriptor>>>>();
208 auto pipeline_future =
210 pipelines_[descriptor] = pipeline_future;
211 auto weak_this = weak_from_this();
213 auto result = reactor_->AddOperation(
214 [promise, weak_this, reactor_ptr = reactor_, descriptor, vert_function,
216 auto strong_this = weak_this.lock();
218 promise->set_value(
nullptr);
219 VALIDATION_LOG <<
"Library was collected before a pending pipeline "
220 "creation could finish.";
223 auto pipeline = std::shared_ptr<PipelineGLES>(
224 new PipelineGLES(reactor_ptr, strong_this, descriptor));
225 auto program = reactor.
GetGLHandle(pipeline->GetProgramHandle());
226 if (!program.has_value()) {
227 promise->set_value(
nullptr);
237 promise->set_value(
nullptr);
241 if (!pipeline->BuildVertexDescriptor(reactor.
GetProcTable(),
243 promise->set_value(
nullptr);
247 if (!pipeline->IsValid()) {
248 promise->set_value(
nullptr);
252 promise->set_value(std::move(pipeline));
256 return pipeline_future;