20 : reactor_(
std::move(reactor)) {}
24 gl.GetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
25 if (log_length == 0) {
29 reinterpret_cast<char*
>(std::calloc(log_length,
sizeof(
char)));
30 gl.GetShaderInfoLog(shader, log_length, &log_length, log_buffer);
31 auto log_string = std::string(log_buffer, log_length);
32 std::free(log_buffer);
40 auto data =
static_cast<char*
>(malloc(10240));
42 gl.GetShaderSource(shader, 10240, &
length,
data);
44 auto result = std::string{
data,
static_cast<size_t>(
length)};
51 std::string_view
name,
54 std::stringstream stream;
55 stream <<
"Failed to compile ";
57 case ShaderStage::kUnknown:
60 case ShaderStage::kVertex:
63 case ShaderStage::kFragment:
66 case ShaderStage::kCompute:
70 stream <<
" shader for '" <<
name <<
"' with error:" << std::endl;
72 stream <<
"Shader source was: " << std::endl;
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) {
102 std::format(
"{} Vertex Shader", descriptor.GetLabel()));
103 gl.SetDebugLabel(DebugResourceType::kShader, frag_shader,
104 std::format(
"{} Fragment Shader", descriptor.GetLabel()));
107 [&gl, vert_shader]() { gl.DeleteShader(vert_shader); });
109 [&gl, frag_shader]() { gl.DeleteShader(frag_shader); });
111 gl.ShaderSourceMapping(vert_shader, *vert_mapping,
112 descriptor.GetSpecializationConstants());
113 gl.ShaderSourceMapping(frag_shader, *frag_mapping,
114 descriptor.GetSpecializationConstants());
116 gl.CompileShader(vert_shader);
117 gl.CompileShader(frag_shader);
119 GLint vert_status = GL_FALSE;
120 GLint frag_status = GL_FALSE;
122 gl.GetShaderiv(vert_shader, GL_COMPILE_STATUS, &vert_status);
123 gl.GetShaderiv(frag_shader, GL_COMPILE_STATUS, &frag_status);
125 if (vert_status != GL_TRUE) {
127 *vert_mapping, ShaderStage::kVertex);
131 if (frag_status != GL_TRUE) {
133 *frag_mapping, ShaderStage::kFragment);
137 auto program = reactor.
GetGLHandle(pipeline->GetProgramHandle());
138 if (!program.has_value()) {
143 gl.AttachShader(*program, vert_shader);
144 gl.AttachShader(*program, frag_shader);
147 [&gl, program = *program, vert_shader]() {
148 gl.DetachShader(program, vert_shader);
151 [&gl, program = *program, frag_shader]() {
152 gl.DetachShader(program, frag_shader);
155 for (
const auto& stage_input :
156 descriptor.GetVertexDescriptor()->GetStageInputs()) {
157 gl.BindAttribLocation(*program,
158 static_cast<GLuint
>(stage_input.location),
163 gl.LinkProgram(*program);
165 GLint link_status = GL_FALSE;
166 gl.GetProgramiv(*program, GL_LINK_STATUS, &link_status);
168 if (link_status != GL_TRUE) {
170 << gl.GetProgramInfoLogString(*program)
171 <<
"\nVertex Shader:\n"
180bool PipelineLibraryGLES::IsValid()
const {
181 return reactor_ !=
nullptr;
184std::shared_ptr<PipelineGLES> PipelineLibraryGLES::CreatePipeline(
185 const std::weak_ptr<PipelineLibrary>& weak_library,
186 const PipelineDescriptor& desc,
187 const std::shared_ptr<const ShaderFunction>& vert_function,
188 const std::shared_ptr<const ShaderFunction>& frag_function,
190 auto strong_library = weak_library.lock();
192 if (!strong_library) {
193 VALIDATION_LOG <<
"Library was collected before a pending pipeline "
194 "creation could finish.";
198 auto& library = PipelineLibraryGLES::Cast(*strong_library);
200 const auto& reactor = library.GetReactor();
206 auto program_key = ProgramKey{vert_function, frag_function,
207 desc.GetSpecializationConstants()};
209 auto cached_program = library.GetProgramForKey(program_key);
211 const auto has_cached_program = !!cached_program;
213 std::shared_ptr<UniqueHandleGLES> program_handle =
nullptr;
214 if (has_cached_program) {
215 program_handle = std::move(cached_program);
217 program_handle = threadsafe ? std::make_shared<UniqueHandleGLES>(
218 reactor, HandleType::kProgram)
219 :
std::make_shared<UniqueHandleGLES>(
220 UniqueHandleGLES::MakeUntracked(
224 auto pipeline = std::shared_ptr<PipelineGLES>(
225 new PipelineGLES(reactor,
228 std::move(program_handle)));
230 auto program = reactor->GetGLHandle(pipeline->GetProgramHandle());
232 if (!program.has_value()) {
237 const auto link_result = !has_cached_program ?
LinkProgram(*reactor,
249 if (!pipeline->BuildVertexDescriptor(reactor->GetProcTable(),
255 if (!pipeline->IsValid()) {
260 if (!has_cached_program) {
261 library.SetProgramForKey(program_key, pipeline->GetSharedHandle());
268PipelineFuture<PipelineDescriptor> PipelineLibraryGLES::GetPipeline(
269 PipelineDescriptor descriptor,
273 return found->second;
279 RealizedFuture<std::shared_ptr<Pipeline<PipelineDescriptor>>>(
nullptr)};
282 auto vert_function = descriptor.GetEntrypointForStage(ShaderStage::kVertex);
283 auto frag_function = descriptor.GetEntrypointForStage(ShaderStage::kFragment);
285 if (!vert_function || !frag_function) {
287 <<
"Could not find stage entrypoint functions in pipeline descriptor.";
290 RealizedFuture<std::shared_ptr<Pipeline<PipelineDescriptor>>>(
nullptr)};
293 auto promise = std::make_shared<
294 std::promise<std::shared_ptr<Pipeline<PipelineDescriptor>>>>();
295 auto pipeline_future =
296 PipelineFuture<PipelineDescriptor>{descriptor, promise->get_future()};
299 const auto result = reactor_->AddOperation([promise,
300 weak_this = weak_from_this(),
305 ](
const ReactorGLES& reactor) {
306 promise->set_value(CreatePipeline(weak_this, descriptor, vert_function,
307 frag_function, threadsafe));
311 return pipeline_future;
315PipelineFuture<ComputePipelineDescriptor> PipelineLibraryGLES::GetPipeline(
316 ComputePipelineDescriptor descriptor,
318 auto promise = std::make_shared<
319 std::promise<std::shared_ptr<Pipeline<ComputePipelineDescriptor>>>>();
320 promise->set_value(
nullptr);
321 return {descriptor, promise->get_future()};
325bool PipelineLibraryGLES::HasPipeline(
const PipelineDescriptor& descriptor) {
330void PipelineLibraryGLES::RemovePipelinesWithEntryPoint(
331 std::shared_ptr<const ShaderFunction> function) {
332 Lock lock(programs_mutex_);
334 PipelineMap::iterator it =
pipelines_.begin();
336 const PipelineDescriptor& desc = it->first;
337 if (desc.GetEntrypointForStage(
function->GetStage())->IsEqual(*function)) {
338 const std::shared_ptr<const ShaderFunction>& vert_function =
339 desc.GetEntrypointForStage(ShaderStage::kVertex);
340 const std::shared_ptr<const ShaderFunction>& frag_function =
341 desc.GetEntrypointForStage(ShaderStage::kFragment);
342 ProgramKey program_key{vert_function, frag_function,
343 desc.GetSpecializationConstants()};
344 programs_.erase(program_key);
353PipelineLibraryGLES::~PipelineLibraryGLES() =
default;
355const std::shared_ptr<ReactorGLES>& PipelineLibraryGLES::GetReactor()
const {
359std::shared_ptr<UniqueHandleGLES> PipelineLibraryGLES::GetProgramForKey(
360 const ProgramKey&
key) {
361 Lock lock(programs_mutex_);
362 auto found = programs_.find(
key);
363 if (found != programs_.end()) {
364 return found->second;
369void PipelineLibraryGLES::SetProgramForKey(
370 const ProgramKey&
key,
371 std::shared_ptr<UniqueHandleGLES> program) {
372 Lock lock(programs_mutex_);
373 programs_[
key] = std::move(program);
Wraps a closure that is invoked in the destructor unless released by the caller.
PipelineLibraryGLES(const PipelineLibraryGLES &)=delete
bool SetDebugLabel(DebugResourceType type, GLint name, std::string_view label) const
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about.
std::optional< GLuint > GetGLHandle(const HandleGLES &handle) const
Returns the OpenGL handle for a reactor handle if one is available. This is typically only safe to ca...
const ProcTableGLES & GetProcTable() const
Get the OpenGL proc. table the reactor uses to manage handles.
std::vector< std::pair< uint64_t, std::unique_ptr< GenericRenderPipelineHandle > > > pipelines_
#define FML_CHECK(condition)
Dart_NativeFunction function
static void LogShaderCompilationFailure(const ProcTableGLES &gl, GLuint shader, std::string_view name, const fml::Mapping &source_mapping, ShaderStage stage)
static std::string GetShaderSource(const ProcTableGLES &gl, GLuint shader)
static std::string GetShaderInfoLog(const ProcTableGLES &gl, GLuint shader)
static bool LinkProgram(const ReactorGLES &reactor, const std::shared_ptr< PipelineGLES > &pipeline, const std::shared_ptr< const ShaderFunction > &vert_function, const std::shared_ptr< const ShaderFunction > &frag_function)
std::shared_ptr< const fml::Mapping > data
#define TRACE_EVENT0(category_group, name)