forked from KolibriOS/kolibrios
GL blitter: use per pixel mask texture as alpha channel source
git-svn-id: svn://kolibrios.org@4523 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
738e6b0db0
commit
7b7c0326ac
@ -15,7 +15,7 @@ void render_blit(struct render *render, enum px_buffer buffer)
|
||||
|
||||
float dst_xscale, dst_yscale;
|
||||
float *vertices = render->vertices;
|
||||
float *texcoords = render->texcoords;
|
||||
float *texcoords = render->tc_src;
|
||||
int r, b;
|
||||
|
||||
if(render == NULL)
|
||||
@ -51,7 +51,7 @@ void render_blit(struct render *render, enum px_buffer buffer)
|
||||
GL_TEXTURE_MAG_FILTER,
|
||||
GL_NEAREST);
|
||||
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float),render->texcoords);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float),render->tc_src);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
dst_xscale = 1.0/render->scr_width;
|
||||
|
@ -89,22 +89,27 @@ struct render* create_render(EGLDisplay dpy, EGLSurface surface, int dx, int dy)
|
||||
const char *vs_src =
|
||||
"attribute vec4 v_position;\n"
|
||||
"attribute vec4 v_texcoord0;\n"
|
||||
"attribute vec4 v_texcoord1;\n"
|
||||
"varying vec2 source_texture;\n"
|
||||
"void main()\n"
|
||||
"varying vec2 mask_texture;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = v_position;\n"
|
||||
" source_texture = v_texcoord0.xy;\n"
|
||||
" mask_texture = v_texcoord1.xy;\n"
|
||||
"}\n";
|
||||
|
||||
const char *fs_src =
|
||||
// "precision mediump float;\n"
|
||||
"varying vec2 source_texture;\n"
|
||||
"uniform sampler2D sampler;\n"
|
||||
"varying vec2 mask_texture;\n"
|
||||
"uniform sampler2D sampler_src;\n"
|
||||
"uniform sampler2D sampler_mask;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" vec3 cg = texture2D(sampler, source_texture).rgb;\n"
|
||||
" gl_FragColor = vec4(cg.r,cg.g,cg.b,1.0);\n"
|
||||
" float ca = texture2D(sampler_mask, mask_texture).r;\n"
|
||||
" gl_FragColor = vec4(texture2D(sampler_src, source_texture).rgb, ca);\n"
|
||||
"}\n";
|
||||
|
||||
EGLint config_attribs[14];
|
||||
EGLConfig config;
|
||||
EGLint num_configs;
|
||||
@ -310,6 +315,7 @@ struct render* create_render(EGLDisplay dpy, EGLSurface surface, int dx, int dy)
|
||||
glAttachShader(render->blit_prog, fs_shader);
|
||||
glBindAttribLocation(render->blit_prog, 0, "v_position");
|
||||
glBindAttribLocation(render->blit_prog, 1, "v_texcoord0");
|
||||
glBindAttribLocation(render->blit_prog, 2, "v_texcoord1");
|
||||
|
||||
glLinkProgram(render->blit_prog);
|
||||
glGetProgramiv(render->blit_prog, GL_LINK_STATUS, &ret);
|
||||
@ -327,17 +333,34 @@ struct render* create_render(EGLDisplay dpy, EGLSurface surface, int dx, int dy)
|
||||
free(info);
|
||||
}
|
||||
|
||||
render->sampler = glGetUniformLocation(render->blit_prog,"sampler");
|
||||
render->sampler = glGetUniformLocation(render->blit_prog,"sampler_src");
|
||||
render->sm_mask = glGetUniformLocation(render->blit_prog,"sampler_mask");
|
||||
|
||||
glUseProgram(render->blit_prog);
|
||||
glUniform1i(render->sampler, 0);
|
||||
glUniform1i(render->sm_mask, 1);
|
||||
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT,GL_FALSE, 2 * sizeof(float),render->vertices);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float),render->texcoords);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float),render->tc_src);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float),render->tc_mask);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
glBindTexture(GL_TEXTURE_2D, render->tx_mask);
|
||||
glTexParameteri(GL_TEXTURE_2D,
|
||||
GL_TEXTURE_MIN_FILTER,
|
||||
GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D,
|
||||
GL_TEXTURE_MAG_FILTER,
|
||||
GL_NEAREST);
|
||||
|
||||
eglMakeCurrent(dpy, surface, surface, context);
|
||||
|
||||
return render;
|
||||
@ -378,9 +401,9 @@ void create_mask(struct render *render)
|
||||
EGL_GL_TEXTURE_LEVEL_KHR, 0,
|
||||
EGL_NONE
|
||||
};
|
||||
struct drm_i915_gem_mmap mmap_arg;
|
||||
struct drm_i915_gem_mmap_gtt mmap_arg;
|
||||
EGLint handle, stride;
|
||||
int pitch;
|
||||
int winw, winh, pitch;
|
||||
void *data;
|
||||
|
||||
glGenTextures(1, &render->tx_mask);
|
||||
@ -394,11 +417,16 @@ void create_mask(struct render *render)
|
||||
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
|
||||
|
||||
pitch = (render->width+3) & -4;
|
||||
winw = render->width+10;
|
||||
winh = render->height+20+5;
|
||||
|
||||
data = user_alloc(pitch*render->height);
|
||||
pitch = (winw+3) & -4;
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, render->width, render->height, 0,
|
||||
data = user_alloc(pitch * winh);
|
||||
|
||||
memset(data, 0x20, pitch * winh);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, winw, winh, 0,
|
||||
GL_RED,GL_UNSIGNED_BYTE, data);
|
||||
|
||||
user_free(data);
|
||||
@ -419,18 +447,19 @@ void create_mask(struct render *render)
|
||||
|
||||
mmap_arg.handle = handle;
|
||||
mmap_arg.offset = 0;
|
||||
mmap_arg.size = stride * render->height;
|
||||
if (drm_ioctl(render->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg))
|
||||
// mmap_arg.size = stride * winh;
|
||||
if (drm_ioctl(render->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg))
|
||||
{
|
||||
printf("%s: failed to mmap image %p handle=%d, %d bytes, into CPU domain\n",
|
||||
__FUNCTION__, render->mask, handle, stride*render->height);
|
||||
__FUNCTION__, render->mask, handle, stride*winh);
|
||||
goto err3;
|
||||
}
|
||||
|
||||
render->mask_buffer = (void *)(uintptr_t)mmap_arg.addr_ptr;
|
||||
render->mask_buffer = (void *)(uintptr_t)mmap_arg.offset;
|
||||
render->mask_handle = handle;
|
||||
|
||||
printf("%s: mmap image %p handle=%d, %d bytes to %p\n",
|
||||
__FUNCTION__, render->mask, handle, stride*render->height, render->mask_buffer);
|
||||
printf("%s: mmap image %p handle=%d, stride %d %d bytes to %p\n",
|
||||
__FUNCTION__, render->mask, handle, stride, stride*winh, render->mask_buffer);
|
||||
|
||||
return;
|
||||
|
||||
|
@ -27,11 +27,12 @@ struct render
|
||||
GLuint tx_buffers[2];
|
||||
GLuint tx_screen;
|
||||
GLuint tx_mask;
|
||||
GLuint mask_handle;
|
||||
void *mask_buffer;
|
||||
int back_buffer;
|
||||
GLuint blit_prog;
|
||||
GLint sampler;
|
||||
float vertices[8], texcoords[8];
|
||||
GLint sampler, sm_mask;
|
||||
float vertices[8],tc_src[8],tc_mask[8];
|
||||
};
|
||||
|
||||
struct render* create_render(EGLDisplay dpy, EGLSurface surface, int dx, int dy);
|
||||
|
@ -3,19 +3,35 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "render.h"
|
||||
#include <i915_drm.h>
|
||||
#include <kos32sys.h>
|
||||
|
||||
static int drm_ioctl(int fd, unsigned long request, void *arg)
|
||||
{
|
||||
ioctl_t io;
|
||||
|
||||
io.handle = fd;
|
||||
io.io_code = request;
|
||||
io.input = arg;
|
||||
io.inp_size = 64;
|
||||
io.output = NULL;
|
||||
io.out_size = 0;
|
||||
|
||||
return call_service(&io);
|
||||
}
|
||||
|
||||
void render_swap_and_blit(struct render *render)
|
||||
{
|
||||
char proc_info[1024];
|
||||
struct drm_i915_mask_update update;
|
||||
|
||||
EGLContext context;
|
||||
EGLSurface draw, read;
|
||||
int winx, winy;
|
||||
int winx, winy, winw, winh;
|
||||
|
||||
float dst_xscale, dst_yscale;
|
||||
float xscale, yscale;
|
||||
float *vertices = render->vertices;
|
||||
float *texcoords = render->texcoords;
|
||||
float *texcoords = render->tc_src;
|
||||
int r, b;
|
||||
|
||||
if(render == NULL)
|
||||
@ -25,6 +41,8 @@ void render_swap_and_blit(struct render *render)
|
||||
|
||||
winx = *(uint32_t*)(proc_info+34);
|
||||
winy = *(uint32_t*)(proc_info+38);
|
||||
winw = *(uint32_t*)(proc_info+42)+1;
|
||||
winh = *(uint32_t*)(proc_info+46)+1;
|
||||
|
||||
context = eglGetCurrentContext();
|
||||
draw = eglGetCurrentSurface(EGL_DRAW);
|
||||
@ -50,6 +68,10 @@ void render_swap_and_blit(struct render *render)
|
||||
|
||||
#endif
|
||||
|
||||
update.handle = render->mask_handle;
|
||||
update.bo_map = (int)render->mask_buffer;
|
||||
drm_ioctl(render->fd, SRV_MASK_UPDATE, &update);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, render->tx_buffers[render->back_buffer]);
|
||||
glTexParameteri(GL_TEXTURE_2D,
|
||||
@ -59,22 +81,23 @@ void render_swap_and_blit(struct render *render)
|
||||
GL_TEXTURE_MAG_FILTER,
|
||||
GL_NEAREST);
|
||||
|
||||
dst_xscale = 1.0/render->scr_width;
|
||||
dst_yscale = 1.0/render->scr_height;
|
||||
|
||||
xscale = 1.0/render->scr_width;
|
||||
yscale = 1.0/render->scr_height;
|
||||
|
||||
r = winx + render->dx + render->width;
|
||||
b = winy + render->dy + render->height;
|
||||
|
||||
float t0, t1, t2, t5;
|
||||
|
||||
vertices[0] = t0 = 2*(winx+render->dx)*dst_xscale - 1.0;
|
||||
vertices[1 * 2] = t2 = 2*r*dst_xscale - 1.0;
|
||||
vertices[0] = t0 = 2*(winx+render->dx)*xscale - 1.0;
|
||||
vertices[1 * 2] = t2 = 2*r*xscale - 1.0;
|
||||
|
||||
vertices[2 * 2] = t2;
|
||||
vertices[3 * 2] = t0;
|
||||
|
||||
vertices[1] = t1 = 2*(winy+render->dy)*dst_yscale - 1.0;
|
||||
vertices[2*2+1] = t5 = 2*b*dst_yscale - 1.0;
|
||||
vertices[1] = t1 = 2*(winy+render->dy)*yscale - 1.0;
|
||||
vertices[2*2+1] = t5 = 2*b*yscale - 1.0;
|
||||
vertices[1*2+1] = t1;
|
||||
vertices[3*2+1] = t5;
|
||||
|
||||
@ -87,7 +110,23 @@ void render_swap_and_blit(struct render *render)
|
||||
texcoords[3*2] = 0.0;
|
||||
texcoords[3*2+1]= 1.0;
|
||||
|
||||
texcoords = render->tc_mask;
|
||||
|
||||
xscale = 1.0/winw;
|
||||
yscale = 1.0/winh;
|
||||
|
||||
texcoords[0] = render->dx * xscale;
|
||||
texcoords[1] = render->dy * yscale;
|
||||
texcoords[1*2] = (render->dx+render->width)*xscale;
|
||||
texcoords[1*2+1]= render->dy * yscale;
|
||||
texcoords[2*2] = (render->dx+render->width)*xscale;
|
||||
texcoords[2*2+1]= (render->dy+render->height)*yscale;
|
||||
texcoords[3*2] = render->dx * xscale;
|
||||
texcoords[3*2+1]= (render->dy+render->height)*yscale;
|
||||
|
||||
|
||||
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
||||
glFlush();
|
||||
|
||||
// glDisableVertexAttribArray(0);
|
||||
// glDisableVertexAttribArray(1);
|
||||
|
Loading…
Reference in New Issue
Block a user