12 #include "osxGraphicsStateGuardian.h"
13 #include "osxGraphicsBuffer.h"
14 #include "string_utils.h"
15 #include "config_osxdisplay.h"
16 #include "depthWriteAttrib.h"
17 #include "depthTestAttrib.h"
18 #include "textureAttrib.h"
21 #include <OpenGL/gl.h>
22 #import <mach-o/dyld.h>
26 #include "resize_box.rgb.c"
28 TypeHandle osxGraphicsStateGuardian::_type_handle;
40 void *osxGraphicsStateGuardian::
41 do_get_extension_func(
const char *name) {
42 string fullname =
"_" + string(name);
43 NSSymbol symbol = NULL;
45 if (NSIsSymbolNameDefined(fullname.c_str())) {
46 symbol = NSLookupAndBindSymbol(fullname.c_str());
49 return symbol ? NSAddressOfSymbol(symbol) : NULL;
57 osxGraphicsStateGuardian::
60 GLGraphicsStateGuardian(engine, pipe),
61 _share_with(share_with),
65 _shared_buffer = 1011;
74 osxGraphicsStateGuardian::
75 ~osxGraphicsStateGuardian() {
76 if (_aglcontext != (AGLContext)NULL) {
77 aglSetCurrentContext(NULL);
78 aglDestroyContext(_aglcontext);
79 report_agl_error(
"aglDestroyContext");
80 _aglcontext = (AGLContext)NULL;
101 GLGraphicsStateGuardian::reset();
103 if (_aglcontext != (AGLContext)NULL) {
105 GLint value = sync_video ? 1 : 0;
106 aglSetInteger(_aglcontext, AGL_SWAP_INTERVAL, &value);
125 state = RenderState::make(TransparencyAttrib::make(TransparencyAttrib::M_alpha),
126 DepthWriteAttrib::make(DepthWriteAttrib::M_off),
127 DepthTestAttrib::make(DepthTestAttrib::M_none));
131 string resize_box_string((
const char *)resize_box, resize_box_len);
134 if (resize_box_pnm.
read(resize_box_strm,
"resize_box.rgb")) {
136 tex->set_name(
"resize_box.rgb");
137 tex->load(resize_box_pnm);
138 tex->set_minfilter(SamplerState::FT_linear);
139 tex->set_magfilter(SamplerState::FT_linear);
140 state = state->add_attrib(TextureAttrib::make(tex));
145 _projection_mat_inv = _projection_mat = TransformState::make_identity();
149 set_state_and_transform(state, TransformState::make_identity());
154 PN_stdfloat inner_x = 1.0f - (15.0f * 2.0f / _viewport_width);
155 PN_stdfloat inner_y = (15.0f * 2.0f / _viewport_height) - 1.0f;
161 glColor4f(1.0, 1.0, 1.0, 1.0);
162 glTexCoord2f(0.0, 0.0);
163 glVertex2f(inner_x, -1.0);
165 glTexCoord2f(0.9375, 0.0);
166 glVertex2f(1.0, -1.0);
168 glTexCoord2f(0.9375, 0.9375);
169 glVertex2f(1.0, inner_y);
171 glTexCoord2f(0.0, 0.9375);
172 glVertex2f(inner_x, inner_y);
185 describe_pixel_format(fb_props);
189 OSStatus err = noErr;
191 GDHandle display = GetMainDevice();
194 if (!fb_props.get_indexed_color()) {
195 attrib.push_back(AGL_RGBA);
196 int color_bits = fb_props.get_color_bits();
197 int alpha_bits = fb_props.get_alpha_bits();
198 attrib.push_back(AGL_BUFFER_SIZE);
199 attrib.push_back(color_bits + alpha_bits);
200 attrib.push_back(AGL_PIXEL_SIZE);
201 attrib.push_back(color_bits);
202 attrib.push_back(AGL_RED_SIZE);
203 attrib.push_back(fb_props.get_red_bits());
204 attrib.push_back(AGL_GREEN_SIZE);
205 attrib.push_back(fb_props.get_green_bits());
206 attrib.push_back(AGL_BLUE_SIZE);
207 attrib.push_back(fb_props.get_blue_bits());
208 attrib.push_back(AGL_ALPHA_SIZE);
209 attrib.push_back(alpha_bits);
211 attrib.push_back(AGL_DEPTH_SIZE);
212 attrib.push_back(fb_props.get_depth_bits());
213 attrib.push_back(AGL_STENCIL_SIZE);
214 attrib.push_back(fb_props.get_stencil_bits());
215 if (fb_props.get_multisamples() != 0) {
216 attrib.push_back(AGL_MULTISAMPLE);
217 attrib.push_back(AGL_SAMPLE_BUFFERS_ARB);
219 attrib.push_back(AGL_SAMPLES_ARB);
220 attrib.push_back(fb_props.get_multisamples());
223 if (fb_props.is_stereo()) {
224 attrib.push_back(AGL_STEREO);
227 if (!fb_props.is_single_buffered()) {
228 attrib.push_back(AGL_DOUBLEBUFFER);
231 attrib.push_back(AGL_FULLSCREEN);
234 attrib.push_back(AGL_PBUFFER);
237 if (fb_props.get_force_hardware()) {
238 attrib.push_back(AGL_ACCELERATED);
239 attrib.push_back(AGL_NO_RECOVERY);
244 attrib.push_back(AGL_MAXIMUM_POLICY);
247 attrib.push_back(AGL_NONE);
251 _aglPixFmt = aglChoosePixelFormat(&display, 1, &attrib[0]);
252 err = report_agl_error(
"aglChoosePixelFormat");
254 if(_share_with == NULL) {
255 _aglcontext = aglCreateContext(_aglPixFmt, NULL);
259 err = report_agl_error(
"aglCreateContext");
261 if (_aglcontext == NULL) {
262 osxdisplay_cat.error()
263 <<
"osxGraphicsStateGuardian::build_gl Error Getting GL Context \n" ;
268 aglSetInteger(_aglcontext, AGL_BUFFER_NAME, &_shared_buffer);
269 err = report_agl_error(
"aglSetInteger AGL_BUFFER_NAME");
273 osxdisplay_cat.error()
274 <<
"osxGraphicsStateGuardian::build_gl Error Getting Pixel Format\n" ;
275 osxdisplay_cat.error()
283 describe_pixel_format(fb_props);
286 if (osxdisplay_cat.is_debug()) {
287 osxdisplay_cat.debug()
288 <<
"osxGraphicsStateGuardian::build_gl Returning :" << err <<
"\n";
289 osxdisplay_cat.debug()
303 void osxGraphicsStateGuardian::
308 if (aglDescribePixelFormat(_aglPixFmt, AGL_RGBA, &value)) {
309 fb_props.set_indexed_color(!value);
310 fb_props.set_rgb_color(value);
312 if (aglDescribePixelFormat(_aglPixFmt, AGL_DEPTH_SIZE, &value)) {
313 fb_props.set_depth_bits(value);
316 if (aglDescribePixelFormat(_aglPixFmt, AGL_RED_SIZE, &value)) {
317 fb_props.set_red_bits(value);
320 if (aglDescribePixelFormat(_aglPixFmt, AGL_GREEN_SIZE, &value)) {
321 fb_props.set_green_bits(value);
324 if (aglDescribePixelFormat(_aglPixFmt, AGL_BLUE_SIZE, &value)) {
325 fb_props.set_blue_bits(value);
329 if (aglDescribePixelFormat(_aglPixFmt, AGL_ALPHA_SIZE, &value)) {
330 fb_props.set_alpha_bits(value);
333 if (aglDescribePixelFormat(_aglPixFmt, AGL_STENCIL_SIZE, &value)) {
334 fb_props.set_stencil_bits(value);
338 if (aglDescribePixelFormat(_aglPixFmt, AGL_ACCUM_RED_SIZE, &value)) {
341 if (aglDescribePixelFormat(_aglPixFmt, AGL_ACCUM_GREEN_SIZE, &value)) {
344 if (aglDescribePixelFormat(_aglPixFmt, AGL_ACCUM_BLUE_SIZE, &value)) {
348 if (aglDescribePixelFormat(_aglPixFmt, AGL_SAMPLES_ARB, &value)) {
349 fb_props.set_multisamples(value);
352 if (aglDescribePixelFormat(_aglPixFmt, AGL_DOUBLEBUFFER, &value)) {
354 fb_props.set_back_buffers(1);
356 fb_props.set_back_buffers(0);
360 if (aglDescribePixelFormat(_aglPixFmt, AGL_STEREO, &value)) {
361 fb_props.set_stereo(value);
366 fb_props.set_force_hardware(
true);
367 fb_props.set_force_software(
true);
370 AGLDevice *gdevs = aglDevicesOfPixelFormat(_aglPixFmt, &ndevs);
371 if (gdevs != (AGLDevice *)NULL) {
372 AGLRendererInfo rinfo = aglQueryRendererInfo(gdevs, ndevs);
374 if (aglDescribeRenderer(rinfo, AGL_ACCELERATED, &value)) {
376 fb_props.set_force_hardware(value);
377 fb_props.set_force_software(!value);
379 if (aglDescribeRenderer(rinfo, AGL_VIDEO_MEMORY, &value)) {
380 osxdisplay_cat.debug()
381 <<
"Reported video memory is " << value <<
"\n";
383 if (aglDescribeRenderer(rinfo, AGL_TEXTURE_MEMORY, &value)) {
384 osxdisplay_cat.debug()
385 <<
"Reported texture memory is " << value <<
"\n";
398 CGDisplayRestoreColorSyncSettings();
399 _cgErr = CGGetDisplayTransferByTable( 0, 256, _gOriginalRedTable, _gOriginalGreenTable, _gOriginalBlueTable, &_sampleCount);
415 CGDisplayRestoreColorSyncSettings();
430 CGGammaValue redTable[ 256 ];
431 CGGammaValue greenTable[ 256 ];
432 CGGammaValue blueTable[ 256 ];
437 for (j = 0; j < 3; j++) {
445 for (i = 0; i < 256; i++) {
446 redTable[i] = _gOriginalRedTable[ i ] * (y[ 0 ] ) / 256;
447 greenTable[ i ] = _gOriginalGreenTable[ i ] * (y[ 1 ] ) / 256;
448 blueTable[ i ] = _gOriginalBlueTable[ i ] * (y[ 2 ] ) / 256;
450 _cgErr = CGSetDisplayTransferByTable( 0, 256, redTable, greenTable, blueTable);
bool set_gamma(PN_stdfloat gamma)
Non static version of setting gamma.
void clear()
Unsets all properties that have been specified so far, and resets the FrameBufferProperties structure...
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
bool read(const Filename &filename, PNMFileType *type=NULL, bool report_unknown_type=true)
Reads the indicated image filename.
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
void draw_resize_box()
Draws an OSX-style resize icon in the bottom right corner of the current display region.
A tiny specialization on GLGraphicsStateGuardian to add some wgl-specific information.
This is our own Panda specialization on the default STL vector.
void atexit_function()
This function is passed to the atexit function.
bool static_set_gamma(bool restore, PN_stdfloat gamma)
Static function for setting gamma which is needed for atexit.
An object to create GraphicsOutputs that share a particular 3-D API.
void restore_gamma()
Restore original gamma.
OSStatus build_gl(bool full_screen, bool pbuffer, FrameBufferProperties &fb_props)
This function will build up a context for a gsg.
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
void set_color_bits(int n)
Sets the number of requested color bits as a single number that represents the sum of the individual ...
This class is the main interface to controlling the render process.
TypeHandle is the identifier used to differentiate C++ class types.
A container for the various kinds of properties we might ask to have on a graphics frameBuffer before...
virtual void reset()
Resets all internal state as if the gsg were newly created.
bool get_gamma_table()
Static function for getting the orig gamma tables.