|
1 | 1 | #include <kore3/framebuffer/framebuffer.h> |
2 | 2 |
|
3 | | -#if 0 |
| 3 | +#include <kore3/gpu/buffer.h> |
| 4 | +#include <kore3/gpu/device.h> |
4 | 5 |
|
5 | | -#include <kore3/graphics4/graphics.h> |
6 | | -#include <kore3/graphics4/indexbuffer.h> |
7 | | -#include <kore3/graphics4/pipeline.h> |
8 | | -#include <kore3/graphics4/shader.h> |
9 | | -#include <kore3/graphics4/texture.h> |
10 | | -#include <kore3/graphics4/vertexbuffer.h> |
11 | | -#include <kore3/io/filereader.h> |
12 | | -#include <kore3/log.h> |
13 | | - |
14 | | -#if 0 |
15 | | - |
16 | | -#ifdef KORE_KONG |
17 | 6 | #include <kong.h> |
18 | | -#endif |
19 | | - |
20 | | -#ifndef KORE_KONG |
21 | | -static kore_g4_shader_t vertexShader; |
22 | | -static kore_g4_shader_t fragmentShader; |
23 | | -static kore_g4_pipeline_t pipeline; |
24 | | -static kore_g4_texture_unit_t tex; |
25 | | -kore_g1_texture_filter_t kore_internal_g1_texture_filter_min = KORE_G1_TEXTURE_FILTER_LINEAR; |
26 | | -kore_g1_texture_filter_t kore_internal_g1_texture_filter_mag = KORE_G1_TEXTURE_FILTER_LINEAR; |
27 | | -kore_g1_mipmap_filter_t kore_internal_g1_mipmap_filter = KORE_G1_MIPMAP_FILTER_NONE; |
28 | | -#endif |
29 | | -static kore_g4_vertex_buffer_t vb; |
30 | | -static kore_g4_index_buffer_t ib; |
31 | | -static kore_g4_texture_t texture; |
32 | | - |
33 | | -uint32_t *kore_internal_g1_image; |
34 | | -int kore_internal_g1_w, kore_internal_g1_h, kore_internal_g1_tex_width; |
35 | | - |
36 | | -void kore_g1_begin(void) { |
37 | | - kore_g4_begin(0); |
38 | | - kore_internal_g1_image = (uint32_t *)kore_g4_texture_lock(&texture); |
39 | | -} |
40 | 7 |
|
41 | | -static inline kore_g4_texture_filter_t map_texture_filter(kore_g1_texture_filter_t filter) { |
42 | | - switch (filter) { |
43 | | - case KORE_G1_TEXTURE_FILTER_POINT: |
44 | | - return KORE_G4_TEXTURE_FILTER_POINT; |
45 | | - case KORE_G1_TEXTURE_FILTER_LINEAR: |
46 | | - return KORE_G4_TEXTURE_FILTER_LINEAR; |
47 | | - case KORE_G1_TEXTURE_FILTER_ANISOTROPIC: |
48 | | - return KORE_G4_TEXTURE_FILTER_ANISOTROPIC; |
49 | | - } |
50 | | - |
51 | | - kore_log(KORE_LOG_LEVEL_WARNING, "unhandled kore_g1_texture_filter_t (%i)", filter); |
52 | | - return KORE_G4_TEXTURE_FILTER_LINEAR; |
53 | | -} |
| 8 | +static kore_gpu_device framebuffer_device; |
| 9 | +static kore_gpu_command_list framebuffer_list; |
| 10 | +static kore_gpu_buffer framebuffer_buffer; |
54 | 11 |
|
55 | | -static inline kore_g4_mipmap_filter_t map_mipmap_filter(kore_g1_mipmap_filter_t filter) { |
56 | | - switch (filter) { |
57 | | - case KORE_G1_MIPMAP_FILTER_NONE: |
58 | | - return KORE_G4_MIPMAP_FILTER_NONE; |
59 | | - case KORE_G1_MIPMAP_FILTER_POINT: |
60 | | - return KORE_G4_MIPMAP_FILTER_POINT; |
61 | | - case KORE_G1_MIPMAP_FILTER_LINEAR: |
62 | | - return KORE_G4_MIPMAP_FILTER_LINEAR; |
63 | | - } |
64 | | - |
65 | | - kore_log(KORE_LOG_LEVEL_WARNING, "unhandled kore_g1_mipmap_filter_t (%i)", filter); |
66 | | - return KORE_G4_MIPMAP_FILTER_NONE; |
67 | | -} |
68 | | - |
69 | | -void kore_g1_end(void) { |
70 | | - kore_internal_g1_image = NULL; |
71 | | - kore_g4_texture_unlock(&texture); |
72 | | - |
73 | | - kore_g4_clear(KORE_G4_CLEAR_COLOR, 0xff000000, 0.0f, 0); |
74 | | - |
75 | | -#ifdef KORE_KONG |
76 | | - kore_g4_set_pipeline(&kore_g1_pipeline); |
77 | | -#else |
78 | | - kore_g4_set_pipeline(&pipeline); |
79 | | -#endif |
80 | | - |
81 | | -#ifndef KORE_KONG |
82 | | - kore_g4_set_texture(tex, &texture); |
83 | | - kore_g4_set_texture_minification_filter(tex, map_texture_filter(kore_internal_g1_texture_filter_min)); |
84 | | - kore_g4_set_texture_magnification_filter(tex, map_texture_filter(kore_internal_g1_texture_filter_mag)); |
85 | | - kore_g4_set_texture_mipmap_filter(tex, map_mipmap_filter(kore_internal_g1_mipmap_filter)); |
86 | | -#endif |
87 | | - kore_g4_set_vertex_buffer(&vb); |
88 | | - kore_g4_set_index_buffer(&ib); |
89 | | - kore_g4_draw_indexed_vertices(); |
90 | | - |
91 | | - kore_g4_end(0); |
92 | | - kore_g4_swap_buffers(); |
93 | | -} |
| 12 | +uint32_t kore_framebuffer_width; |
| 13 | +uint32_t kore_framebuffer_height; |
| 14 | +uint32_t kore_framebuffer_stride; |
94 | 15 |
|
95 | | -void kore_g1_init(int width, int height) { |
96 | | - kore_internal_g1_w = width; |
97 | | - kore_internal_g1_h = height; |
98 | | - |
99 | | -#ifndef KORE_KONG |
100 | | - { |
101 | | - kore_file_reader_t file; |
102 | | - kore_file_reader_open(&file, "g1.vert", KORE_FILE_TYPE_ASSET); |
103 | | - void *data = malloc(kore_file_reader_size(&file)); |
104 | | - kore_file_reader_read(&file, data, kore_file_reader_size(&file)); |
105 | | - kore_file_reader_close(&file); |
106 | | - kore_g4_shader_init(&vertexShader, data, kore_file_reader_size(&file), KORE_G4_SHADER_TYPE_VERTEX); |
107 | | - free(data); |
108 | | - } |
109 | | - |
110 | | - { |
111 | | - kore_file_reader_t file; |
112 | | - kore_file_reader_open(&file, "g1.frag", KORE_FILE_TYPE_ASSET); |
113 | | - void *data = malloc(kore_file_reader_size(&file)); |
114 | | - kore_file_reader_read(&file, data, kore_file_reader_size(&file)); |
115 | | - kore_file_reader_close(&file); |
116 | | - kore_g4_shader_init(&fragmentShader, data, kore_file_reader_size(&file), KORE_G4_SHADER_TYPE_FRAGMENT); |
117 | | - free(data); |
118 | | - } |
119 | | - |
120 | | - kore_g4_vertex_structure_t structure; |
121 | | - kore_g4_vertex_structure_init(&structure); |
122 | | - kore_g4_vertex_structure_add(&structure, "pos", KORE_G4_VERTEX_DATA_F32_3X); |
123 | | - kore_g4_vertex_structure_add(&structure, "tex", KORE_G4_VERTEX_DATA_F32_2X); |
124 | | - kore_g4_pipeline_init(&pipeline); |
125 | | - pipeline.input_layout[0] = &structure; |
126 | | - pipeline.input_layout[1] = NULL; |
127 | | - pipeline.vertex_shader = &vertexShader; |
128 | | - pipeline.fragment_shader = &fragmentShader; |
129 | | - kore_g4_pipeline_compile(&pipeline); |
130 | | - |
131 | | - tex = kore_g4_pipeline_get_texture_unit(&pipeline, "texy"); |
132 | | -#endif |
133 | | - |
134 | | - kore_g4_texture_init(&texture, width, height, KORE_IMAGE_FORMAT_RGBA32); |
135 | | - kore_internal_g1_tex_width = texture.tex_width; |
136 | | - |
137 | | - kore_internal_g1_image = (uint32_t *)kore_g4_texture_lock(&texture); |
138 | | - int stride = kore_g4_texture_stride(&texture) / 4; |
139 | | - for (int y = 0; y < texture.tex_height; ++y) { |
140 | | - for (int x = 0; x < texture.tex_width; ++x) { |
141 | | - kore_internal_g1_image[y * stride + x] = 0; |
142 | | - } |
143 | | - } |
144 | | - kore_g4_texture_unlock(&texture); |
145 | | - |
146 | | - // Correct for the difference between the texture's desired size and the actual power of 2 size |
147 | | - float xAspect = (float)width / texture.tex_width; |
148 | | - float yAspect = (float)height / texture.tex_height; |
149 | | - |
150 | | -#ifdef KORE_KONG |
151 | | - kore_g4_vertex_buffer_init(&vb, 4, &kore_g1_vertex_in_structure, KORE_G4_USAGE_STATIC, 0); |
152 | | -#else |
153 | | - kore_g4_vertex_buffer_init(&vb, 4, &structure, KORE_G4_USAGE_STATIC, 0); |
154 | | -#endif |
155 | | - float *v = kore_g4_vertex_buffer_lock_all(&vb); |
156 | | - { |
157 | | - int i = 0; |
158 | | - v[i++] = -1; |
159 | | - v[i++] = 1; |
160 | | - v[i++] = 0.5; |
161 | | - v[i++] = 0; |
162 | | - v[i++] = 0; |
163 | | - v[i++] = 1; |
164 | | - v[i++] = 1; |
165 | | - v[i++] = 0.5; |
166 | | - v[i++] = xAspect; |
167 | | - v[i++] = 0; |
168 | | - v[i++] = 1; |
169 | | - v[i++] = -1; |
170 | | - v[i++] = 0.5; |
171 | | - v[i++] = xAspect; |
172 | | - v[i++] = yAspect; |
173 | | - v[i++] = -1; |
174 | | - v[i++] = -1; |
175 | | - v[i++] = 0.5; |
176 | | - v[i++] = 0; |
177 | | - v[i++] = yAspect; |
178 | | - } |
179 | | - kore_g4_vertex_buffer_unlock_all(&vb); |
180 | | - |
181 | | - kore_g4_index_buffer_init(&ib, 6, KORE_G4_INDEX_BUFFER_FORMAT_32BIT, KORE_G4_USAGE_STATIC); |
182 | | - uint32_t *ii = (uint32_t *)kore_g4_index_buffer_lock_all(&ib); |
183 | | - { |
184 | | - int i = 0; |
185 | | - ii[i++] = 0; |
186 | | - ii[i++] = 1; |
187 | | - ii[i++] = 3; |
188 | | - ii[i++] = 1; |
189 | | - ii[i++] = 2; |
190 | | - ii[i++] = 3; |
191 | | - } |
192 | | - kore_g4_index_buffer_unlock_all(&ib); |
193 | | -} |
| 16 | +uint8_t *kore_framebuffer_pixels; |
194 | 17 |
|
195 | | -#if defined(KORE_DYNAMIC_COMPILE) || defined(KORE_DYNAMIC) |
| 18 | +void kore_fb_init(uint32_t width, uint32_t height) { |
| 19 | + kore_gpu_device_wishlist wishlist = {0}; |
| 20 | + kore_gpu_device_create(&framebuffer_device, &wishlist); |
196 | 21 |
|
197 | | -void kore_g1_set_pixel(int x, int y, float red, float green, float blue) { |
198 | | - if (x < 0 || x >= kore_internal_g1_w || y < 0 || y >= kore_internal_g1_h) |
199 | | - return; |
200 | | - int r = (int)(red * 255); |
201 | | - int g = (int)(green * 255); |
202 | | - int b = (int)(blue * 255); |
203 | | - kore_internal_g1_image[y * kore_internal_g1_tex_width + x] = 0xff << 24 | b << 16 | g << 8 | r; |
204 | | -} |
| 22 | + kore_framebuffer_width = width; |
| 23 | + kore_framebuffer_height = height; |
| 24 | + kore_framebuffer_stride = kore_gpu_device_align_texture_row_bytes(&framebuffer_device, width * 4); |
205 | 25 |
|
206 | | -int kore_g1_width() { |
207 | | - return kore_internal_g1_w; |
208 | | -} |
| 26 | + kong_init(&framebuffer_device); |
209 | 27 |
|
210 | | -int kore_g1_height() { |
211 | | - return kore_internal_g1_h; |
212 | | -} |
| 28 | + kore_gpu_device_create_command_list(&framebuffer_device, KORE_GPU_COMMAND_LIST_TYPE_GRAPHICS, &framebuffer_list); |
213 | 29 |
|
214 | | -void kore_g1_set_texture_magnification_filter(kore_g1_texture_filter_t filter) { |
215 | | - kore_internal_g1_texture_filter_mag = filter; |
| 30 | + kore_gpu_buffer_parameters parameters = { |
| 31 | + .size = kore_framebuffer_height * kore_framebuffer_stride, |
| 32 | + .usage_flags = KORE_GPU_BUFFER_USAGE_CPU_WRITE | KORE_GPU_BUFFER_USAGE_COPY_SRC, |
| 33 | + }; |
| 34 | + kore_gpu_device_create_buffer(&framebuffer_device, ¶meters, &framebuffer_buffer); |
216 | 35 | } |
217 | 36 |
|
218 | | -void kore_g1_set_texture_minification_filter(kore_g1_texture_filter_t filter) { |
219 | | - kore_internal_g1_texture_filter_min = filter; |
| 37 | +void kore_fb_begin(void) { |
| 38 | + kore_framebuffer_pixels = (uint8_t *)kore_gpu_buffer_lock_all(&framebuffer_buffer); |
220 | 39 | } |
221 | 40 |
|
222 | | -void kore_g1_set_texture_mipmap_filter(kore_g1_mipmap_filter_t filter) { |
223 | | - kore_internal_g1_mipmap_filter = filter; |
| 41 | +void kore_fb_end(void) { |
| 42 | + kore_gpu_buffer_unlock(&framebuffer_buffer); |
| 43 | + |
| 44 | + kore_gpu_texture *gpu_framebuffer = kore_gpu_device_get_framebuffer(&framebuffer_device); |
| 45 | + |
| 46 | + kore_gpu_color clear_color = { |
| 47 | + .r = 0.0f, |
| 48 | + .g = 0.0f, |
| 49 | + .b = 0.0f, |
| 50 | + .a = 1.0f, |
| 51 | + }; |
| 52 | + |
| 53 | + kore_gpu_render_pass_parameters parameters = { |
| 54 | + .color_attachments_count = 1, |
| 55 | + .color_attachments = |
| 56 | + { |
| 57 | + { |
| 58 | + .load_op = KORE_GPU_LOAD_OP_CLEAR, |
| 59 | + .clear_value = clear_color, |
| 60 | + .texture = |
| 61 | + { |
| 62 | + .texture = gpu_framebuffer, |
| 63 | + .array_layer_count = 1, |
| 64 | + .mip_level_count = 1, |
| 65 | + .format = kore_gpu_device_framebuffer_format(&framebuffer_device), |
| 66 | + .dimension = KORE_GPU_TEXTURE_VIEW_DIMENSION_2D, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }; |
| 71 | + kore_gpu_command_list_begin_render_pass(&framebuffer_list, ¶meters); |
| 72 | + |
| 73 | + kore_gpu_command_list_end_render_pass(&framebuffer_list); |
| 74 | + |
| 75 | + kore_gpu_image_copy_buffer copy_buffer = { |
| 76 | + .buffer = &framebuffer_buffer, |
| 77 | + .bytes_per_row = kore_framebuffer_stride, |
| 78 | + .offset = 0, |
| 79 | + .rows_per_image = kore_framebuffer_height, |
| 80 | + }; |
| 81 | + |
| 82 | + kore_gpu_image_copy_texture copy_texture = { |
| 83 | + .texture = gpu_framebuffer, |
| 84 | + .origin_x = 0, |
| 85 | + .origin_y = 0, |
| 86 | + .origin_z = 0, |
| 87 | + .mip_level = 0, |
| 88 | + .aspect = KORE_GPU_IMAGE_COPY_ASPECT_ALL, |
| 89 | + }; |
| 90 | + |
| 91 | + kore_gpu_command_list_copy_buffer_to_texture(&framebuffer_list, ©_buffer, ©_texture, kore_framebuffer_width, kore_framebuffer_height, 1); |
| 92 | + |
| 93 | + kore_gpu_command_list_present(&framebuffer_list); |
| 94 | + |
| 95 | + kore_gpu_device_execute_command_list(&framebuffer_device, &framebuffer_list); |
224 | 96 | } |
225 | | - |
226 | | -#endif |
227 | | - |
228 | | -#endif |
229 | | - |
230 | | -#endif |
0 commit comments