Created
December 9, 2014 14:11
-
-
Save thevaber/1d11662f3994adc48b51 to your computer and use it in GitHub Desktop.
Possible bgfx issue with transient buffers and D3D11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
different bgfx transient buffer issue (dx11) | |
Reproduction steps: | |
1. use Direct3D11 renderer | |
2. in each frame: | |
2.1. render anything (even single vertex) with one vertex decl | |
2.2. render some triangles (or other geometry) with different vertex decl | |
some additional comments in the code | |
(the dx shaders are compiled with vs/ps_4_0 profile) | |
*/ | |
#include <SDL2/SDL.h> | |
#include <bgfx.h> | |
#include <bgfxplatform.h> | |
#include <bx/fpumath.h> | |
/* | |
varying.def.sc: | |
vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); | |
vec3 a_position : POSITION; | |
vec4 a_color0 : COLOR0; | |
*/ | |
/* | |
vertex shader: | |
$input a_position, a_color0 | |
$output v_color0 | |
#include "bgfx_shader.sh" | |
void main() | |
{ | |
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); | |
v_color0 = a_color0; | |
} | |
*/ | |
static const uint8_t vs_dx11_bin[510] = | |
{ | |
0x56, 0x53, 0x48, 0x03, 0xa4, 0x8b, 0xef, 0x49, 0x01, 0x00, 0x0f, 0x75, 0x5f, 0x6d, 0x6f, 0x64, // VSH....I...u_mod | |
0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x09, 0x00, 0xe0, 0x09, 0x04, 0x00, // elViewProj...... | |
0xd4, 0x01, 0x44, 0x58, 0x42, 0x43, 0x50, 0x7e, 0x8e, 0xb1, 0x12, 0xfc, 0x84, 0xb1, 0x3c, 0x6f, // ..DXBCP~......<o | |
0xba, 0x68, 0xa8, 0x04, 0x63, 0x8f, 0x01, 0x00, 0x00, 0x00, 0xd4, 0x01, 0x00, 0x00, 0x03, 0x00, // .h..c........... | |
0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x49, 0x53, // ..,...|.......IS | |
0x47, 0x4e, 0x48, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x38, 0x00, // GNH...........8. | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // ................ | |
0x00, 0x00, 0x0f, 0x0f, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ......>......... | |
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x07, 0x00, 0x00, 0x43, 0x4f, // ..............CO | |
0x4c, 0x4f, 0x52, 0x00, 0x50, 0x4f, 0x53, 0x49, 0x54, 0x49, 0x4f, 0x4e, 0x00, 0xab, 0x4f, 0x53, // LOR.POSITION..OS | |
0x47, 0x4e, 0x4c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x38, 0x00, // GNL...........8. | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // ................ | |
0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ......D......... | |
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x53, 0x56, // ..............SV | |
0x5f, 0x50, 0x4f, 0x53, 0x49, 0x54, 0x49, 0x4f, 0x4e, 0x00, 0x43, 0x4f, 0x4c, 0x4f, 0x52, 0x00, // _POSITION.COLOR. | |
0xab, 0xab, 0x53, 0x48, 0x44, 0x52, 0xfc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x01, 0x00, 0x3f, 0x00, // ..SHDR....@...?. | |
0x00, 0x00, 0x59, 0x00, 0x00, 0x04, 0x46, 0x8e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa2, 0x00, // ..Y...F. ....... | |
0x00, 0x00, 0x5f, 0x00, 0x00, 0x03, 0xf2, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x00, // .._..........._. | |
0x00, 0x03, 0x72, 0x10, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x67, 0x00, 0x00, 0x04, 0xf2, 0x20, // ..r.......g.... | |
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x03, 0xf2, 0x20, // ..........e.... | |
0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x38, 0x00, // ......h.......8. | |
0x00, 0x08, 0xf2, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x15, 0x10, 0x00, 0x01, 0x00, // ..........V..... | |
0x00, 0x00, 0x46, 0x8e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f, 0x00, 0x00, 0x00, 0x32, 0x00, // ..F. .........2. | |
0x00, 0x0a, 0xf2, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x8e, 0x20, 0x00, 0x00, 0x00, // ..........F. ... | |
0x00, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x06, 0x10, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x46, 0x0e, // ..............F. | |
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x0a, 0xf2, 0x00, 0x10, 0x00, 0x00, 0x00, // ......2......... | |
0x00, 0x00, 0x46, 0x8e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00, 0xa6, 0x1a, // ..F. ........... | |
0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x46, 0x0e, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ......F......... | |
0x00, 0x08, 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x0e, 0x10, 0x00, 0x00, 0x00, // ... ......F..... | |
0x00, 0x00, 0x46, 0x8e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa1, 0x00, 0x00, 0x00, 0x36, 0x00, // ..F. .........6. | |
0x00, 0x05, 0xf2, 0x20, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x46, 0x1e, 0x10, 0x00, 0x00, 0x00, // ... ......F..... | |
0x00, 0x00, 0x3e, 0x00, 0x00, 0x01, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00, 0x30, 0x0a, // ..>.........0. | |
}; | |
static const uint8_t vs_gl_bin[325] = | |
{ | |
0x56, 0x53, 0x48, 0x03, 0xa4, 0x8b, 0xef, 0x49, 0x01, 0x00, 0x0f, 0x75, 0x5f, 0x6d, 0x6f, 0x64, // VSH....I...u_mod | |
0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x09, 0x01, 0x00, 0x00, 0x01, 0x00, // elViewProj...... | |
0x20, 0x01, 0x00, 0x00, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x68, 0x69, // ...attribute hi | |
0x67, 0x68, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x61, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, // ghp vec4 a_color | |
0x30, 0x3b, 0x0a, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x68, 0x69, 0x67, // 0;.attribute hig | |
0x68, 0x70, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, // hp vec3 a_positi | |
0x6f, 0x6e, 0x3b, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, 0x68, 0x69, 0x67, 0x68, // on;.varying high | |
0x70, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, // p vec4 v_color0; | |
0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x68, 0x69, 0x67, 0x68, 0x70, 0x20, 0x6d, // .uniform highp m | |
0x61, 0x74, 0x34, 0x20, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, // at4 u_modelViewP | |
0x72, 0x6f, 0x6a, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, // roj;.void main ( | |
0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x68, 0x69, 0x67, 0x68, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, // ).{. highp vec4 | |
0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, // tmpvar_1;. tmp | |
0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, 0x77, 0x20, 0x3d, 0x20, 0x31, 0x2e, 0x30, 0x3b, 0x0a, 0x20, // var_1.w = 1.0;. | |
0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, 0x78, 0x79, 0x7a, 0x20, 0x3d, 0x20, // tmpvar_1.xyz = | |
0x61, 0x5f, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x20, 0x20, 0x67, 0x6c, // a_position;. gl | |
0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x28, 0x75, 0x5f, 0x6d, // _Position = (u_m | |
0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x20, 0x2a, 0x20, 0x74, // odelViewProj * t | |
0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x76, 0x5f, 0x63, 0x6f, // mpvar_1);. v_co | |
0x6c, 0x6f, 0x72, 0x30, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, // lor0 = a_color0; | |
0x0a, 0x7d, 0x0a, 0x0a, 0x00, // .}... | |
}; | |
/* | |
fragment shader: | |
$input v_color0 | |
#include "bgfx_shader.sh" | |
void main() | |
{ | |
gl_FragColor = v_color0; | |
} | |
*/ | |
static const uint8_t fs_dx11_bin[260] = | |
{ | |
0x46, 0x53, 0x48, 0x03, 0xa4, 0x8b, 0xef, 0x49, 0x00, 0x00, 0xf4, 0x00, 0x44, 0x58, 0x42, 0x43, // FSH....I....DXBC | |
0xa6, 0x7f, 0x08, 0xe2, 0x95, 0xbd, 0x5f, 0xa3, 0x3f, 0x5b, 0x58, 0x8e, 0x54, 0x0f, 0x89, 0x67, // ......_.?[X.T..g | |
0x01, 0x00, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, // ............,... | |
0x80, 0x00, 0x00, 0x00, 0xb4, 0x00, 0x00, 0x00, 0x49, 0x53, 0x47, 0x4e, 0x4c, 0x00, 0x00, 0x00, // ........ISGNL... | |
0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........8....... | |
0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, // ................ | |
0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // D............... | |
0x01, 0x00, 0x00, 0x00, 0x0f, 0x0f, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x50, 0x4f, 0x53, 0x49, 0x54, // ........SV_POSIT | |
0x49, 0x4f, 0x4e, 0x00, 0x43, 0x4f, 0x4c, 0x4f, 0x52, 0x00, 0xab, 0xab, 0x4f, 0x53, 0x47, 0x4e, // ION.COLOR...OSGN | |
0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, // ,........... ... | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ................ | |
0x0f, 0x00, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x54, 0x41, 0x52, 0x47, 0x45, 0x54, 0x00, 0xab, 0xab, // ....SV_TARGET... | |
0x53, 0x48, 0x44, 0x52, 0x38, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, // SHDR8...@....... | |
0x62, 0x10, 0x00, 0x03, 0xf2, 0x10, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x03, // b...........e... | |
0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x05, 0xf2, 0x20, 0x10, 0x00, // . ......6.... .. | |
0x00, 0x00, 0x00, 0x00, 0x46, 0x1e, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x01, // ....F.......>... | |
0x00, 0x00, 0x00, 0x00, // .... | |
}; | |
static const uint8_t fs_gl_bin[89] = | |
{ | |
0x46, 0x53, 0x48, 0x03, 0xa4, 0x8b, 0xef, 0x49, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x76, 0x61, // FSH....I..J...va | |
0x72, 0x79, 0x69, 0x6e, 0x67, 0x20, 0x68, 0x69, 0x67, 0x68, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, // rying highp vec4 | |
0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, // v_color0;.void | |
0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x46, // main ().{. gl_F | |
0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x5f, 0x63, 0x6f, 0x6c, // ragColor = v_col | |
0x6f, 0x72, 0x30, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // or0;.}... | |
}; | |
struct vertex_t { | |
float x, y, z; | |
uint32_t abgr; | |
static void init() { | |
decl.begin().add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float).add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true).end(); | |
} | |
static bgfx::VertexDecl decl; | |
}; | |
bgfx::VertexDecl vertex_t::decl; | |
struct vertex_with_uv_t { | |
float x, y, z; | |
uint32_t abgr; | |
float u, v; | |
static void init() { | |
decl.begin().add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float).add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true).add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float).end(); | |
} | |
static bgfx::VertexDecl decl; | |
}; | |
bgfx::VertexDecl vertex_with_uv_t::decl; | |
int main(int argc, char** argv) { | |
bool use_dx11 = true; // false = OpenGL (no bug) | |
const int res_x = 1280, res_y = 1024; | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window* window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, res_x, res_y, SDL_WINDOW_SHOWN); | |
bgfx::sdlSetWindow(window); | |
bgfx::init(use_dx11 ? bgfx::RendererType::Direct3D11 : bgfx::RendererType::OpenGL); | |
bgfx::reset(res_x, res_y); | |
bgfx::ShaderHandle vsh, fsh; | |
if (use_dx11) { | |
vsh = bgfx::createShader(bgfx::makeRef(vs_dx11_bin, sizeof(vs_dx11_bin))); | |
fsh = bgfx::createShader(bgfx::makeRef(fs_dx11_bin, sizeof(fs_dx11_bin))); | |
} else { | |
vsh = bgfx::createShader(bgfx::makeRef(vs_gl_bin, sizeof(vs_gl_bin))); | |
fsh = bgfx::createShader(bgfx::makeRef(fs_gl_bin, sizeof(fs_gl_bin))); | |
} | |
bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh, true); | |
vertex_t::init(); | |
vertex_with_uv_t::init(); | |
float view[16]; | |
bx::mtxIdentity( view ); | |
float proj[16]; | |
bx::mtxOrtho(proj, 0.0f, 1600.0f, 1200.0f, 0.0f, -100.0f, 100.0f); | |
bgfx::setViewTransform(0, view, proj); | |
bgfx::setViewRect(0, 0, 0, res_x, res_y); | |
bgfx::setViewClear(0, BGFX_CLEAR_COLOR_BIT, 0x222222ff); | |
while(1) { | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) {} | |
bgfx::setViewClear(0, BGFX_CLEAR_COLOR_BIT, 0x222222ff); | |
bgfx::setViewClear(1, BGFX_CLEAR_NONE); | |
// Render some dummy geometry with first vertex decl | |
{ | |
typedef vertex_t vertex_type; // bug doesn't occur if same as vertex_type of the code that renders triangles | |
if (bgfx::checkAvailTransientVertexBuffer(1, vertex_t::decl)) { | |
// Vertex count or primitive type doesn't seem to matter, anything seems to trigger the bug | |
vertex_type v = {0}; | |
if (bgfx::checkAvailTransientVertexBuffer(1, vertex_type::decl)) { | |
bgfx::TransientVertexBuffer transient_buffer; | |
bgfx::allocTransientVertexBuffer(&transient_buffer, 1, vertex_type::decl); | |
memcpy(transient_buffer.data, &v, 1 * sizeof(vertex_type)); | |
bgfx::setProgram(program); | |
bgfx::setState(BGFX_STATE_RGB_WRITE | BGFX_STATE_PT_POINTS); // (zero state also triggers it) | |
bgfx::setVertexBuffer(&transient_buffer); | |
bgfx::submit(0); | |
} | |
} | |
} | |
// Render some triangles with second vertex decl | |
for (float f = 0.0f; f < 800.0f; f += 100.0f) { | |
typedef vertex_with_uv_t vertex_type; | |
vertex_type v[3]; | |
v[0].x = f+0.0f; | |
v[0].y = f+0.0f; | |
v[0].z = 0.0f; | |
v[0].u = 0.0f; | |
v[0].v = 0.0f; | |
v[0].abgr = 0xff0000ff; | |
v[1].x = f+1000.0f; | |
v[1].y = f+0.0f; | |
v[1].z = 0.0f; | |
v[1].u = 0.0f; | |
v[1].v = 0.0f; | |
v[1].abgr = 0xff00ff00; | |
v[2].x = f+1000.0f; | |
v[2].y = f+1000.0f; | |
v[2].z = 0.0f; | |
v[2].u = 0.0f; | |
v[2].v = 0.0f; | |
v[2].abgr = 0xffff0000; | |
if (bgfx::checkAvailTransientVertexBuffer(3, vertex_type::decl)) { | |
bgfx::TransientVertexBuffer transient_buffer; | |
bgfx::allocTransientVertexBuffer(&transient_buffer, 3, vertex_type::decl); | |
memcpy(transient_buffer.data, v, 3 * sizeof(vertex_type)); | |
bgfx::setProgram(program); | |
bgfx::setState(BGFX_STATE_RGB_WRITE); | |
bgfx::setVertexBuffer(&transient_buffer); | |
bgfx::submit(0); | |
} | |
} | |
bgfx::frame(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment