Advertisement
tinyevil

Untitled

Dec 15th, 2019
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. virtual void on_bind(Surface* surface) override {
  2.     this->surface = surface;
  3.  
  4.     using namespace render;
  5.  
  6.     Device* device = surface_get_device(surface);
  7.     staging_buffer = buffer_create(
  8.         device,
  9.         (
  10.             BufferUsage::MapClientWriteBit |
  11.             BufferUsage::MapClientPersistentBit |
  12.             BufferUsage::TransferSrcBit
  13.         ),
  14.         BufferResidence::Client,
  15.         256,
  16.         nullptr
  17.     );
  18.    
  19.     u8* p = buffer_map(
  20.         device,
  21.         staging_buffer,
  22.         (
  23.             BufferMap::DiscardAllBit |
  24.             BufferMap::WriteBit |
  25.             BufferMap::PersistentBit
  26.         ),
  27.         0,
  28.         256
  29.     );
  30.     memcpy(p, vdata, sizeof(vdata));
  31.     memcpy(p + 128, idata, sizeof(idata));
  32.     buffer_flush(device, staging_buffer, 0, sizeof(vdata));
  33.     buffer_flush(device, staging_buffer, 128, sizeof(idata));
  34.     memory_barrier(device, MemoryBarrierStage::ClientMapWriteBit);
  35.    
  36.     vertex_buffer = buffer_create(
  37.         device,
  38.         (
  39.             BufferUsage::TransferDstBit |
  40.             BufferUsage::VertexDataBit
  41.         ),
  42.         BufferResidence::DeviceLocal,
  43.         sizeof(vdata),
  44.         nullptr
  45.     );
  46.     buffer_copy(device, staging_buffer, 0, vertex_buffer, 0, sizeof(vdata));
  47.  
  48.     index_buffer = buffer_create(
  49.         device,
  50.         (
  51.             BufferUsage::TransferDstBit |
  52.             BufferUsage::IndexDataBit |
  53.             BufferUsage::ClientUpdateBit
  54.         ),
  55.         BufferResidence::DeviceLocal,
  56.         sizeof(idata),
  57.         nullptr
  58.     );
  59.     buffer_copy(device, staging_buffer, 128, index_buffer, 0, sizeof(idata));
  60.    
  61.     #define N "\n"
  62.     Shader* vs = shader_create(device, ShaderType::Vertex, (
  63.         "#version 450" N
  64.         "#extension GL_ARB_separate_shader_objects : enable" N
  65.         "layout(location = 0) in vec2 in_position;" N
  66.         "" N
  67.         "void main() {" N
  68.         "  gl_Position = vec4(in_position, 0.0, 1.0);" N
  69.         "}" N          
  70.     ));
  71.     Shader* fs = shader_create(device, ShaderType::Fragment, (
  72.         "#version 450" N
  73.         "#extension GL_ARB_separate_shader_objects : enable" N
  74.         "" N
  75.         "layout(location = 0) out vec4 out_color;" N
  76.         "" N
  77.         "void main() {" N
  78.         "  out_color = vec4(1.0, 0.5, 0.0, 1.0);" N
  79.         "}" N
  80.     ));
  81.     #undef N
  82.    
  83.     VertexBinding vertex_binding {0, sizeof(Vertex)};
  84.  
  85.     VertexAttrib vertex_attribs[] = {
  86.         {0, 0, VertexAttribFormat::R32G32_SFLOAT, 0},
  87.     };
  88.  
  89.     VertexInput vertex_input {};
  90.     vertex_input.vertex_bindings = &vertex_binding;
  91.     vertex_input.vertex_bindings_count = 1;
  92.     vertex_input.vertex_attribs = vertex_attribs;
  93.     vertex_input.vertex_attribs_count = elements_of(vertex_attribs);
  94.  
  95.     ShaderStages shader_stages {};
  96.     shader_stages.vertex_shader = vs;
  97.     shader_stages.fragment_shader = fs;
  98.  
  99.     PipelineDefinition pipeline_def{};
  100.     pipeline_def.shader_stages = &shader_stages;
  101.     pipeline_def.vertex_input = &vertex_input;
  102.     pipeline_def.primitive_type = PrimitiveType::TriangleList;
  103.  
  104.     pipeline = pipeline_create(device, &pipeline_def);
  105.  
  106.    
  107. }
  108.  
  109. virtual void on_render(Surface* surface, render::Device* device) override {
  110.     using namespace render;
  111.  
  112.     cmd_bind_pipeline(device, pipeline);
  113.     cmd_bind_vertex_buffer(device, 0, vertex_buffer, 0);
  114.     cmd_bind_index_buffer(device, index_buffer, IndexFormat::U16, 0);
  115.     cmd_draw_indexed(device, 6, 0, 0);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement