Skip to content

Commit c7631f3

Browse files
committed
sokol: Don't request vs/fs slot by string each command
Also put compare code behind ifdef for testing
1 parent 95fe86a commit c7631f3

File tree

3 files changed

+126
-131
lines changed

3 files changed

+126
-131
lines changed

Source/Render/sokol/SokolRenderPipeline.cpp

+71-88
Original file line numberDiff line numberDiff line change
@@ -227,93 +227,6 @@ void cSokolRender::RegisterPipeline(SokolPipelineContext context) {
227227
fprintf(stderr, "RegisterPipeline: invalid shader ID pipeline '%s'\n", desc.label);
228228
return;
229229
}
230-
SOKOL_SHADER_ID shader_id = context.shader_funcs->get_id();
231-
switch (shader_id) {
232-
case SOKOL_SHADER_ID_mesh_color_tex1:
233-
case SOKOL_SHADER_ID_mesh_color_tex2:
234-
if (0 > context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_VS, "mesh_color_texture_vs_params")) {
235-
fprintf(stderr, "RegisterPipeline: 'mesh_color_texture_vs_params' uniform slot not found at pipeline '%s'\n", desc.label);
236-
xassert(0);
237-
return;
238-
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_VS, "mesh_color_texture_vs_params") != sizeof(mesh_color_texture_vs_params_t)) {
239-
fprintf(stderr, "RegisterPipeline: 'mesh_color_texture_vs_params' uniform size doesnt match at pipeline '%s'\n", desc.label);
240-
xassert(0);
241-
return;
242-
} else if (0 > context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_FS, "mesh_color_texture_fs_params")) {
243-
fprintf(stderr, "RegisterPipeline: 'mesh_color_texture_fs_params' uniform slot not found at pipeline '%s'\n", desc.label);
244-
xassert(0);
245-
return;
246-
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_FS, "mesh_color_texture_fs_params") != sizeof(mesh_color_texture_fs_params_t)) {
247-
fprintf(stderr, "RegisterPipeline: 'mesh_color_texture_fs_params' uniform size doesnt match at pipeline '%s'\n", desc.label);
248-
xassert(0);
249-
return;
250-
}
251-
break;
252-
case SOKOL_SHADER_ID_mesh_normal_tex1:
253-
if (0 > context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_VS, "mesh_normal_texture_vs_params")) {
254-
fprintf(stderr, "RegisterPipeline: 'mesh_normal_texture_vs_params' uniform slot not found at pipeline '%s'\n", desc.label);
255-
xassert(0);
256-
return;
257-
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_VS, "mesh_normal_texture_vs_params") != sizeof(mesh_normal_texture_vs_params_t)) {
258-
fprintf(stderr, "RegisterPipeline: 'mesh_normal_texture_vs_params' uniform size doesnt match at pipeline '%s'\n", desc.label);
259-
xassert(0);
260-
return;
261-
}
262-
break;
263-
case SOKOL_SHADER_ID_shadow_tex1:
264-
case SOKOL_SHADER_ID_shadow_normal_tex1:
265-
if (0 > context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_VS, "shadow_texture_vs_params")) {
266-
fprintf(stderr, "RegisterPipeline: 'shadow_texture_vs_params' uniform slot not found at pipeline '%s'\n", desc.label);
267-
xassert(0);
268-
return;
269-
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_VS, "shadow_texture_vs_params") != sizeof(shadow_texture_vs_params_t)) {
270-
fprintf(stderr, "RegisterPipeline: 'shadow_texture_vs_params' uniform size doesnt match at pipeline '%s'\n", desc.label);
271-
xassert(0);
272-
return;
273-
} else if (0 > context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_FS, "shadow_texture_fs_params")) {
274-
fprintf(stderr, "RegisterPipeline: 'shadow_texture_fs_params' uniform slot not found at pipeline '%s'\n", desc.label);
275-
xassert(0);
276-
return;
277-
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_FS, "shadow_texture_fs_params") != sizeof(mesh_color_texture_fs_params_t)) {
278-
fprintf(stderr, "RegisterPipeline: 'shadow_texture_fs_params' uniform size doesnt match at pipeline '%s'\n", desc.label);
279-
xassert(0);
280-
return;
281-
}
282-
break;
283-
case SOKOL_SHADER_ID_mesh_tex1:
284-
if (0 > context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_VS, "mesh_texture_vs_params")) {
285-
fprintf(stderr, "RegisterPipeline: 'mesh_texture_vs_params' uniform slot not found at pipeline '%s'\n", desc.label);
286-
xassert(0);
287-
return;
288-
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_VS, "mesh_texture_vs_params") != sizeof(mesh_texture_vs_params_t)) {
289-
fprintf(stderr, "RegisterPipeline: 'mesh_texture_vs_params' uniform size doesnt match at pipeline '%s'\n", desc.label);
290-
xassert(0);
291-
return;
292-
}
293-
break;
294-
case SOKOL_SHADER_ID_tile_map:
295-
if (0 > context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_VS, "tile_map_vs_params")) {
296-
fprintf(stderr, "RegisterPipeline: 'tile_map_vs_params' uniform slot not found at pipeline '%s'\n", desc.label);
297-
xassert(0);
298-
return;
299-
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_VS, "tile_map_vs_params") != sizeof(tile_map_vs_params_t)) {
300-
fprintf(stderr, "RegisterPipeline: 'tile_map_vs_params' uniform size doesnt match at pipeline '%s'\n", desc.label);
301-
xassert(0);
302-
return;
303-
} else if (0 > context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_FS, "tile_map_fs_params")) {
304-
fprintf(stderr, "RegisterPipeline: 'tile_map_fs_params' uniform slot not found at pipeline '%s'\n", desc.label);
305-
xassert(0);
306-
return;
307-
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_FS, "tile_map_fs_params") != sizeof(tile_map_fs_params_t)) {
308-
fprintf(stderr, "RegisterPipeline: 'tile_map_fs_params' uniform size doesnt match at pipeline '%s'\n", desc.label);
309-
xassert(0);
310-
return;
311-
}
312-
break;
313-
default:
314-
fprintf(stderr, "RegisterPipeline: Unknown shader id '%d' at pipeline '%s'\n", shader_id, desc.label);
315-
return;
316-
}
317230

318231
//Check for any missing slots
319232
if (context.vertex_fmt & VERTEX_FMT_TEX1 &&
@@ -339,7 +252,8 @@ void cSokolRender::RegisterPipeline(SokolPipelineContext context) {
339252
bind_vertex_fmt(context, VERTEX_FMT_NORMAL);
340253

341254
//Created, store on our pipelines
342-
//printf("RegisterPipeline: '%s' at '%d'\n", desc.label, id);
255+
SOKOL_SHADER_ID shader_id = context.shader_funcs->get_id();
256+
//printf("RegisterPipeline: '%s' at '%d'\n", desc.label, shader_id);
343257
SokolPipeline* pipeline = new SokolPipeline {
344258
context,
345259
sg_make_pipeline(desc),
@@ -354,6 +268,75 @@ void cSokolRender::RegisterPipeline(SokolPipelineContext context) {
354268
xxassert(0, "RegisterPipeline: invalid pipeline vertex format " + std::string(desc.label));
355269
return;
356270
}
271+
const char* vs_params_name = nullptr;
272+
const char* fs_params_name = nullptr;
273+
size_t vs_params_size = 0;
274+
size_t fs_params_size = 0;
275+
switch (shader_id) {
276+
case SOKOL_SHADER_ID_mesh_color_tex1:
277+
case SOKOL_SHADER_ID_mesh_color_tex2:
278+
vs_params_name = "mesh_color_texture_vs_params";
279+
fs_params_name = "mesh_color_texture_fs_params";
280+
vs_params_size = sizeof(mesh_color_texture_vs_params_t);
281+
fs_params_size = sizeof(mesh_color_texture_fs_params_t);
282+
break;
283+
case SOKOL_SHADER_ID_mesh_normal_tex1:
284+
vs_params_name = "mesh_normal_texture_vs_params";
285+
fs_params_name = "mesh_normal_texture_fs_params";
286+
vs_params_size = sizeof(mesh_normal_texture_vs_params_t);
287+
fs_params_size = sizeof(mesh_normal_texture_fs_params_t);
288+
break;
289+
case SOKOL_SHADER_ID_shadow_tex1:
290+
case SOKOL_SHADER_ID_shadow_normal_tex1:
291+
vs_params_name = "shadow_texture_vs_params";
292+
fs_params_name = "shadow_texture_fs_params";
293+
vs_params_size = sizeof(shadow_texture_vs_params_t);
294+
fs_params_size = sizeof(shadow_texture_fs_params_t);
295+
break;
296+
case SOKOL_SHADER_ID_mesh_tex1:
297+
vs_params_name = "mesh_texture_vs_params";
298+
vs_params_size = sizeof(mesh_texture_vs_params_t);
299+
break;
300+
case SOKOL_SHADER_ID_tile_map:
301+
vs_params_name = "tile_map_vs_params";
302+
fs_params_name = "tile_map_fs_params";
303+
vs_params_size = sizeof(tile_map_vs_params_t);
304+
fs_params_size = sizeof(tile_map_fs_params_t);
305+
break;
306+
case SOKOL_SHADER_ID_NONE:
307+
default:
308+
fprintf(stderr, "RegisterPipeline: Unknown shader id '%d' at pipeline '%s'\n", shader_id, desc.label);
309+
break;
310+
}
311+
312+
if (vs_params_name) {
313+
pipeline->vs_params_slot = context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_VS, vs_params_name);
314+
if (0 > pipeline->vs_params_slot) {
315+
fprintf(stderr, "RegisterPipeline: '%s' vs uniform slot not found at pipeline '%s'\n", vs_params_name,
316+
desc.label);
317+
xassert(0);
318+
return;
319+
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_VS, vs_params_name) != vs_params_size) {
320+
fprintf(stderr, "RegisterPipeline: '%s' vs uniform size doesnt match at pipeline '%s'\n",
321+
vs_params_name, desc.label);
322+
xassert(0);
323+
return;
324+
}
325+
}
326+
if (fs_params_name) {
327+
pipeline->fs_params_slot = context.shader_funcs->uniformblock_slot(SG_SHADERSTAGE_FS, fs_params_name);
328+
if (0 > pipeline->fs_params_slot) {
329+
fprintf(stderr, "RegisterPipeline: '%s' fs uniform slot not found at pipeline '%s'\n",
330+
fs_params_name, desc.label);
331+
xassert(0);
332+
return;
333+
} else if (context.shader_funcs->uniformblock_size(SG_SHADERSTAGE_FS, fs_params_name) != fs_params_size) {
334+
fprintf(stderr, "RegisterPipeline: '%s' fs uniform size doesnt match at pipeline '%s'\n",
335+
fs_params_name, desc.label);
336+
xassert(0);
337+
return;
338+
}
339+
}
357340
for (int i = 0; i < PERIMETER_SOKOL_TEXTURES; ++i) {
358341
std::string name = "un_tex" + std::to_string(i);
359342
pipeline->shader_fs_texture_slot[i] = context.shader_funcs->image_slot(SG_SHADERSTAGE_FS, name.c_str());

Source/Render/sokol/SokolRenderPipeline.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ struct SokolPipeline {
1414
//Mapping of texture slots in fragment shader
1515
int shader_fs_texture_slot[PERIMETER_SOKOL_TEXTURES] = {};
1616
//Sampler slot
17-
int shader_fs_sampler_slot = 0;
18-
int shader_fs_shadow_sampler_slot = 0;
17+
int shader_fs_sampler_slot = -1;
18+
int shader_fs_shadow_sampler_slot = -1;
19+
//Uniform params slots
20+
int vs_params_slot = -1;
21+
int fs_params_slot = -1;
1922

2023
SokolPipeline() = default;
2124
~SokolPipeline();

Source/Render/sokol/SokolRenderState.cpp

+50-41
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,29 @@ void cSokolRender::DoSokolRendering() {
133133
ProcessRenderPass(swapchain_pass, swapchainCommands);
134134
}
135135

136+
#define CMDS_COMPARE_PREV_COMMAND
136137
void cSokolRender::ProcessRenderPass(sg_pass& render_pass, const std::vector<SokolCommand*>& pass_commands) {
137138
std::string pass_group_label = "pass_";
138139
pass_group_label += render_pass.label;
139140
sg_push_debug_group(pass_group_label.c_str());
140141
sg_begin_pass(&render_pass);
142+
#ifdef PERIMETER_DEBUG
143+
//printf("@@@ %s @@@\n",pass_group_label.c_str());
144+
#endif
141145

142146
//Iterate each command
147+
#ifdef CMDS_COMPARE_PREV_COMMAND
143148
const SokolCommand* prev_command = nullptr;
149+
#ifdef DEBUG_TEST_SAME
150+
int same_i = 0;
151+
#endif
152+
#endif
144153
const SokolCommand* command = nullptr;
145154
bool open_debug_group = false;
146155
for (size_t passcmd_i = 0; passcmd_i < pass_commands.size(); ++passcmd_i) {
156+
#ifdef CMDS_COMPARE_PREV_COMMAND
147157
prev_command = command;
158+
#endif
148159
command = pass_commands[passcmd_i];
149160

150161
//Make/Close debug group
@@ -191,8 +202,8 @@ void cSokolRender::ProcessRenderPass(sg_pass& render_pass, const std::vector<Sok
191202
xxassert(0, "cSokolRender::ProcessRenderPass missing pipeline");
192203
continue;
193204
}
194-
shader_funcs* shader_funcs = pipeline->context.shader_funcs;
195205

206+
#ifdef CMDS_COMPARE_PREV_COMMAND
196207
bool pipeline_diff = !prev_command || prev_command->pipeline != pipeline;
197208
bool vs_params_diff = !prev_command || pipeline_diff
198209
|| prev_command->vs_params_len != command->vs_params_len
@@ -209,6 +220,23 @@ void cSokolRender::ProcessRenderPass(sg_pass& render_pass, const std::vector<Sok
209220
command->fs_params_len
210221
));
211222

223+
#ifdef DEBUG_TEST_SAME
224+
if (prev_command && prev_command->vertex_buffer && command->vertex_buffer
225+
&& prev_command->pipeline == command->pipeline
226+
&& prev_command->base_elements == command->base_elements
227+
&& prev_command->indices == command->indices
228+
&& !fs_params_diff
229+
&& prev_command->vertex_buffer->res.id == command->vertex_buffer->res.id) {
230+
same_i++;
231+
continue;
232+
} else if (0 < same_i) {
233+
if (2< same_i) {
234+
printf("!! Up %d\n", same_i);
235+
}
236+
same_i = 0;
237+
}
238+
#endif
239+
212240
if (!prev_command
213241
|| vs_params_diff || fs_params_diff
214242
|| prev_command->vertex_buffer != command->vertex_buffer
@@ -219,7 +247,9 @@ void cSokolRender::ProcessRenderPass(sg_pass& render_pass, const std::vector<Sok
219247
command->sokol_images,
220248
sizeof(SokolResourceImage*) * PERIMETER_SOKOL_TEXTURES
221249
))
222-
) {
250+
)
251+
#endif
252+
{
223253
#if defined(PERIMETER_DEBUG) && 0
224254
printf("id: 0x%X fmt: 0x%X vtx: %d idx: %d\n",
225255
command->pipeline_id, pipeline->vertex_fmt,
@@ -300,47 +330,27 @@ void cSokolRender::ProcessRenderPass(sg_pass& render_pass, const std::vector<Sok
300330
sg_apply_bindings(&bindings);
301331

302332
//Apply VS uniforms
303-
SOKOL_SHADER_ID shader_id = shader_funcs->get_id();
304-
const char* vs_params_name = nullptr;
305-
const char* fs_params_name = nullptr;
306-
switch (shader_id) {
307-
case SOKOL_SHADER_ID_mesh_color_tex1:
308-
case SOKOL_SHADER_ID_mesh_color_tex2:
309-
vs_params_name = "mesh_color_texture_vs_params";
310-
fs_params_name = "mesh_color_texture_fs_params";
311-
break;
312-
case SOKOL_SHADER_ID_mesh_normal_tex1:
313-
vs_params_name = "mesh_normal_texture_vs_params";
314-
fs_params_name = "mesh_normal_texture_fs_params";
315-
break;
316-
case SOKOL_SHADER_ID_shadow_tex1:
317-
case SOKOL_SHADER_ID_shadow_normal_tex1:
318-
vs_params_name = "shadow_texture_vs_params";
319-
fs_params_name = "shadow_texture_fs_params";
320-
break;
321-
case SOKOL_SHADER_ID_mesh_tex1:
322-
vs_params_name = "mesh_texture_vs_params";
323-
break;
324-
case SOKOL_SHADER_ID_tile_map:
325-
vs_params_name = "tile_map_vs_params";
326-
fs_params_name = "tile_map_fs_params";
327-
break;
328-
default:
329-
case SOKOL_SHADER_ID_NONE:
330-
xassert(0);
331-
}
332-
333-
if (vs_params_name && vs_params_diff) {
334-
int vs_params_slot = shader_funcs->uniformblock_slot(SG_SHADERSTAGE_VS, vs_params_name);
335-
xxassert(0 <= vs_params_slot, "No vs slot found");
333+
#ifdef CMDS_COMPARE_PREV_COMMAND
334+
if (vs_params_diff)
335+
#endif
336+
if (0 <= pipeline->vs_params_slot) {
336337
xxassert(command->vs_params, "No vs parameters set in command");
337-
sg_apply_uniforms(SG_SHADERSTAGE_VS, vs_params_slot, sg_range { command->vs_params, command->vs_params_len });
338+
sg_apply_uniforms(
339+
SG_SHADERSTAGE_VS,
340+
pipeline->vs_params_slot,
341+
sg_range { command->vs_params, command->vs_params_len }
342+
);
338343
}
339-
if (fs_params_name && fs_params_diff) {
340-
int fs_params_slot = shader_funcs->uniformblock_slot(SG_SHADERSTAGE_FS, fs_params_name);
341-
xxassert(0 <= fs_params_slot, "No fs slot found");
344+
#ifdef CMDS_COMPARE_PREV_COMMAND
345+
if (fs_params_diff)
346+
#endif
347+
if (0 <= pipeline->fs_params_slot) {
342348
xxassert(command->fs_params, "No fs parameters set in command");
343-
sg_apply_uniforms(SG_SHADERSTAGE_FS, fs_params_slot, sg_range { command->fs_params, command->fs_params_len });
349+
sg_apply_uniforms(
350+
SG_SHADERSTAGE_FS,
351+
pipeline->fs_params_slot,
352+
sg_range { command->fs_params, command->fs_params_len }
353+
);
344354
}
345355
}
346356

@@ -834,7 +844,6 @@ void cSokolRender::CreateCommand(VertexBuffer* vb, size_t vertices, IndexBuffer*
834844
case SOKOL_SHADER_ID_shadow_normal_tex1: {
835845
auto vs_params = reinterpret_cast<shadow_texture_vs_params_t*>(cmd->vs_params);
836846
auto fs_params = reinterpret_cast<shadow_texture_fs_params_t*>(cmd->fs_params);
837-
vs_params->un_mvp = isOrthographicProjSet ? orthoVP : (activeCommandW * activeCommandVP);
838847
shader_set_common_params(vs_params, fs_params);
839848
break;
840849
}

0 commit comments

Comments
 (0)