From b7032a89576ea7f228e5a97f32c3a82c32581ea9 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 13 Feb 2016 14:49:27 -0500 Subject: [PATCH] Rendering is now working --- index.html | 2 +- main.cpp | 4 +- moonlight.hpp | 16 +++--- viddec.cpp | 156 +++++++++++++++++++++++++++----------------------- 4 files changed, 94 insertions(+), 84 deletions(-) diff --git a/index.html b/index.html index 388f357..9b03939 100644 --- a/index.html +++ b/index.html @@ -7,7 +7,7 @@ - +

Moonlight

Status: NO-STATUS

diff --git a/main.cpp b/main.cpp index 63fca50..b1c7554 100644 --- a/main.cpp +++ b/main.cpp @@ -73,8 +73,8 @@ void MoonlightInstance::HandleMessage(const pp::Var& var_message) { LiInitializeStreamConfiguration(&s_StreamConfig); s_StreamConfig.width = 1280; s_StreamConfig.height = 720; - s_StreamConfig.fps = 30; - s_StreamConfig.bitrate = 10; // megabits per second + s_StreamConfig.fps = 60; + s_StreamConfig.bitrate = 15000; // kilobits per second s_StreamConfig.packetSize = 1024; s_StreamConfig.streamingRemotely = 0; s_StreamConfig.audioConfiguration = AUDIO_CONFIGURATION_STEREO; diff --git a/moonlight.hpp b/moonlight.hpp index ea074fe..af69b97 100644 --- a/moonlight.hpp +++ b/moonlight.hpp @@ -12,6 +12,9 @@ #include "ppapi/utility/completion_callback_factory.h" +#include +#include + #include "nacl_io/nacl_io.h" #include @@ -24,19 +27,17 @@ struct Shader { GLint texcoord_scale_location; }; -class MoonlightInstance : public pp::Instance, public pp::MouseLock, public pp::Graphics3DClient { +class MoonlightInstance : public pp::Instance, public pp::MouseLock { public: - MoonlightInstance(PP_Instance instance) : + explicit MoonlightInstance(PP_Instance instance) : pp::Instance(instance), pp::MouseLock(this), - pp::Graphics3DClient(this), m_CallbackFactory(this), m_MouseLocked(false) { // This function MUST be used otherwise sockets don't work (nacl_io_init() doesn't work!) nacl_io_init_ppapi(pp_instance(), pp::Module::Get()->get_browser_interface()); m_GamepadApi = static_cast(pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_INTERFACE)); - m_GlesApi = static_cast(pp::Module::Get()->GetBrowserInterface(PPB_OPENGLES2_INTERFACE)); } virtual ~MoonlightInstance(); @@ -67,12 +68,11 @@ class MoonlightInstance : public pp::Instance, public pp::MouseLock, public pp:: static void ClDisplayMessage(char* message); static void ClDisplayTransientMessage(char* message); - - virtual void Graphics3DContextLost() {} static Shader CreateProgram(const char* vertexShader, const char* fragmentShader); static void CreateShader(GLuint program, GLenum type, const char* source, int size); static void PaintPicture(PP_VideoPicture picture); + void DispatchRendering(int32_t unused); void DispatchGetPicture(uint32_t unused); void PictureReady(int32_t result, PP_VideoPicture picture); @@ -84,13 +84,13 @@ class MoonlightInstance : public pp::Instance, public pp::MouseLock, public pp:: static CONNECTION_LISTENER_CALLBACKS s_ClCallbacks; static DECODER_RENDERER_CALLBACKS s_DrCallbacks; - pp::Graphics3D* m_Graphics3D; + pp::Graphics3D m_Graphics3D; pp::VideoDecoder* m_VideoDecoder; pp::Size m_ViewSize; - const PPB_OpenGLES2* m_GlesApi; Shader m_Texture2DShader; Shader m_RectangleArbShader; Shader m_ExternalOesShader; + PP_VideoPicture m_LastPicture; double m_LastPadTimestamps[4]; const PPB_Gamepad* m_GamepadApi; diff --git a/viddec.cpp b/viddec.cpp index 25d3dc0..24629b9 100644 --- a/viddec.cpp +++ b/viddec.cpp @@ -3,6 +3,8 @@ #include #include +#include "ppapi/lib/gl/gles2/gl2ext_ppapi.h" + #define INITIAL_DECODE_BUFFER_LEN 128 * 1024 static unsigned char* s_DecodeBuffer; @@ -64,28 +66,30 @@ void MoonlightInstance::DidChangeView(const pp::Rect& position, m_ViewSize = position.size(); printf("View size: %dx%d\n", m_ViewSize.width(), m_ViewSize.height()); + if (!glInitializePPAPI(pp::Module::Get()->get_browser_interface())) { + return; + } + int32_t contextAttributes[] = { - PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, - PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, - PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8, - PP_GRAPHICS3DATTRIB_RED_SIZE, 8, - PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0, - PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 0, - PP_GRAPHICS3DATTRIB_SAMPLES, 0, - PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0, - PP_GRAPHICS3DATTRIB_WIDTH, g_Instance->m_ViewSize.width(), - PP_GRAPHICS3DATTRIB_HEIGHT, g_Instance->m_ViewSize.height(), - PP_GRAPHICS3DATTRIB_NONE, + PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, + PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24, + PP_GRAPHICS3DATTRIB_WIDTH, position.size().width(), + PP_GRAPHICS3DATTRIB_HEIGHT, position.size().height(), + PP_GRAPHICS3DATTRIB_NONE }; - g_Instance->m_Graphics3D = new pp::Graphics3D(g_Instance, contextAttributes); - assert(!g_Instance->m_Graphics3D->is_null()); + g_Instance->m_Graphics3D = pp::Graphics3D(this, contextAttributes); - assert(BindGraphics(*m_Graphics3D)); + if (!BindGraphics(m_Graphics3D)) { + fprintf(stderr, "Unable to bind 3d context!\n"); + m_Graphics3D = pp::Graphics3D(); + glSetCurrentContextPPAPI(0); + return; + } - PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource(); + glSetCurrentContextPPAPI(m_Graphics3D.pp_resource()); - g_Instance->m_GlesApi->ClearColor(graphics3D, 1, 0, 0, 1); - g_Instance->m_GlesApi->Clear(graphics3D, GL_COLOR_BUFFER_BIT); + glClearColor(1.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); assertNoGLError(); @@ -95,15 +99,16 @@ void MoonlightInstance::DidChangeView(const pp::Rect& position, }; GLuint buffer; - g_Instance->m_GlesApi->GenBuffers(graphics3D, 1, &buffer); - g_Instance->m_GlesApi->BindBuffer(graphics3D, GL_ARRAY_BUFFER, buffer); + glGenBuffers(1, &buffer); + glBindBuffer(GL_ARRAY_BUFFER, buffer); - g_Instance->m_GlesApi->BufferData(graphics3D, - GL_ARRAY_BUFFER, - sizeof(k_Vertices), - k_Vertices, - GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, + sizeof(k_Vertices), + k_Vertices, + GL_STATIC_DRAW); assertNoGLError(); + + g_Instance->m_Graphics3D.SwapBuffers(g_Instance->m_CallbackFactory.NewCallback(&MoonlightInstance::DispatchRendering)); } void MoonlightInstance::VidDecSetup(int width, int height, int redrawRate, void* context, int drFlags) { @@ -112,7 +117,7 @@ void MoonlightInstance::VidDecSetup(int width, int height, int redrawRate, void* s_DecodeBufferLength = INITIAL_DECODE_BUFFER_LEN; s_DecodeBuffer = (unsigned char *)malloc(s_DecodeBufferLength); - g_Instance->m_VideoDecoder->Initialize(*g_Instance->m_Graphics3D, + g_Instance->m_VideoDecoder->Initialize(g_Instance->m_Graphics3D, PP_VIDEOPROFILE_H264HIGH, PP_HARDWAREACCELERATION_ONLY, 0, @@ -123,6 +128,12 @@ void MoonlightInstance::VidDecSetup(int width, int height, int redrawRate, void* } void MoonlightInstance::DispatchGetPicture(uint32_t unused) { + /*glClearColor(0.5, 0.5, 0.5, 1); + glClear(GL_COLOR_BUFFER_BIT); + + m_Graphics3D.SwapBuffers( + m_CallbackFactory.NewCallback(&MoonlightInstance::DispatchGetPicture));*/ + // Queue the initial GetPicture callback on the main thread g_Instance->m_VideoDecoder->GetPicture( g_Instance->m_CallbackFactory.NewCallbackWithOutput(&MoonlightInstance::PictureReady)); @@ -132,18 +143,15 @@ void MoonlightInstance::VidDecCleanup(void) { free(s_DecodeBuffer); delete g_Instance->m_VideoDecoder; - PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource(); if (g_Instance->m_Texture2DShader.program) { - g_Instance->m_GlesApi->DeleteProgram(graphics3D, g_Instance->m_Texture2DShader.program); + glDeleteProgram(g_Instance->m_Texture2DShader.program); } if (g_Instance->m_RectangleArbShader.program) { - g_Instance->m_GlesApi->DeleteProgram(graphics3D, g_Instance->m_RectangleArbShader.program); + glDeleteProgram(g_Instance->m_RectangleArbShader.program); } if (g_Instance->m_ExternalOesShader.program) { - g_Instance->m_GlesApi->DeleteProgram(graphics3D, g_Instance->m_ExternalOesShader.program); + glDeleteProgram(g_Instance->m_ExternalOesShader.program); } - - delete g_Instance->m_Graphics3D; } int MoonlightInstance::VidDecSubmitDecodeUnit(PDECODE_UNIT decodeUnit) { @@ -174,79 +182,79 @@ int MoonlightInstance::VidDecSubmitDecodeUnit(PDECODE_UNIT decodeUnit) { void MoonlightInstance::CreateShader(GLuint program, GLenum type, const char* source, int size) { - PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource(); - GLuint shader = g_Instance->m_GlesApi->CreateShader(graphics3D, type); - g_Instance->m_GlesApi->ShaderSource(graphics3D, shader, 1, &source, &size); - g_Instance->m_GlesApi->CompileShader(graphics3D, shader); - g_Instance->m_GlesApi->AttachShader(graphics3D, program, shader); - g_Instance->m_GlesApi->DeleteShader(graphics3D, shader); + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &source, &size); + glCompileShader(shader); + glAttachShader(program, shader); + glDeleteShader(shader); } Shader MoonlightInstance::CreateProgram(const char* vertexShader, const char* fragmentShader) { Shader shader; - PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource(); - shader.program = g_Instance->m_GlesApi->CreateProgram(graphics3D); + shader.program = glCreateProgram(); CreateShader(shader.program, GL_VERTEX_SHADER, vertexShader, strlen(vertexShader)); CreateShader(shader.program, GL_FRAGMENT_SHADER, fragmentShader, strlen(fragmentShader)); - g_Instance->m_GlesApi->LinkProgram(graphics3D, shader.program); - g_Instance->m_GlesApi->UseProgram(graphics3D, shader.program); + glLinkProgram(shader.program); + glUseProgram(shader.program); - g_Instance->m_GlesApi->Uniform1i(graphics3D, - g_Instance->m_GlesApi->GetUniformLocation(graphics3D, shader.program, "s_texture"), 0); + glUniform1i(glGetUniformLocation(shader.program, "s_texture"), 0); assertNoGLError(); - shader.texcoord_scale_location = g_Instance->m_GlesApi->GetUniformLocation(graphics3D, shader.program, "v_scale"); + shader.texcoord_scale_location = glGetUniformLocation(shader.program, "v_scale"); - GLint pos_location = g_Instance->m_GlesApi->GetAttribLocation(graphics3D, shader.program, "a_position"); - GLint tc_location = g_Instance->m_GlesApi->GetAttribLocation(graphics3D, shader.program, "a_texCoord"); + GLint pos_location = glGetAttribLocation(shader.program, "a_position"); + GLint tc_location = glGetAttribLocation(shader.program, "a_texCoord"); assertNoGLError(); - g_Instance->m_GlesApi->EnableVertexAttribArray(graphics3D, pos_location); - g_Instance->m_GlesApi->VertexAttribPointer(graphics3D, pos_location, 2, GL_FLOAT, GL_FALSE, 0, 0); - g_Instance->m_GlesApi->EnableVertexAttribArray(graphics3D, tc_location); - g_Instance->m_GlesApi->VertexAttribPointer(graphics3D, tc_location, 2, GL_FLOAT, GL_FALSE, 0, static_cast(0) + 8); + glEnableVertexAttribArray( pos_location); + glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(tc_location); + glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 0, static_cast(0) + 8); - g_Instance->m_GlesApi->UseProgram(graphics3D, 0); + glUseProgram(0); assertNoGLError(); return shader; } void MoonlightInstance::PaintPicture(PP_VideoPicture picture) { - PP_Resource graphics3D = g_Instance->m_Graphics3D->pp_resource(); - if (picture.texture_target == GL_TEXTURE_2D) { if (!g_Instance->m_Texture2DShader.program) { g_Instance->m_Texture2DShader = CreateProgram(k_VertexShader, k_FragmentShader2D); } - g_Instance->m_GlesApi->UseProgram(graphics3D, g_Instance->m_Texture2DShader.program); - g_Instance->m_GlesApi->Uniform2f(graphics3D, g_Instance->m_Texture2DShader.texcoord_scale_location, - 1.0, 1.0); + glUseProgram(g_Instance->m_Texture2DShader.program); + glUniform2f(g_Instance->m_Texture2DShader.texcoord_scale_location, 1.0, 1.0); } else if (picture.texture_target == GL_TEXTURE_RECTANGLE_ARB) { if (!g_Instance->m_RectangleArbShader.program) { g_Instance->m_RectangleArbShader = CreateProgram(k_VertexShader, k_FragmentShaderRectangle); } - g_Instance->m_GlesApi->UseProgram(graphics3D, g_Instance->m_RectangleArbShader.program); - g_Instance->m_GlesApi->Uniform2f(graphics3D, g_Instance->m_RectangleArbShader.texcoord_scale_location, - picture.texture_size.width, picture.texture_size.height); + glUseProgram(g_Instance->m_RectangleArbShader.program); + glUniform2f(g_Instance->m_RectangleArbShader.texcoord_scale_location, + picture.texture_size.width, picture.texture_size.height); } - else { + else if (picture.texture_target == GL_TEXTURE_EXTERNAL_OES){ if (!g_Instance->m_ExternalOesShader.program) { g_Instance->m_ExternalOesShader = CreateProgram(k_VertexShader, k_FragmentShaderExternal); } - g_Instance->m_GlesApi->UseProgram(graphics3D, g_Instance->m_ExternalOesShader.program); - g_Instance->m_GlesApi->Uniform2f(graphics3D, g_Instance->m_ExternalOesShader.texcoord_scale_location, - 1.0, 1.0); + glUseProgram(g_Instance->m_ExternalOesShader.program); + glUniform2f(g_Instance->m_ExternalOesShader.texcoord_scale_location, 1.0, 1.0); } - g_Instance->m_GlesApi->Viewport(graphics3D, 0, 0, g_Instance->m_ViewSize.width(), g_Instance->m_ViewSize.height()); - g_Instance->m_GlesApi->ActiveTexture(graphics3D, GL_TEXTURE0); - g_Instance->m_GlesApi->BindTexture(graphics3D, picture.texture_target, picture.texture_id); - g_Instance->m_GlesApi->DrawArrays(graphics3D, GL_TRIANGLE_STRIP, 0, 4); - g_Instance->m_GlesApi->UseProgram(graphics3D, 0); + if (picture.texture_target != 0) { + glViewport(0, 0, g_Instance->m_ViewSize.width(), g_Instance->m_ViewSize.height()); + glActiveTexture(GL_TEXTURE0); + glBindTexture(picture.texture_target, picture.texture_id); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glUseProgram(0); + } - g_Instance->m_Graphics3D->SwapBuffers(pp::BlockUntilComplete()); + g_Instance->m_Graphics3D.SwapBuffers(g_Instance->m_CallbackFactory.NewCallback(&MoonlightInstance::DispatchRendering)); +} + +void MoonlightInstance::DispatchRendering(int32_t unused) { + // Paint the image on screen + PaintPicture(m_LastPicture); } void MoonlightInstance::PictureReady(int32_t result, PP_VideoPicture picture) { @@ -254,10 +262,12 @@ void MoonlightInstance::PictureReady(int32_t result, PP_VideoPicture picture) { return; } - // Paint the image on screen - PaintPicture(picture); - - g_Instance->m_VideoDecoder->RecyclePicture(picture); + // Replace the last picture with this one and free the old one + PP_VideoPicture oldPic = m_LastPicture; + m_LastPicture = picture; + if (oldPic.texture_target) { + g_Instance->m_VideoDecoder->RecyclePicture(oldPic); + } // Queue another callback g_Instance->m_VideoDecoder->GetPicture(