demos/gears: Add a gears demo.

This commit is contained in:
Henri Verbeet 2016-11-02 14:56:13 +01:00
parent cd4f4a2976
commit 20508aee7c
11 changed files with 1115 additions and 9 deletions

View File

@ -19,20 +19,27 @@ vkd3d_public_headers = \
include/vkd3d_utils.h \
include/vkd3d_windows.h
spv_gears_shaders = \
demos/gears.vert.spv \
demos/gears_flat.frag.spv \
demos/gears_smooth.frag.spv
spv_triangle_shaders = \
demos/triangle.vert.spv \
demos/triangle.frag.spv
spv_shaders = \
$(spv_gears_shaders) \
$(spv_triangle_shaders)
hlsl_shaders = \
demos/gears.hlsl \
demos/triangle.hlsl
vkd3d_tests = \
tests/d3d12
vkd3d_demos = \
demos/gears \
demos/triangle
vkd3d_demos_headers = \
@ -84,11 +91,18 @@ AM_DEFAULT_SOURCE_EXT = .c
TESTS = $(vkd3d_tests)
tests_d3d12_LDADD = $(LDADD) @PTHREAD_LIBS@
DEMOS_LDADD = libvkd3d.la $(LDADD) @XCB_LIBS@
DEMOS_CFLAGS = @XCB_CFLAGS@
noinst_PROGRAMS = $(vkd3d_demos)
EXTRA_DIST += $(vkd3d_demos_headers)
EXTRA_demos_gears_DEPENDENCIES = $(spv_gears_shaders)
demos_gears_CFLAGS = $(DEMOS_CFLAGS)
demos_gears_LDADD = $(DEMOS_LDADD) -lm
EXTRA_demos_triangle_DEPENDENCIES = $(spv_triangle_shaders)
demos_triangle_CFLAGS = @XCB_CFLAGS@
demos_triangle_LDADD = libvkd3d.la $(LDADD) @XCB_LIBS@
demos_triangle_CFLAGS = $(DEMOS_CFLAGS)
demos_triangle_LDADD = $(DEMOS_LDADD)
VKD3D_V_WIDL = $(vkd3d_v_widl_@AM_V@)
vkd3d_v_widl_ = $(vkd3d_v_widl_@AM_DEFAULT_V@)

1
demos/.gitignore vendored
View File

@ -1,2 +1,3 @@
gears
triangle
*.spv

View File

@ -47,6 +47,10 @@
#define DEMO_KEY_UNKNOWN 0x0000
#define DEMO_KEY_ESCAPE 0xff1b
#define DEMO_KEY_LEFT 0xff51
#define DEMO_KEY_UP 0xff52
#define DEMO_KEY_RIGHT 0xff53
#define DEMO_KEY_DOWN 0xff54
struct demo_vec3
{
@ -68,6 +72,21 @@ struct demo_swapchain_desc
typedef uint32_t demo_key;
static inline void demo_vec3_set(struct demo_vec3 *v, float x, float y, float z)
{
v->x = x;
v->y = y;
v->z = z;
}
static inline void demo_vec4_set(struct demo_vec4 *v, float x, float y, float z, float w)
{
v->x = x;
v->y = y;
v->z = z;
v->w = w;
}
static inline void demo_rasterizer_desc_init_default(D3D12_RASTERIZER_DESC *desc)
{
desc->FillMode = D3D12_FILL_MODE_SOLID;

View File

@ -35,6 +35,9 @@ struct demo
ID3DBlob **code, ID3DBlob **errors);
size_t window_count;
bool quit;
void *user_data;
void (*idle_func)(struct demo *demo, void *user_data);
};
struct demo_window
@ -101,8 +104,32 @@ static inline void demo_window_destroy(struct demo_window *window)
static inline demo_key demo_key_from_vkey(DWORD vkey)
{
if (vkey == VK_ESCAPE)
return DEMO_KEY_ESCAPE;
static const struct
{
DWORD vkey;
demo_key demo_key;
}
lookup[] =
{
{VK_ESCAPE, DEMO_KEY_ESCAPE},
{VK_LEFT, DEMO_KEY_LEFT},
{VK_RIGHT, DEMO_KEY_RIGHT},
{VK_UP, DEMO_KEY_UP},
{VK_DOWN, DEMO_KEY_DOWN},
};
unsigned int i;
if (vkey >= '0' && vkey <= '9')
return vkey;
if (vkey >= 'A' && vkey <= 'Z')
return vkey;
for (i = 0; i < ARRAY_SIZE(lookup); ++i)
{
if (lookup[i].vkey == vkey)
return lookup[i].demo_key;
}
return DEMO_KEY_UNKNOWN;
}
@ -149,8 +176,19 @@ static inline void demo_process_events(struct demo *demo)
{
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0) != -1)
for (;;)
{
if (!demo->idle_func)
{
if (GetMessageW(&msg, NULL, 0, 0) == -1)
break;
}
else if (!PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
{
demo->idle_func(demo, demo->user_data);
continue;
}
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
@ -160,7 +198,7 @@ static inline void demo_process_events(struct demo *demo)
}
}
static inline bool demo_init(struct demo *demo)
static inline bool demo_init(struct demo *demo, void *user_data)
{
WNDCLASSEXW wc;
@ -184,7 +222,10 @@ static inline bool demo_init(struct demo *demo)
if (!RegisterClassExW(&wc))
goto fail;
demo->window_count = 0;
demo->quit = false;
demo->user_data = user_data;
demo->idle_func = NULL;
return true;
@ -199,6 +240,12 @@ static inline void demo_cleanup(struct demo *demo)
FreeLibrary(demo->d3dcompiler);
}
static inline void demo_set_idle_func(struct demo *demo,
void (*idle_func)(struct demo *demo, void *user_data))
{
demo->idle_func = idle_func;
}
static inline struct demo_swapchain *demo_swapchain_create(ID3D12CommandQueue *command_queue,
struct demo_window *window, const struct demo_swapchain_desc *desc)
{

View File

@ -41,6 +41,9 @@ struct demo
struct demo_window **windows;
size_t windows_size;
size_t window_count;
void *user_data;
void (*idle_func)(struct demo *demo, void *user_data);
};
struct demo_window
@ -207,8 +210,19 @@ static inline void demo_process_events(struct demo *demo)
xcb_flush(demo->connection);
while (demo->window_count && (event = xcb_wait_for_event(demo->connection)))
while (demo->window_count)
{
if (!demo->idle_func)
{
if (!(event = xcb_wait_for_event(demo->connection)))
break;
}
else if (!(event = xcb_poll_for_event(demo->connection)))
{
demo->idle_func(demo, demo->user_data);
continue;
}
switch (XCB_EVENT_RESPONSE_TYPE(event))
{
case XCB_EXPOSE:
@ -238,7 +252,7 @@ static inline void demo_process_events(struct demo *demo)
}
}
static inline bool demo_init(struct demo *demo)
static inline bool demo_init(struct demo *demo, void *user_data)
{
if (!(demo->connection = xcb_connect(NULL, NULL)))
return false;
@ -254,6 +268,8 @@ static inline bool demo_init(struct demo *demo)
demo->windows = NULL;
demo->windows_size = 0;
demo->window_count = 0;
demo->user_data = user_data;
demo->idle_func = NULL;
return true;
@ -269,6 +285,12 @@ static inline void demo_cleanup(struct demo *demo)
xcb_disconnect(demo->connection);
}
static inline void demo_set_idle_func(struct demo *demo,
void (*idle_func)(struct demo *demo, void *user_data))
{
demo->idle_func = idle_func;
}
static inline DXGI_FORMAT demo_get_srgb_format(DXGI_FORMAT format)
{
switch (format)

888
demos/gears.c Normal file

File diff suppressed because it is too large Load Diff

55
demos/gears.hlsl Normal file
View File

@ -0,0 +1,55 @@
cbuffer gear_block : register(b0)
{
float4x4 mvp_matrix;
float3x3 normal_matrix;
};
struct vs_in
{
float4 position : POSITION;
float3 normal : NORMAL;
float3 diffuse : DIFFUSE;
float4 transform : TRANSFORM;
};
struct vs_out
{
float4 position : SV_POSITION;
float4 colour : COLOR;
};
struct vs_out vs_main(struct vs_in i)
{
const float3 l_pos = float3(5.0, 5.0, 10.0);
float3 dir, normal;
float4 position;
struct vs_out o;
float att;
position.x = i.transform.x * i.position.x - i.transform.y * i.position.y + i.transform.z;
position.y = i.transform.x * i.position.y + i.transform.y * i.position.x + i.transform.w;
position.zw = i.position.zw;
o.position = mul(mvp_matrix, position);
dir = normalize(l_pos - o.position.xyz / o.position.w);
normal.x = i.transform.x * i.normal.x - i.transform.y * i.normal.y;
normal.y = i.transform.x * i.normal.y + i.transform.y * i.normal.x;
normal.z = i.normal.z;
att = 0.2 + dot(dir, normalize(mul(normal_matrix, normal)));
o.colour.xyz = i.diffuse.xyz * att;
o.colour.w = 1.0;
return o;
}
float4 ps_main_smooth(float4 position : SV_POSITION, float4 colour : COLOR) : SV_TARGET
{
return colour;
}
float4 ps_main_flat(float4 position : SV_POSITION, nointerpolation float4 colour : COLOR) : SV_TARGET
{
return colour;
}

40
demos/gears.vert Normal file
View File

@ -0,0 +1,40 @@
#version 150
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec4 position_in;
layout(location = 1) in vec3 normal_in;
layout(location = 2) in vec3 diffuse_in;
layout(location = 3) in vec4 transform_in;
layout(location = 0) out vec4 colour_out;
uniform gear_block
{
uniform mat4 mvp_matrix;
uniform mat3 normal_matrix;
} gear;
void main()
{
const vec3 l_pos = vec3(5.0, 5.0, 10.0);
vec3 dir, normal;
vec4 position;
float att;
position.x = transform_in.x * position_in.x - transform_in.y * position_in.y + transform_in.z;
position.y = transform_in.x * position_in.y + transform_in.y * position_in.x + transform_in.w;
position.zw = position_in.zw;
gl_Position = gear.mvp_matrix * position;
dir = normalize(l_pos - gl_Position.xyz / gl_Position.w);
normal.x = transform_in.x * normal_in.x - transform_in.y * normal_in.y;
normal.y = transform_in.x * normal_in.y + transform_in.y * normal_in.x;
normal.z = normal_in.z;
att = 0.2 + dot(dir, normalize(gear.normal_matrix * normal));
colour_out.xyz = diffuse_in.xyz * att;
colour_out.w = 1.0;
gl_Position.y = -gl_Position.y;
}

10
demos/gears_flat.frag Normal file
View File

@ -0,0 +1,10 @@
#version 150
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) flat in vec4 colour_in;
layout(location = 0) out vec4 colour_out;
void main(void)
{
colour_out = colour_in;
}

10
demos/gears_smooth.frag Normal file
View File

@ -0,0 +1,10 @@
#version 150
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec4 colour_in;
layout(location = 0) out vec4 colour_out;
void main(void)
{
colour_out = colour_in;
}

View File

@ -371,7 +371,7 @@ static int cxt_main(void)
memset(&cxt, 0, sizeof(cxt));
if (!demo_init(&cxt.demo))
if (!demo_init(&cxt.demo, NULL))
return EXIT_FAILURE;
cxt.window = demo_window_create(&cxt.demo, "Vkd3d Triangle", width, height, &cxt);