Commit 29a9a615 authored by Peter Kovář's avatar Peter Kovář

WIP

parent 99f03275
file Cube
break prepare
#break prepare_vs
#break prepare_fs
break loadTexture
run
This source diff could not be displayed because it is too large. You can view the blob instead.
TARGET = Cube
CFILES = $(TARGET).c
HEADERS = ../../Headers
SHADERS = ../../Shaders
CFLAGS += -I$(HEADERS) -I$(SHADERS)
LIBS = -lFontasy -lvulkan -lm
Linux_CFLAGS += -DVK_USE_PLATFORM_XCB_KHR
Linux_LIBS = -lxcb
include $(PEAK)/include/make/definitions
include $(PROGRAMRULES)
#include <stdio.h>
#include <string.h>
#include <vulkan/vulkan.h>
#include <Fontasy.h>
// ------------------------------------------------------- typedef & struct ---
typedef struct
typedef struct vertex_t
{
float x, y, z; // position
float s, t; // texture
float r, g, b, a; // color
float x, y, z; // position
float s, t; // texture
float r, g, b, a; // color
} vertex_t;
typedef struct
typedef struct point_s
{
float x, y, z;
vec4 color;
......@@ -25,7 +26,7 @@ vertex_buffer_t *line_buffer;
vertex_buffer_t *point_buffer;
uint shader, text_shader;
mat4 model, view, projection;
const int width = 1200;
const int width = 1600;
const int height = 1200;
......@@ -73,9 +74,9 @@ void init (void)
vec4 black = {{0,0,0,1}};
atlas = texture_atlas_new (512, 512, 1);
texture_font_t *big = texture_font_new_from_file (atlas, 400, "fonts/Vera.ttf");
texture_font_t *small = texture_font_new_from_file (atlas, 18, "fonts/Vera.ttf");
texture_font_t *title = texture_font_new_from_file (atlas, 32, "fonts/Vera.ttf");
texture_font_t *big = texture_font_new_from_file (atlas, 400, "/usr/share/fonts/bitstream-vera/Vera.ttf");
texture_font_t *small = texture_font_new_from_file (atlas, 18, "/usr/share/fonts/bitstream-vera/Vera.ttf");
texture_font_t *title = texture_font_new_from_file (atlas, 32, "/usr/share/fonts/bitstream-vera/Vera.ttf");
text_buffer = vertex_buffer_new ("vertex:3f,tex_coord:2f,color:4f");
line_buffer = vertex_buffer_new ("vertex:3f,color:4f");
......@@ -179,6 +180,8 @@ void init (void)
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RED, atlas->width, atlas->height,
0, GL_RED, GL_UNSIGNED_BYTE, atlas->data);
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
*/
uint i = 0;
......@@ -281,8 +284,8 @@ void error_callback (int error, const char *description)
// ------------------------------------------------------------------- main ---
int main (int argc, char **argv)
{
shaders ();
/*
shaders ();
GLFWwindow* window;
glfwSetErrorCallback (error_callback);
......
SUBDIRS = Glyph Subpixel
SUBDIRS = Cube Glyph Subpixel
all: subdirs
......
......@@ -1460,10 +1460,13 @@ static void prepare_depth (demo_t *demo)
}
/* Convert ppm image data from header file into RGBA texture image */
#include "lunarg.ppm.h"
boolean loadTexture (const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height)
{
(void)filename;
*width = 2048;
*height = 1024;
/*
char *cPtr;
cPtr = (char *)lunarg_ppm;
if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3))
......@@ -1490,12 +1493,13 @@ boolean loadTexture (const char *filename, uint8_t *rgba_data, VkSubresourceLayo
for (int x = 0; x < *width; x++)
{
memcpy(rowPtr, cPtr, 3);
rowPtr[3] = 255; /* Alpha of 1 */
rowPtr[3] = 255; // Alpha of 1
rowPtr += 4;
cPtr += 3;
}
rgba_data += layout->rowPitch;
}
*/
return true;
}
......@@ -2881,15 +2885,27 @@ static void init_vk (demo_t *demo)
"vkCreateInstance Failure");
}
/* Make initial call to query gpu_count, then second call for gpu info*/
// Make initial call to query gpu_count, then second call for gpu info
err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
assert(!err);
if (gpu_count > 0) {
VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
VkPhysicalDeviceProperties physical_device_props = {};
// Set the desired version we want
uint32_t desired_version = VK_API_VERSION_1_1;
err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
assert(!err);
/* For cube demo we just grab the first physical device */
for (uint32_t i = 0; i < gpu_count; i++) {
vkGetPhysicalDeviceProperties(physical_devices[i], &physical_device_props);
if (physical_device_props.apiVersion >= desired_version) {
fprintf(stderr, "%s API Version 1.1\n", physical_device_props.deviceName);
}
}
// For subpixel demo we just grab the first physical device
demo->gpu = physical_devices[0];
free(physical_devices);
} else {
......@@ -2900,7 +2916,7 @@ static void init_vk (demo_t *demo)
"vkEnumeratePhysicalDevices Failure");
}
/* Look for device extensions */
// Look for device extensions
uint32_t device_extension_count = 0;
VkBool32 swapchainExtFound = 0;
demo->enabled_extension_count = 0;
......@@ -2915,7 +2931,9 @@ static void init_vk (demo_t *demo)
assert(!err);
for (uint32_t i = 0; i < device_extension_count; i++) {
if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
fprintf(stderr, "%s\n", device_extensions[i].extensionName);
if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
swapchainExtFound = 1;
demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
}
......
The Fontasy Library
===================
SUBDIRS = Shaders Library Demos Documentation
SUBDIRS = Shaders Library Demos #Documentation
all: subdirs
......
# The Fontasy Library
\ No newline at end of file
# The Fontasy Library
## Attributions / Licenses
- Vulkan and the Vulkan logo are trademarks of the [Khronos Group Inc.](http://www.khronos.org)
GLSLFILES = cube.frag cube.vert text.frag text.vert
GLSLFILES = cube.vert cube.frag \
text.vert text.frag \
distance-field.vert distance-field.frag distance-field-2.frag \
text.vert text.frag \
v3f-c4f.vert v3f-c4f.frag \
v3f-t2f-c4f.vert v3f-t2f-c4f.frag
SPIR += $(GLSLFILES:.frag=.frag.spv)
SPIR += $(GLSLFILES:.vert=.vert.spv)
SPIR += $(GLSLFILES:.frag=.frag.spv)
INC += $(GLSLFILES:.frag=.frag.h)
INC += $(GLSLFILES:.vert=.vert.h)
INC += $(GLSLFILES:.frag=.frag.h)
all: $(SPIR) $(INC)
......
#version 450
#version 400
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 1) uniform sampler2D tex;
layout(location = 0) in vec3 fragColor;
layout (location = 0) in vec4 texcoord;
layout (location = 1) in vec3 frag_pos;
layout (location = 0) out vec4 uFragColor;
layout(location = 0) out vec4 outColor;
const vec3 lightDir= vec3(0.424, 0.566, 0.707);
void main()
void main ()
{
outColor = vec4(fragColor, 1.0);
vec3 dX = dFdx(frag_pos);
vec3 dY = dFdy(frag_pos);
vec3 normal = normalize(cross(dX,dY));
float light = max(0.0, dot(lightDir, normal));
uFragColor = light * texture(tex, texcoord.xy);
}
/*
* Vertex shader used by Cube demo.
*/
#version 400
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout(std140, binding = 0) uniform buf {
mat4 MVP;
vec4 position[12*3];
vec4 attr[12*3];
layout (std140, binding = 0) uniform buf
{
mat4 MVP;
vec4 position[12*3];
vec4 attr[12*3];
} ubuf;
layout (location = 0) out vec4 texcoord;
layout (location = 1) out vec3 frag_pos;
void main()
void main ()
{
texcoord = ubuf.attr[gl_VertexIndex];
gl_Position = ubuf.MVP * ubuf.position[gl_VertexIndex];
frag_pos = gl_Position.xyz;
texcoord = ubuf.attr[gl_VertexIndex];
gl_Position = ubuf.MVP * ubuf.position[gl_VertexIndex];
frag_pos = gl_Position.xyz;
}
uniform sampler2D u_texture;
#version 400
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
void main(void)
layout (binding = 0) uniform sampler2D u_texture;
layout (location = 0) in vec4 texcoord;
layout (location = 2) in vec4 Color;
layout (location = 0) out vec4 uFragColor;
void main (void)
{
float dist = texture2D(u_texture, gl_TexCoord[0].st).r;
float width = fwidth(dist);
float alpha = smoothstep(0.5-width, 0.5+width, dist);
gl_FragColor = vec4(gl_Color.rgb, alpha*gl_Color.a);
float dist = texture(u_texture, texcoord.xy).r;
float width = fwidth (dist);
float alpha = smoothstep (0.5 - width, 0.5 + width, dist);
uFragColor = vec4 (Color.rgb, alpha * Color.a);
}
uniform sampler2D u_texture;
#version 400
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 0) uniform sampler2D u_texture;
layout (location = 0) in vec4 texcoord;
vec3 glyph_color = vec3(1.0,1.0,1.0);
const float glyph_center = 0.50;
......@@ -7,9 +13,9 @@ const float outline_center = 0.55;
vec3 glow_color = vec3(1.0,1.0,1.0);
const float glow_center = 1.25;
void main(void)
void main (void)
{
vec4 color = texture2D(u_texture, gl_TexCoord[0].st);
vec4 color = texture(u_texture, texcoord.xy);
float dist = color.r;
float width = fwidth(dist);
float alpha = smoothstep(glyph_center-width, glyph_center+width, dist);
......
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
uniform vec4 u_color;
#version 450
// #extension GL_ARB_separate_shader_objects : enable
// #extension GL_ARB_shading_language_420pack : enable
attribute vec3 vertex;
attribute vec2 tex_coord;
attribute vec4 color;
layout (std140, binding = 0) uniform buf
{
mat4 model;
mat4 view;
mat4 projection;
vec4 color;
} ubuf;
layout (location = 0) in vec3 tex_coord;
layout (location = 0) out vec3 frag_pos;
layout (location = 1) out vec2 TexCoord;
layout (location = 2) out vec4 FrontColor;
layout (location = 3) out vec4 Position;
vec3 vertex;
vec4 color;
void main(void)
void main (void)
{
gl_TexCoord[0].xy = tex_coord.xy;
gl_FrontColor = color * u_color;
gl_Position = u_projection*(u_view*(u_model*vec4(vertex,1.0)));
TexCoord.st = tex_coord.xy;
FrontColor = color * ubuf.color;
Position = ubuf.projection * (ubuf.view * (ubuf.model * vec4 (vertex, 1.0)));
}
#version 400
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 1) uniform sampler2D tex;
layout (location = 0) in vec4 texcoord;
layout (location = 1) in vec3 frag_pos;
layout (location = 0) out vec4 uFragColor;
const vec3 lightDir= vec3(0.424, 0.566, 0.707);
void main ()
{
vec3 dX = dFdx(frag_pos);
vec3 dY = dFdy(frag_pos);
vec3 normal = normalize(cross(dX,dY));
float light = max(0.0, dot(lightDir, normal));
uFragColor = light * texture(tex, texcoord.xy);
}
/*
#version 450
#extension GL_ARB_separate_shader_objects : enable
......@@ -87,29 +107,27 @@ void main ()
float g = current.g;
float b = current.b;
/*
if (vshift <= 0.333)
{
float z = vshift / 0.333;
r = mix (current.r, previous.b, z);
g = mix (current.g, current.r, z);
b = mix (current.b, current.g, z);
}
else if (vshift <= 0.666)
{
float z = (vshift - 0.33) / 0.333;
r = mix (previous.b, previous.g, z);
g = mix (current.r, previous.b, z);
b = mix (current.g, current.r, z);
}
else if (vshift < 1.0)
{
float z = (vshift - 0.66) / 0.334;
r = mix(previous.g, previous.r, z);
g = mix(previous.b, previous.g, z);
b = mix(current.r, previous.b, z);
}
*/
// if (vshift <= 0.333)
// {
// float z = vshift / 0.333;
// r = mix (current.r, previous.b, z);
// g = mix (current.g, current.r, z);
// b = mix (current.b, current.g, z);
// }
// else if (vshift <= 0.666)
// {
// float z = (vshift - 0.33) / 0.333;
// r = mix (previous.b, previous.g, z);
// g = mix (current.r, previous.b, z);
// b = mix (current.g, current.r, z);
// }
// else if (vshift < 1.0)
// {
// float z = (vshift - 0.66) / 0.334;
// r = mix(previous.g, previous.r, z);
// g = mix(previous.b, previous.g, z);
// b = mix(current.r, previous.b, z);
// }
// float t = max (max (r, g), b);
// vec4 color = vec4 (vcolor.rgb, (r + g + b) / 3.0);
......@@ -118,3 +136,4 @@ void main ()
// outColor = vec4 (color.rgb, vcolor.a * color.a);
outColor = color;
}
*/
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
#version 450
attribute vec3 vertex;
attribute vec4 color;
void main()
mat4 model;
mat4 view;
mat4 projection;
vec3 vertex;
vec4 color;
layout (location = 0) out vec4 FrontColor;
layout (location = 1) out vec4 Position;
void main ()
{
gl_FrontColor = color;
gl_Position = projection*(view*(model*vec4(vertex,1.0)));
FrontColor = color;
Position = projection * (view * (model * vec4 (vertex, 1.0)));
}
#version 450
#extension GL_ARB_separate_shader_objects : enable
uniform sampler2D texture;
layout (binding = 0) uniform sampler2D u_texture;
layout (location = 0) in vec4 texcoord;
layout (location = 2) in vec4 Color;
layout (location = 0) out vec4 uFragColor;
void main ()
{
float a = texture2D(texture, gl_TexCoord[0].xy).r;
gl_FragColor = vec4(gl_Color.rgb, gl_Color.a*a);
float a = texture (u_texture, texcoord.xy).r;
uFragColor = vec4 (Color.rgb, Color.a * a);
}
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
attribute vec3 vertex;
attribute vec2 tex_coord;
attribute vec4 color;
void main()
#version 450
mat4 model;
mat4 view;
mat4 projection;
vec3 vertex;
vec2 tex_coord;
vec4 color;
layout (location = 0) out vec2 TexCoord;
layout (location = 1) out vec4 FrontColor;
layout (location = 2) out vec4 Position;
void main ()
{
gl_TexCoord[0].xy = tex_coord.xy;
gl_FrontColor = color;
gl_Position = projection*(view*(model*vec4(vertex,1.0)));
TexCoord = tex_coord.xy;
FrontColor = color;
Position = projection * (view * (model * vec4 (vertex, 1.0)));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment