v 0. Pasted by Anonymous as cpp at 2014-10-31 16:50:03 MSK and set expiration to never.

Paste will expire never.

  1. #pragma once
  2.  
  3. #include "nu_Classes.h"
  4. #include "nu_Cach.h"
  5. #include "nu_Matrix4.h"
  6. #include "nu_Color.h"
  7. #include "nu_Geom.h"
  8. #include "nu_Texture.h"
  9. #include "nu_Sampler.h"
  10.  
  11. NAMESPACE_NULIGINE_BEGIN;
  12.  
  13. #define nuSetStackData(i,d) { auto val = d; stack->SetShaderData(i, &val, sizeof(val) ); }
  14. #define NU_MAX_COLOR_ATTACHMENTS 4
  15. #define METAL_TEXTURE_OFFSET 6
  16. #define METAL_BUFFER_OFFSET 4
  17.  
  18.  
  19. struct Ds;
  20.  
  21. enum TexLoadAction{
  22.     TexLoadActionDontCare = 0,
  23.     TexLoadActionLoad = 1,
  24.     TexLoadActionClear = 2
  25. };
  26. enum TexStoreAction{
  27.     TexStoreActionDontCare = 0,
  28.     TexStoreActionStore = 1,
  29.     TexStoreActionMultisampleResolve = 2
  30. };
  31.  
  32. struct RS_Base{
  33.     RS_Type type;
  34.     size_t size;
  35.     virtual void Release(){}
  36. };
  37. struct RS_BackBufferSize: public RS_Base{
  38.     u32 w, h;
  39. };
  40. struct RS_Cam: public RS_Base{
  41.     Matrix4 proj_matrix, view_matrix, vp, inv_vp, last_vp;
  42.     Vec3 pos;
  43. };
  44. struct RS_Depth : public RS_Base{
  45.     bool depth_test, depth_write;
  46.     CompFunct depth_funct;
  47. };
  48. struct RS_BackFaceCull : public RS_Base{
  49.     bool enable;
  50. };
  51. struct RS_AlphaBlend : public RS_Base{
  52.     bool enable;
  53.     bool write_a_chanel;
  54. };
  55. struct RS_DepthLess : public RS_Base{
  56.     bool is_depth_less;
  57. };
  58. struct RS_ShaderVsPs : public RS_Base{
  59.     ShaderGpu *gpu_vs, *gpu_ps;
  60. };
  61. struct RS_ShaderData: public RS_Base{
  62.     size_t index;
  63.     size_t ptr_size;
  64. };
  65. struct RS_ProgramGLValue: public RS_Base{
  66.     ShaderID id;
  67.     size_t val_size;
  68.     size_t val_count;
  69. };
  70. struct RS_ScissorEnable : public RS_Base{
  71.     bool is_enable;
  72. };
  73. struct RS_RastirizerType : public RS_Base{
  74.     RastirizerType set_type;
  75. };
  76. struct RS_ColorMask : public RS_Base{
  77.     bool rgba[4];
  78. };
  79. struct RS_Viewport : public RS_Base{
  80.     int x, y, w, h;
  81. };
  82. struct RS_BreakPoint  : public RS_Base{
  83.     FromStruct from;
  84. };
  85. struct RS_DrawInstancing : public RS_Base{
  86.     size_t offset;
  87.     size_t count;
  88.     DrawBufferGpu *vb, *ib;
  89.     size_t stride_size;
  90.     FromStruct from;
  91. };
  92. struct RS_StartSector : public RS_Base{
  93.     const char *sector_name;
  94.     int setctor_index;
  95. };
  96. struct RS_CanSkipEnable : public RS_Base{
  97.     bool is_enable;
  98. };
  99. struct RS_StencilParam : public RS_Base{
  100.     StencilState state;
  101.     CompFunct funct;
  102.     int ref;
  103.     StencilOp stencil_fail;  // Action to take if the stencil test fails.
  104.     StencilOp depth_fail; // Action to take if the stencil test is successful, but the depth test failed.
  105.     StencilOp both_fail; // Action to take if both the stencil test and depth tests pass.
  106.    
  107.     RS_StencilParam(){
  108.         funct = C_ALWAYS;
  109.         stencil_fail = depth_fail = both_fail = OP_KEEP;
  110.         ref = -1; // -1 its meen not set
  111.     }
  112. };
  113. struct RS_GlobalRenderTexture : public RS_Base {
  114.     TextureGpuPtr tex_gpu;
  115.     GlobalRenderTexture grt_type;
  116.     Sampler sampler;
  117.     void Release()override{
  118.         tex_gpu.Reset();
  119.     }
  120. };
  121. struct NU_API colorAttachmentDesc{
  122.     TextureGpuPtr texture;
  123.     TexLoadAction loadAction;
  124.     TexStoreAction storeAction;
  125.     Color clearColor;
  126.     u32 slice;
  127.     colorAttachmentDesc(){
  128.         loadAction = TexLoadActionDontCare;
  129.         storeAction = TexStoreActionDontCare;
  130.         slice = 0;
  131.     }
  132.     void Set(Texture *tex, TexLoadAction loadAction, TexStoreAction storeAction, Color clearColor = Color()){
  133.         nu_assert_ptr(tex);
  134.         this->loadAction = loadAction;
  135.         this->storeAction = storeAction;
  136.         this->clearColor = clearColor;
  137.         texture = tex->GetTexureGpu();
  138.     }
  139. };
  140. struct NU_API depthAttachmentDesc{
  141.     TextureGpuPtr texture;
  142.     TexLoadAction loadAction;
  143.     TexStoreAction storeAction;
  144.     float clearDepth;
  145.     depthAttachmentDesc(){
  146.         loadAction = TexLoadActionDontCare;
  147.         storeAction = TexStoreActionDontCare;
  148.         clearDepth = 1.0;
  149.     }
  150.     void Set(Texture *tex, TexLoadAction loadAction, TexStoreAction storeAction, float clearDepth =  1.0){
  151.         nu_assert_ptr(tex);
  152.         this->loadAction = loadAction;
  153.         this->storeAction = storeAction;
  154.         this->clearDepth = clearDepth;
  155.         texture = tex->GetTexureGpu();
  156.     }
  157. };
  158. struct NU_API stencilAttachmentDesc{
  159.     TextureGpuPtr texture;
  160.     TexLoadAction loadAction;
  161.     TexStoreAction storeAction;
  162.     u32 clearStencil;
  163.     stencilAttachmentDesc(){
  164.         loadAction = TexLoadActionDontCare;
  165.         storeAction = TexStoreActionDontCare;
  166.         clearStencil = 0;
  167.     }
  168.     void Set(Texture *tex, TexLoadAction loadAction, TexStoreAction storeAction, u32 clearStencil = 0){
  169.         nu_assert_ptr(tex);
  170.         this->loadAction = loadAction;
  171.         this->storeAction = storeAction;
  172.         this->clearStencil = clearStencil;
  173.         texture = tex->GetTexureGpu();
  174.     }
  175. };
  176. struct NU_API RenderPassDesc{
  177.     FromStruct from;
  178.     colorAttachmentDesc colorAttachments[NU_MAX_COLOR_ATTACHMENTS];
  179.     depthAttachmentDesc depthAttachment;
  180.     stencilAttachmentDesc stencilAttachment;
  181.     RenderPassDesc(FromStruct set_from = FROM){
  182.         from = set_from;
  183.     }
  184. };
  185. struct RS_RenderPass : public RS_Base{
  186.     RenderPassDesc desc;
  187.     void Release()override{
  188.         desc.depthAttachment.texture.Reset();
  189.         desc.stencilAttachment.texture.Reset();
  190.         for (int i = 0; i < NU_MAX_COLOR_ATTACHMENTS; i++)
  191.             desc.colorAttachments[i].texture.Reset();
  192.     }
  193. };
  194. struct RS_Texture : public RS_Base{
  195.     TextureGpuPtr tex_gpu;
  196.     size_t index;
  197.     bool by_index;
  198.     ShaderID id;
  199.     Sampler sampler;
  200.     virtual void Release()override{
  201.         tex_gpu.Reset();
  202.     }
  203. };
  204. struct RS_ReadTextureData : public RS_Base{
  205.     TextureGpuPtr tex;
  206.     void Release()override{
  207.         tex.Reset();
  208.     }
  209. };
  210. struct ForwardParam{
  211.     void Pack( Matrix4 *m, float alpha ){
  212.         float data[4] = {1,1,1, alpha };
  213.         mat_w_and_color = *m;
  214.         mat_w_and_color.SetUserDataFloat4( data );
  215.     }
  216.     void Pack( Matrix4 *m, Color &c, float alpha ){
  217.         Color set_c = c;
  218.         set_c.a *= alpha;
  219.         mat_w_and_color = *m;
  220.         mat_w_and_color.SetUserDataFloat4( (float*)&set_c );
  221.     }
  222.     void Pack( Matrix4 *m, Color &c, float alpha, float is_dynamik ){
  223.         Color set_c = c;
  224.         set_c.a *= alpha;
  225.         set_c.r = set_c.r * 0.5 + is_dynamik;
  226.         mat_w_and_color = *m;
  227.         mat_w_and_color.SetUserDataFloat4( (float*)&set_c );
  228.     }
  229.     Matrix4 mat_w_and_color;
  230. };
  231. struct DefferedParam{
  232.     Matrix4 matW;
  233.     Matrix4 matLastW;
  234.     void Pack( Matrix4 *m, Matrix4 *m_last, Color &c, float alpha, bool is_dunamik ){
  235.         memcpy( &matW, m, sizeof( Matrix4 ) );
  236.         memcpy( &matLastW, m_last, sizeof( Matrix4 ) );
  237.         matW.SetUserDataFloat4( (float*)&c );
  238.         float set_dunamik = is_dunamik?1:0;
  239.         float user_data[4] = { alpha, set_dunamik, 0, 1 };
  240.         matLastW.SetUserDataFloat4( user_data );
  241.     }
  242. };
  243.  
  244. class NU_API DrawStackArr{
  245. public:
  246.     Bytes stack;
  247.     Cach<GpuLoad*> gpu_added_ref;
  248.     Bytes inst_buff;
  249.     size_t count_to_render, was_count_to_render;
  250.     DrawStackArr();
  251.     void *AddSize(size_t size);
  252.     void RealeseAllGpuAddRef();
  253. };
  254. class NU_API DrawStack{
  255.     RenderEngineType render_engine_type;
  256. public:
  257.     Scene *scene;
  258.     DrawStackArr stack_arrays[2];
  259.     DrawStackArr *cur_arr;
  260. private:
  261.     MatRender *cur_mat_render;
  262.     SubMesh *inst_mesh;
  263.     Lines *lines;
  264.     Lines2D *lines2D;
  265.     size_t stride_size;
  266.     bool color_mask[4];
  267.     FromStruct last_draw_from;
  268.     size_t inst_offset_buffer;
  269.     size_t inst_count;
  270.     int cur_sector_index;
  271.     void InstDo();
  272.     void RealDrawMesh( SubMesh *sm );
  273.     void AddInstParam( void *src_data, size_t src_size );
  274.     void AddGpuRef( GpuLoad *gpu_load );
  275. public:
  276.     DrawStack(Scene *scene);
  277.     virtual ~DrawStack();
  278.     DrawStackArr *UseNextArrReturnLast();
  279.     void SetCanSkipEnable( bool is_enable );
  280.     void StartSector( const char *sector_name );
  281.     void SetCam( Camera *cam );
  282.     void Stencil( StencilState state,
  283.                  CompFunct test = C_ALWAYS,
  284.                  int ref = -1,
  285.                  StencilOp stencil_fail = OP_KEEP,
  286.                  StencilOp depth_fail= OP_KEEP,
  287.                  StencilOp both_fail= OP_KEEP );
  288.     void Depth(bool depth_test, bool depth_write, CompFunct depth_funct = C_LEQUAL );
  289.     void SetAlphaBlend( bool enable, bool write_a_chanel = true );
  290.     void SetDepthLess( bool is_depth_less );
  291.     bool SetShaderVsPs( Shader *vs, Shader *ps );
  292.     void SetTextureByID( ShaderID id, Texture *tex, const Sampler &sampler );
  293.     void SetTextureByIndex( const size_t &index, Texture *tex, const Sampler &sampler );
  294.     void SetTextureByIndex( const size_t &index, Vec<Texture*> &textures );
  295.     void SetShaderData( size_t index, void *ptr, size_t size );
  296.     void SetProgramGLValueByID(ShaderID id, void *val_src, size_t val_size , size_t val_count = 1);
  297.     void SetRenderPass( const RenderPassDesc &desc );
  298.     void ReadTextureData( Texture *tex );
  299.     void SetRenderToBackBuffer( FromStruct from );
  300.     void SetScissorEnable( bool is_enable );
  301.     void SetScissorViewport( int x, int y, int w, int h );
  302.     void SetRastirizerType( RastirizerType set_type );
  303.     void SetViewport( int x, int y, int w, int h );
  304.     void SetBackFaceCullEnable( bool is_enable );
  305.     void SetColorMask( bool r, bool g, bool b, bool a );
  306.     void DrawLines2D( Lines2D *lines ){ UNUSED(lines); }
  307.     void SetGlobalRenderTexture( Texture *txt, GlobalRenderTexture grt_type, const Sampler &sampler );
  308.     void Draw( SubMesh *mesh, void *params, size_t size_params, FromStruct from );
  309.     void Draw( SubMesh *mesh, ForwardParam *params, FromStruct from );
  310.     void Draw( Lines *lines, void *params, size_t size_params, FromStruct from );
  311.     void Draw( Lines2D *lines, void *params, size_t size_params, FromStruct from );
  312.     void Reset();
  313.     void SetBreakPoint( FromStruct from );
  314.     void SetMat( Mat *mat, MatRenderType type );
  315.     void DrawFullScrean( FromStruct from, bool flip_y = false );
  316.     void ChangeState();
  317.     void DrawSkyBox(MatRenderType render_type, Camera *cam, FromStruct from);
  318. private:
  319.     void DrawInstancing();
  320. private:
  321.     void CreateFullScreanPlane();
  322.     bool is_alpha_blend_enable, is_alpha_blend_write_a;
  323.     bool is_depth_less;
  324.     bool is_depth_test, is_depth_write;
  325.     CompFunct depth_funct;
  326.     RastirizerType rasterizer_type;
  327.     Geom viewport;
  328.     bool is_scissor_enable;
  329.     bool is_back_face_cull;
  330.     SubMesh *full_screan, *full_screan_flip_y;
  331. };
  332.  
  333.  
  334. NAMESPACE_NULIGINE_END;