Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.24 KB | None | 0 0
  1. #
  2. in: console VRAM
  3. 320x240 palletized pixel data
  4. 16-color (subject to change) pallete in RGB 24-bit format
  5.  
  6. using import glsl
  7. using import glm
  8.  
  9. #shader constants
  10. let +palette-width+ = 4
  11. let +palette-height+ = 4
  12. let +screen-width+ = 320
  13. let +screen-height+ = 240
  14.  
  15. in position : vec4
  16. in uv : vec2
  17. inout screen-uv : vec2
  18.  
  19. fn vertex-shader ()
  20. screen-uv.out = uv
  21. gl_Position = position
  22.  
  23. +vertex-shader-source+ := (compile-glsl 330 'vertex (typify vertex-shader))
  24.  
  25. uniform screen-buffer : sampler2D
  26. uniform palette : sampler2D
  27. out out-color : vec4
  28.  
  29. fn frag-shader ()
  30. let screen-column = ((screen-uv.in.x * +screen-width+) as i32)
  31. let color-pair-byte = ((. ((texture screen-buffer screen-uv.in) * 255) r) as i32)
  32.  
  33. let color-index =
  34. if ((screen-column % 2) == 0)
  35. (color-pair-byte & 0x0f)
  36. else
  37. (color-pair-byte >> 4)
  38.  
  39. let pcolor =
  40. texelFetch
  41. palette
  42. ivec2
  43. (color-index % +palette-width+)
  44. (color-index // +palette-width+)
  45. 0
  46.  
  47. out-color = (vec4 pcolor.rgb 1.0)
  48.  
  49. +fragment-shader-source+ := (compile-glsl 330 'fragment (typify frag-shader))
  50. run-stage;
  51.  
  52. include "stdio.h"
  53. include "stdlib.h"
  54. include "time.h"
  55. include
  56. (import rnd)
  57. """"#define RND_IMPLEMENTATION
  58. #include "include/rnd.h"
  59.  
  60. include
  61. (import sokol)
  62. """"#include "include/sokol/sokol_gfx.h"
  63. #include "include/sokol/sokol_app.h"
  64.  
  65. #rng stuff
  66. global *rnd-well* : rnd.rnd_well_t
  67.  
  68. #VRAM state globals
  69. global *screen-buffer* : (mutable (pointer i8))
  70. global *palette-buffer* : (mutable (pointer i8))
  71.  
  72. global *screen-texture* : sokol.sg_image
  73. global *palette-texture* : sokol.sg_image
  74. global *gfx-bindings* : sokol.sg_bindings
  75. global *gfx-pipeline* : sokol.sg_pipeline
  76.  
  77. # TODO: change into own module so every module uses the same constants
  78. # vram constants
  79. let +screen-buffer-size+ = (+screen-width+ * +screen-height+ // 2)
  80. let +palette-buffer-size+ = (+palette-width+ * +palette-height+ * (sizeof i32))
  81.  
  82. using import enum
  83. enum memfill plain
  84. incremental = 0
  85. random
  86. incremental-4bit
  87. random-4bit
  88. zero
  89.  
  90.  
  91. fn fill-buffer (mem size method)
  92. switch method
  93. case memfill.incremental
  94. for i in (range size)
  95. if (size < 256)
  96. #can't express the whole range, so we subdivide it
  97. mem @ i = (i * ((256 / size) as i32) % 256) as i8
  98. else
  99. mem @ i = (i % 256) as i8
  100. pass memfill.random-4bit
  101. case memfill.random
  102. for i in (range size)
  103. mem @ i = ((rnd.rnd_well_next &*rnd-well*) % 256) as i8
  104. case memfill.incremental-4bit
  105. for i in (range size)
  106. let high-nibble = (((i % 8) * 2) << 4)
  107. let low-nibble = (((i % 8) * 2) + 1)
  108. mem @ i = (high-nibble | low-nibble) as i8
  109. case memfill.zero
  110. for i in (range size)
  111. mem @ i = 0:i8
  112. default ();
  113. ;
  114.  
  115. fn init ()
  116. rnd.rnd_well_seed &*rnd-well* ((clock) as u32)
  117.  
  118. using sokol
  119.  
  120. local screen-quad =
  121. #TODO: change this to account for screen scaling using a projection matrix
  122. arrayof f32
  123. # position uv
  124. \ -1.0 1.0 0.5 0.0 1.0 # top-left
  125. \ -1.0 -1.0 0.5 0.0 0.0 # bottom-left
  126. \ 1.0 -1.0 0.5 1.0 0.0 # bottom-right
  127. \ -1.0 1.0 0.5 0.0 1.0 # top-left
  128. \ 1.0 -1.0 0.5 1.0 0.0 # bottom-right
  129. \ 1.0 1.0 0.5 1.0 1.0 # top-right
  130.  
  131.  
  132. *gfx-bindings*.vertex_buffers @ 0 =
  133. sg_make_buffer
  134. &
  135. local sg_buffer_desc
  136. size = (sizeof screen-quad)
  137. type = SG_BUFFERTYPE_VERTEXBUFFER
  138. usage = _SG_USAGE_DEFAULT
  139. content = &screen-quad
  140. label = "screen-quad"
  141.  
  142.  
  143. local shader-desc : sg_shader_desc
  144. vs = (local sg_shader_stage_desc (source = +vertex-shader-source+))
  145. fs =
  146. local sg_shader_stage_desc
  147. source = +fragment-shader-source+
  148. label = "console-screen-shader"
  149. (. (shader-desc.attrs @ 0) name) = "position"
  150. (. (shader-desc.attrs @ 0) sem_name) = "POS"
  151. (. (shader-desc.attrs @ 1) name) = "uv"
  152. (. (shader-desc.attrs @ 1) sem_name) = "UV"
  153. (shader-desc.fs.images @ 0) =
  154. local sg_shader_image_desc
  155. name = "screen_buffer"
  156. type = SG_IMAGETYPE_2D
  157. (shader-desc.fs.images @ 1) =
  158. local sg_shader_image_desc
  159. name = "palette"
  160. type = SG_IMAGETYPE_2D
  161.  
  162. local console-screen-shader = (sg_make_shader &shader-desc)
  163.  
  164. local def-stencil-state : sg_stencil_state
  165. fail_op = _SG_STENCILOP_DEFAULT
  166. depth_fail_op = _SG_STENCILOP_DEFAULT
  167. pass_op = (bitcast 0 sg_stencil_op)#_SG_STENCILOP_DEFAULT
  168. compare_func = _SG_COMPAREFUNC_DEFAULT
  169.  
  170. local pip-desc : sg_pipeline_desc
  171. shader = console-screen-shader
  172. primitive_type = _SG_PRIMITIVETYPE_DEFAULT
  173. index_type = _SG_INDEXTYPE_DEFAULT
  174. depth_stencil =
  175. local sg_depth_stencil_state
  176. stencil_front = def-stencil-state
  177. stencil_back = def-stencil-state
  178. depth_compare_func = _SG_COMPAREFUNC_DEFAULT
  179. blend =
  180. local sg_blend_state
  181. src_factor_rgb = _SG_BLENDFACTOR_DEFAULT
  182. dst_factor_rgb = _SG_BLENDFACTOR_DEFAULT
  183. op_rgb = _SG_BLENDOP_DEFAULT
  184. src_factor_alpha = _SG_BLENDFACTOR_DEFAULT
  185. dst_factor_alpha = _SG_BLENDFACTOR_DEFAULT
  186. op_alpha = _SG_BLENDOP_DEFAULT
  187. color_format = _SG_PIXELFORMAT_DEFAULT
  188. depth_format = _SG_PIXELFORMAT_DEFAULT
  189. rasterizer =
  190. local sg_rasterizer_state
  191. cull_mode = _SG_CULLMODE_DEFAULT
  192. face_winding = _SG_FACEWINDING_DEFAULT
  193. label = "console-screen-pipeline"
  194.  
  195. pip-desc.layout.attrs @ 0 = (local sg_vertex_attr_desc (format = SG_VERTEXFORMAT_FLOAT3))
  196. pip-desc.layout.attrs @ 1 = (local sg_vertex_attr_desc (format = SG_VERTEXFORMAT_FLOAT2))
  197.  
  198. *gfx-pipeline* = (sg_make_pipeline &pip-desc)
  199.  
  200. *screen-buffer* = (malloc-array i8 +screen-buffer-size+)
  201. *palette-buffer* = (malloc-array i8 +palette-buffer-size+)
  202.  
  203. *screen-texture* =
  204. sg_make_image
  205. &
  206. local sg_image_desc
  207. type = SG_IMAGETYPE_2D
  208. width = (+screen-width+ // 2) #FIXME: we need better names to convey why this is necessary
  209. height = +screen-height+
  210. usage = SG_USAGE_STREAM
  211. pixel_format = SG_PIXELFORMAT_L8
  212. min_filter = SG_FILTER_NEAREST
  213. mag_filter = SG_FILTER_NEAREST
  214. wrap_u = SG_WRAP_REPEAT
  215. wrap_v = SG_WRAP_REPEAT
  216. wrap_w = SG_WRAP_REPEAT
  217.  
  218. *gfx-bindings*.fs_images @ 0 = *screen-texture*
  219.  
  220. *palette-texture* =
  221. sg_make_image
  222. &
  223. local sg_image_desc
  224. type = SG_IMAGETYPE_2D
  225. width = +palette-width+
  226. height = +palette-height+
  227. usage = SG_USAGE_STREAM
  228. pixel_format = SG_PIXELFORMAT_RGBA8
  229. min_filter = SG_FILTER_NEAREST
  230. mag_filter = SG_FILTER_NEAREST
  231. wrap_u = SG_WRAP_REPEAT
  232. wrap_v = SG_WRAP_REPEAT
  233. wrap_w = SG_WRAP_REPEAT
  234.  
  235. *gfx-bindings*.fs_images @ 1 = *palette-texture*
  236.  
  237. fill-buffer *palette-buffer* +palette-buffer-size+ memfill.random
  238. ;
  239.  
  240. fn draw-screen ()
  241. fill-buffer *screen-buffer* +screen-buffer-size+ memfill.incremental-4bit
  242. # fill-buffer *palette-buffer* +palette-buffer-size+ memfill.random
  243.  
  244. local screen-content : sokol.sg_image_content
  245. (@ screen-content.subimage 0 0) =
  246. local sokol.sg_subimage_content
  247. ptr = (bitcast (deref *screen-buffer*) voidstar)
  248. size = +screen-buffer-size+
  249. local palette-content : sokol.sg_image_content
  250. (@ palette-content.subimage 0 0) =
  251. local sokol.sg_subimage_content
  252. ptr = (bitcast (deref *palette-buffer*) voidstar)
  253. size = +palette-buffer-size+
  254.  
  255. local pass-action : sokol.sg_pass_action
  256. depth =
  257. local sokol.sg_depth_attachment_action
  258. action = sokol._SG_ACTION_DEFAULT
  259. stencil =
  260. local sokol.sg_stencil_attachment_action
  261. action = sokol._SG_ACTION_DEFAULT
  262. pass-action.colors @ 0 =
  263. local sokol.sg_color_attachment_action
  264. action = sokol.SG_ACTION_CLEAR
  265. val = (arrayof f32 0.14 0.14 0.14 1.0)
  266.  
  267. sokol.sg_update_image *screen-texture* &screen-content
  268. sokol.sg_update_image *palette-texture* &palette-content
  269. sokol.sg_begin_default_pass &pass-action (sokol.sapp_width) (sokol.sapp_height)
  270. sokol.sg_apply_pipeline *gfx-pipeline*
  271. sokol.sg_apply_bindings &*gfx-bindings*
  272. sokol.sg_draw 0 6 1
  273. sokol.sg_end_pass;
  274. sokol.sg_commit;
  275.  
  276. fn cleanup ()
  277. free *screen-buffer*
  278. free *palette-buffer*
  279. sokol.sg_destroy_image *screen-texture*
  280. sokol.sg_destroy_image *palette-texture*
  281. ;
  282.  
  283. do
  284. let
  285. init
  286. draw-screen
  287. cleanup
  288. locals;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement