dark-hammer / src / engine / d3d / gfx-cmdqueue-d3d.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/

#if defined(_D3D_)

#include <dxgi.h>
#include <d3d11.h>

#include "core/core.h"

#include "gfx-cmdqueue.h"
#include "mem-ids.h"
#include "gfx-device.h"
#include "gfx.h"
#include "gfx-shader.h"
#include "engine.h"
#include "app.h"

#if !defined(RELEASE)
#define RELEASE(x)  if ((x) != NULL)  {   (x)->Release();    (x) = NULL;   }
#endif

/* structures */
struct gfx_cmdqueue_s
{
    ID3D11DeviceContext* context;
    struct gfx_framestats stats;
    bool_t shaders_set[GFX_PROGRAM_MAX_SHADERS];
    gfx_depthstencilstate default_depthstencil;
    gfx_rasterstate default_raster;
    gfx_blendstate default_blend;
    int32 rtv_width;
    int32 rtv_height;
    gfx_rendertarget cur_rt;
    uint32 input_bindings[GFX_MAX_VBUFFERS];
    uint32 input_binding_cnt;
    
    uint32 blit_shaderid;   /* blit shader is used for d3d10.0 spec */
    gfx_depthstencilstate blit_ds; 
};

/* fwd */
/* implemented in app-d3d.cpp */
ID3D11Texture2D* app_d3d_getbackbuff();
ID3D11Texture2D* app_d3d_getdepthbuff();

/* inlines/callbacks */
void _setshader_vs(ID3D11DeviceContext* context, ID3D11DeviceChild* shader)
{
    context->VSSetShader((ID3D11VertexShader*)shader, NULL, 0);
}
void _setshader_ps(ID3D11DeviceContext* context, ID3D11DeviceChild* shader)
{
    context->PSSetShader((ID3D11PixelShader*)shader, NULL, 0);
}
void _setshader_gs(ID3D11DeviceContext* context, ID3D11DeviceChild* shader)
{
    context->GSSetShader((ID3D11GeometryShader*)shader, NULL, 0);
}

void _resetshader_vs(ID3D11DeviceContext* context)
{
    context->VSSetShader(NULL, NULL, 0);
}
void _resetshader_ps(ID3D11DeviceContext* context)
{
    context->PSSetShader(NULL, NULL, 0);
}
void _resetshader_gs(ID3D11DeviceContext* context)
{
    context->GSSetShader(NULL, NULL, 0);
}

void _vs_set_cbuffers(ID3D11DeviceContext* context, uint32 slot_idx, uint32 buffer_cnt, 
                      ID3D11Buffer* const* cbuffers)
{
    context->VSSetConstantBuffers(slot_idx, buffer_cnt, cbuffers);
}
void _ps_set_cbuffers(ID3D11DeviceContext* context, uint32 slot_idx, uint32 buffer_cnt, 
                      ID3D11Buffer* const* cbuffers)
{
    context->PSSetConstantBuffers(slot_idx, buffer_cnt, cbuffers);
}
void _gs_set_cbuffers(ID3D11DeviceContext* context, uint32 slot_idx, uint32 buffer_cnt, 
                      ID3D11Buffer* const* cbuffers)
{
    context->GSSetConstantBuffers(slot_idx, buffer_cnt, cbuffers);
}

void _vs_set_samplers(ID3D11DeviceContext* context, uint32 slot_idx, uint32 sampler_cnt, 
                      ID3D11SamplerState* const* samplers)
{
    context->VSSetSamplers(slot_idx, sampler_cnt, samplers);
}
void _ps_set_samplers(ID3D11DeviceContext* context, uint32 slot_idx, uint32 sampler_cnt, 
                      ID3D11SamplerState* const* samplers)
{
    context->PSSetSamplers(slot_idx, sampler_cnt, samplers);
}
void _gs_set_samplers(ID3D11DeviceContext* context, uint32 slot_idx, uint32 sampler_cnt, 
                      ID3D11SamplerState* const* samplers)
{
    context->GSSetSamplers(slot_idx, sampler_cnt, samplers);
}

void _vs_set_resources(ID3D11DeviceContext* context, uint32 slot_idx, uint32 rsv_cnt, 
                       ID3D11ShaderResourceView* const* srvs)
{
    context->VSSetShaderResources(slot_idx, rsv_cnt, srvs);
}
void _ps_set_resources(ID3D11DeviceContext* context, uint32 slot_idx, uint32 rsv_cnt, 
                       ID3D11ShaderResourceView* const* srvs)
{
    context->PSSetShaderResources(slot_idx, rsv_cnt, srvs);
}
void _gs_set_resources(ID3D11DeviceContext* context, uint32 slot_idx, uint32 rsv_cnt, 
                       ID3D11ShaderResourceView* const* srvs)
{
    context->GSSetShaderResources(slot_idx, rsv_cnt, srvs);
}

/* globals/callbacks */
typedef void (*pfn_setshader)(ID3D11DeviceContext*, ID3D11DeviceChild*);
typedef void (*pfn_resetshader)(ID3D11DeviceContext*);
typedef void (*pfn_set_cbuffers)(ID3D11DeviceContext*, uint32, uint32, ID3D11Buffer* const*);
typedef void (*pfn_set_samplers)(ID3D11DeviceContext*, uint32, uint32, ID3D11SamplerState* const*);
typedef void (*pfn_set_resources)(ID3D11DeviceContext*, uint32, uint32, ID3D11ShaderResourceView* const*);

pfn_setshader g_fn_setshader[] = {
    _setshader_vs, _setshader_ps, _setshader_gs
};
pfn_resetshader g_fn_resetshader[] = {
    _resetshader_vs, _resetshader_ps, _resetshader_gs
};
pfn_set_cbuffers g_fn_setcbuffers[] = {
    _vs_set_cbuffers, _ps_set_cbuffers, _gs_set_cbuffers
};
pfn_set_samplers g_fn_setsamplers[] = {
    _vs_set_samplers, _ps_set_samplers, _gs_set_samplers
};
pfn_set_resources g_fn_setresources[] = {
    _vs_set_resources, _ps_set_resources, _gs_set_resources
};

/*  */
gfx_cmdqueue gfx_create_cmdqueue()
{
    gfx_cmdqueue cmdqueue =  (gfx_cmdqueue)ALLOC(sizeof(struct gfx_cmdqueue_s), MID_GFX);
    if (cmdqueue == NULL)
        return NULL;
    memset(cmdqueue, 0x00, sizeof(struct gfx_cmdqueue_s));
    return cmdqueue;
}

void gfx_destroy_cmdqueue(gfx_cmdqueue cmdqueue)
{
    FREE(cmdqueue);
}

result_t gfx_initcmdqueue(gfx_cmdqueue cmdqueue, void* param)
{
    /* param is d3d main device context, if =NULL we should create a new one */
    if (param != NULL)
        cmdqueue->context = (ID3D11DeviceContext*)param;
    else
        ASSERT(0);

    /* create default states */
    cmdqueue->default_raster = gfx_create_rasterstate(gfx_get_defaultraster());
    cmdqueue->default_depthstencil = gfx_create_depthstencilstate(gfx_get_defaultdepthstencil());
    cmdqueue->default_blend = gfx_create_blendstate(gfx_get_defaultblend());
    if (cmdqueue->default_raster == NULL || 
        cmdqueue->default_depthstencil == NULL ||
        cmdqueue->default_blend == NULL)
    {
        err_print(__FILE__, __LINE__, "gfx-cmdqueue init failed: could not create default states");
        return RET_FAIL;
    }

    /* blitting stuff (only for d3d10.0) */
    if (app_get_gfxver() == GFX_HWVER_D3D10_0)   {
        struct gfx_depthstencil_desc dsdesc;
        memcpy(&dsdesc, gfx_get_defaultdepthstencil(), sizeof(dsdesc));
        dsdesc.depth_enable = TRUE;
        dsdesc.depth_write = TRUE;
        dsdesc.depth_func = GFX_CMP_ALWAYS;
        cmdqueue->blit_ds = gfx_create_depthstencilstate(&dsdesc);
        if (cmdqueue->blit_ds == NULL)  {
            err_print(__FILE__, __LINE__, "gfx-cmdqueue init failed: could not create blit ds state");
            return RET_FAIL;
        }

        const struct gfx_input_element_binding bindings[] = {
            {GFX_INPUTELEMENT_ID_POSITION, "vsi_pos", -1},
            {GFX_INPUTELEMENT_ID_TEXCOORD0, "vsi_coord", -1}
        };
        cmdqueue->blit_shaderid = gfx_shader_load("blit-raw", eng_get_lsralloc(), 
            "shaders/fsq.vs", "shaders/blit-raw.ps", NULL,
            bindings, 2, NULL, 0, NULL);
        if (cmdqueue->blit_shaderid == 0)   {
            err_print(__FILE__, __LINE__, "gfx-cmdqueue init failed: could not create blit shader");
            return RET_FAIL;
        }
    }

    gfx_output_setrasterstate(cmdqueue, cmdqueue->default_raster);
    gfx_output_setdepthstencilstate(cmdqueue, cmdqueue->default_depthstencil, 0);
    gfx_output_setblendstate(cmdqueue, cmdqueue->default_blend, NULL);

    return RET_OK;
}

void gfx_releasecmdqueue(gfx_cmdqueue cmdqueue)
{
    /* destroy default states */
    GFX_DESTROY_DEVOBJ(gfx_destroy_rasterstate, cmdqueue->default_raster);
    GFX_DESTROY_DEVOBJ(gfx_destroy_blendstate, cmdqueue->default_blend);
    GFX_DESTROY_DEVOBJ(gfx_destroy_depthstencilstate, cmdqueue->default_depthstencil);
    GFX_DESTROY_DEVOBJ(gfx_destroy_depthstencilstate, cmdqueue->blit_ds);

    if (cmdqueue->blit_shaderid != 0)  
        gfx_shader_unload(cmdqueue->blit_shaderid);

    /* if it's not a main-context we can release it */
    if (cmdqueue->context != app_get_mainctx())
        RELEASE(cmdqueue->context);

    memset(cmdqueue, 0x00, sizeof(struct gfx_cmdqueue_s));
}

void gfx_input_setlayout(gfx_cmdqueue cmdqueue, gfx_inputlayout inputlayout)
{
    ASSERT(inputlayout->type == GFX_OBJ_INPUTLAYOUT);
    
    /* vertex buffers */
    ID3D11Buffer* vbs[GFX_MAX_VBUFFERS];
    uint32 offsets[GFX_MAX_VBUFFERS];
    uint32 strides[GFX_MAX_VBUFFERS];
    struct gfx_obj_desc* desc = &inputlayout->desc;
    uint32 vb_cnt = cmdqueue->input_binding_cnt;
    
    for (uint32 i = 0; i < vb_cnt; i++) {
        uint32 vid = cmdqueue->input_bindings[i];
        vbs[i] = (ID3D11Buffer*)((gfx_inputlayout)desc->il.vbuffs[vid])->api_obj;
        strides[i] = desc->il.strides[vid];
        offsets[i] = 0;
        ASSERT(vbs[i] != NULL);
    }
    
    cmdqueue->context->IASetVertexBuffers(0, vb_cnt, vbs, strides, offsets);

    /* index buffer */
    if (desc->il.ibuff != NULL)
        cmdqueue->context->IASetIndexBuffer((ID3D11Buffer*)((gfx_inputlayout)desc->il.ibuff)->api_obj,
            (DXGI_FORMAT)desc->il.idxfmt, 0);
}

void gfx_program_set(gfx_cmdqueue cmdqueue, gfx_program prog)
{
    bool_t shaders_set[GFX_PROGRAM_MAX_SHADERS];
    memset(shaders_set, 0x00, sizeof(shaders_set));

    for (uint32 i = 0; i < prog->desc.prog.shader_cnt; i++) {
        uint32 idx = (uint32)prog->desc.prog.shader_types[i] - 1;
        g_fn_setshader[idx](cmdqueue->context, (ID3D11DeviceChild*)prog->desc.prog.shaders[i]);
        shaders_set[idx] = TRUE;
    }

    /* reset previously set shaders */
    for (uint32 i = 0; i < GFX_PROGRAM_MAX_SHADERS; i++)    {
        if (!shaders_set[i] && cmdqueue->shaders_set[i])
            g_fn_resetshader[i](cmdqueue->context);
        cmdqueue->shaders_set[i] = shaders_set[i];
    }

    /* set inputlayout */
    cmdqueue->context->IASetInputLayout((ID3D11InputLayout*)prog->desc.prog.d3d_il);
    cmdqueue->stats.shaderchange_cnt ++;
    cmdqueue->stats.input_cnt ++;
}

void gfx_buffer_update(gfx_cmdqueue cmdqueue, gfx_buffer buffer, const void* data, uint32 size)
{
    ID3D11DeviceContext* context = cmdqueue->context;
    D3D11_MAPPED_SUBRESOURCE mapped;
    HRESULT hr;
    hr = context->Map((ID3D11Resource*)buffer->api_obj, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped); 
    if (SUCCEEDED(hr))  {
        memcpy(mapped.pData, data, size);
        context->Unmap((ID3D11Resource*)buffer->api_obj, 0);
        cmdqueue->stats.map_cnt ++;
    }
}

void gfx_draw(gfx_cmdqueue cmdqueue, enum gfx_primitive_type type, uint32 vert_idx,
    uint32 vert_cnt, uint32 draw_id)
{
    cmdqueue->context->IASetPrimitiveTopology((D3D11_PRIMITIVE_TOPOLOGY)type);
    cmdqueue->context->Draw(vert_cnt, vert_idx);

    cmdqueue->stats.draw_cnt ++;
    cmdqueue->stats.prims_cnt += vert_cnt;
    cmdqueue->stats.draw_group_cnt[draw_id] ++;
    cmdqueue->stats.draw_prim_cnt[draw_id] += vert_cnt;
}

void gfx_draw_indexed(gfx_cmdqueue cmdqueue, enum gfx_primitive_type type,
    uint32 ib_idx, uint32 idx_cnt, enum gfx_index_type ib_type, uint32 draw_id)
{
    cmdqueue->context->IASetPrimitiveTopology((D3D11_PRIMITIVE_TOPOLOGY)type);
    cmdqueue->context->DrawIndexed(idx_cnt, ib_idx, 0);

    cmdqueue->stats.draw_cnt ++;
    cmdqueue->stats.prims_cnt += idx_cnt;
    cmdqueue->stats.draw_group_cnt[draw_id] ++;
    cmdqueue->stats.draw_prim_cnt[draw_id] += idx_cnt;
}

void gfx_draw_instance(gfx_cmdqueue cmdqueue, enum gfx_primitive_type type,
    uint32 vert_idx, uint32 vert_cnt, uint32 instance_cnt, uint32 draw_id)
{
    cmdqueue->context->IASetPrimitiveTopology((D3D11_PRIMITIVE_TOPOLOGY)type);
    cmdqueue->context->DrawInstanced(vert_cnt, instance_cnt, vert_idx, 0);

    uint32 prim_cnt = instance_cnt*vert_cnt;
    cmdqueue->stats.draw_cnt ++;
    cmdqueue->stats.prims_cnt += prim_cnt;
    cmdqueue->stats.draw_group_cnt[draw_id] ++;
    cmdqueue->stats.draw_prim_cnt[draw_id] += prim_cnt;
}

void gfx_draw_indexedinstance(gfx_cmdqueue cmdqueue, enum gfx_primitive_type type,
    uint32 ib_idx, uint32 idx_cnt, enum gfx_index_type ib_type, uint32 instance_cnt,
    uint32 draw_id)
{
    cmdqueue->context->IASetPrimitiveTopology((D3D11_PRIMITIVE_TOPOLOGY)type);
    cmdqueue->context->DrawIndexedInstanced(idx_cnt, instance_cnt, ib_idx, 0, 0);

    uint32 prim_cnt = instance_cnt*idx_cnt;
    cmdqueue->stats.draw_cnt ++;
    cmdqueue->stats.prims_cnt += prim_cnt;
    cmdqueue->stats.draw_group_cnt[draw_id] ++;
    cmdqueue->stats.draw_prim_cnt[draw_id] += prim_cnt;
}

void gfx_reset_framestats(gfx_cmdqueue cmdqueue)
{
    memset(&cmdqueue->stats, 0x00, sizeof(struct gfx_framestats));
}

const struct gfx_framestats* gfx_get_framestats(gfx_cmdqueue cmdqueue)
{
    return &cmdqueue->stats;
}

void gfx_program_setcblock(gfx_cmdqueue cmdqueue, gfx_program prog, enum gfx_shader_type shader,
    gfx_buffer buffer, uint32 shaderbind_id, uint32 bind_idx)
{
    ASSERT(shader != GFX_SHADER_NONE);

    uint32 shader_idx = (uint32)shader - 1;
    ID3D11Buffer* vb = (ID3D11Buffer*)buffer->api_obj;
    g_fn_setcbuffers[shader_idx](cmdqueue->context, shaderbind_id, 1, &vb);
}

void gfx_program_setsampler(gfx_cmdqueue cmdqueue, gfx_program prog, enum gfx_shader_type shader,
    gfx_sampler sampler, uint32 shaderbind_id, uint32 texture_unit)
{
    ASSERT(shader != GFX_SHADER_NONE);

    uint32 shader_idx = (uint32)shader - 1;
    ID3D11SamplerState* s = (ID3D11SamplerState*)sampler->api_obj;
    g_fn_setsamplers[shader_idx](cmdqueue->context, shaderbind_id, 1, &s);
}

void gfx_program_settexture(gfx_cmdqueue cmdqueue, gfx_program prog, enum gfx_shader_type shader,
    gfx_texture tex, uint32 texture_unit)
{
    ASSERT(shader != GFX_SHADER_NONE);

    uint32 shader_idx = (uint32)shader - 1;
    ID3D11ShaderResourceView* srv = (ID3D11ShaderResourceView*)tex->desc.tex.d3d_srv;
    g_fn_setresources[shader_idx](cmdqueue->context, texture_unit, 1, &srv);
}

void gfx_program_setcblock_tbuffer(gfx_cmdqueue cmdqueue, gfx_program prog, 
    enum gfx_shader_type shader, gfx_buffer buffer, uint32 shaderbind_id, uint32 texture_unit)
{
    ASSERT(shader != GFX_SHADER_NONE);

    uint32 shader_idx = (uint32)shader - 1;
    ID3D11ShaderResourceView* srv = (ID3D11ShaderResourceView*)buffer->desc.buff.d3d_srv;
    g_fn_setresources[shader_idx](cmdqueue->context, shaderbind_id, 1, &srv);
}


void gfx_output_setblendstate(gfx_cmdqueue cmdqueue, gfx_blendstate blend,
    OPTIONAL const fl32* blend_color)
{
    if (blend == NULL)
        blend = cmdqueue->default_blend;

    const static fl32 default_clr[] = {0.0f, 0.0f, 0.0f, 0.0f};
    const fl32* clr = (blend_color != NULL) ? blend_color : default_clr;
    ID3D11BlendState* b = (blend != NULL) ? (ID3D11BlendState*)blend->api_obj : 
        (ID3D11BlendState*)cmdqueue->default_blend->api_obj;
    cmdqueue->context->OMSetBlendState(b, clr, 0xffffffff);
    cmdqueue->stats.blendstatechange_cnt ++;
}

void gfx_output_setdepthstencilstate(gfx_cmdqueue cmdqueue, gfx_depthstencilstate ds,
		int32 stencil_ref)
{
    if (ds == NULL)
        ds = cmdqueue->default_depthstencil;

    ID3D11DepthStencilState* d = (ds != NULL) ? (ID3D11DepthStencilState*)ds->api_obj :
        (ID3D11DepthStencilState*)cmdqueue->default_depthstencil->api_obj;
    cmdqueue->context->OMSetDepthStencilState(d, (uint32)stencil_ref);
    cmdqueue->stats.dsstatechange_cnt ++;
}

void gfx_output_setrasterstate(gfx_cmdqueue cmdqueue, gfx_rasterstate raster)
{
    if (raster == NULL)
        raster = cmdqueue->default_raster;

    ID3D11RasterizerState* r = (raster != NULL) ? (ID3D11RasterizerState*)raster->api_obj :
        (ID3D11RasterizerState*)cmdqueue->default_raster->api_obj;
    cmdqueue->context->RSSetState(r);
    cmdqueue->stats.rsstatechange_cnt ++;
}

void gfx_output_setscissor(gfx_cmdqueue cmdqueue, int32 x, int32 y, int32 width, int32 height)
{
    D3D11_RECT d3d_rect = {x, y, x + width, y + height};
    cmdqueue->context->RSSetScissorRects(1, &d3d_rect);
}

void gfx_output_setviewport(gfx_cmdqueue cmdqueue, int32 x, int32 y, int32 width, int32 height)
{
    D3D11_VIEWPORT vp = {(FLOAT)x, (FLOAT)y, (FLOAT)width, (FLOAT)height, 0.0f, 1.0f};
    cmdqueue->context->RSSetViewports(1, &vp);
}

void gfx_output_setviewportbias(gfx_cmdqueue cmdqueue, int32 x, int32 y, int32 width, int32 height)
{
    const fl32 bias = 0.0000152588f;
    D3D11_VIEWPORT vp = {(FLOAT)x, (FLOAT)y, (FLOAT)width, (FLOAT)height, 0.0f, 1.0f};
    vp.MinDepth += bias*2.0f;
    vp.MaxDepth -= bias*2.0f;
    cmdqueue->context->RSSetViewports(1, &vp);
}


void* gfx_buffer_map(gfx_cmdqueue cmdqueue, gfx_buffer buffer, uint32 offset, uint32 size,
    uint32 mode /* enum gfx_map_mode */, bool_t sync_cpu)
{
    ASSERT(buffer->type == GFX_OBJ_BUFFER);

    D3D11_MAPPED_SUBRESOURCE mapped;

    UINT flags = sync_cpu ? 0 : D3D11_MAP_FLAG_DO_NOT_WAIT;

    /* for now we always use synced cpu in D3D, because of differences in opengl/d3d drivers 
     * TODO: I haven't figured out how to properly optimize opengl mapping */
    HRESULT dxhr = cmdqueue->context->Map((ID3D11Resource*)buffer->api_obj,
        0, (D3D11_MAP)mode, flags, &mapped);

    if (SUCCEEDED(dxhr))   {
        cmdqueue->stats.map_cnt ++;
        return ((uint8*)mapped.pData + offset);
    }   else    {
        return NULL;
    }
}

void gfx_buffer_unmap(gfx_cmdqueue cmdqueue, gfx_buffer buffer)
{
    ASSERT(buffer->type == GFX_OBJ_BUFFER);
    cmdqueue->context->Unmap((ID3D11Resource*)buffer->api_obj, 0);
}

void gfx_cmdqueue_setrtvsize(gfx_cmdqueue cmdqueue, uint32 width, uint32 height)
{
	cmdqueue->rtv_width = (int32)width;
    cmdqueue->rtv_height = (int32)height;
}

void gfx_cmdqueue_getrtvsize(gfx_cmdqueue cmdqueue, OUT uint32* width, OUT uint32* height)
{
	*width = (uint32)cmdqueue->rtv_width;
	*height = (uint32)cmdqueue->rtv_height;
}

void gfx_reset_devstates(gfx_cmdqueue cmdqueue)
{
    gfx_output_setblendstate(cmdqueue, NULL, NULL);
    gfx_output_setrasterstate(cmdqueue, NULL);
    gfx_output_setdepthstencilstate(cmdqueue, NULL, 0);
}

void gfx_output_setrendertarget(gfx_cmdqueue cmdqueue, OPTIONAL gfx_rendertarget rt)
{
    if (rt != NULL) {
        gfx_set_rtvsize(rt->desc.rt.width, rt->desc.rt.height);

        ID3D11RenderTargetView* rtvs[GFX_RT_MAX_TEXTURES];
        uint32 rt_cnt = rt->desc.rt.rt_cnt;
        for (uint32 i = 0; i < rt_cnt; i++) {
            rtvs[i] = (ID3D11RenderTargetView*)
            ((gfx_texture)rt->desc.rt.rt_textures[i])->desc.tex.d3d_rtv;
        }

        ID3D11DepthStencilView* dsv = NULL;
        if (rt->desc.rt.ds_texture != NULL)
            dsv = (ID3D11DepthStencilView*)((gfx_texture)rt->desc.rt.ds_texture)->desc.tex.d3d_rtv;
        cmdqueue->context->OMSetRenderTargets(rt_cnt, rt_cnt > 0 ? rtvs : NULL, dsv);
        cmdqueue->cur_rt = rt;
    }   else    {
        app_set_rendertarget(NULL);
        cmdqueue->cur_rt = NULL;
    }

    cmdqueue->stats.rtchange_cnt ++;
}

void gfx_output_clearrendertarget(gfx_cmdqueue cmdqueue, gfx_rendertarget rt, 
    const fl32 color[4], fl32 depth, uint8 stencil, uint32 flags)
{
    if (rt == NULL) {
        app_clear_rendertarget(color, depth, stencil, flags);
        return;
    }

    if (rt->desc.rt.ds_texture != NULL && 
        BIT_CHECK(flags, GFX_CLEAR_DEPTH) || BIT_CHECK(flags, GFX_CLEAR_STENCIL))   
    {
        ID3D11DepthStencilView* dsv = 
            (ID3D11DepthStencilView*)((gfx_texture)rt->desc.rt.ds_texture)->desc.tex.d3d_rtv;
        cmdqueue->context->ClearDepthStencilView(dsv, flags, depth, stencil);
        cmdqueue->stats.cleards_cnt ++;
    }

    if (rt->desc.rt.rt_cnt > 0 && BIT_CHECK(flags, GFX_CLEAR_COLOR))  {
        ID3D11RenderTargetView* rtvs[GFX_RT_MAX_TEXTURES];
        uint32 rt_cnt = rt->desc.rt.rt_cnt;
        for (uint32 i = 0; i < rt_cnt; i++) {
            ID3D11RenderTargetView* rtv = (ID3D11RenderTargetView*)
                ((gfx_texture)rt->desc.rt.rt_textures[i])->desc.tex.d3d_rtv;
            cmdqueue->context->ClearRenderTargetView(rtv, color);
        }
        cmdqueue->stats.clearrt_cnt += rt_cnt;
    }
}


