Skip to content

Commit

Permalink
Merge pull request #129 from floooh/feature/issue-931
Browse files Browse the repository at this point in the history
Add testing code for new backend-resource access functions.
  • Loading branch information
floooh authored Oct 30, 2023
2 parents 236beb8 + babcf82 commit 3e746d8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 4 deletions.
18 changes: 17 additions & 1 deletion d3d11/inject-d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
};
ID3D11Device* d3d11_dev = (ID3D11Device*) d3d11_get_context().d3d11.device;
ID3D11Device* d3d11_dev = (ID3D11Device*) sg_d3d11_device();
D3D11_BUFFER_DESC d3d11_vbuf_desc = {
.ByteWidth = sizeof(vertices),
.Usage = D3D11_USAGE_IMMUTABLE,
Expand Down Expand Up @@ -115,11 +115,13 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
.size = sizeof(vertices),
.d3d11_buffer = d3d11_vbuf
});
assert(sg_d3d11_query_buffer_info(vbuf).buf == d3d11_vbuf);
sg_buffer ibuf = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(indices),
.type = SG_BUFFERTYPE_INDEXBUFFER,
.d3d11_buffer = d3d11_ibuf
});
assert(sg_d3d11_query_buffer_info(ibuf).buf == d3d11_ibuf);

// can release the native D3D11 buffers now, sokol_gfx is holding on to them
d3d11_vbuf->lpVtbl->Release(d3d11_vbuf); d3d11_vbuf = 0;
Expand Down Expand Up @@ -161,6 +163,10 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
.d3d11_texture = d3d11_tex,
.d3d11_shader_resource_view = d3d11_srv
});
assert(sg_d3d11_query_image_info(img).tex2d == d3d11_tex);
assert(sg_d3d11_query_image_info(img).tex3d == 0);
assert(sg_d3d11_query_image_info(img).res == d3d11_tex);
assert(sg_d3d11_query_image_info(img).srv == d3d11_srv);
if (d3d11_tex) {
d3d11_tex->lpVtbl->Release(d3d11_tex);
d3d11_tex = 0;
Expand Down Expand Up @@ -191,6 +197,7 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
.wrap_v = SG_WRAP_REPEAT,
.d3d11_sampler = d3d11_smp,
});
assert(sg_d3d11_query_sampler_info(smp).smp == d3d11_smp);
d3d11_smp->lpVtbl->Release(d3d11_smp); d3d11_smp = 0;

// create shader
Expand Down Expand Up @@ -232,6 +239,11 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
"}\n"
},
});
assert(sg_d3d11_query_shader_info(shd).vs != 0);
assert(sg_d3d11_query_shader_info(shd).fs != 0);
assert(sg_d3d11_query_shader_info(shd).vs_cbufs[0] != 0);
assert(sg_d3d11_query_shader_info(shd).vs_cbufs[1] == 0);
assert(sg_d3d11_query_shader_info(shd).fs_cbufs[0] == 0);

// pipeline object
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
Expand All @@ -249,6 +261,10 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
},
.cull_mode = SG_CULLMODE_BACK,
});
assert(sg_d3d11_query_pipeline_info(pip).il != 0);
assert(sg_d3d11_query_pipeline_info(pip).rs != 0);
assert(sg_d3d11_query_pipeline_info(pip).dss != 0);
assert(sg_d3d11_query_pipeline_info(pip).bs != 0);

// default pass action (clear to gray)
sg_pass_action pass_action = { 0 };
Expand Down
4 changes: 2 additions & 2 deletions d3d11/mipmap-d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
// the last 4 samplers use different anistropy levels
smp_desc.min_lod = 0.0f;
smp_desc.max_lod = 0.0f; // for max_lod, zero-initialized means "FLT_MAX"
smp_desc.min_filter = SG_FILTER_NEAREST;
smp_desc.mag_filter = SG_FILTER_NEAREST;
smp_desc.min_filter = SG_FILTER_LINEAR;
smp_desc.mag_filter = SG_FILTER_LINEAR;
smp_desc.mipmap_filter = SG_FILTER_LINEAR;
for (int i = 0; i < 4; i++) {
smp_desc.max_anisotropy = 1<<i;
Expand Down
4 changes: 4 additions & 0 deletions d3d11/offscreen-d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
.color_attachments[0].image = color_img,
.depth_stencil_attachment.image = depth_img
});
assert(sg_d3d11_query_pass_info(offscreen_pass).color_rtv[0] != 0);
assert(sg_d3d11_query_pass_info(offscreen_pass).color_rtv[1] == 0);
assert(sg_d3d11_query_pass_info(offscreen_pass).resolve_rtv[0] == 0);
assert(sg_d3d11_query_pass_info(offscreen_pass).dsv != 0);

