1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2011-2017 - Daniel De Matteis
3  *
4  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
5  *  of the GNU General Public License as published by the Free Software Found-
6  *  ation, either version 3 of the License, or (at your option) any later version.
7  *
8  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  *  PURPOSE.  See the GNU General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License along with RetroArch.
13  *  If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #include <retro_miscellaneous.h>
17 
18 #ifdef HAVE_CONFIG_H
19 #include "../../config.h"
20 #endif
21 
22 #include "../gfx_display.h"
23 
24 #include "../../retroarch.h"
25 #include "../font_driver.h"
26 #include "../common/ctr_common.h"
27 #include "../drivers/ctr_gu.h"
28 #include "../../ctr/gpu_old.h"
29 
gfx_display_ctr_draw(gfx_display_ctx_draw_t * draw,void * data,unsigned video_width,unsigned video_height)30 static void gfx_display_ctr_draw(gfx_display_ctx_draw_t *draw,
31       void *data, unsigned video_width, unsigned video_height)
32 {
33    ctr_scale_vector_t scale_vector;
34    int colorR, colorG, colorB, colorA;
35    ctr_vertex_t *v                  = NULL;
36    struct ctr_texture *texture      = NULL;
37    const float *color               = NULL;
38    ctr_video_t             *ctr     = (ctr_video_t*)data;
39 
40    if (!ctr || !draw)
41       return;
42 
43    texture            = (struct ctr_texture*)draw->texture;
44    color              = draw->coords->color;
45 
46    if (!texture)
47       return;
48 
49    ctr_set_scale_vector(&scale_vector,
50          CTR_TOP_FRAMEBUFFER_WIDTH, CTR_TOP_FRAMEBUFFER_HEIGHT,
51          texture->width, texture->height);
52    GPUCMD_AddWrite(GPUREG_GSH_BOOLUNIFORM, 0);
53    ctrGuSetVertexShaderFloatUniform(0, (float*)&scale_vector, 1);
54 
55    if ((ctr->vertex_cache.size - (ctr->vertex_cache.current
56                - ctr->vertex_cache.buffer)) < 1)
57       ctr->vertex_cache.current = ctr->vertex_cache.buffer;
58 
59    v     = ctr->vertex_cache.current++;
60 
61    v->x0 = draw->x;
62    v->y0 = 240 - draw->height - draw->y;
63    v->x1 = v->x0 + draw->width;
64    v->y1 = v->y0 + draw->height;
65    v->u0 = 0;
66    v->v0 = 0;
67    v->u1 = texture->active_width;
68    v->v1 = texture->active_height;
69 
70    ctrGuSetAttributeBuffers(2,
71          VIRT_TO_PHYS(v),
72          CTRGU_ATTRIBFMT(GPU_SHORT, 4) << 0 |
73          CTRGU_ATTRIBFMT(GPU_SHORT, 4) << 4,
74          sizeof(ctr_vertex_t));
75 
76    color  = draw->coords->color;
77    colorR = (int)((*color++)*255.f);
78    colorG = (int)((*color++)*255.f);
79    colorB = (int)((*color++)*255.f);
80    colorA = (int)((*color++)*255.f);
81 
82    GPU_SetTexEnv(0,
83          GPU_TEVSOURCES(GPU_TEXTURE0, GPU_CONSTANT, 0),
84          GPU_TEVSOURCES(GPU_TEXTURE0, GPU_CONSTANT, 0),
85          0,
86          0,
87          GPU_MODULATE, GPU_MODULATE,
88          COLOR_ABGR(colorR,colorG,colorB,colorA)
89          );
90 
91 #if 0
92    GPU_SetTexEnv(0,
93          GPU_TEVSOURCES(GPU_CONSTANT, GPU_CONSTANT, 0),
94          GPU_TEVSOURCES(GPU_CONSTANT, GPU_CONSTANT, 0),
95          0,
96          GPU_TEVOPERANDS(GPU_TEVOP_RGB_SRC_COLOR, GPU_TEVOP_RGB_SRC_COLOR, 0),
97          GPU_REPLACE, GPU_REPLACE,
98          0x3FFFFFFF);
99 #endif
100 
101    ctrGuSetTexture(GPU_TEXUNIT0,
102          VIRT_TO_PHYS(texture->data),
103          texture->width,
104          texture->height,
105            GPU_TEXTURE_MAG_FILTER(GPU_LINEAR)
106          | GPU_TEXTURE_MIN_FILTER(GPU_LINEAR)
107          | GPU_TEXTURE_WRAP_S(GPU_CLAMP_TO_EDGE)
108          | GPU_TEXTURE_WRAP_T(GPU_CLAMP_TO_EDGE),
109          GPU_RGBA8);
110 
111    GPU_SetViewport(NULL,
112          VIRT_TO_PHYS(ctr->drawbuffers.top.left),
113          0, 0, CTR_TOP_FRAMEBUFFER_HEIGHT,
114          ctr->video_mode == CTR_VIDEO_MODE_2D_800X240
115          ? CTR_TOP_FRAMEBUFFER_WIDTH * 2
116          : CTR_TOP_FRAMEBUFFER_WIDTH);
117 
118    GPU_DrawArray(GPU_GEOMETRY_PRIM, 0, 1);
119 
120    if (ctr->video_mode == CTR_VIDEO_MODE_3D)
121    {
122       GPU_SetViewport(NULL,
123             VIRT_TO_PHYS(ctr->drawbuffers.top.right),
124             0, 0, CTR_TOP_FRAMEBUFFER_HEIGHT,
125             CTR_TOP_FRAMEBUFFER_WIDTH);
126       GPU_DrawArray(GPU_GEOMETRY_PRIM, 0, 1);
127    }
128 
129    GPU_SetTexEnv(0, GPU_TEXTURE0, GPU_TEXTURE0, 0, 0, GPU_REPLACE, GPU_REPLACE, 0);
130 #if 0
131    printf("(%i,%i,%i,%i) , (%i,%i)\n", (int)draw->x,
132          (int)draw->y, (int)draw->width, (int)draw->height,
133          texture->width, texture->height);
134 #endif
135 }
136 
gfx_display_ctr_font_init_first(void ** font_handle,void * video_data,const char * font_path,float font_size,bool is_threaded)137 static bool gfx_display_ctr_font_init_first(
138       void **font_handle, void *video_data,
139       const char *font_path, float font_size,
140       bool is_threaded)
141 {
142    font_data_t **handle = (font_data_t**)font_handle;
143    *handle = font_driver_init_first(video_data,
144          font_path, font_size, true,
145          is_threaded,
146          FONT_DRIVER_RENDER_CTR);
147    return *handle;
148 }
149 
150 gfx_display_ctx_driver_t gfx_display_ctx_ctr = {
151    gfx_display_ctr_draw,
152    NULL,                                     /* draw_pipeline          */
153    NULL,                                     /* blend_begin            */
154    NULL,                                     /* blend_end              */
155    NULL,                                     /* get_default_mvp        */
156    NULL,                                     /* get_default_vertices   */
157    NULL,                                     /* get_default_tex_coords */
158    gfx_display_ctr_font_init_first,
159    GFX_VIDEO_DRIVER_CTR,
160    "ctr",
161    true,
162    NULL,
163    NULL
164 };
165