Skip to content

Commit 6f1b41d

Browse files
authored
fix(graphics): cache the shader's COMPILE_STATUS to avoid blocking call (#254)
1 parent a484cf9 commit 6f1b41d

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

src/client/graphics/webgl_context.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -378,20 +378,27 @@ namespace client_graphics
378378

379379
int WebGLContext::getShaderParameter(shared_ptr<WebGLShader> shader, int pname)
380380
{
381+
if (pname == WEBGL_SHADER_TYPE)
382+
return static_cast<int>(shader->type);
383+
if (pname == WEBGL_COMPILE_STATUS &&
384+
shader->hasCompileStatus())
385+
return static_cast<int>(shader->getCompileStatus());
386+
387+
// Otherwise, send a command buffer request and wait for the response.
381388
auto req = GetShaderParamCommandBufferRequest(shader->id, pname);
382389
sendCommandBufferRequest(req, true);
383390

384391
auto resp = recvResponse<GetShaderParamCommandBufferResponse>(COMMAND_BUFFER_GET_SHADER_PARAM_RES, req);
385-
if (resp != nullptr) [[likely]]
386-
{
387-
int value = resp->value;
388-
delete resp;
389-
return value;
390-
}
392+
assert(resp != nullptr && "Response should not be null");
393+
394+
shader->setShaderParameters(resp->deleteStatus,
395+
resp->compileStatus);
396+
if (pname == WEBGL_DELETE_STATUS)
397+
return static_cast<int>(resp->deleteStatus);
398+
else if (pname == WEBGL_COMPILE_STATUS)
399+
return static_cast<int>(resp->compileStatus);
391400
else
392-
{
393-
throw runtime_error("Failed to get shader parameter: timeout.");
394-
}
401+
throw runtime_error("Unsupported shader parameter: " + to_string(pname));
395402
}
396403

397404
string WebGLContext::getShaderInfoLog(shared_ptr<WebGLShader> shader)

src/client/graphics/webgl_shader.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,45 @@ namespace client_graphics
1616

1717
class WebGLShader : public WebGLObject
1818
{
19+
friend class WebGLContext;
20+
1921
public:
2022
WebGLShader(WebGLShaderType type)
2123
: WebGLObject(WebGLObjectType::Shader)
2224
, type(type)
2325
{
2426
}
2527

28+
bool hasDeleteStatus() const
29+
{
30+
return delete_status_.has_value();
31+
}
32+
bool hasCompileStatus() const
33+
{
34+
return compile_status_.has_value();
35+
}
36+
bool getDeleteStatus()
37+
{
38+
return delete_status_.value_or(false);
39+
}
40+
bool getCompileStatus()
41+
{
42+
return compile_status_.value_or(false);
43+
}
44+
45+
private:
46+
void setShaderParameters(bool deleteStatus, bool compileStatus)
47+
{
48+
delete_status_ = deleteStatus;
49+
compile_status_ = compileStatus;
50+
}
51+
2652
public:
2753
WebGLShaderType type;
2854
std::string source;
55+
56+
private:
57+
std::optional<bool> delete_status_;
58+
std::optional<bool> compile_status_;
2959
};
3060
}

src/common/command_buffers/details/shader.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,20 @@ namespace commandbuffers
334334
{
335335
public:
336336
GetShaderParamCommandBufferResponse() = delete;
337-
GetShaderParamCommandBufferResponse(GetShaderParamCommandBufferRequest *req, int32_t value)
337+
GetShaderParamCommandBufferResponse(GetShaderParamCommandBufferRequest *req)
338338
: TrCommandBufferSimpleResponse(COMMAND_BUFFER_GET_SHADER_PARAM_RES, req)
339-
, value(value)
340339
{
341340
}
342341
GetShaderParamCommandBufferResponse(const GetShaderParamCommandBufferResponse &that, bool clone)
343342
: TrCommandBufferSimpleResponse(that, clone)
344-
, value(that.value)
343+
, deleteStatus(that.deleteStatus)
344+
, compileStatus(that.compileStatus)
345345
{
346346
}
347347

348348
public:
349-
int32_t value;
349+
bool deleteStatus;
350+
bool compileStatus;
350351
};
351352

352353
class GetShaderInfoLogCommandBufferRequest final

src/renderer/render_api_opengles.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,12 @@ class RHI_OpenGL : public TrRenderHardwareInterface
850850
{
851851
auto &glObjectManager = reqContentRenderer->getContextGL()->ObjectManagerRef();
852852
GLuint shader = glObjectManager.FindShader(req->shader);
853-
GLint value;
854-
glGetShaderiv(shader, req->pname, &value);
853+
GetShaderParamCommandBufferResponse res(req);
854+
glGetShaderiv(shader, GL_DELETE_STATUS, reinterpret_cast<GLint *>(&res.deleteStatus));
855+
glGetShaderiv(shader, GL_COMPILE_STATUS, reinterpret_cast<GLint *>(&res.compileStatus));
855856

856-
GetShaderParamCommandBufferResponse res(req, value);
857857
if (TR_UNLIKELY(CheckError(req, reqContentRenderer) != GL_NO_ERROR || options.printsCall))
858-
DEBUG(DEBUG_TAG, "[%d] GL::GetShaderParameter: %d", options.isDefaultQueue(), res.value);
858+
PrintDebugInfo(req, nullptr, nullptr, options);
859859
reqContentRenderer->sendCommandBufferResponse(res);
860860
}
861861
TR_OPENGL_FUNC void OnGetShaderInfoLog(GetShaderInfoLogCommandBufferRequest *req,

0 commit comments

Comments
 (0)