// a sampler object for when the rendertarget image is used as texture
sg_sampler smp = sg_make_sampler(&(sg_sampler_desc){
Expand Down
13 changes: 13 additions & 0 deletions glfw/inject-glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,18 @@ int main() {
.size = sizeof(vertices),
.gl_buffers[0] = gl_vbuf
});
assert(sg_gl_query_buffer_info(vbuf).buf[0] == gl_vbuf);
assert(sg_gl_query_buffer_info(vbuf).buf[1] == 0);
assert(sg_gl_query_buffer_info(vbuf).active_slot == 0);
sg_buffer ibuf = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_INDEXBUFFER,
.usage = SG_USAGE_IMMUTABLE,
.size = sizeof(indices),
.gl_buffers[0] = gl_ibuf
});
assert(sg_gl_query_buffer_info(ibuf).buf[0] == gl_ibuf);
assert(sg_gl_query_buffer_info(ibuf).buf[1] == 0);
assert(sg_gl_query_buffer_info(ibuf).active_slot == 0);

/* create dynamically updated textures, in the GL backend
dynamic textures are rotated through, so need to create
Expand All @@ -129,6 +135,11 @@ int main() {
}
sg_reset_state_cache();
sg_image img = sg_make_image(&img_desc);
assert(sg_gl_query_image_info(img).tex[0] == img_desc.gl_textures[0]);
assert(sg_gl_query_image_info(img).tex[1] == img_desc.gl_textures[1]);
assert(sg_gl_query_image_info(img).active_slot == 0);
assert(sg_gl_query_image_info(img).tex_target == GL_TEXTURE_2D);
assert(sg_gl_query_image_info(img).msaa_render_buffer == 0);

// create a GL sampler object
sg_sampler_desc smp_desc = {
Expand All @@ -144,6 +155,7 @@ int main() {
glSamplerParameteri(smp_desc.gl_sampler, GL_TEXTURE_WRAP_T, GL_REPEAT);
sg_reset_state_cache();
sg_sampler smp = sg_make_sampler(&smp_desc);
assert(sg_gl_query_sampler_info(smp).smp == smp_desc.gl_sampler);

// define resource bindings
sg_bindings bind = {
Expand Down Expand Up @@ -186,6 +198,7 @@ int main() {
"}\n"
}
});
assert(sg_gl_query_shader_info(shd).prog != 0);

// create pipeline object
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
Expand Down
2 changes: 2 additions & 0 deletions glfw/offscreen-glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ int main() {
.color_attachments[0].image = color_img,
.depth_stencil_attachment.image = depth_img
});
assert(sg_gl_query_pass_info(offscreen_pass).frame_buffer != 0);
assert(sg_gl_query_pass_info(offscreen_pass).msaa_resolve_framebuffer[0] == 0);

// pass action for offscreen pass, clearing to black
sg_pass_action offscreen_pass_action = {
Expand Down
13 changes: 13 additions & 0 deletions metal/inject-metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,16 @@ static void init(void) {
.mtl_buffers[0] = (__bridge const void*) mtl_vbuf
};
state.bind.vertex_buffers[0] = sg_make_buffer(&vbuf_desc);
assert(((__bridge id<MTLBuffer>) sg_mtl_query_buffer_info(state.bind.vertex_buffers[0]).buf[0]) == mtl_vbuf);
assert(((__bridge id<MTLBuffer>) sg_mtl_query_buffer_info(state.bind.vertex_buffers[0]).buf[1]) == nil);
sg_buffer_desc ibuf_desc = {
.type = SG_BUFFERTYPE_INDEXBUFFER,
.size = sizeof(indices),
.mtl_buffers[0] = (__bridge const void*) mtl_ibuf
};
state.bind.index_buffer = sg_make_buffer(&ibuf_desc);
assert(((__bridge id<MTLBuffer>) sg_mtl_query_buffer_info(state.bind.index_buffer).buf[0]) == mtl_ibuf);
assert(((__bridge id<MTLBuffer>) sg_mtl_query_buffer_info(state.bind.index_buffer).buf[1]) == nil);

// create dynamically updated Metal texture objects, these will
// be rotated through by sokol_gfx as they are updated, so we need
Expand All @@ -131,6 +135,8 @@ static void init(void) {

sg_reset_state_cache();
state.bind.fs.images[0] = sg_make_image(&img_desc);
assert(((__bridge id<MTLTexture>) sg_mtl_query_image_info(state.bind.fs.images[0]).tex[0]) == mtl_tex[0]);
assert(((__bridge id<MTLTexture>) sg_mtl_query_image_info(state.bind.fs.images[0]).tex[1]) == mtl_tex[1]);

// create a Metal sampler object and inject into a sokol-gfx sampler object
MTLSamplerDescriptor* mtl_smp_desc = [[MTLSamplerDescriptor alloc] init];
Expand All @@ -150,6 +156,7 @@ static void init(void) {

sg_reset_state_cache();
state.bind.fs.samplers[0] = sg_make_sampler(&smp_desc);
assert(((__bridge id<MTLSamplerState>) sg_mtl_query_sampler_info(state.bind.fs.samplers[0]).smp) == mtl_smp);

// a shader
sg_shader_desc shader_desc = {
Expand Down Expand Up @@ -197,6 +204,10 @@ static void init(void) {
}
};
sg_shader shd = sg_make_shader(&shader_desc);
assert(((__bridge id<MTLLibrary>) sg_mtl_query_shader_info(shd).vs_lib) != nil);
assert(((__bridge id<MTLLibrary>) sg_mtl_query_shader_info(shd).fs_lib) != nil);
assert(((__bridge id<MTLFunction>) sg_mtl_query_shader_info(shd).vs_func) != nil);
assert(((__bridge id<MTLFunction>) sg_mtl_query_shader_info(shd).fs_func) != nil);

// a pipeline state object
sg_pipeline_desc pip_desc = {
Expand All @@ -215,6 +226,8 @@ static void init(void) {
.cull_mode = SG_CULLMODE_BACK,
};
state.pip = sg_make_pipeline(&pip_desc);
assert(((__bridge id<MTLRenderPipelineState>) sg_mtl_query_pipeline_info(state.pip).rps) != nil);
assert(((__bridge id<MTLDepthStencilState>) sg_mtl_query_pipeline_info(state.pip).dss) != nil);

// view-projection matrix
hmm_mat4 proj = HMM_Perspective(60.0f, (float)WIDTH/(float)HEIGHT, 0.01f, 10.0f);
Expand Down
17 changes: 16 additions & 1 deletion wgpu/inject-wgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static struct {
WGPUBuffer wgpu_vbuf;
WGPUBuffer wgpu_ibuf;
WGPUTexture wgpu_tex;
WGPUTextureView wgpu_tex_view;
WGPUSampler wgpu_smp;
} state;

Expand Down Expand Up @@ -129,11 +130,13 @@ static void init(void) {
.size = sizeof(vertices),
.wgpu_buffer = state.wgpu_vbuf,
});
assert(sg_wgpu_query_buffer_info(state.bind.vertex_buffers[0]).buf == state.wgpu_vbuf);
state.bind.index_buffer = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_INDEXBUFFER,
.size = sizeof(indices),
.wgpu_buffer = state.wgpu_ibuf,
});
assert(sg_wgpu_query_buffer_info(state.bind.index_buffer).buf == state.wgpu_ibuf);

// create dynamically updated WebGPU texture object
state.wgpu_tex = wgpuDeviceCreateTexture(wgpu_dev, &(WGPUTextureDescriptor) {
Expand All @@ -149,6 +152,10 @@ static void init(void) {
.mipLevelCount = 1,
.sampleCount = 1,
});
state.wgpu_tex_view = wgpuTextureCreateView(state.wgpu_tex, &(WGPUTextureViewDescriptor) {
.mipLevelCount = 1,
.arrayLayerCount = 1,
});
sg_reset_state_cache();

// ... and the sokol-gfx texture object with the injected WGPU texture
Expand All @@ -157,8 +164,11 @@ static void init(void) {
.width = IMG_WIDTH,
.height = IMG_HEIGHT,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.wgpu_texture = state.wgpu_tex
.wgpu_texture = state.wgpu_tex,
.wgpu_texture_view = state.wgpu_tex_view,
});
assert(sg_wgpu_query_image_info(state.bind.fs.images[0]).tex == state.wgpu_tex);
assert(sg_wgpu_query_image_info(state.bind.fs.images[0]).view == state.wgpu_tex_view);

// a WebGPU sampler object...
state.wgpu_smp = wgpuDeviceCreateSampler(wgpu_dev, &(WGPUSamplerDescriptor){
Expand All @@ -179,6 +189,7 @@ static void init(void) {
.mag_filter = SG_FILTER_LINEAR,
.wgpu_sampler = state.wgpu_smp,
});
assert(sg_wgpu_query_sampler_info(state.bind.fs.samplers[0]).smp == state.wgpu_smp);

// a sokol-gfx shader object
sg_shader shd = sg_make_shader(&(sg_shader_desc){
Expand Down Expand Up @@ -212,6 +223,9 @@ static void init(void) {
"}\n"
},
});
assert(sg_wgpu_query_shader_info(shd).vs_mod != 0);
assert(sg_wgpu_query_shader_info(shd).fs_mod != 0);
assert(sg_wgpu_query_shader_info(shd).bgl != 0);

// a pipeline state object
sg_pipeline_desc pip_desc = {
Expand All @@ -230,6 +244,7 @@ static void init(void) {
.cull_mode = SG_CULLMODE_BACK,
};
state.pip = sg_make_pipeline(&pip_desc);
assert(sg_wgpu_query_pipeline_info(state.pip).pip != 0);
}

void frame() {
Expand Down
4 changes: 4 additions & 0 deletions wgpu/offscreen-wgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ static void init(void) {
.color_attachments[0].image = color_img,
.depth_stencil_attachment.image = depth_img
});
assert(sg_wgpu_query_pass_info(state.offscreen.pass).color_view[0] != 0);
assert(sg_wgpu_query_pass_info(state.offscreen.pass).color_view[1] == 0);
assert(sg_wgpu_query_pass_info(state.offscreen.pass).resolve_view[0] == 0);
assert(sg_wgpu_query_pass_info(state.offscreen.pass).ds_view != 0);

// a sampler object for when the render target is used as texture
sg_sampler smp = sg_make_sampler(&(sg_sampler_desc){
Expand Down

0 comments on commit 3e746d8

Please sign in to comment.