void gfx_rendertarget_blit(gfx_cmdqueue cmdqueue,
    int32 dest_x, int32 dest_y, int32 dest_width, int32 dest_height,
    gfx_rendertarget src_rt, int32 src_x, int32 src_y, int32 src_width, int32 src_height)
{
    ID3D11Resource* dest_res = cmdqueue->cur_rt != NULL ? 
        (ID3D11Resource*)((gfx_texture)cmdqueue->cur_rt->desc.rt.rt_textures[0])->api_obj :
        app_d3d_getbackbuff();

    D3D11_BOX src_box = {
        src_x, src_y, 0, 
        src_x + src_width, src_y + src_height, 0
    };
    ID3D11Resource* src_res = 
        (ID3D11Resource*)((gfx_texture)src_rt->desc.rt.rt_textures[0])->api_obj;
    cmdqueue->context->CopySubresourceRegion(dest_res, 0, dest_x, dest_y, 0, 
        src_res, 0, &src_box);
}

void gfx_rendertarget_blitraw(gfx_cmdqueue cmdqueue, gfx_rendertarget src_rt)
{
    if (app_get_gfxver() != GFX_HWVER_D3D10_0 || src_rt->desc.rt.ds_texture == NULL)   {
        /* copy color buffer to current render-target */
        ID3D11Resource* dest_res = cmdqueue->cur_rt != NULL ? 
            (ID3D11Resource*)((gfx_texture)cmdqueue->cur_rt->desc.rt.rt_textures[0])->api_obj :
            app_d3d_getbackbuff();
        ID3D11Resource* src_res = 
            (ID3D11Resource*)((gfx_texture)src_rt->desc.rt.rt_textures[0])->api_obj;
        cmdqueue->context->CopyResource(dest_res, src_res);

        /* do it for depth-buffer too */
        if (src_rt->desc.rt.ds_texture != NULL) {
            ID3D11Resource* src_depth = 
                (ID3D11Resource*)((gfx_texture)src_rt->desc.rt.ds_texture)->api_obj;
            ID3D11Resource* dest_depth = cmdqueue->cur_rt != NULL ? 
                (ID3D11Resource*)((gfx_texture)cmdqueue->cur_rt->desc.rt.ds_texture)->api_obj :
                app_d3d_getdepthbuff();
            cmdqueue->context->CopyResource(dest_depth, src_depth);
        }
    }   else    {
        struct gfx_shader* shader = gfx_shader_get(cmdqueue->blit_shaderid);
        gfx_output_setdepthstencilstate(cmdqueue, cmdqueue->blit_ds, 0);
        gfx_shader_bind(cmdqueue, shader);
        gfx_shader_bindtexture(cmdqueue, shader, SHADER_NAME(s_color), 
            (gfx_texture)src_rt->desc.rt.rt_textures[0]);
        gfx_shader_bindtexture(cmdqueue, shader, SHADER_NAME(s_depth),
            (gfx_texture)src_rt->desc.rt.ds_texture);
        gfx_draw_fullscreenquad();
        gfx_output_setdepthstencilstate(cmdqueue, NULL, 0);
    }
}

void gfx_program_setbindings(gfx_cmdqueue cmdqueue, const uint32* bindings, uint32 binding_cnt)
{
    cmdqueue->input_binding_cnt = binding_cnt;
    memcpy(cmdqueue->input_bindings, bindings, sizeof(uint32)*binding_cnt);
}

void gfx_cmdqueue_resetsrvs(gfx_cmdqueue cmdqueue)
{
    static ID3D11ShaderResourceView* srvs[] = {
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL
    };
    cmdqueue->context->PSSetShaderResources(0, 16, srvs);
}

void gfx_texture_generatemips(gfx_cmdqueue cmdqueue, gfx_texture tex)
{
    cmdqueue->context->GenerateMips((ID3D11ShaderResourceView*)tex->desc.tex.d3d_srv);
}

void gfx_texture_update(gfx_cmdqueue cmdqueue, gfx_texture tex, const void* pixels)
{
    D3D11_MAPPED_SUBRESOURCE mapped;
    cmdqueue->context->Map((ID3D11Resource*)tex->api_obj, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped);
    memcpy(mapped.pData, pixels, tex->desc.tex.size);
    cmdqueue->context->Unmap((ID3D11Resource*)tex->api_obj, 0);
}

#endif  /* _D3D_ */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.