自己編寫一個小的OpenGL程式,附加在mesa工程裡調試,開始進入:
// OpenGLDC.cpp檔案,從第59行起// 本代碼塊之前會調用ChoosePixelFormat/SetPixelFormat但不會運行mesa裡的對應發布函數/*59*/if(!(m_hRC = wglCreateContext(m_hDC)))// 擷取渲染描述控制代碼{MessageBox(NULL, "不能建立OpenGL渲染描述表", "錯誤",MB_OK|MB_ICONEXCLAMATION);return false;}if(!wglMakeCurrent(m_hDC, m_hRC))// 嘗試啟用著色描述表{MessageBox(NULL, "不能啟用當前的OpenGL渲染描述表", "錯誤",MB_OK|MB_ICONEXCLAMATION);return false;/*70*/}
經第59行處的wglCreateContext函數,進入wgl.c裡第179行位置的wglCreateContext,此即mesa工程的wglCreateContext發布函數,該處代碼如下所示:
wglCreateContext// wgl.c 從第179行到203行WINGDIAPI HGLRC GLAPIENTRY wglCreateContext(HDC hdc){ int i = 0; if (!ctx_count) {for(i=0;i<MESAWGL_CTX_MAX_COUNT;i++) { wgl_ctx[i].ctx = NULL;} } for( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ ) { if ( wgl_ctx[i].ctx == NULL ) { wgl_ctx[i].ctx = WMesaCreateContext(hdc, NULL, (GLboolean)GL_TRUE, (GLboolean) (pfd[curPFD-1].doubleBuffered ? GL_TRUE : GL_FALSE), (GLboolean)(pfd[curPFD-1].pfd.cAlphaBits ? GL_TRUE : GL_FALSE) ); if (wgl_ctx[i].ctx == NULL) break; ctx_count++; return ((HGLRC)wgl_ctx[i].ctx); } } SetLastError(0); return(NULL);}
在wgl.c檔案聲明有wgl_ctx數組(限定大小MESAWGL_CTX_MAX_COUNT,當前取值20),ctx_count儲存該數組被初始化的數量。
第182行處判定若ctx_count大於0,則第一次初始化wgl_ctx數組所有元素全部為NULL;
第187處的for迴圈,目的在於尋找到一個空的wgl_ctx數組元素儲存即將建立的WMesaContext結構地址,只要建立一個,即增加ctx_count計數1,並返回所建立的結構地址(即HGLRC控制代碼);
第201處是不應該被執行的,當已建立的WMesaContext數量大於wgl_ctx數組寬度時,就會到達此處。所以不能建立超過20個HGLRC控制代碼,就本工程而言。
WMesaCreateContext函數在wmesa.c第1418行起始,該函數代碼如下所示;
WMesaCreateContext// wmesa.c 從第1418行至1531行WMesaContext WMesaCreateContext(HDC hDC, HPALETTE* Pal,GLboolean rgb_flag,GLboolean db_flag,GLboolean alpha_flag){ WMesaContext c; struct dd_function_table functions; GLint red_bits, green_bits, blue_bits, alpha_bits; GLcontext *ctx; GLvisual *visual; (void) Pal; /* Indexed mode not supported */ if (!rgb_flag)return NULL; /* Allocate wmesa context */ c = CALLOC_STRUCT(wmesa_context); if (!c)return NULL;#if 0 /* I do not understand this contributed code */ /* Support memory and device contexts */ if(WindowFromDC(hDC) != NULL) {c->hDC = GetDC(WindowFromDC(hDC)); /* huh ???? */ } else {c->hDC = hDC; }#else c->hDC = hDC;#endif /* Get data for visual */ /* Dealing with this is actually a bit of overkill because Mesa will end * up treating all color component size requests less than 8 by using * a single byte per channel. In addition, the interface to the span * routines passes colors as an entire byte per channel anyway, so there * is nothing to be saved by telling the visual to be 16 bits if the device * is 16 bits. That is, Mesa is going to compute colors down to 8 bits per * channel anyway. * But we go through the motions here anyway. */ switch (GetDeviceCaps(c->hDC, BITSPIXEL)) { case 16:red_bits = green_bits = blue_bits = 5;alpha_bits = 0;break; default:red_bits = green_bits = blue_bits = 8;alpha_bits = 8;break; } /* Create visual based on flags */ visual = _mesa_create_visual(rgb_flag, db_flag, /* db_flag */ GL_FALSE, /* stereo */ red_bits, green_bits, blue_bits, /* color RGB */ alpha_flag ? alpha_bits : 0, /* color A */ 0, /* index bits */ DEFAULT_SOFTWARE_DEPTH_BITS, /* depth_bits */ 8, /* stencil_bits */ 16,16,16, /* accum RGB */ alpha_flag ? 16 : 0, /* accum A */ 1); /* num samples */ if (!visual) {_mesa_free(c);return NULL; } /* Set up driver functions */ _mesa_init_driver_functions(&functions); functions.GetString = wmesa_get_string; functions.UpdateState = wmesa_update_state; functions.GetBufferSize = wmesa_get_buffer_size; functions.Flush = wmesa_flush; functions.Clear = clear; functions.ClearIndex = clear_index; functions.ClearColor = clear_color; functions.ResizeBuffers = wmesa_resize_buffers; functions.Viewport = wmesa_viewport; /* initialize the Mesa context data */ ctx = &c->gl_ctx; _mesa_initialize_context(ctx, visual, NULL, &functions, (void *)c); /* visual no longer needed - it was copied by _mesa_initialize_context() */ _mesa_destroy_visual(visual); _mesa_enable_sw_extensions(ctx); _mesa_enable_1_3_extensions(ctx); _mesa_enable_1_4_extensions(ctx); _mesa_enable_1_5_extensions(ctx); _mesa_enable_2_0_extensions(ctx); _mesa_enable_2_1_extensions(ctx); /* Initialize the software rasterizer and helper modules. */ if (!_swrast_CreateContext(ctx) || !_vbo_CreateContext(ctx) || !_tnl_CreateContext(ctx) ||!_swsetup_CreateContext(ctx)) {_mesa_free_context_data(ctx);_mesa_free(c);return NULL; } _swsetup_Wakeup(ctx); TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline; return c;}
第1493行的_mesa_init_driver_functions(&functions)初始化dd_function_table結構,即設定結構的函數指標地址,該結構定義在dd.h標頭檔第68行起,Device driver functions table. (裝置驅動函數表),這些裡的絕大多數函數直接與OpenGL狀態指令對應。
第1506行的_mesa_initialize_context函數初始化一個GLcontext結構,該結構為mesa的三大核心結構之一,成員很多。參見後文的解釋。
第1519行有4個內部模組在做建立初始化:_swrast、_vbo、_tnl、_swsetup。
第1528行處設定_tnl管道函數,它引導了後來的對頂點變換、視窗裁剪、光照紋理計算、直至最終的渲染。
暫時停止對這些中途被呼叫內建函式的分析,來看wglMakeCurrent函數會做什麼,wgl.c第234行起是工程對應的wglMakeCurrent發布函數。
wglMakeCurrent// wgl.c檔案 第234行至254行WINGDIAPI BOOL GLAPIENTRY wglMakeCurrent(HDC hdc, HGLRC hglrc){ int i; CurrentHDC = hdc; if (!hdc || !hglrc) {WMesaMakeCurrent(NULL, NULL);ctx_current = -1;return TRUE; } for ( i = 0; i < MESAWGL_CTX_MAX_COUNT; i++ ) {if ( wgl_ctx[i].ctx == (WMesaContext) hglrc ) { WMesaMakeCurrent( (WMesaContext) hglrc, hdc ); ctx_current = i; return TRUE;} } return FALSE;}
第238行儲存了當前裝置DC控制代碼;
第240行檢查是否是要取消hDC與hglrc之間的關聯,如果是,則以NULL為參數調用WMesaMakeCurrent;
第246行起,for迴圈檢查輸入參數hglrc是否有效,即必須是先前建立時儲存過的變數地址,如果能夠找到,則調用WMesaMakeCurrent關聯hglrc與hdc裝置DC;
ctx_current顯然是指向當前有效hglrc對應的wgl_ctx數組處的序號。
之後,程式轉入到wmesa.c裡的WMesaMakeCurrent函數,原始碼為:
WMesaMakeCurrent// wmesa.c 第1592行起至1648行void WMesaMakeCurrent(WMesaContext c, HDC hdc){ WMesaFramebuffer pwfb; { /* return if already current */ GET_CURRENT_CONTEXT(ctx); WMesaContext pwc = wmesa_context(ctx); if (pwc && c == pwc && pwc->hDC == hdc) return; } pwfb = wmesa_lookup_framebuffer(hdc); /* Lazy creation of framebuffers */ if (c && !pwfb && hdc) { struct gl_renderbuffer *rb; GLvisual *visual = &c->gl_ctx.Visual; GLuint width, height; get_window_size(hdc, &width, &height);c->clearPen = CreatePen(PS_SOLID, 1, 0); c->clearBrush = CreateSolidBrush(0); pwfb = wmesa_new_framebuffer(hdc, visual);/* Create back buffer if double buffered */if (visual->doubleBufferMode == 1) { wmCreateBackingStore(pwfb, width, height);} /* make render buffers */ if (visual->doubleBufferMode == 1) { rb = wmesa_new_renderbuffer(); _mesa_add_renderbuffer(&pwfb->Base, BUFFER_BACK_LEFT, rb); wmesa_set_renderbuffer_funcs(rb, pwfb->pixelformat, pwfb->cColorBits, 1);} rb = wmesa_new_renderbuffer(); _mesa_add_renderbuffer(&pwfb->Base, BUFFER_FRONT_LEFT, rb); wmesa_set_renderbuffer_funcs(rb, pwfb->pixelformat, pwfb->cColorBits, 0);/* Let Mesa own the Depth, Stencil, and Accum buffers */ _mesa_add_soft_renderbuffers(&pwfb->Base, GL_FALSE, /* color */ visual->depthBits > 0, visual->stencilBits > 0, visual->accumRedBits > 0, visual->alphaBits >0, GL_FALSE); } if (c && pwfb)_mesa_make_current(&c->gl_ctx, &pwfb->Base, &pwfb->Base); else _mesa_make_current(NULL, NULL, NULL);}
第1596行至第1602行,顯然在確認hglrc與hdc是否已經是關聯的,如果是,不用做什麼事直接返回即可;
第1604行尋找已建立的且使用hdc的WMesaFramebuffer,若找不到則,返回NULL;
第1607處的if判斷:c(hglrc)與hdc有效,即不是要刪除,而pwfb為NULL的話,則執行第1608至1642的代碼體;
第1644行的if判斷,c(hglrc)與pwfb是否有效,若有效則調用_mesa_make_current關聯,否則用NULL參數取消關聯。